~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tree.py

  • Committer: Vincent Ladeuil
  • Date: 2012-03-13 17:25:29 UTC
  • mfrom: (6499 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6501.
  • Revision ID: v.ladeuil+lp@free.fr-20120313172529-i0suyjnepsor25i7
Merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
"""Tree classes, representing directory at point in time.
18
18
"""
19
19
 
 
20
from __future__ import absolute_import
 
21
 
20
22
import os
21
23
 
22
24
from bzrlib.lazy_import import lazy_import
35
37
    rules,
36
38
    trace,
37
39
    )
 
40
from bzrlib.i18n import gettext
38
41
""")
39
42
 
40
43
from bzrlib.decorators import needs_read_lock
58
61
    trees or versioned trees.
59
62
    """
60
63
 
 
64
    def has_versioned_directories(self):
 
65
        """Whether this tree can contain explicitly versioned directories.
 
66
 
 
67
        This defaults to True, but some implementations may want to override
 
68
        it.
 
69
        """
 
70
        return True
 
71
 
61
72
    def changes_from(self, other, want_unchanged=False, specific_files=None,
62
73
        extra_trees=None, require_versioned=False, include_root=False,
63
74
        want_unversioned=False):
277
288
 
278
289
        :param file_id: The file_id of the file.
279
290
        :param path: The path of the file.
 
291
 
280
292
        If both file_id and path are supplied, an implementation may use
281
293
        either one.
 
294
 
 
295
        :returns: A single byte string for the whole file.
282
296
        """
283
297
        my_file = self.get_file(file_id, path)
284
298
        try:
297
311
        """
298
312
        return osutils.split_lines(self.get_file_text(file_id, path))
299
313
 
 
314
    def get_file_verifier(self, file_id, path=None, stat_value=None):
 
315
        """Return a verifier for a file.
 
316
 
 
317
        The default implementation returns a sha1.
 
318
 
 
319
        :param file_id: The handle for this file.
 
320
        :param path: The path that this file can be found at.
 
321
            These must point to the same object.
 
322
        :param stat_value: Optional stat value for the object
 
323
        :return: Tuple with verifier name and verifier data
 
324
        """
 
325
        return ("SHA1", self.get_file_sha1(file_id, path=path,
 
326
            stat_value=stat_value))
 
327
 
300
328
    def get_file_sha1(self, file_id, path=None, stat_value=None):
301
329
        """Return the SHA1 file for a file.
302
330
 
 
331
        :note: callers should use get_file_verifier instead
 
332
            where possible, as the underlying repository implementation may
 
333
            have quicker access to a non-sha1 verifier.
 
334
 
303
335
        :param file_id: The handle for this file.
304
336
        :param path: The path that this file can be found at.
305
337
            These must point to the same object.
325
357
        """
326
358
        raise NotImplementedError(self.get_file_size)
327
359
 
328
 
    def get_file_by_path(self, path):
329
 
        raise NotImplementedError(self.get_file_by_path)
330
 
 
331
360
    def is_executable(self, file_id, path=None):
332
361
        """Check if a file is executable.
333
362
 
503
532
        return find_ids_across_trees(paths, [self] + list(trees), require_versioned)
504
533
 
505
534
    def iter_children(self, file_id):
506
 
        entry = self.iter_entries_by_dir([file_id]).next()[1]
507
 
        for child in getattr(entry, 'children', {}).itervalues():
508
 
            yield child.file_id
 
535
        """Iterate over the file ids of the children of an entry.
 
536
 
 
537
        :param file_id: File id of the entry
 
538
        :return: Iterator over child file ids.
 
539
        """
 
540
        raise NotImplementedError(self.iter_children)
509
541
 
510
542
    def lock_read(self):
511
543
        """Lock this tree for multiple read only operations.
512
 
        
 
544
 
513
545
        :return: A bzrlib.lock.LogicalLockResult.
514
546
        """
515
547
        pass
602
634
        prefs = self.iter_search_rules([path], filter_pref_names).next()
603
635
        stk = filters._get_filter_stack_for(prefs)
604
636
        if 'filters' in debug.debug_flags:
605
 
            trace.note("*** %s content-filter: %s => %r" % (path,prefs,stk))
 
637
            trace.note(gettext("*** {0} content-filter: {1} => {2!r}").format(path,prefs,stk))
606
638
        return stk
607
639
 
608
640
    def _content_filter_stack_provider(self):
739
771
            yield cur_path
740
772
        # all done.
741
773
 
 
774
    @deprecated_method(deprecated_in((2, 5, 0)))
742
775
    def _get_inventory(self):
743
776
        return self._inventory
744
777
 
745
778
    inventory = property(_get_inventory,
746
779
                         doc="Inventory of this Tree")
747
780
 
 
781
    def _get_root_inventory(self):
 
782
        return self._inventory
 
783
 
 
784
    root_inventory = property(_get_root_inventory,
 
785
        doc="Root inventory of this tree")
 
786
 
 
787
    def _unpack_file_id(self, file_id):
 
788
        """Find the inventory and inventory file id for a tree file id.
 
