196
183
raise NotImplementedError(self.iter_entries_by_dir)
198
def iter_child_entries(self, file_id, path=None):
199
"""Iterate over the children of a directory or tree reference.
201
:param file_id: File id of the directory/tree-reference
202
:param path: Optional path of the directory
203
:raise NoSuchId: When the file_id does not exist
204
:return: Iterator over entries in the directory
206
raise NotImplementedError(self.iter_child_entries)
208
185
def list_files(self, include_root=False, from_dir=None, recursive=True):
209
186
"""List all files in this tree.
316
290
:param file_id: The file_id of the file.
317
291
:param path: The path of the file.
319
292
If both file_id and path are supplied, an implementation may use
322
295
return osutils.split_lines(self.get_file_text(file_id, path))
324
def get_file_verifier(self, file_id, path=None, stat_value=None):
325
"""Return a verifier for a file.
327
The default implementation returns a sha1.
329
:param file_id: The handle for this file.
330
:param path: The path that this file can be found at.
331
These must point to the same object.
332
:param stat_value: Optional stat value for the object
333
:return: Tuple with verifier name and verifier data
335
return ("SHA1", self.get_file_sha1(file_id, path=path,
336
stat_value=stat_value))
338
def get_file_sha1(self, file_id, path=None, stat_value=None):
297
def get_file_sha1(self, file_id, path=None):
339
298
"""Return the SHA1 file for a file.
341
:note: callers should use get_file_verifier instead
342
where possible, as the underlying repository implementation may
343
have quicker access to a non-sha1 verifier.
345
300
:param file_id: The handle for this file.
346
301
:param path: The path that this file can be found at.
347
302
These must point to the same object.
348
:param stat_value: Optional stat value for the object
350
304
raise NotImplementedError(self.get_file_sha1)
402
359
cur_file = (self.get_file_text(file_id),)
403
360
yield identifier, cur_file
405
def get_symlink_target(self, file_id, path=None):
362
def get_symlink_target(self, file_id):
406
363
"""Get the target for a given file_id.
408
365
It is assumed that the caller already knows that file_id is referencing
410
367
:param file_id: Handle for the symlink entry.
411
:param path: The path of the file.
412
If both file_id and path are supplied, an implementation may use
414
368
:return: The path the symlink points to.
416
370
raise NotImplementedError(self.get_symlink_target)
418
373
def get_root_id(self):
419
374
"""Return the file_id for the root of this tree."""
420
375
raise NotImplementedError(self.get_root_id)
542
497
return find_ids_across_trees(paths, [self] + list(trees), require_versioned)
544
499
def iter_children(self, file_id):
545
"""Iterate over the file ids of the children of an entry.
547
:param file_id: File id of the entry
548
:return: Iterator over child file ids.
550
raise NotImplementedError(self.iter_children)
500
entry = self.iter_entries_by_dir([file_id]).next()[1]
501
for child in getattr(entry, 'children', {}).itervalues():
552
504
def lock_read(self):
553
505
"""Lock this tree for multiple read only operations.
555
507
:return: A bzrlib.lock.LogicalLockResult.
784
@deprecated_method(deprecated_in((2, 5, 0)))
785
736
def _get_inventory(self):
786
737
return self._inventory
788
739
inventory = property(_get_inventory,
789
740
doc="Inventory of this Tree")
791
def _get_root_inventory(self):
792
return self._inventory
794
root_inventory = property(_get_root_inventory,
795
doc="Root inventory of this tree")
797
def _unpack_file_id(self, file_id):
798
"""Find the inventory and inventory file id for a tree file id.
800
:param file_id: The tree file id, as bytestring or tuple
801
:return: Inventory and inventory file id
803
if isinstance(file_id, tuple):
804
if len(file_id) != 1:
805
raise ValueError("nested trees not yet supported: %r" % file_id)
807
return self.root_inventory, file_id
810
743
def path2id(self, path):
811
744
"""Return the id for path in this tree."""
812
return self._path2inv_file_id(path)[1]
814
def _path2inv_file_id(self, path):
815
"""Lookup a inventory and inventory file id by path.
817
:param path: Path to look up
818
:return: tuple with inventory and inventory file id
820
# FIXME: Support nested trees
821
return self.root_inventory, self.root_inventory.path2id(path)
745
return self._inventory.path2id(path)
823
747
def id2path(self, file_id):
824
748
"""Return the path for a file id.
826
750
:raises NoSuchId:
828
inventory, file_id = self._unpack_file_id(file_id)
829
return inventory.id2path(file_id)
752
return self.inventory.id2path(file_id)
831
754
def has_id(self, file_id):
832
inventory, file_id = self._unpack_file_id(file_id)
833
return inventory.has_id(file_id)
755
return self.inventory.has_id(file_id)
835
757
def has_or_had_id(self, file_id):
836
inventory, file_id = self._unpack_file_id(file_id)
837
return inventory.has_id(file_id)
839
def all_file_ids(self):
841
[entry.file_id for path, entry in self.iter_entries_by_dir()])
843
@deprecated_method(deprecated_in((2, 4, 0)))
758
return self.inventory.has_id(file_id)
844
760
def __iter__(self):
845
return iter(self.all_file_ids())
761
return iter(self.inventory)
847
763
def filter_unversioned_files(self, paths):
848
764
"""Filter out paths that are versioned.
867
784
down to specific_file_ids that have been requested. This has no
868
785
impact if specific_file_ids is None.
870
if specific_file_ids is None:
871
inventory_file_ids = None
873
inventory_file_ids = []
874
for tree_file_id in specific_file_ids:
875
inventory, inv_file_id = self._unpack_file_id(tree_file_id)
876
if not inventory is self.root_inventory: # for now
877
raise AssertionError("%r != %r" % (
878
inventory, self.root_inventory))
879
inventory_file_ids.append(inv_file_id)
880
# FIXME: Handle nested trees
881
return self.root_inventory.iter_entries_by_dir(
882
specific_file_ids=inventory_file_ids, yield_parents=yield_parents)
885
def iter_child_entries(self, file_id, path=None):
886
inv, inv_file_id = self._unpack_file_id(file_id)
887
return inv[inv_file_id].children.itervalues()
889
@deprecated_method(deprecated_in((2, 5, 0)))
787
return self.inventory.iter_entries_by_dir(
788
specific_file_ids=specific_file_ids, yield_parents=yield_parents)
890
790
def get_file_by_path(self, path):
891
return self.get_file(self.path2id(path), path)
893
def iter_children(self, file_id, path=None):
894
"""See Tree.iter_children."""
895
entry = self.iter_entries_by_dir([file_id]).next()[1]
896
for child in getattr(entry, 'children', {}).itervalues():
791
return self.get_file(self._inventory.path2id(path), path)
794
######################################################################
797
# TODO: Merge these two functions into a single one that can operate
798
# on either a whole tree or a set of files.
800
# TODO: Return the diff in order by filename, not by category or in
801
# random order. Can probably be done by lock-stepping through the
802
# filenames from both trees.
805
def file_status(filename, old_tree, new_tree):
806
"""Return single-letter status, old and new names for a file.
808
The complexity here is in deciding how to represent renames;
809
many complex cases are possible.
811
old_inv = old_tree.inventory
812
new_inv = new_tree.inventory
813
new_id = new_inv.path2id(filename)
814
old_id = old_inv.path2id(filename)
816
if not new_id and not old_id:
817
# easy: doesn't exist in either; not versioned at all
818
if new_tree.is_ignored(filename):
819
return 'I', None, None
821
return '?', None, None
823
# There is now a file of this name, great.
826
# There is no longer a file of this name, but we can describe
827
# what happened to the file that used to have
828
# this name. There are two possibilities: either it was
829
# deleted entirely, or renamed.
830
if new_inv.has_id(old_id):
831
return 'X', old_inv.id2path(old_id), new_inv.id2path(old_id)
833
return 'D', old_inv.id2path(old_id), None
835
# if the file_id is new in this revision, it is added
836
if new_id and not old_inv.has_id(new_id):
839
# if there used to be a file of this name, but that ID has now
840
# disappeared, it is deleted
841
if old_id and not new_inv.has_id(old_id):
900
847
def find_ids_across_trees(filenames, trees, require_versioned=True):
1048
989
if source_kind != target_kind:
1049
990
changed_content = True
1050
991
elif source_kind == 'file':
1051
if not self.file_content_matches(file_id, file_id, source_path,
1052
target_path, source_stat, target_stat):
992
if (self.source.get_file_sha1(file_id, source_path, source_stat) !=
993
self.target.get_file_sha1(file_id, target_path, target_stat)):
1053
994
changed_content = True
1054
995
elif source_kind == 'symlink':
1055
996
if (self.source.get_symlink_target(file_id) !=
1056
997
self.target.get_symlink_target(file_id)):
1057
998
changed_content = True
1058
elif source_kind == 'tree-reference':
1059
if (self.source.get_reference_revision(file_id, source_path)
1060
!= self.target.get_reference_revision(file_id, target_path)):
999
# XXX: Yes, the indentation below is wrong. But fixing it broke
1000
# test_merge.TestMergerEntriesLCAOnDisk.
1001
# test_nested_tree_subtree_renamed_and_modified. We'll wait for
1002
# the fix from bzr.dev -- vila 2009026
1003
elif source_kind == 'tree-reference':
1004
if (self.source.get_reference_revision(file_id, source_path)
1005
!= self.target.get_reference_revision(file_id, target_path)):
1061
1006
changed_content = True
1062
1007
parent = (source_parent, target_parent)
1063
1008
name = (source_name, target_name)
1359
1304
if old_entry is None:
1360
1305
# Reusing a discarded change.
1361
1306
old_entry = self._get_entry(self.source, file_id)
1362
precise_file_ids.update(
1363
self.source.iter_children(file_id))
1307
for child in old_entry.children.values():
1308
precise_file_ids.add(child.file_id)
1364
1309
changed_file_ids.add(result[0])
1368
def file_content_matches(self, source_file_id, target_file_id,
1369
source_path=None, target_path=None, source_stat=None, target_stat=None):
1370
"""Check if two files are the same in the source and target trees.
1372
This only checks that the contents of the files are the same,
1373
it does not touch anything else.
1375
:param source_file_id: File id of the file in the source tree
1376
:param target_file_id: File id of the file in the target tree
1377
:param source_path: Path of the file in the source tree
1378
:param target_path: Path of the file in the target tree
1379
:param source_stat: Optional stat value of the file in the source tree
1380
:param target_stat: Optional stat value of the file in the target tree
1381
:return: Boolean indicating whether the files have the same contents
1383
source_verifier_kind, source_verifier_data = self.source.get_file_verifier(
1384
source_file_id, source_path, source_stat)
1385
target_verifier_kind, target_verifier_data = self.target.get_file_verifier(
1386
target_file_id, target_path, target_stat)
1387
if source_verifier_kind == target_verifier_kind:
1388
return (source_verifier_data == target_verifier_data)
1389
# Fall back to SHA1 for now
1390
if source_verifier_kind != "SHA1":
1391
source_sha1 = self.source.get_file_sha1(source_file_id,
1392
source_path, source_stat)
1394
source_sha1 = source_verifier_data
1395
if target_verifier_kind != "SHA1":
1396
target_sha1 = self.target.get_file_sha1(target_file_id,
1397
target_path, target_stat)
1399
target_sha1 = target_verifier_data
1400
return (source_sha1 == target_sha1)
1402
InterTree.register_optimiser(InterTree)
1405
1313
class MultiWalker(object):
1406
1314
"""Walk multiple trees simultaneously, getting combined results."""