~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/mutabletree.py

  • Committer: Martin Pool
  • Date: 2005-09-30 05:56:05 UTC
  • mto: (1185.14.2)
  • mto: This revision was merged to the branch mainline in revision 1396.
  • Revision ID: mbp@sourcefrog.net-20050930055605-a2c534529b392a7d
- fix upgrade for transport changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Canonical Ltd
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
 
17
 
"""MutableTree object.
18
 
 
19
 
See MutableTree for more details.
20
 
"""
21
 
 
22
 
 
23
 
from bzrlib import (
24
 
    osutils,
25
 
    tree,
26
 
    )
27
 
from bzrlib.decorators import needs_read_lock, needs_write_lock
28
 
from bzrlib.osutils import splitpath
29
 
from bzrlib.symbol_versioning import DEPRECATED_PARAMETER
30
 
 
31
 
 
32
 
def needs_tree_write_lock(unbound):
33
 
    """Decorate unbound to take out and release a tree_write lock."""
34
 
    def tree_write_locked(self, *args, **kwargs):
35
 
        self.lock_tree_write()
36
 
        try:
37
 
            return unbound(self, *args, **kwargs)
38
 
        finally:
39
 
            self.unlock()
40
 
    tree_write_locked.__doc__ = unbound.__doc__
41
 
    tree_write_locked.__name__ = unbound.__name__
42
 
    return tree_write_locked
43
 
 
44
 
 
45
 
class MutableTree(tree.Tree):
46
 
    """A MutableTree is a specialisation of Tree which is able to be mutated.
47
 
 
48
 
    Generally speaking these mutations are only possible within a lock_write
49
 
    context, and will revert if the lock is broken abnormally - but this cannot
50
 
    be guaranteed - depending on the exact implementation of the mutable state.
51
 
 
52
 
    The most common form of Mutable Tree is WorkingTree, see bzrlib.workingtree.
53
 
    For tests we also have MemoryTree which is a MutableTree whose contents are
54
 
    entirely in memory.
55
 
 
56
 
    For now, we are not treating MutableTree as an interface to provide
57
 
    conformance tests for - rather we are testing MemoryTree specifically, and 
58
 
    interface testing implementations of WorkingTree.
59
 
 
60
 
    A mutable tree always has an associated Branch and BzrDir object - the
61
 
    branch and bzrdir attributes.
62
 
    """
63
 
 
64
 
    @needs_write_lock
65
 
    def add(self, files, ids=None, kinds=None):
66
 
        """Add paths to the set of versioned paths.
67
 
 
68
 
        Note that the command line normally calls smart_add instead,
69
 
        which can automatically recurse.
70
 
 
71
 
        This adds the files to the inventory, so that they will be
72
 
        recorded by the next commit.
73
 
 
74
 
        :param files: List of paths to add, relative to the base of the tree.
75
 
        :param ids: If set, use these instead of automatically generated ids.
76
 
            Must be the same length as the list of files, but may
77
 
            contain None for ids that are to be autogenerated.
78
 
        :param kinds: Optional parameter to specify the kinds to be used for
79
 
            each file.
80
 
 
81
 
        TODO: Perhaps callback with the ids and paths as they're added.
82
 
        """
83
 
        if isinstance(files, basestring):
84
 
            assert(ids is None or isinstance(ids, basestring))
85
 
            assert(kinds is None or isinstance(kinds, basestring))
86
 
            files = [files]
87
 
            if ids is not None:
88
 
                ids = [ids]
89
 
            if kinds is not None:
90
 
                kinds = [kinds]
91
 
 
92
 
        if ids is None:
93
 
            ids = [None] * len(files)
94
 
        else:
95
 
            assert(len(ids) == len(files))
96
 
            ids = [osutils.safe_file_id(file_id) for file_id in ids]
97
 
 
98
 
        if kinds is None:
99
 
            kinds = [None] * len(files)
100
 
        else:
101
 
            assert(len(kinds) == len(files))
