~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/inventory.py

Merge bzr.dev, update to use new hooks.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
31
31
lazy_import(globals(), """
32
32
import collections
33
33
import copy
34
 
import os
35
34
import re
36
35
import tarfile
37
36
 
43
42
    )
44
43
""")
45
44
 
46
 
from bzrlib.errors import (
47
 
    BzrCheckError,
48
 
    BzrError,
 
45
from bzrlib import (
 
46
    lazy_regex,
 
47
    trace,
49
48
    )
50
 
from bzrlib.trace import mutter
 
49
 
51
50
from bzrlib.static_tuple import StaticTuple
 
51
from bzrlib.symbol_versioning import (
 
52
    deprecated_in,
 
53
    deprecated_method,
 
54
    )
52
55
 
53
56
 
54
57
class InventoryEntry(object):
101
104
    InventoryDirectory('2325', 'wibble', parent_id='123', revision=None)
102
105
    >>> i.path2id('src/wibble')
103
106
    '2325'
104
 
    >>> '2325' in i
105
 
    True
106
107
    >>> i.add(InventoryFile('2326', 'wibble.c', '2325'))
107
108
    InventoryFile('2326', 'wibble.c', parent_id='2325', sha1=None, len=None, revision=None)
108
109
    >>> i['2326']
171
172
        candidates = {}
172
173
        # identify candidate head revision ids.
173
174
        for inv in previous_inventories:
174
 
            if self.file_id in inv:
 
175
            if inv.has_id(self.file_id):
175
176
                ie = inv[self.file_id]
176
177
                if ie.revision in candidates:
177
178
                    # same revision value in two different inventories:
224
225
 
225
226
    def kind_character(self):
226
227
        """Return a short kind indicator useful for appending to names."""
227
 
        raise BzrError('unknown kind %r' % self.kind)
 
228
        raise errors.BzrError('unknown kind %r' % self.kind)
228
229
 
229
230
    known_kinds = ('file', 'directory', 'symlink')
230
231
 
250
251
        """
251
252
        if self.parent_id is not None:
252
253
            if not inv.has_id(self.parent_id):
253
 
                raise BzrCheckError('missing parent {%s} in inventory for revision {%s}'
254
 
                        % (self.parent_id, rev_id))
 
254
                raise errors.BzrCheckError(
 
255
                    'missing parent {%s} in inventory for revision {%s}' % (
 
256
                        self.parent_id, rev_id))
255
257
        checker._add_entry_to_text_key_references(inv, self)
256
258
        self._check(checker, rev_id)
257
259
 
539
541
        # FIXME: which _modified field should we use ? RBC 20051003
540
542
        text_modified = (self.symlink_target != old_entry.symlink_target)
541
543
        if text_modified:
542
 
            mutter("    symlink target changed")
 
544
            trace.mutter("    symlink target changed")
543
545
        meta_modified = False
544
546
        return text_modified, meta_modified
545
547
 
629
631
    inserted, other than through the Inventory API.
630
632
    """
631
633
 
 
634
    @deprecated_method(deprecated_in((2, 4, 0)))
632
635
    def __contains__(self, file_id):
633
636
        """True if this entry contains a file with given id.
634
637
 
635
638
        >>> inv = Inventory()
636
639
        >>> inv.add(InventoryFile('123', 'foo.c', ROOT_ID))
637
640
        InventoryFile('123', 'foo.c', parent_id='TREE_ROOT', sha1=None, len=None, revision=None)
638
 
        >>> '123' in inv
 
641
        >>> inv.has_id('123')
639
642
        True
640
 
        >>> '456' in inv
 
643
        >>> inv.has_id('456')
641
644
        False
642
645
 
643
646
        Note that this method along with __iter__ are not encouraged for use as
718
721
                # if we finished all children, pop it off the stack
719
722
                stack.pop()
720
723
 
 
724
    def _preload_cache(self):
 
725
        """Populate any caches, we are about to access all items.
 
726
        
 
727
        The default implementation does nothing, because CommonInventory doesn't
 
728
        have a cache.
 
729
        """
 
730
        pass
 
731
    
721
732
    def iter_entries_by_dir(self, from_dir=None, specific_file_ids=None,
722
733
        yield_parents=False):
723
734
        """Iterate over the entries in a directory first order.
736
747
            specific_file_ids = set(specific_file_ids)
737
748
        # TODO? Perhaps this should return the from_dir so that the root is
738
749
        # yielded? or maybe an option?
 
750
        if from_dir is None and specific_file_ids is None:
 
751
            # They are iterating from the root, and have not specified any
 
752
            # specific entries to look at. All current callers fully consume the
 
753
            # iterator, so we can safely assume we are accessing all entries
 
754
            self._preload_cache()
739
755
        if from_dir is None:
740
756
            if self.root is None:
741
757
                return
743
759
            if (not yield_parents and specific_file_ids is not None and
744
760
                len(specific_file_ids) == 1):
745
761
                file_id = list(specific_file_ids)[0]
746
 
                if file_id in self:
 
762
                if self.has_id(file_id):
747
763
                    yield self.id2path(file_id), self[file_id]
748
764
                return
749
765
            from_dir = self.root
759
775
            parents = set()
760
776
            byid = self
761
777
            def add_ancestors(file_id):
762
 
                if file_id not in byid:
 
778
                if not byid.has_id(file_id):
763
779
                    return
764
780
                parent_id = byid[file_id].parent_id
765
781
                if parent_id is None:
809
825
                    file_id, self[file_id]))
