~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/inventory.py

  • Committer: Tarmac
  • Author(s): Vincent Ladeuil
  • Date: 2017-01-30 14:42:05 UTC
  • mfrom: (6620.1.1 trunk)
  • Revision ID: tarmac-20170130144205-r8fh2xpmiuxyozpv
Merge  2.7 into trunk including fix for bug #1657238 [r=vila]

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
# But those depend on its position within a particular inventory, and
24
24
# it would be nice not to need to hold the backpointer here.
25
25
 
 
26
from __future__ import absolute_import
 
27
 
26
28
# This should really be an id randomly assigned when the tree is
27
29
# created, but it's not for now.
28
30
ROOT_ID = "TREE_ROOT"
631
633
    inserted, other than through the Inventory API.
632
634
    """
633
635
 
634
 
    @deprecated_method(deprecated_in((2, 4, 0)))
635
 
    def __contains__(self, file_id):
636
 
        """True if this entry contains a file with given id.
637
 
 
638
 
        >>> inv = Inventory()
639
 
        >>> inv.add(InventoryFile('123', 'foo.c', ROOT_ID))
640
 
        InventoryFile('123', 'foo.c', parent_id='TREE_ROOT', sha1=None, len=None, revision=None)
641
 
        >>> inv.has_id('123')
642
 
        True
643
 
        >>> inv.has_id('456')
644
 
        False
645
 
 
646
 
        Note that this method along with __iter__ are not encouraged for use as
647
 
        they are less clear than specific query methods - they may be rmeoved
648
 
        in the future.
649
 
        """
650
 
        return self.has_id(file_id)
651
 
 
652
636
    def has_filename(self, filename):
653
637
        return bool(self.path2id(filename))
654
638
 
848
832
            descend(self.root, u'')
849
833
        return accum
850
834
 
851
 
    def directories(self):
852
 
        """Return (path, entry) pairs for all directories, including the root.
853
 
        """
854
 
        accum = []
855
 
        def descend(parent_ie, parent_path):
856
 
            accum.append((parent_path, parent_ie))
857
 
 
858
 
            kids = [(ie.name, ie) for ie in parent_ie.children.itervalues() if ie.kind == 'directory']
859
 
            kids.sort()
860
 
 
861
 
            for name, child_ie in kids:
862
 
                child_path = osutils.pathjoin(parent_path, name)
863
 
                descend(child_ie, child_path)
864
 
        descend(self.root, u'')
865
 
        return accum
866
 
 
867
835
    def path2id(self, relpath):
868
836
        """Walk down through directories to return entry of last component.
869
837
 
1490
1458
            if entry.kind == 'directory':
1491
1459
                directories_to_expand.add(entry.file_id)
1492
1460
            interesting.add(entry.parent_id)
1493
 
            children_of_parent_id.setdefault(entry.parent_id, []
1494
 
                                             ).append(entry.file_id)
 
1461
            children_of_parent_id.setdefault(entry.parent_id, set()
 
1462
                                             ).add(entry.file_id)
1495
1463
 
1496
1464
        # Now, interesting has all of the direct parents, but not the
1497
1465
        # parents of those parents. It also may have some duplicates with
1505
1473
            next_parents = set()
1506
1474
            for entry in self._getitems(remaining_parents):
1507
1475
                next_parents.add(entry.parent_id)
1508
 
                children_of_parent_id.setdefault(entry.parent_id, []
1509
 
                                                 ).append(entry.file_id)
 
1476
                children_of_parent_id.setdefault(entry.parent_id, set()
 
1477
                                                 ).add(entry.file_id)
1510
1478
            # Remove any search tips we've already processed
1511
1479
            remaining_parents = next_parents.difference(interesting)
1512
1480
            interesting.update(remaining_parents)
1525
1493
            for entry in self._getitems(next_file_ids):
1526
1494
                if entry.kind == 'directory':
1527
1495
                    directories_to_expand.add(entry.file_id)
1528
 
                children_of_parent_id.setdefault(entry.parent_id, []
1529
 
                                                 ).append(entry.file_id)
 
1496
                children_of_parent_id.setdefault(entry.parent_id, set()
 
1497
                                                 ).add(entry.file_id)
1530
1498
        return interesting, children_of_parent_id
1531
1499
 
1532
1500
    def filter(self, specific_fileids):
2125
2093
    def path2id(self, relpath):
2126
2094
        """See CommonInventory.path2id()."""
2127
2095
        # TODO: perhaps support negative hits?
 
2096
        if isinstance(relpath, basestring):
 
2097
            names = osutils.splitpath(relpath)
 
2098
        else:
 
2099
            names = relpath
 
2100
            if relpath == []:
 
2101
                relpath = [""]
 
2102
            relpath = osutils.pathjoin(*relpath)
2128
2103
        result = self._path_to_fileid_cache.get(relpath, None)
2129
2104
        if result is not None:
2130
2105
            return result
2131
 
        if isinstance(relpath, basestring):
2132
 
            names = osutils.splitpath(relpath)
2133
 
        else:
2134
 
            names = relpath
2135
2106
        current_id = self.root_id
2136
2107
        if current_id is None:
2137
2108
            return None