13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
17
"""Tree classes, representing directory at point in time.
21
from collections import deque
22
from cStringIO import StringIO
24
revision as _mod_revision,
27
from .inter import InterObject
30
class FileTimestampUnavailable(errors.BzrError):
32
_fmt = "The filestamp for %(path)s is not available."
36
def __init__(self, path):
40
class MissingNestedTree(errors.BzrError):
42
_fmt = "The nested tree for %(path)s can not be resolved."""
44
def __init__(self, path):
48
class TreeEntry(object):
49
"""An entry that implements the minimum interface used by commands.
54
def __eq__(self, other):
55
# yes, this is ugly, TODO: best practice __eq__ style.
56
return (isinstance(other, TreeEntry)
57
and other.__class__ == self.__class__)
61
def kind_character(self):
64
def is_unmodified(self, other):
65
"""Does this entry reference the same entry?
67
This is mostly the same as __eq__, but returns False
68
for entries without enough information (i.e. revision is None)
73
class TreeDirectory(TreeEntry):
74
"""See TreeEntry. This is a directory in a working tree."""
80
def kind_character(self):
84
class TreeFile(TreeEntry):
85
"""See TreeEntry. This is a regular file in a working tree."""
91
def kind_character(self):
95
class TreeLink(TreeEntry):
96
"""See TreeEntry. This is a symlink in a working tree."""
102
def kind_character(self):
106
class TreeReference(TreeEntry):
107
"""See TreeEntry. This is a reference to a nested tree in a working tree."""
111
kind = 'tree-reference'
113
def kind_character(self):
117
class TreeChange(object):
118
"""Describes the changes between the same item in two different trees."""
120
__slots__ = ['path', 'changed_content', 'versioned',
121
'name', 'kind', 'executable', 'copied']
123
def __init__(self, path, changed_content, versioned,
124
name, kind, executable, copied=False):
126
self.changed_content = changed_content
127
self.versioned = versioned
130
self.executable = executable
134
return "%s%r" % (self.__class__.__name__, self._as_tuple())
137
return (self.path, self.changed_content, self.versioned,
138
self.name, self.kind, self.executable, self.copied)
140
def __eq__(self, other):
141
if isinstance(other, TreeChange):
142
return self._as_tuple() == other._as_tuple()
143
if isinstance(other, tuple):
144
return self._as_tuple() == other
147
def __lt__(self, other):
148
return self._as_tuple() < other._as_tuple()
150
def meta_modified(self):
151
if self.versioned == (True, True):
152
return (self.executable[0] != self.executable[1])
159
None not in self.name and
160
self.path[0] != self.path[1])
162
def is_reparented(self):
163
return os.path.dirname(self.path[0]) != os.path.dirname(self.path[1])
165
def discard_new(self):
166
return self.__class__(
167
(self.path[0], None), self.changed_content,
168
(self.versioned[0], None),
169
(self.name[0], None), (self.kind[0], None),
170
(self.executable[0], None),
30
from bzrlib.decorators import needs_read_lock
31
from bzrlib.errors import BzrError, BzrCheckError
32
from bzrlib import errors
33
from bzrlib.inventory import Inventory, InventoryFile
34
from bzrlib.inter import InterObject
35
from bzrlib.osutils import fingerprint_file
36
import bzrlib.revision
37
from bzrlib.trace import mutter, note
174
40
class Tree(object):
175
41
"""Abstract file tree.
177
43
There are several subclasses:
179
45
* `WorkingTree` exists as files on disk editable by the user.
181
47
* `RevisionTree` is a tree as recorded at some point in the past.
49
Trees contain an `Inventory` object, and also know how to retrieve
50
file texts mentioned in the inventory, either from a working
51
directory or from a store.
53
It is possible for trees to contain files that are not described
54
in their inventory or vice versa; for this use `filenames()`.
183
56
Trees can be compared, etc, regardless of whether they are working
184
57
trees or versioned trees.
187
def supports_rename_tracking(self):
188
"""Whether this tree supports rename tracking.
190
This defaults to True, but some implementations may want to override
195
def has_versioned_directories(self):
196
"""Whether this tree can contain explicitly versioned directories.
198
This defaults to True, but some implementations may want to override
203
def supports_symlinks(self):
204
"""Does this tree support symbolic links?
206
return osutils.has_symlinks()
208
60
def changes_from(self, other, want_unchanged=False, specific_files=None,
209
extra_trees=None, require_versioned=False, include_root=False,
210
want_unversioned=False):
61
extra_trees=None, require_versioned=False, include_root=False,
62
want_unversioned=False):
211
63
"""Return a TreeDelta of the changes from other to this tree.
213
65
:param other: A tree to compare with.
239
91
want_unversioned=want_unversioned,
242
def iter_changes(self, from_tree, include_unchanged=False,
94
def _iter_changes(self, from_tree, include_unchanged=False,
243
95
specific_files=None, pb=None, extra_trees=None,
244
96
require_versioned=True, want_unversioned=False):
245
"""See InterTree.iter_changes"""
246
97
intertree = InterTree.get(from_tree, self)
247
return intertree.iter_changes(include_unchanged, specific_files, pb,
248
extra_trees, require_versioned,
249
want_unversioned=want_unversioned)
98
return intertree._iter_changes(include_unchanged, specific_files, pb,
99
extra_trees, require_versioned, want_unversioned=want_unversioned)
251
101
def conflicts(self):
252
102
"""Get a list of the conflicts in the tree.
254
Each conflict is an instance of breezy.conflicts.Conflict.
104
Each conflict is an instance of bzrlib.conflicts.Conflict.
256
from . import conflicts as _mod_conflicts
257
return _mod_conflicts.ConflictList()
259
108
def extras(self):
260
109
"""For trees that can have unversioned files, return all such paths."""
263
112
def get_parent_ids(self):
264
"""Get the parent ids for this tree.
113
"""Get the parent ids for this tree.
266
115
:return: a list of parent ids. [] is returned to indicate
267
116
a tree with no parents.
268
117
:raises: BzrError if the parents are not known.
270
119
raise NotImplementedError(self.get_parent_ids)
272
121
def has_filename(self, filename):
273
122
"""True if the tree has given filename."""
274
raise NotImplementedError(self.has_filename)
123
raise NotImplementedError()
125
def has_id(self, file_id):
126
file_id = osutils.safe_file_id(file_id)
127
return self.inventory.has_id(file_id)
129
__contains__ = has_id
131
def has_or_had_id(self, file_id):
132
file_id = osutils.safe_file_id(file_id)
133
if file_id == self.inventory.root.file_id:
135
return self.inventory.has_id(file_id)
276
137
def is_ignored(self, filename):
277
138
"""Check whether the filename is ignored by this tree.
284
def all_file_ids(self):
285
"""Iterate through all file ids, including ids for missing files."""
286
raise NotImplementedError(self.all_file_ids)
288
def all_versioned_paths(self):
289
"""Iterate through all paths, including paths for missing files."""
290
raise NotImplementedError(self.all_versioned_paths)
292
def id2path(self, file_id, recurse='down'):
146
return iter(self.inventory)
148
def id2path(self, file_id):
293
149
"""Return the path for a file id.
295
151
:raises NoSuchId:
297
raise NotImplementedError(self.id2path)
299
def iter_entries_by_dir(self, specific_files=None, recurse_nested=False):
153
file_id = osutils.safe_file_id(file_id)
154
return self.inventory.id2path(file_id)
156
def is_control_filename(self, filename):
157
"""True if filename is the name of a control file in this tree.
159
:param filename: A filename within the tree. This is a relative path
160
from the root of this tree.
162
This is true IF and ONLY IF the filename is part of the meta data
163
that bzr controls in this tree. I.E. a random .bzr directory placed
164
on disk will not be a control file for this tree.
166
return self.bzrdir.is_control_filename(filename)
169
def iter_entries_by_dir(self, specific_file_ids=None):
300
170
"""Walk the tree in 'by_dir' order.
302
This will yield each entry in the tree as a (path, entry) tuple.
303
The order that they are yielded is:
305
Directories are walked in a depth-first lexicographical order,
306
however, whenever a directory is reached, all of its direct child
307
nodes are yielded in lexicographical order before yielding the
310
For example, in the tree::
320
The yield order (ignoring root) would be::
322
a, f, a/b, a/d, a/b/c, a/d/e, f/g
324
If recurse_nested is enabled then nested trees are included as if
325
they were a part of the tree. If is disabled then TreeReference
326
objects (without any children) are yielded.
328
raise NotImplementedError(self.iter_entries_by_dir)
330
def iter_child_entries(self, path):
331
"""Iterate over the children of a directory or tree reference.
333
:param path: Path of the directory
334
:raise NoSuchFile: When the path does not exist
335
:return: Iterator over entries in the directory
337
raise NotImplementedError(self.iter_child_entries)
339
def list_files(self, include_root=False, from_dir=None, recursive=True,
340
recurse_nested=False):
341
"""List all files in this tree.
343
:param include_root: Whether to include the entry for the tree root
344
:param from_dir: Directory under which to list files
345
:param recursive: Whether to list files recursively
346
:param recurse_nested: enter nested trees
347
:return: iterator over tuples of
348
(path, versioned, kind, inventory entry)
350
raise NotImplementedError(self.list_files)
172
This will yield each entry in the tree as a (path, entry) tuple. The
173
order that they are yielded is: the contents of a directory are
174
preceeded by the parent of a directory, and all the contents of a
175
directory are grouped together.
177
return self.inventory.iter_entries_by_dir(
178
specific_file_ids=specific_file_ids)
352
180
def iter_references(self):
353
if self.supports_tree_reference():
354
for path, entry in self.iter_entries_by_dir():
355
if entry.kind == 'tree-reference':
358
def get_containing_nested_tree(self, path):
359
"""Find the nested tree that contains a path.
361
:return: tuple with (nested tree and path inside the nested tree)
363
for nested_path in self.iter_references():
365
if path.startswith(nested_path):
366
nested_tree = self.get_nested_tree(nested_path)
367
return nested_tree, path[len(nested_path):]
371
def get_nested_tree(self, path):
372
"""Open the nested tree at the specified path.
374
:param path: Path from which to resolve tree reference.
375
:return: A Tree object for the nested tree
376
:raise MissingNestedTree: If the nested tree can not be resolved
378
raise NotImplementedError(self.get_nested_tree)
380
def kind(self, path):
181
for path, entry in self.iter_entries_by_dir():
182
if entry.kind == 'tree-reference':
183
yield path, entry.file_id
185
def kind(self, file_id):
381
186
raise NotImplementedError("Tree subclass %s must implement kind"
382
% self.__class__.__name__)
384
def stored_kind(self, path):
385
"""File kind stored for this path.
387
May not match kind on disk for working trees. Always available
388
for versioned files, even when the file itself is missing.
390
return self.kind(path)
392
def path_content_summary(self, path):
393
"""Get a summary of the information about path.
395
All the attributes returned are for the canonical form, not the
396
convenient form (if content filters are in use.)
398
:param path: A relative path within the tree.
399
:return: A tuple containing kind, size, exec, sha1-or-link.
400
Kind is always present (see tree.kind()).
401
size is present if kind is file and the size of the
402
canonical form can be cheaply determined, None otherwise.
403
exec is None unless kind is file and the platform supports the 'x'
405
sha1-or-link is the link target if kind is symlink, or the sha1 if
406
it can be obtained without reading the file.
408
raise NotImplementedError(self.path_content_summary)
410
def get_reference_revision(self, path, branch=None):
187
% self.__class__.__name__)
189
def get_reference_revision(self, file_id, path=None):
411
190
raise NotImplementedError("Tree subclass %s must implement "
412
191
"get_reference_revision"
413
% self.__class__.__name__)
192
% self.__class__.__name__)
415
194
def _comparison_data(self, entry, path):
416
195
"""Return a tuple of kind, executable, stat_value for a file.
424
203
raise NotImplementedError(self._comparison_data)
426
def get_file(self, path):
427
"""Return a file object for the file path in the tree.
205
def _file_size(self, entry, stat_value):
206
raise NotImplementedError(self._file_size)
208
def _get_inventory(self):
209
return self._inventory
211
def get_file(self, file_id):
212
"""Return a file object for the file file_id in the tree."""
429
213
raise NotImplementedError(self.get_file)
431
def get_file_with_stat(self, path):
432
"""Get a file handle and stat object for path.
434
The default implementation returns (self.get_file, None) for backwards
437
:param path: The path of the file.
438
:return: A tuple (file_handle, stat_value_or_None). If the tree has
439
no stat facility, or need for a stat cache feedback during commit,
440
it may return None for the second element of the tuple.
442
return (self.get_file(path), None)
444
def get_file_text(self, path):
445
"""Return the byte content of a file.
447
:param path: The path of the file.
449
:returns: A single byte string for the whole file.
451
with self.get_file(path) as my_file:
452
return my_file.read()
454
def get_file_lines(self, path):
455
"""Return the content of a file, as lines.
457
:param path: The path of the file.
459
return osutils.split_lines(self.get_file_text(path))
461
def get_file_verifier(self, path, stat_value=None):
462
"""Return a verifier for a file.
464
The default implementation returns a sha1.
215
def get_file_mtime(self, file_id, path=None):
216
"""Return the modification time for a file.
218
:param file_id: The handle for this file.
466
219
:param path: The path that this file can be found at.
467
220
These must point to the same object.
468
:param stat_value: Optional stat value for the object
469
:return: Tuple with verifier name and verifier data
471
return ("SHA1", self.get_file_sha1(path, stat_value=stat_value))
473
def get_file_sha1(self, path, stat_value=None):
474
"""Return the SHA1 file for a file.
476
:note: callers should use get_file_verifier instead
477
where possible, as the underlying repository implementation may
478
have quicker access to a non-sha1 verifier.
480
:param path: The path that this file can be found at.
481
:param stat_value: Optional stat value for the object
483
raise NotImplementedError(self.get_file_sha1)
485
def get_file_mtime(self, path):
486
"""Return the modification time for a file.
488
:param path: The path that this file can be found at.
490
222
raise NotImplementedError(self.get_file_mtime)
492
def get_file_size(self, path):
493
"""Return the size of a file in bytes.
495
This applies only to regular files. If invoked on directories or
496
symlinks, it will return None.
498
raise NotImplementedError(self.get_file_size)
500
def is_executable(self, path):
501
"""Check if a file is executable.
503
:param path: The path that this file can be found at.
505
raise NotImplementedError(self.is_executable)
507
def iter_files_bytes(self, desired_files):
508
"""Iterate through file contents.
510
Files will not necessarily be returned in the order they occur in
511
desired_files. No specific order is guaranteed.
513
Yields pairs of identifier, bytes_iterator. identifier is an opaque
514
value supplied by the caller as part of desired_files. It should
515
uniquely identify the file version in the caller's context. (Examples:
516
an index number or a TreeTransform trans_id.)
518
bytes_iterator is an iterable of bytestrings for the file. The
519
kind of iterable and length of the bytestrings are unspecified, but for
520
this implementation, it is a tuple containing a single bytestring with
521
the complete text of the file.
523
:param desired_files: a list of (path, identifier) pairs
525
for path, identifier in desired_files:
526
# We wrap the string in a tuple so that we can return an iterable
527
# of bytestrings. (Technically, a bytestring is also an iterable
528
# of bytestrings, but iterating through each character is not
530
cur_file = (self.get_file_text(path),)
531
yield identifier, cur_file
533
def get_symlink_target(self, path):
534
"""Get the target for a given path.
536
It is assumed that the caller already knows that path is referencing
224
def get_file_by_path(self, path):
225
return self.get_file(self._inventory.path2id(path))
227
def get_symlink_target(self, file_id):
228
"""Get the target for a given file_id.
230
It is assumed that the caller already knows that file_id is referencing
538
:param path: The path of the file.
232
:param file_id: Handle for the symlink entry.
539
233
:return: The path the symlink points to.
541
235
raise NotImplementedError(self.get_symlink_target)
543
def annotate_iter(self, path,
544
default_revision=_mod_revision.CURRENT_REVISION):
545
"""Return an iterator of revision_id, line tuples.
237
def annotate_iter(self, file_id):
238
"""Return an iterator of revision_id, line tuples
547
240
For working trees (and mutable trees in general), the special
548
241
revision_id 'current:' will be used for lines that are new in this
549
242
tree, e.g. uncommitted changes.
550
:param path: The file to produce an annotated version from
551
:param default_revision: For lines that don't match a basis, mark them
552
with this revision id. Not all implementations will make use of
243
:param file_id: The file to produce an annotated version from
555
245
raise NotImplementedError(self.annotate_iter)
247
inventory = property(_get_inventory,
248
doc="Inventory of this Tree")
250
def _check_retrieved(self, ie, f):
253
fp = fingerprint_file(f)
256
if ie.text_size is not None:
257
if ie.text_size != fp['size']:
258
raise BzrError("mismatched size for file %r in %r" % (ie.file_id, self._store),
259
["inventory expects %d bytes" % ie.text_size,
260
"file is actually %d bytes" % fp['size'],
261
"store is probably damaged/corrupt"])
263
if ie.text_sha1 != fp['sha1']:
264
raise BzrError("wrong SHA-1 for file %r in %r" % (ie.file_id, self._store),
265
["inventory expects %s" % ie.text_sha1,
266
"file is actually %s" % fp['sha1'],
267
"store is probably damaged/corrupt"])
557
270
def path2id(self, path):
558
271
"""Return the id for path in this tree."""
559
raise NotImplementedError(self.path2id)
561
def is_versioned(self, path):
562
"""Check whether path is versioned.
564
:param path: Path to check
567
return self.path2id(path) is not None
569
def find_related_paths_across_trees(self, paths, trees=[],
570
require_versioned=True):
571
"""Find related paths in tree corresponding to specified filenames in any
574
All matches in all trees will be used, and all children of matched
575
directories will be used.
577
:param paths: The filenames to find related paths for (if None, returns
579
:param trees: The trees to find file_ids within
580
:param require_versioned: if true, all specified filenames must occur in
582
:return: a set of paths for the specified filenames and their children
585
raise NotImplementedError(self.find_related_paths_across_trees)
272
return self._inventory.path2id(path)
274
def paths2ids(self, paths, trees=[], require_versioned=True):
275
"""Return all the ids that can be reached by walking from paths.
277
Each path is looked up in each this tree and any extras provided in
278
trees, and this is repeated recursively: the children in an extra tree
279
of a directory that has been renamed under a provided path in this tree
280
are all returned, even if none exist until a provided path in this
281
tree, and vice versa.
283
:param paths: An iterable of paths to start converting to ids from.
284
Alternatively, if paths is None, no ids should be calculated and None
285
will be returned. This is offered to make calling the api unconditional
286
for code that *might* take a list of files.
287
:param trees: Additional trees to consider.
288
:param require_versioned: If False, do not raise NotVersionedError if
289
an element of paths is not versioned in this tree and all of trees.
291
return find_ids_across_trees(paths, [self] + list(trees), require_versioned)
293
def print_file(self, file_id):
294
"""Print file with id `file_id` to stdout."""
295
file_id = osutils.safe_file_id(file_id)
297
sys.stdout.write(self.get_file_text(file_id))
587
299
def lock_read(self):
588
"""Lock this tree for multiple read only operations.
590
:return: A breezy.lock.LogicalLockResult.
592
return lock.LogicalLockResult(self.unlock)
594
302
def revision_tree(self, revision_id):
595
303
"""Obtain a revision tree for the revision revision_id.
597
305
The intention of this method is to allow access to possibly cached
598
306
tree data. Implementors of this method should raise NoSuchRevision if
599
the tree is not locally available, even if they could obtain the
600
tree via a repository or some other means. Callers are responsible
307
the tree is not locally available, even if they could obtain the
308
tree via a repository or some other means. Callers are responsible
601
309
for finding the ultimate source for a revision tree.
603
311
:param revision_id: The revision_id of the requested tree.
657
369
raise NotImplementedError(self.walkdirs)
659
def supports_content_filtering(self):
372
class EmptyTree(Tree):
375
self._inventory = Inventory(root_id=None)
376
symbol_versioning.warn('EmptyTree is deprecated as of bzr 0.9 please'
377
' use repository.revision_tree instead.',
378
DeprecationWarning, stacklevel=2)
380
def get_parent_ids(self):
383
def get_symlink_target(self, file_id):
386
def has_filename(self, filename):
662
def _content_filter_stack(self, path=None):
663
"""The stack of content filters for a path if filtering is supported.
665
Readers will be applied in first-to-last order.
666
Writers will be applied in last-to-first order.
667
Either the path or the file-id needs to be provided.
669
:param path: path relative to the root of the tree
671
:return: the list of filters - [] if there are none
673
from . import debug, filters
674
filter_pref_names = filters._get_registered_names()
675
if len(filter_pref_names) == 0:
677
prefs = next(self.iter_search_rules([path], filter_pref_names))
678
stk = filters._get_filter_stack_for(prefs)
679
if 'filters' in debug.debug_flags:
680
trace.note("*** {0} content-filter: {1} => {2!r}").format(path, prefs, stk)
683
def _content_filter_stack_provider(self):
684
"""A function that returns a stack of ContentFilters.
686
The function takes a path (relative to the top of the tree) and a
687
file-id as parameters.
689
:return: None if content filtering is not supported by this tree.
691
if self.supports_content_filtering():
692
return self._content_filter_stack
696
def iter_search_rules(self, path_names, pref_names=None,
697
_default_searcher=None):
698
"""Find the preferences for filenames in a tree.
700
:param path_names: an iterable of paths to find attributes for.
701
Paths are given relative to the root of the tree.
702
:param pref_names: the list of preferences to lookup - None for all
703
:param _default_searcher: private parameter to assist testing - don't use
704
:return: an iterator of tuple sequences, one per path-name.
705
See _RulesSearcher.get_items for details on the tuple sequence.
708
if _default_searcher is None:
709
_default_searcher = rules._per_user_searcher
710
searcher = self._get_rules_searcher(_default_searcher)
711
if searcher is not None:
712
if pref_names is not None:
713
for path in path_names:
714
yield searcher.get_selected_items(path, pref_names)
716
for path in path_names:
717
yield searcher.get_items(path)
719
def _get_rules_searcher(self, default_searcher):
720
"""Get the RulesSearcher for this tree given the default one."""
721
searcher = default_searcher
724
def archive(self, format, name, root='', subdir=None,
726
"""Create an archive of this tree.
728
:param format: Format name (e.g. 'tar')
729
:param name: target file name
730
:param root: Root directory name (or None)
731
:param subdir: Subdirectory to export (or None)
732
:return: Iterator over archive chunks
734
from .archive import create_archive
735
with self.lock_read():
736
return create_archive(format, self, name, root,
737
subdir, force_mtime=force_mtime)
740
def versionable_kind(cls, kind):
741
"""Check if this tree support versioning a specific file kind."""
742
return (kind in ('file', 'directory', 'symlink', 'tree-reference'))
744
def preview_transform(self, pb=None):
745
"""Obtain a transform object."""
746
raise NotImplementedError(self.preview_transform)
389
def kind(self, file_id):
390
file_id = osutils.safe_file_id(file_id)
391
assert self._inventory[file_id].kind == "directory"
394
def list_files(self, include_root=False):
397
def __contains__(self, file_id):
398
file_id = osutils.safe_file_id(file_id)
399
return (file_id in self._inventory)
401
def get_file_sha1(self, file_id, path=None, stat_value=None):
405
######################################################################
408
# TODO: Merge these two functions into a single one that can operate
409
# on either a whole tree or a set of files.
411
# TODO: Return the diff in order by filename, not by category or in
412
# random order. Can probably be done by lock-stepping through the
413
# filenames from both trees.
416
def file_status(filename, old_tree, new_tree):
417
"""Return single-letter status, old and new names for a file.
419
The complexity here is in deciding how to represent renames;
420
many complex cases are possible.
422
old_inv = old_tree.inventory
423
new_inv = new_tree.inventory
424
new_id = new_inv.path2id(filename)
425
old_id = old_inv.path2id(filename)
427
if not new_id and not old_id:
428
# easy: doesn't exist in either; not versioned at all
429
if new_tree.is_ignored(filename):
430
return 'I', None, None
432
return '?', None, None
434
# There is now a file of this name, great.
437
# There is no longer a file of this name, but we can describe
438
# what happened to the file that used to have
439
# this name. There are two possibilities: either it was
440
# deleted entirely, or renamed.
442
if new_inv.has_id(old_id):
443
return 'X', old_inv.id2path(old_id), new_inv.id2path(old_id)
445
return 'D', old_inv.id2path(old_id), None
447
# if the file_id is new in this revision, it is added
448
if new_id and not old_inv.has_id(new_id):
451
# if there used to be a file of this name, but that ID has now
452
# disappeared, it is deleted
453
if old_id and not new_inv.has_id(old_id):
460
def find_renames(old_inv, new_inv):
461
for file_id in old_inv:
462
if file_id not in new_inv:
464
old_name = old_inv.id2path(file_id)
465
new_name = new_inv.id2path(file_id)
466
if old_name != new_name:
467
yield (old_name, new_name)
470
def find_ids_across_trees(filenames, trees, require_versioned=True):
471
"""Find the ids corresponding to specified filenames.
473
All matches in all trees will be used, and all children of matched
474
directories will be used.
476
:param filenames: The filenames to find file_ids for (if None, returns
478
:param trees: The trees to find file_ids within
479
:param require_versioned: if true, all specified filenames must occur in
481
:return: a set of file ids for the specified filenames and their children.
485
specified_path_ids = _find_ids_across_trees(filenames, trees,
487
return _find_children_across_trees(specified_path_ids, trees)
490
def _find_ids_across_trees(filenames, trees, require_versioned):
491
"""Find the ids corresponding to specified filenames.
493
All matches in all trees will be used, but subdirectories are not scanned.
495
:param filenames: The filenames to find file_ids for
496
:param trees: The trees to find file_ids within
497
:param require_versioned: if true, all specified filenames must occur in
499
:return: a set of (path, file ids) for the specified filenames
502
interesting_ids = set()
503
for tree_path in filenames:
506
file_id = tree.path2id(tree_path)
507
if file_id is not None:
508
interesting_ids.add(file_id)
511
not_versioned.append(tree_path)
512
if len(not_versioned) > 0 and require_versioned:
513
raise errors.PathsNotVersionedError(not_versioned)
514
return interesting_ids
517
def _find_children_across_trees(specified_ids, trees):
518
"""Return a set including specified ids and their children
520
All matches in all trees will be used.
522
:param trees: The trees to find file_ids within
523
:return: a set containing all specified ids and their children
525
interesting_ids = set(specified_ids)
526
pending = interesting_ids
527
# now handle children of interesting ids
528
# we loop so that we handle all children of each id in both trees
529
while len(pending) > 0:
531
for file_id in pending:
533
if not tree.has_id(file_id):
535
entry = tree.inventory[file_id]
536
for child in getattr(entry, 'children', {}).itervalues():
537
if child.file_id not in interesting_ids:
538
new_pending.add(child.file_id)
539
interesting_ids.update(new_pending)
540
pending = new_pending
541
return interesting_ids
749
544
class InterTree(InterObject):
826
621
:param require_versioned: Raise errors.PathsNotVersionedError if a
827
622
path in the specific_files list is not versioned in one of
828
623
source, target or extra_trees.
829
:param specific_files: An optional list of file paths to restrict the
830
comparison to. When mapping filenames to ids, all matches in all
831
trees (including optional extra_trees) are used, and all children
832
of matched directories are included. The parents in the target tree
833
of the specific files up to and including the root of the tree are
834
always evaluated for changes too.
835
624
:param want_unversioned: Should unversioned files be returned in the
836
625
output. An unversioned file is defined as one with (False, False)
837
626
for the versioned pair.
839
raise NotImplementedError(self.iter_changes)
841
def file_content_matches(
842
self, source_path, target_path,
843
source_stat=None, target_stat=None):
844
"""Check if two files are the same in the source and target trees.
846
This only checks that the contents of the files are the same,
847
it does not touch anything else.
849
:param source_path: Path of the file in the source tree
850
:param target_path: Path of the file in the target tree
851
:param source_stat: Optional stat value of the file in the source tree
852
:param target_stat: Optional stat value of the file in the target tree
853
:return: Boolean indicating whether the files have the same contents
855
with self.lock_read():
856
source_verifier_kind, source_verifier_data = (
857
self.source.get_file_verifier(source_path, source_stat))
858
target_verifier_kind, target_verifier_data = (
859
self.target.get_file_verifier(
860
target_path, target_stat))
861
if source_verifier_kind == target_verifier_kind:
862
return (source_verifier_data == target_verifier_data)
863
# Fall back to SHA1 for now
864
if source_verifier_kind != "SHA1":
865
source_sha1 = self.source.get_file_sha1(
866
source_path, source_stat)
868
source_sha1 = source_verifier_data
869
if target_verifier_kind != "SHA1":
870
target_sha1 = self.target.get_file_sha1(
871
target_path, target_stat)
873
target_sha1 = target_verifier_data
874
return (source_sha1 == target_sha1)
876
def find_target_path(self, path, recurse='none'):
877
"""Find target tree path.
879
:param path: Path to search for (exists in source)
880
:return: path in target, or None if there is no equivalent path.
881
:raise NoSuchFile: If the path doesn't exist in source
883
raise NotImplementedError(self.find_target_path)
885
def find_source_path(self, path, recurse='none'):
886
"""Find the source tree path.
888
:param path: Path to search for (exists in target)
889
:return: path in source, or None if there is no equivalent path.
890
:raise NoSuchFile: if the path doesn't exist in target
892
raise NotImplementedError(self.find_source_path)
894
def find_target_paths(self, paths, recurse='none'):
895
"""Find target tree paths.
897
:param paths: Iterable over paths in target to search for
898
:return: Dictionary mapping from source paths to paths in target , or
899
None if there is no equivalent path.
903
ret[path] = self.find_target_path(path, recurse=recurse)
906
def find_source_paths(self, paths, recurse='none'):
907
"""Find source tree paths.
909
:param paths: Iterable over paths in target to search for
910
:return: Dictionary mapping from target paths to paths in source, or
911
None if there is no equivalent path.
915
ret[path] = self.find_source_path(path, recurse=recurse)
919
def find_previous_paths(from_tree, to_tree, paths, recurse='none'):
920
"""Find previous tree paths.
922
:param from_tree: From tree
923
:param to_tree: To tree
924
:param paths: Iterable over paths in from_tree to search for
925
:return: Dictionary mapping from from_tree paths to paths in to_tree, or
926
None if there is no equivalent path.
928
return InterTree.get(to_tree, from_tree).find_source_paths(paths, recurse=recurse)
931
def find_previous_path(from_tree, to_tree, path, recurse='none'):
932
"""Find previous tree path.
934
:param from_tree: From tree
935
:param to_tree: To tree
936
:param path: Path to search for (exists in from_tree)
937
:return: path in to_tree, or None if there is no equivalent path.
938
:raise NoSuchFile: If the path doesn't exist in from_tree
940
return InterTree.get(to_tree, from_tree).find_source_path(
941
path, recurse=recurse)
944
def get_canonical_path(tree, path, normalize):
945
"""Find the canonical path of an item, ignoring case.
947
:param tree: Tree to traverse
948
:param path: Case-insensitive path to look up
949
:param normalize: Function to normalize a filename for comparison
950
:return: The canonical path
954
bit_iter = iter(path.split("/"))
956
lelt = normalize(elt)
959
for child in tree.iter_child_entries(cur_path):
961
if child.name == elt:
962
# if we found an exact match, we can stop now; if
963
# we found an approximate match we need to keep
964
# searching because there might be an exact match
966
new_path = osutils.pathjoin(cur_path, child.name)
968
elif normalize(child.name) == lelt:
969
new_path = osutils.pathjoin(cur_path, child.name)
970
except errors.NoSuchId:
971
# before a change is committed we can see this error...
973
except errors.NotADirectory:
629
lookup_trees = [self.source]
631
lookup_trees.extend(extra_trees)
632
specific_file_ids = self.target.paths2ids(specific_files,
633
lookup_trees, require_versioned=require_versioned)
635
all_unversioned = sorted([(p.split('/'), p) for p in self.target.extras()
636
if not specific_files or
637
osutils.is_inside_any(specific_files, p)])
638
all_unversioned = deque(all_unversioned)
978
# got to the end of this directory and no entries matched.
979
# Return what matched so far, plus the rest as specified.
980
cur_path = osutils.pathjoin(cur_path, elt, *list(bit_iter))
640
all_unversioned = deque()
642
from_entries_by_dir = list(self.source.inventory.iter_entries_by_dir(
643
specific_file_ids=specific_file_ids))
644
from_data = dict((e.file_id, (p, e)) for p, e in from_entries_by_dir)
645
to_entries_by_dir = list(self.target.inventory.iter_entries_by_dir(
646
specific_file_ids=specific_file_ids))
647
num_entries = len(from_entries_by_dir) + len(to_entries_by_dir)
649
# the unversioned path lookup only occurs on real trees - where there
650
# can be extras. So the fake_entry is solely used to look up
651
# executable it values when execute is not supported.
652
fake_entry = InventoryFile('unused', 'unused', 'unused')
653
for to_path, to_entry in to_entries_by_dir:
654
while all_unversioned and all_unversioned[0][0] < to_path.split('/'):
655
unversioned_path = all_unversioned.popleft()
656
to_kind, to_executable, to_stat = \
657
self.target._comparison_data(fake_entry, unversioned_path[1])
658
yield (None, (None, unversioned_path[1]), True, (False, False),
660
(None, unversioned_path[0][-1]),
662
(None, to_executable))
663
file_id = to_entry.file_id
664
to_paths[file_id] = to_path
666
changed_content = False
667
from_path, from_entry = from_data.get(file_id, (None, None))
668
from_versioned = (from_entry is not None)
669
if from_entry is not None:
670
from_versioned = True
671
from_name = from_entry.name
672
from_parent = from_entry.parent_id
673
from_kind, from_executable, from_stat = \
674
self.source._comparison_data(from_entry, from_path)
677
from_versioned = False
681
from_executable = None
682
versioned = (from_versioned, True)
683
to_kind, to_executable, to_stat = \
684
self.target._comparison_data(to_entry, to_path)
685
kind = (from_kind, to_kind)
686
if kind[0] != kind[1]:
687
changed_content = True
688
elif from_kind == 'file':
689
from_size = self.source._file_size(from_entry, from_stat)
690
to_size = self.target._file_size(to_entry, to_stat)
691
if from_size != to_size:
692
changed_content = True
693
elif (self.source.get_file_sha1(file_id, from_path, from_stat) !=
694
self.target.get_file_sha1(file_id, to_path, to_stat)):
695
changed_content = True
696
elif from_kind == 'symlink':
697
if (self.source.get_symlink_target(file_id) !=
698
self.target.get_symlink_target(file_id)):
699
changed_content = True
700
elif from_kind == 'tree-reference':
701
if (self.source.get_reference_revision(file_id, from_path)
702
!= self.target.get_reference_revision(file_id, to_path)):
703
changed_content = True
704
parent = (from_parent, to_entry.parent_id)
705
name = (from_name, to_entry.name)
706
executable = (from_executable, to_executable)
708
pb.update('comparing files', entry_count, num_entries)
709
if (changed_content is not False or versioned[0] != versioned[1]
710
or parent[0] != parent[1] or name[0] != name[1] or
711
executable[0] != executable[1] or include_unchanged):
712
yield (file_id, (from_path, to_path), changed_content,
713
versioned, parent, name, kind, executable)
715
while all_unversioned:
716
# yield any trailing unversioned paths
717
unversioned_path = all_unversioned.popleft()
718
to_kind, to_executable, to_stat = \
719
self.target._comparison_data(fake_entry, unversioned_path[1])
720
yield (None, (None, unversioned_path[1]), True, (False, False),
722
(None, unversioned_path[0][-1]),
724
(None, to_executable))
726
def get_to_path(to_entry):
727
if to_entry.parent_id is None:
728
to_path = '' # the root
730
if to_entry.parent_id not in to_paths:
732
return get_to_path(self.target.inventory[to_entry.parent_id])
733
to_path = osutils.pathjoin(to_paths[to_entry.parent_id],
735
to_paths[to_entry.file_id] = to_path
738
for path, from_entry in from_entries_by_dir:
739
file_id = from_entry.file_id
740
if file_id in to_paths:
743
if not file_id in self.target.inventory:
744
# common case - paths we have not emitted are not present in
748
to_path = get_to_path(self.target.inventory[file_id])
751
pb.update('comparing files', entry_count, num_entries)
752
versioned = (True, False)
753
parent = (from_entry.parent_id, None)
754
name = (from_entry.name, None)
755
from_kind, from_executable, stat_value = \
756
self.source._comparison_data(from_entry, path)
757
kind = (from_kind, None)
758
executable = (from_executable, None)
759
changed_content = True
760
# the parent's path is necessarily known at this point.
761
yield(file_id, (path, to_path), changed_content, versioned, parent,
762
name, kind, executable)
765
# This was deprecated before 0.12, but did not have an official warning
766
@symbol_versioning.deprecated_function(symbol_versioning.zero_twelve)
767
def RevisionTree(*args, **kwargs):
768
"""RevisionTree has moved to bzrlib.revisiontree.RevisionTree()
770
Accessing it as bzrlib.tree.RevisionTree has been deprecated as of
773
from bzrlib.revisiontree import RevisionTree as _RevisionTree
774
return _RevisionTree(*args, **kwargs)