810
826
        return delta
811
827
 
812
 
    def _get_mutable_inventory(self):
813
 
        """Returns a mutable copy of the object.
814
 
 
815
 
        Some inventories are immutable, yet working trees, for example, needs
816
 
        to mutate exisiting inventories instead of creating a new one.
817
 
        """
818
 
        raise NotImplementedError(self._get_mutable_inventory)
819
 
 
820
828
    def make_entry(self, kind, name, parent_id, file_id=None):
821
829
        """Simple thunk to bzrlib.inventory.make_entry."""
822
830
        return make_entry(kind, name, parent_id, file_id)
957
965
 
958
966
    >>> inv.path2id('hello.c')
959
967
    '123-123'
960
 
    >>> '123-123' in inv
 
968
    >>> inv.has_id('123-123')
961
969
    True
962
970
 
963
971
    There are iterators over the contents:
1120
1128
            other.add(entry.copy())
1121
1129
        return other
1122
1130
 
1123
 
    def _get_mutable_inventory(self):
1124
 
        """See CommonInventory._get_mutable_inventory."""
1125
 
        return copy.deepcopy(self)
1126
 
 
1127
1131
    def __iter__(self):
1128
1132
        """Iterate over all file-ids."""
1129
1133
        return iter(self._byid)
1169
1173
    def _add_child(self, entry):
1170
1174
        """Add an entry to the inventory, without adding it to its parent"""
1171
1175
        if entry.file_id in self._byid:
1172
 
            raise BzrError("inventory already contains entry with id {%s}" %
1173
 
                           entry.file_id)
 
1176
            raise errors.BzrError(
 
1177
                "inventory already contains entry with id {%s}" %
 
1178
                entry.file_id)
1174
1179
        self._byid[entry.file_id] = entry
1175
1180
        for child in getattr(entry, 'children', {}).itervalues():
1176
1181
            self._add_child(child)
1229
1234
        >>> inv = Inventory()
1230
1235
        >>> inv.add(InventoryFile('123', 'foo.c', ROOT_ID))
1231
1236
        InventoryFile('123', 'foo.c', parent_id='TREE_ROOT', sha1=None, len=None, revision=None)
1232
 
        >>> '123' in inv
 
1237
        >>> inv.has_id('123')
1233
1238
        True
1234
1239
        >>> del inv['123']
1235
 
        >>> '123' in inv
 
1240
        >>> inv.has_id('123')
1236
1241
        False
1237
1242
        """
1238
1243
        ie = self[file_id]
1340
1345
        """
1341
1346
        new_name = ensure_normalized_name(new_name)
1342
1347
        if not is_valid_name(new_name):
1343
 
            raise BzrError("not an acceptable filename: %r" % new_name)
 
1348
            raise errors.BzrError("not an acceptable filename: %r" % new_name)
