2255.2.158
by Martin Pool
Most of the integration of dirstate and subtree |
1 |
# Copyright (C) 2006, 2007 Canonical Ltd
|
1986.1.2
by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely |
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 |
||
2255.7.66
by Robert Collins
MutableTree.add only needs a tree write lock. |
65 |
@needs_tree_write_lock
|
1986.1.2
by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely |
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 |
||
2323.4.2
by Robert Collins
Fix the behaviour of dirstate optimised iter_changes recursing its disk iterator into subtrees inappropriately. |
93 |
files = [path.strip('/') for path in files] |
94 |
||
1986.1.2
by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely |
95 |
if ids is None: |
96 |
ids = [None] * len(files) |
|
97 |
else: |
|
98 |
assert(len(ids) == len(files)) |
|
2294.1.10
by John Arbash Meinel
Switch all apis over to utf8 file ids. All tests pass |
99 |
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 |
100 |
|
101 |
if kinds is None: |
|
102 |
kinds = [None] * len(files) |
|
103 |
else: |
|
104 |
assert(len(kinds) == len(files)) |
|
105 |
for f in files: |
|
106 |
# generic constraint checks:
|
|
107 |
if self.is_control_filename(f): |
|
108 |
raise errors.ForbiddenControlFileError(filename=f) |
|
109 |
fp = splitpath(f) |
|
110 |
# fill out file kinds for all files [not needed when we stop
|
|
111 |
# caring about the instantaneous file kind within a uncommmitted tree
|
|
112 |
#
|
|
113 |
self._gather_kinds(files, kinds) |
|
114 |
self._add(files, ids, kinds) |
|
115 |
||
2255.6.8
by Aaron Bentley
Merge refactoring of add_reference |
116 |
def add_reference(self, sub_tree): |
117 |
"""Add a TreeReference to the tree, pointing at sub_tree"""
|
|
118 |
raise errors.UnsupportedOperation(self.add_reference, self) |
|
119 |
||
120 |
def _add_reference(self, sub_tree): |
|
121 |
"""Standard add_reference implementation, for use by subclasses"""
|
|
122 |
try: |
|
123 |
sub_tree_path = self.relpath(sub_tree.basedir) |
|
124 |
except errors.PathNotChild: |
|
125 |
raise errors.BadReferenceTarget(self, sub_tree, |
|
126 |
'Target not inside tree.') |
|
127 |
sub_tree_id = sub_tree.get_root_id() |
|
128 |
if sub_tree_id == self.get_root_id(): |
|
129 |
raise errors.BadReferenceTarget(self, sub_tree, |
|
130 |
'Trees have the same root id.') |
|
131 |
if sub_tree_id in self.inventory: |
|
132 |
raise errors.BadReferenceTarget(self, sub_tree, |
|
133 |
'Root id already present in tree') |
|
134 |
self._add([sub_tree_path], [sub_tree_id], ['tree-reference']) |
|
135 |
||
1986.1.2
by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely |
136 |
def _add(self, files, ids, kinds): |
2255.6.8
by Aaron Bentley
Merge refactoring of add_reference |
137 |
"""Helper function for add - updates the inventory.
|
138 |
||
139 |
:param files: sequence of pathnames, relative to the tree root
|
|
140 |
:param ids: sequence of suggested ids for the files (may be None)
|
|
141 |
:param kinds: sequence of inventory kinds of the files (i.e. may
|
|
142 |
contain "tree-reference")
|
|
143 |
"""
|
|
1986.1.2
by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely |
144 |
raise NotImplementedError(self._add) |
145 |
||
2376.2.2
by Aaron Bentley
Reduce lock to tree lock |
146 |
@needs_tree_write_lock
|
2376.2.1
by Aaron Bentley
Implement MutableTree.apply_inventory_delta |
147 |
def apply_inventory_delta(self, changes): |
148 |
"""Apply changes to the inventory as an atomic operation.
|
|
149 |
||
150 |
The argument is a set of changes to apply. It must describe a
|
|
151 |
valid result, but the order is not important. Specifically,
|
|
152 |
intermediate stages *may* be invalid, such as when two files
|
|
153 |
swap names.
|
|
154 |
||
155 |
The changes should be structured as a list of tuples, of the form
|
|
156 |
(old_path, new_path, file_id, new_entry). For creation, old_path
|
|
157 |
must be None. For deletion, new_path and new_entry must be None.
|
|
158 |
file_id is always non-None. For renames and other mutations, all
|
|
159 |
values must be non-None.
|
|
160 |
||
161 |
If the new_entry is a directory, its children should be an empty
|
|
162 |
dict. Children are handled by apply_inventory_delta itself.
|
|
163 |
||
2376.2.9
by Aaron Bentley
Cleanup from review |
164 |
:param changes: A list of tuples for the change to apply:
|
165 |
[(old_path, new_path, file_id, new_inventory_entry), ...]
|
|
166 |
"""
|
|
2376.2.1
by Aaron Bentley
Implement MutableTree.apply_inventory_delta |
167 |
self.flush() |
168 |
inv = self.inventory |
|
169 |
children = {} |
|
170 |
for old_path, file_id in sorted(((op, f) for op, np, f, e in changes |
|
171 |
if op is not None), reverse=True): |
|
2376.2.2
by Aaron Bentley
Reduce lock to tree lock |
172 |
if file_id not in inv: |
173 |
continue
|
|
2376.2.1
by Aaron Bentley
Implement MutableTree.apply_inventory_delta |
174 |
children[file_id] = getattr(inv[file_id], 'children', {}) |
175 |
inv.remove_recursive_id(file_id) |
|
176 |
for new_path, new_entry in sorted((np, e) for op, np, f, e in |
|
177 |
changes if np is not None): |
|
178 |
if getattr(new_entry, 'children', None) is not None: |
|
179 |
new_entry.children = children.get(new_entry.file_id, {}) |
|
180 |
inv.add(new_entry) |
|
181 |
self._write_inventory(inv) |
|
182 |
||
183 |
@needs_write_lock
|
|
2255.2.218
by Robert Collins
Make the nested tree commit smoke test be more rigourous. |
184 |
def commit(self, message=None, revprops=None, *args, |
2100.3.27
by Aaron Bentley
Enable nested commits |
185 |
**kwargs): |
1986.1.3
by Robert Collins
Merge bzr.dev. |
186 |
# avoid circular imports
|
1986.1.2
by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely |
187 |
from bzrlib import commit |
188 |
if revprops is None: |
|
189 |
revprops = {} |
|
190 |
if not 'branch-nick' in revprops: |
|
191 |
revprops['branch-nick'] = self.branch.nick |
|
192 |
# args for wt.commit start at message from the Commit.commit method,
|
|
2367.2.1
by Robert Collins
Remove bzrlib 0.8 compatability where it was making the code unclear or messy. (Robert Collins) |
193 |
args = (message, ) + args |
1986.1.2
by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely |
194 |
committed_id = commit.Commit().commit(working_tree=self, |
1986.1.3
by Robert Collins
Merge bzr.dev. |
195 |
revprops=revprops, *args, **kwargs) |
1986.1.2
by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely |
196 |
return committed_id |
197 |
||
198 |
def _gather_kinds(self, files, kinds): |
|
199 |
"""Helper function for add - sets the entries of kinds."""
|
|
200 |
raise NotImplementedError(self._gather_kinds) |
|
201 |
||
1986.1.6
by Robert Collins
Add MemoryTree.last_revision. |
202 |
@needs_read_lock
|
203 |
def last_revision(self): |
|
1986.1.7
by Robert Collins
Merge bzr.dev. |
204 |
"""Return the revision id of the last commit performed in this tree.
|
205 |
||
206 |
In early tree formats the result of last_revision is the same as the
|
|
207 |
branch last_revision, but that is no longer the case for modern tree
|
|
208 |
formats.
|
|
209 |
|
|
210 |
last_revision returns the left most parent id, or None if there are no
|
|
211 |
parents.
|
|
212 |
||
213 |
last_revision was deprecated as of 0.11. Please use get_parent_ids
|
|
214 |
instead.
|
|
215 |
"""
|
|
1986.1.6
by Robert Collins
Add MemoryTree.last_revision. |
216 |
raise NotImplementedError(self.last_revision) |
217 |
||
1986.1.8
by Robert Collins
Update to bzr.dev, which involves adding lock_tree_write to MutableTree and MemoryTree. |
218 |
def lock_tree_write(self): |
219 |
"""Lock the working tree for write, and the branch for read.
|
|
220 |
||
221 |
This is useful for operations which only need to mutate the working
|
|
222 |
tree. Taking out branch write locks is a relatively expensive process
|
|
223 |
and may fail if the branch is on read only media. So branch write locks
|
|
224 |
should only be taken out when we are modifying branch data - such as in
|
|
225 |
operations like commit, pull, uncommit and update.
|
|
226 |
"""
|
|
227 |
raise NotImplementedError(self.lock_tree_write) |
|
228 |
||
1986.1.2
by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely |
229 |
def lock_write(self): |
230 |
"""Lock the tree and its branch. This allows mutating calls to be made.
|
|
231 |
||
232 |
Some mutating methods will take out implicit write locks, but in
|
|
233 |
general you should always obtain a write lock before calling mutating
|
|
234 |
methods on a tree.
|
|
235 |
"""
|
|
236 |
raise NotImplementedError(self.lock_write) |
|
237 |
||
238 |
@needs_write_lock
|
|
239 |
def mkdir(self, path, file_id=None): |
|
240 |
"""Create a directory in the tree. if file_id is None, one is assigned.
|
|
241 |
||
242 |
:param path: A unicode file path.
|
|
243 |
:param file_id: An optional file-id.
|
|
244 |
:return: the file id of the new directory.
|
|
245 |
"""
|
|
246 |
raise NotImplementedError(self.mkdir) |
|
247 |
||
248 |
def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False): |
|
249 |
"""Set the parents of the working tree.
|
|
250 |
||
251 |
:param parents_list: A list of (revision_id, tree) tuples.
|
|
252 |
If tree is None, then that element is treated as an unreachable
|
|
253 |
parent tree - i.e. a ghost.
|
|
254 |
"""
|
|
255 |
raise NotImplementedError(self.set_parent_trees) |