/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
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
2100.3.27 by Aaron Bentley
Enable nested commits
23
from bzrlib import (
24
    errors,
2294.1.10 by John Arbash Meinel
Switch all apis over to utf8 file ids. All tests pass
25
    osutils,
2100.3.27 by Aaron Bentley
Enable nested commits
26
    tree,
27
    )
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
28
from bzrlib.decorators import needs_read_lock, needs_write_lock
29
from bzrlib.osutils import splitpath
30
from bzrlib.symbol_versioning import DEPRECATED_PARAMETER
31
32
1986.1.8 by Robert Collins
Update to bzr.dev, which involves adding lock_tree_write to MutableTree and MemoryTree.
33
def needs_tree_write_lock(unbound):
34
    """Decorate unbound to take out and release a tree_write lock."""
35
    def tree_write_locked(self, *args, **kwargs):
36
        self.lock_tree_write()
37
        try:
38
            return unbound(self, *args, **kwargs)
39
        finally:
40
            self.unlock()
41
    tree_write_locked.__doc__ = unbound.__doc__
42
    tree_write_locked.__name__ = unbound.__name__
43
    return tree_write_locked
44
45
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
46
class MutableTree(tree.Tree):
47
    """A MutableTree is a specialisation of Tree which is able to be mutated.
48
49
    Generally speaking these mutations are only possible within a lock_write
50
    context, and will revert if the lock is broken abnormally - but this cannot
51
    be guaranteed - depending on the exact implementation of the mutable state.
52
53
    The most common form of Mutable Tree is WorkingTree, see bzrlib.workingtree.
54
    For tests we also have MemoryTree which is a MutableTree whose contents are
55
    entirely in memory.
56
57
    For now, we are not treating MutableTree as an interface to provide
58
    conformance tests for - rather we are testing MemoryTree specifically, and 
59
    interface testing implementations of WorkingTree.
60
61
    A mutable tree always has an associated Branch and BzrDir object - the
62
    branch and bzrdir attributes.
63
    """
64
65
    @needs_write_lock
66
    def add(self, files, ids=None, kinds=None):
67
        """Add paths to the set of versioned paths.
68
69
        Note that the command line normally calls smart_add instead,
70
        which can automatically recurse.
71
72
        This adds the files to the inventory, so that they will be
73
        recorded by the next commit.
74
75
        :param files: List of paths to add, relative to the base of the tree.
76
        :param ids: If set, use these instead of automatically generated ids.
77
            Must be the same length as the list of files, but may
78
            contain None for ids that are to be autogenerated.
79
        :param kinds: Optional parameter to specify the kinds to be used for
80
            each file.
81
82
        TODO: Perhaps callback with the ids and paths as they're added.
83
        """
84
        if isinstance(files, basestring):
85
            assert(ids is None or isinstance(ids, basestring))
86
            assert(kinds is None or isinstance(kinds, basestring))
87
            files = [files]
88
            if ids is not None:
89
                ids = [ids]
90
            if kinds is not None:
91
                kinds = [kinds]
92
93
        if ids is None:
94
            ids = [None] * len(files)
95
        else:
96
            assert(len(ids) == len(files))
2294.1.10 by John Arbash Meinel
Switch all apis over to utf8 file ids. All tests pass
97
            ids = [osutils.safe_file_id(file_id) for file_id in ids]
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
98
99
        if kinds is None:
100
            kinds = [None] * len(files)
101
        else:
102
            assert(len(kinds) == len(files))
103
        for f in files:
104
            # generic constraint checks:
105
            if self.is_control_filename(f):
106
                raise errors.ForbiddenControlFileError(filename=f)
107
            fp = splitpath(f)
108
        # fill out file kinds for all files [not needed when we stop 
109
        # caring about the instantaneous file kind within a uncommmitted tree
110
        #
111
        self._gather_kinds(files, kinds)
112
        self._add(files, ids, kinds)
113
114
    def _add(self, files, ids, kinds):
115
        """Helper function for add - updates the inventory."""