1344
1349
 
1345
1350
        new_parent = self._byid[new_parent_id]
1346
1351
        if new_name in new_parent.children:
1347
 
            raise BzrError("%r already exists in %r" % (new_name, self.id2path(new_parent_id)))
 
1352
            raise errors.BzrError("%r already exists in %r" %
 
1353
                (new_name, self.id2path(new_parent_id)))
1348
1354
 
1349
1355
        new_parent_idpath = self.get_idpath(new_parent_id)
1350
1356
        if file_id in new_parent_idpath:
1351
 
            raise BzrError("cannot move directory %r into a subdirectory of itself, %r"
 
1357
            raise errors.BzrError(
 
1358
                "cannot move directory %r into a subdirectory of itself, %r"
1352
1359
                    % (self.id2path(file_id), self.id2path(new_parent_id)))
1353
1360
 
1354
1361
        file_ie = self._byid[file_id]
1390
1397
    def __init__(self, search_key_name):
1391
1398
        CommonInventory.__init__(self)
1392
1399
        self._fileid_to_entry_cache = {}
 
1400
        self._fully_cached = False
1393
1401
        self._path_to_fileid_cache = {}
1394
1402
        self._search_key_name = search_key_name
1395
1403
        self.root_id = None
1482
1490
            if entry.kind == 'directory':
1483
1491
                directories_to_expand.add(entry.file_id)
1484
1492
            interesting.add(entry.parent_id)
1485
 
            children_of_parent_id.setdefault(entry.parent_id, []
1486
 
                                             ).append(entry.file_id)
 
1493
            children_of_parent_id.setdefault(entry.parent_id, set()
 
1494
                                             ).add(entry.file_id)
1487
1495
 
1488
1496
        # Now, interesting has all of the direct parents, but not the
1489
1497
        # parents of those parents. It also may have some duplicates with
1497
1505
            next_parents = set()
1498
1506
            for entry in self._getitems(remaining_parents):
1499
1507
                next_parents.add(entry.parent_id)
1500
 
                children_of_parent_id.setdefault(entry.parent_id, []
1501
 
                                                 ).append(entry.file_id)
 
1508
                children_of_parent_id.setdefault(entry.parent_id, set()
 
1509
                                                 ).add(entry.file_id)
1502
1510
            # Remove any search tips we've already processed
1503
1511
            remaining_parents = next_parents.difference(interesting)
1504
1512
            interesting.update(remaining_parents)
1517
1525
            for entry in self._getitems(next_file_ids):
1518
1526
                if entry.kind == 'directory':
1519
1527
                    directories_to_expand.add(entry.file_id)
1520
 
                children_of_parent_id.setdefault(entry.parent_id, []
1521
 
                                                 ).append(entry.file_id)
 
1528
                children_of_parent_id.setdefault(entry.parent_id, set()
 
1529
                                                 ).add(entry.file_id)
1522
1530
        return interesting, children_of_parent_id
1523
1531
 
1524
1532
    def filter(self, specific_fileids):
1602
1610
        self._fileid_to_entry_cache[result.file_id] = result
1603
1611
        return result
1604
1612
 
1605
 
    def _get_mutable_inventory(self):
1606
 
        """See CommonInventory._get_mutable_inventory."""
1607
 
        entries = self.iter_entries()
1608
 
        inv = Inventory(None, self.revision_id)
1609
 
        for path, inv_entry in entries:
1610
 
            inv.add(inv_entry.copy())
1611
 
        return inv
1612
 
 
1613
1613
    def create_by_apply_delta(self, inventory_delta, new_revision_id,
1614
1614
        propagate_caches=False):
1615
1615
        """Create a new CHKInventory by applying inventory_delta to this one.
1956
1956
 
1957
1957
    def iter_just_entries(self):
1958
1958
        """Iterate over all entries.
1959
 
        
 
1959
 
1960
1960
        Unlike iter_entries(), just the entries are returned (not (path, ie))
1961
1961
        and the order of entries is undefined.
1962
1962
 
1970
1970
                self._fileid_to_entry_cache[file_id] = ie
1971
1971
            yield ie
1972
1972
 
 
1973
    def _preload_cache(self):
 
1974
        """Make sure all file-ids are in _fileid_to_entry_cache"""
 
1975
        if self._fully_cached:
 
1976
            return # No need to do it again
 
1977
        # The optimal sort order is to use iteritems() directly
 
1978
        cache = self._fileid_to_entry_cache
 
1979
        for key, entry in self.id_to_entry.iteritems():
 
1980
            file_id = key[0]
 
1981
            if file_id not in cache:
 
1982
                ie = self._bytes_to_entry(entry)
 
1983
                cache[file_id] = ie
 
1984
            else:
 
1985
                ie = cache[file_id]
 
1986
        last_parent_id = last_parent_ie = None
 
1987
        pid_items = self.parent_id_basename_to_file_id.iteritems()
 
1988
        for key, child_file_id in pid_items:
 
1989
            if key == ('', ''): # This is the root
 
1990
                if child_file_id != self.root_id:
 
1991
                    raise ValueError('Data inconsistency detected.'
 
1992
                        ' We expected data with key ("","") to match'
 
1993
                        ' the root id, but %s != %s'
 
1994
                        % (child_file_id, self.root_id))
 
1995
                continue
 
1996
            parent_id, basename = key
 
1997
            ie = cache[child_file_id]
 
1998
            if parent_id == last_parent_id:
 
1999
                parent_ie = last_parent_ie
 
2000
            else:
 
2001
                parent_ie = cache[parent_id]
 
2002
            if parent_ie.kind != 'directory':
 
2003
                raise ValueError('Data inconsistency detected.'
 
2004
                    ' An entry in the parent_id_basename_to_file_id map'
 
2005
                    ' has parent_id {%s} but the kind of that object'
 
2006
                    ' is %r not "directory"' % (parent_id, parent_ie.kind))
 
2007
            if parent_ie._children is None:
 
2008
                parent_ie._children = {}
 
2009
            basename = basename.decode('utf-8')
 
2010
            if basename in parent_ie._children:
 
2011
                existing_ie = parent_ie._children[basename]
 
2012
                if existing_ie != ie:
 
2013
                    raise ValueError('Data inconsistency detected.'
 
2014
                        ' Two entries with basename %r were found'
 
2015
                        ' in the parent entry {%s}'
 
2016
                        % (basename, parent_id))
 
2017
            if basename != ie.name:
 
2018
                raise ValueError('Data inconsistency detected.'
 
2019
                    ' In the parent_id_basename_to_file_id map, file_id'
 
2020
                    ' {%s} is listed as having basename %r, but in the'
 
2021
                    ' id_to_entry map it is %r'
 
2022
                    % (child_file_id, basename, ie.name))
 
2023
            parent_ie._children[basename] = ie
 
2024
        self._fully_cached = True
 
2025
 
1973
2026
    def iter_changes(self, basis):
1974
2027
        """Generate a Tree.iter_changes change list between this and basis.
1975
2028
 
2232
2285
    return name
2233
2286
 
2234
2287
 
2235
 
_NAME_RE = None
 
2288
_NAME_RE = lazy_regex.lazy_compile(r'^[^/\\]+$')
2236
2289
 
2237
2290
def is_valid_name(name):
2238
 
    global _NAME_RE
2239
 
    if _NAME_RE is None:
2240
 
        _NAME_RE = re.compile(r'^[^/\\]+$')
2241
 
 
2242
2291
    return bool(_NAME_RE.match(name))
2243
2292
 
2244
2293
 
2334
2383
            raise errors.InconsistentDelta(new_path, item[1],
2335
2384
                "new_path with no entry")
2336
2385
        yield item
 
2386
 
 
2387
 
 
2388
def mutable_inventory_from_tree(tree):
 
2389
    """Create a new inventory that has the same contents as a specified tree.
 
2390
 
 
2391
    :param tree: Revision tree to create inventory from
 
2392
    """
 
2393
    entries = tree.iter_entries_by_dir()
 
2394
    inv = Inventory(None, tree.get_revision_id())
 
2395
    for path, inv_entry in entries:
 
2396
        inv.add(inv_entry.copy())
 
2397
    return inv