1
# Copyright (C) 2006 Canonical Ltd
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.
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.
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
17
"""MutableTree object.
19
See MutableTree for more details.
23
from bzrlib import tree
24
from bzrlib.decorators import needs_read_lock, needs_write_lock
25
from bzrlib.osutils import splitpath
26
from bzrlib.symbol_versioning import DEPRECATED_PARAMETER
29
def needs_tree_write_lock(unbound):
30
"""Decorate unbound to take out and release a tree_write lock."""
31
def tree_write_locked(self, *args, **kwargs):
32
self.lock_tree_write()
34
return unbound(self, *args, **kwargs)
37
tree_write_locked.__doc__ = unbound.__doc__
38
tree_write_locked.__name__ = unbound.__name__
39
return tree_write_locked
42
class MutableTree(tree.Tree):
43
"""A MutableTree is a specialisation of Tree which is able to be mutated.
45
Generally speaking these mutations are only possible within a lock_write
46
context, and will revert if the lock is broken abnormally - but this cannot
47
be guaranteed - depending on the exact implementation of the mutable state.
49
The most common form of Mutable Tree is WorkingTree, see bzrlib.workingtree.
50
For tests we also have MemoryTree which is a MutableTree whose contents are
53
For now, we are not treating MutableTree as an interface to provide
54
conformance tests for - rather we are testing MemoryTree specifically, and
55
interface testing implementations of WorkingTree.
57
A mutable tree always has an associated Branch and BzrDir object - the
58
branch and bzrdir attributes.
62
def add(self, files, ids=None, kinds=None):
63
"""Add paths to the set of versioned paths.
65
Note that the command line normally calls smart_add instead,
66
which can automatically recurse.
68
This adds the files to the inventory, so that they will be
69
recorded by the next commit.
71
:param files: List of paths to add, relative to the base of the tree.
72
:param ids: If set, use these instead of automatically generated ids.
73
Must be the same length as the list of files, but may
74
contain None for ids that are to be autogenerated.
75
:param kinds: Optional parameter to specify the kinds to be used for
78
TODO: Perhaps callback with the ids and paths as they're added.
80
if isinstance(files, basestring):
81
assert(ids is None or isinstance(ids, basestring))
82
assert(kinds is None or isinstance(kinds, basestring))
90
ids = [None] * len(files)
92
assert(len(ids) == len(files))
95
kinds = [None] * len(files)
97
assert(len(kinds) == len(files))
99
# generic constraint checks:
100
if self.is_control_filename(f):
101
raise errors.ForbiddenControlFileError(filename=f)
103
# fill out file kinds for all files [not needed when we stop
104
# caring about the instantaneous file kind within a uncommmitted tree
106
self._gather_kinds(files, kinds)
107
self._add(files, ids, kinds)
109
def _add(self, files, ids, kinds):
110
"""Helper function for add - updates the inventory."""
111
raise NotImplementedError(self._add)
114
def commit(self, message=None, revprops=None, *args, **kwargs):
115
# avoid circular imports
116
from bzrlib import commit
119
if not 'branch-nick' in revprops:
120
revprops['branch-nick'] = self.branch.nick
121
# args for wt.commit start at message from the Commit.commit method,
122
# but with branch a kwarg now, passing in args as is results in the
123
#message being used for the branch
124
args = (DEPRECATED_PARAMETER, message, ) + args
125
committed_id = commit.Commit().commit(working_tree=self,
126
revprops=revprops, *args, **kwargs)
129
def _gather_kinds(self, files, kinds):
130
"""Helper function for add - sets the entries of kinds."""
131
raise NotImplementedError(self._gather_kinds)
134
def last_revision(self):
135
"""Return the revision id of the last commit performed in this tree.
137
In early tree formats the result of last_revision is the same as the
138
branch last_revision, but that is no longer the case for modern tree
141
last_revision returns the left most parent id, or None if there are no
144
last_revision was deprecated as of 0.11. Please use get_parent_ids
147
raise NotImplementedError(self.last_revision)
149
def lock_tree_write(self):
150
"""Lock the working tree for write, and the branch for read.
152
This is useful for operations which only need to mutate the working
153
tree. Taking out branch write locks is a relatively expensive process
154
and may fail if the branch is on read only media. So branch write locks
155
should only be taken out when we are modifying branch data - such as in
156
operations like commit, pull, uncommit and update.
158
raise NotImplementedError(self.lock_tree_write)
160
def lock_write(self):
161
"""Lock the tree and its branch. This allows mutating calls to be made.
163
Some mutating methods will take out implicit write locks, but in
164
general you should always obtain a write lock before calling mutating
167
raise NotImplementedError(self.lock_write)
170
def mkdir(self, path, file_id=None):
171
"""Create a directory in the tree. if file_id is None, one is assigned.
173
:param path: A unicode file path.
174
:param file_id: An optional file-id.
175
:return: the file id of the new directory.
177
raise NotImplementedError(self.mkdir)
179
def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
180
"""Set the parents of the working tree.
182
:param parents_list: A list of (revision_id, tree) tuples.
183
If tree is None, then that element is treated as an unreachable
184
parent tree - i.e. a ghost.
186
raise NotImplementedError(self.set_parent_trees)