789
 
 
790
        :param file_id: The tree file id, as bytestring or tuple
 
791
        :return: Inventory and inventory file id
 
792
        """
 
793
        if isinstance(file_id, tuple):
 
794
            if len(file_id) != 1:
 
795
                raise ValueError("nested trees not yet supported: %r" % file_id)
 
796
            file_id = file_id[0]
 
797
        return self.root_inventory, file_id
 
798
 
748
799
    @needs_read_lock
749
800
    def path2id(self, path):
750
801
        """Return the id for path in this tree."""
751
 
        return self._inventory.path2id(path)
 
802
        return self._path2inv_file_id(path)[1]
 
803
 
 
804
    def _path2inv_file_id(self, path):
 
805
        """Lookup a inventory and inventory file id by path.
 
806
 
 
807
        :param path: Path to look up
 
808
        :return: tuple with inventory and inventory file id
 
809
        """
 
810
        # FIXME: Support nested trees
 
811
        return self.root_inventory, self.root_inventory.path2id(path)
752
812
 
753
813
    def id2path(self, file_id):
754
814
        """Return the path for a file id.
755
815
 
756
816
        :raises NoSuchId:
757
817
        """
758
 
        return self.inventory.id2path(file_id)
 
818
        inventory, file_id = self._unpack_file_id(file_id)
 
819
        return inventory.id2path(file_id)
759
820
 
760
821
    def has_id(self, file_id):
761
 
        return self.inventory.has_id(file_id)
 
822
        inventory, file_id = self._unpack_file_id(file_id)
 
823
        return inventory.has_id(file_id)
762
824
 
763
825
    def has_or_had_id(self, file_id):
764
 
        return self.inventory.has_id(file_id)
 
826
        inventory, file_id = self._unpack_file_id(file_id)
 
827
        return inventory.has_id(file_id)
765
828
 
766
829
    def all_file_ids(self):
767
 
        return set(self.inventory)
 
830
        return set(
 
831
            [entry.file_id for path, entry in self.iter_entries_by_dir()])
768
832
 
769
833
    @deprecated_method(deprecated_in((2, 4, 0)))
770
834
    def __iter__(self):
771
 
        return iter(self.inventory)
 
835
        return iter(self.all_file_ids())
772
836
 
773
837
    def filter_unversioned_files(self, paths):
774
838
        """Filter out paths that are versioned.
778
842
        # NB: we specifically *don't* call self.has_filename, because for
779
843
        # WorkingTrees that can indicate files that exist on disk but that
780
844
        # are not versioned.
781
 
        pred = self.inventory.has_filename
782
 
        return set((p for p in paths if not pred(p)))
 
845
        return set((p for p in paths if self.path2id(p) is None))
783
846
 
784
847
    @needs_read_lock
785
848
    def iter_entries_by_dir(self, specific_file_ids=None, yield_parents=False):
794
857
            down to specific_file_ids that have been requested. This has no
795
858
            impact if specific_file_ids is None.