102
 
        for f in files:
103
 
            # generic constraint checks:
104
 
            if self.is_control_filename(f):
105
 
                raise errors.ForbiddenControlFileError(filename=f)
106
 
            fp = splitpath(f)
107
 
        # fill out file kinds for all files [not needed when we stop 
108
 
        # caring about the instantaneous file kind within a uncommmitted tree
109
 
        #
110
 
        self._gather_kinds(files, kinds)
111
 
        self._add(files, ids, kinds)
112
 
 
113
 
    def _add(self, files, ids, kinds):
114
 
        """Helper function for add - updates the inventory."""
115
 
        raise NotImplementedError(self._add)
116
 
 
117
 
    @needs_write_lock
118
 
    def commit(self, message=None, revprops=None, *args, **kwargs):
119
 
        # avoid circular imports
120
 
        from bzrlib import commit
121
 
        if revprops is None:
122
 
            revprops = {}
123
 
        if not 'branch-nick' in revprops:
124
 
            revprops['branch-nick'] = self.branch.nick
125
 
        # args for wt.commit start at message from the Commit.commit method,
126
 
        # but with branch a kwarg now, passing in args as is results in the
127
 
        #message being used for the branch
128
 
        args = (DEPRECATED_PARAMETER, message, ) + args
129
 
        committed_id = commit.Commit().commit(working_tree=self,
130
 
            revprops=revprops, *args, **kwargs)
131
 
        return committed_id
132
 
 
133
 
    def _gather_kinds(self, files, kinds):
134
 
        """Helper function for add - sets the entries of kinds."""
135
 
        raise NotImplementedError(self._gather_kinds)
136
 
 
137
 
    @needs_read_lock
138
 
    def last_revision(self):
139
 
        """Return the revision id of the last commit performed in this tree.
140
 
 
141
 
        In early tree formats the result of last_revision is the same as the
142
 
        branch last_revision, but that is no longer the case for modern tree
143
 
        formats.
144
 
        
145
 
        last_revision returns the left most parent id, or None if there are no
146
 
        parents.
147
 
 
148
 
        last_revision was deprecated as of 0.11. Please use get_parent_ids
149
 
        instead.
150
 
        """
151
 
        raise NotImplementedError(self.last_revision)
152
 
 
153
 
    def lock_tree_write(self):
154
 
        """Lock the working tree for write, and the branch for read.
155
 
 
156
 
        This is useful for operations which only need to mutate the working
157
 
        tree. Taking out branch write locks is a relatively expensive process
158
 
        and may fail if the branch is on read only media. So branch write locks
159
 
        should only be taken out when we are modifying branch data - such as in
160
 
        operations like commit, pull, uncommit and update.
161
 
        """
162
 
        raise NotImplementedError(self.lock_tree_write)
163
 
 
164
 
    def lock_write(self):
165
 
        """Lock the tree and its branch. This allows mutating calls to be made.
166
 
 
167
 
        Some mutating methods will take out implicit write locks, but in 
168
 
        general you should always obtain a write lock before calling mutating
169
 
        methods on a tree.
170
 
        """
171
 
        raise NotImplementedError(self.lock_write)
172
 
 
173
 
    @needs_write_lock
174
 
    def mkdir(self, path, file_id=None):
175
 
        """Create a directory in the tree. if file_id is None, one is assigned.
176
 
 
177
 
        :param path: A unicode file path.
178
 
        :param file_id: An optional file-id.
179
 
        :return: the file id of the new directory.
180
 
        """
181
 
        raise NotImplementedError(self.mkdir)
182
 
 
183
 
    def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
184
 
        """Set the parents of the working tree.
185
 
 
186
 
        :param parents_list: A list of (revision_id, tree) tuples. 
187
 
            If tree is None, then that element is treated as an unreachable
188
 
            parent tree - i.e. a ghost.
189
 
        """
190
 
        raise NotImplementedError(self.set_parent_trees)