116
        raise NotImplementedError(self._add)
117
118
    @needs_write_lock
2100.3.27 by Aaron Bentley
Enable nested commits
119
    def commit(self, message=None, revprops=None, recursive='down', *args,
120
               **kwargs):
121
        if recursive == 'down':
122
            for tree in self.iter_nested_trees():
123
                try:
124
                    tree.commit(message, revprops, recursive, *args, **kwargs)
125
                except errors.PointlessCommit:
126
                    pass
1986.1.3 by Robert Collins
Merge bzr.dev.
127
        # avoid circular imports
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
128
        from bzrlib import commit
129
        if revprops is None:
130
            revprops = {}
131
        if not 'branch-nick' in revprops:
132
            revprops['branch-nick'] = self.branch.nick
133
        # args for wt.commit start at message from the Commit.commit method,
134
        # but with branch a kwarg now, passing in args as is results in the
135
        #message being used for the branch
2149.1.3 by Aaron Bentley
Updates from review comments
136
        args = (DEPRECATED_PARAMETER, message, ) + args
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
137
        committed_id = commit.Commit().commit(working_tree=self,
1986.1.3 by Robert Collins
Merge bzr.dev.
138
            revprops=revprops, *args, **kwargs)
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
139
        return committed_id
140
141
    def _gather_kinds(self, files, kinds):
142
        """Helper function for add - sets the entries of kinds."""
143
        raise NotImplementedError(self._gather_kinds)
144
1986.1.6 by Robert Collins
Add MemoryTree.last_revision.
145
    @needs_read_lock
146
    def last_revision(self):
1986.1.7 by Robert Collins
Merge bzr.dev.
147
        """Return the revision id of the last commit performed in this tree.
148
149
        In early tree formats the result of last_revision is the same as the
150
        branch last_revision, but that is no longer the case for modern tree
151
        formats.
152
        
153
        last_revision returns the left most parent id, or None if there are no
154
        parents.
155
156
        last_revision was deprecated as of 0.11. Please use get_parent_ids
157
        instead.
158
        """
1986.1.6 by Robert Collins
Add MemoryTree.last_revision.
159
        raise NotImplementedError(self.last_revision)
160
1986.1.8 by Robert Collins
Update to bzr.dev, which involves adding lock_tree_write to MutableTree and MemoryTree.
161
    def lock_tree_write(self):
162
        """Lock the working tree for write, and the branch for read.
163
164
        This is useful for operations which only need to mutate the working
165
        tree. Taking out branch write locks is a relatively expensive process
166
        and may fail if the branch is on read only media. So branch write locks
167
        should only be taken out when we are modifying branch data - such as in
168
        operations like commit, pull, uncommit and update.
169
        """
170
        raise NotImplementedError(self.lock_tree_write)
171
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
172
    def lock_write(self):
173
        """Lock the tree and its branch. This allows mutating calls to be made.
174
175
        Some mutating methods will take out implicit write locks, but in 
176
        general you should always obtain a write lock before calling mutating
177
        methods on a tree.
178
        """
179
        raise NotImplementedError(self.lock_write)
180
181
    @needs_write_lock
182
    def mkdir(self, path, file_id=None):
183
        """Create a directory in the tree. if file_id is None, one is assigned.
184
185
        :param path: A unicode file path.
186
        :param file_id: An optional file-id.
187
        :return: the file id of the new directory.
188
        """
189
        raise NotImplementedError(self.mkdir)
190
191
    def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
192
        """Set the parents of the working tree.
193
194
        :param parents_list: A list of (revision_id, tree) tuples. 
195
            If tree is None, then that element is treated as an unreachable
196
            parent tree - i.e. a ghost.
197
        """
198
        raise NotImplementedError(self.set_parent_trees)
2100.3.27 by Aaron Bentley
Enable nested commits
199
200
    def iter_nested_trees(self):
201
        for path, entry in self.iter_reference_entries():
202
            yield self.get_nested_tree(entry, path)