796
859
        """
797
 
        return self.inventory.iter_entries_by_dir(
798
 
            specific_file_ids=specific_file_ids, yield_parents=yield_parents)
 
860
        if specific_file_ids is None:
 
861
            inventory_file_ids = None
 
862
        else:
 
863
            inventory_file_ids = []
 
864
            for tree_file_id in specific_file_ids:
 
865
                inventory, inv_file_id = self._unpack_file_id(tree_file_id)
 
866
                if not inventory is self.root_inventory: # for now
 
867
                    raise AssertionError("%r != %r" % (
 
868
                        inventory, self.root_inventory))
 
869
                inventory_file_ids.append(inv_file_id)
 
870
        # FIXME: Handle nested trees
 
871
        return self.root_inventory.iter_entries_by_dir(
 
872
            specific_file_ids=inventory_file_ids, yield_parents=yield_parents)
799
873
 
 
874
    @deprecated_method(deprecated_in((2, 5, 0)))
800
875
    def get_file_by_path(self, path):
801
 
        return self.get_file(self._inventory.path2id(path), path)
 
876
        return self.get_file(self.path2id(path), path)
 
877
 
 
878
    def iter_children(self, file_id, path=None):
 
879
        """See Tree.iter_children."""
 
880
        entry = self.iter_entries_by_dir([file_id]).next()[1]
 
881
        for child in getattr(entry, 'children', {}).itervalues():
 
882
            yield child.file_id
802
883
 
803
884
 
804
885
def find_ids_across_trees(filenames, trees, require_versioned=True):
952
1033
        if source_kind != target_kind:
953
1034
            changed_content = True
954
1035
        elif source_kind == 'file':
955
 
            if (self.source.get_file_sha1(file_id, source_path, source_stat) !=
956
 
                self.target.get_file_sha1(file_id, target_path, target_stat)):
 
1036
            if not self.file_content_matches(file_id, file_id, source_path,
 
1037
                    target_path, source_stat, target_stat):
957
1038
                changed_content = True
958
1039
        elif source_kind == 'symlink':
959
1040
            if (self.source.get_symlink_target(file_id) !=
960
1041
                self.target.get_symlink_target(file_id)):
961
1042
                changed_content = True
962
 
            # XXX: Yes, the indentation below is wrong. But fixing it broke
963
 
            # test_merge.TestMergerEntriesLCAOnDisk.
964
 
            # test_nested_tree_subtree_renamed_and_modified. We'll wait for
965
 
            # the fix from bzr.dev -- vila 2009026
966
 
            elif source_kind == 'tree-reference':
967
 
                if (self.source.get_reference_revision(file_id, source_path)
968
 
                    != self.target.get_reference_revision(file_id, target_path)):
 
1043
        elif source_kind == 'tree-reference':
 
1044
            if (self.source.get_reference_revision(file_id, source_path)
 
1045
                != self.target.get_reference_revision(file_id, target_path)):
969
1046
                    changed_content = True
970
1047
        parent = (source_parent, target_parent)
971
1048
        name = (source_name, target_name)
1186
1263
        :param file_id: The file_id to lookup.
1187
1264
        """
1188
1265
        try:
1189
 
            inventory = tree.inventory
 
1266
            inventory = tree.root_inventory
1190
1267
        except NotImplementedError:
1191
1268
            # No inventory available.
1192
1269
            try:
1267
1344
                        if old_entry is None:
1268
1345
                            # Reusing a discarded change.
1269
1346
                            old_entry = self._get_entry(self.source, file_id)
1270
 
                        for child in old_entry.children.values():
1271
 
                            precise_file_ids.add(child.file_id)
 
1347
                        for child in self.source.iter_children(file_id):
 
1348
                            precise_file_ids.add(child)
1272
1349
                    changed_file_ids.add(result[0])
1273
1350
                    yield result
1274
1351
 
 
1352
    @needs_read_lock
 
1353
    def file_content_matches(self, source_file_id, target_file_id,
 
1354
            source_path=None, target_path=None, source_stat=None, target_stat=None):
 
1355
        """Check if two files are the same in the source and target trees.
 
1356
 
 
1357
        This only checks that the contents of the files are the same,
 
1358
        it does not touch anything else.
 
1359
 
 
1360
        :param source_file_id: File id of the file in the source tree
 
1361
        :param target_file_id: File id of the file in the target tree
 
1362
        :param source_path: Path of the file in the source tree
 
1363
        :param target_path: Path of the file in the target tree
 
1364
        :param source_stat: Optional stat value of the file in the source tree
 
1365
        :param target_stat: Optional stat value of the file in the target tree
 
1366
        :return: Boolean indicating whether the files have the same contents
 
1367
        """
 
1368
        source_verifier_kind, source_verifier_data = self.source.get_file_verifier(
 
1369
            source_file_id, source_path, source_stat)
 
1370
        target_verifier_kind, target_verifier_data = self.target.get_file_verifier(
 
1371
            target_file_id, target_path, target_stat)
 
1372
        if source_verifier_kind == target_verifier_kind:
 
1373
            return (source_verifier_data == target_verifier_data)
 
1374
        # Fall back to SHA1 for now
 
1375
        if source_verifier_kind != "SHA1":
 
1376
            source_sha1 = self.source.get_file_sha1(source_file_id,
 
1377
                    source_path, source_stat)
 
1378
        else:
 
1379
            source_sha1 = source_verifier_data
 
1380
        if target_verifier_kind != "SHA1":
 
1381
            target_sha1 = self.target.get_file_sha1(target_file_id,
 
1382
                    target_path, target_stat)
 
1383
        else:
 
1384
            target_sha1 = target_verifier_data
 
1385
        return (source_sha1 == target_sha1)
1275
1386
 
1276
1387
InterTree.register_optimiser(InterTree)
1277
1388
 
1383
1494
            return (None, None)
1384
1495
        else:
1385
1496
            self._out_of_order_processed.add(file_id)
1386
 
            cur_ie = other_tree.inventory[file_id]
 
1497
            cur_ie = other_tree.root_inventory[file_id]
1387
1498
            return (cur_path, cur_ie)
1388
1499
 
1389
1500
    def iter_all(self):