104
101
InventoryDirectory('2325', 'wibble', parent_id='123', revision=None)
105
102
>>> i.path2id('src/wibble')
107
106
>>> i.add(InventoryFile('2326', 'wibble.c', '2325'))
108
107
InventoryFile('2326', 'wibble.c', parent_id='2325', sha1=None, len=None, revision=None)
252
251
if self.parent_id is not None:
253
252
if not inv.has_id(self.parent_id):
254
raise errors.BzrCheckError(
255
'missing parent {%s} in inventory for revision {%s}' % (
256
self.parent_id, rev_id))
253
raise BzrCheckError('missing parent {%s} in inventory for revision {%s}'
254
% (self.parent_id, rev_id))
257
255
checker._add_entry_to_text_key_references(inv, self)
258
256
self._check(checker, rev_id)
631
629
inserted, other than through the Inventory API.
634
@deprecated_method(deprecated_in((2, 4, 0)))
635
632
def __contains__(self, file_id):
636
633
"""True if this entry contains a file with given id.
638
635
>>> inv = Inventory()
639
636
>>> inv.add(InventoryFile('123', 'foo.c', ROOT_ID))
640
637
InventoryFile('123', 'foo.c', parent_id='TREE_ROOT', sha1=None, len=None, revision=None)
641
>>> inv.has_id('123')
643
>>> inv.has_id('456')
646
643
Note that this method along with __iter__ are not encouraged for use as
721
718
# if we finished all children, pop it off the stack
724
def _preload_cache(self):
725
"""Populate any caches, we are about to access all items.
727
The default implementation does nothing, because CommonInventory doesn't
732
721
def iter_entries_by_dir(self, from_dir=None, specific_file_ids=None,
733
722
yield_parents=False):
734
723
"""Iterate over the entries in a directory first order.
747
736
specific_file_ids = set(specific_file_ids)
748
737
# TODO? Perhaps this should return the from_dir so that the root is
749
738
# 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()
755
739
if from_dir is None:
756
740
if self.root is None:
825
809
file_id, self[file_id]))
812
def _get_mutable_inventory(self):
813
"""Returns a mutable copy of the object.
815
Some inventories are immutable, yet working trees, for example, needs
816
to mutate exisiting inventories instead of creating a new one.
818
raise NotImplementedError(self._get_mutable_inventory)
828
820
def make_entry(self, kind, name, parent_id, file_id=None):
829
821
"""Simple thunk to bzrlib.inventory.make_entry."""
830
822
return make_entry(kind, name, parent_id, file_id)
1173
1169
def _add_child(self, entry):
1174
1170
"""Add an entry to the inventory, without adding it to its parent"""
1175
1171
if entry.file_id in self._byid:
1176
raise errors.BzrError(
1177
"inventory already contains entry with id {%s}" %
1172
raise BzrError("inventory already contains entry with id {%s}" %
1179
1174
self._byid[entry.file_id] = entry
1180
1175
for child in getattr(entry, 'children', {}).itervalues():
1181
1176
self._add_child(child)
1346
1341
new_name = ensure_normalized_name(new_name)
1347
1342
if not is_valid_name(new_name):
1348
raise errors.BzrError("not an acceptable filename: %r" % new_name)
1343
raise BzrError("not an acceptable filename: %r" % new_name)
1350
1345
new_parent = self._byid[new_parent_id]
1351
1346
if new_name in new_parent.children:
1352
raise errors.BzrError("%r already exists in %r" %
1353
(new_name, self.id2path(new_parent_id)))
1347
raise BzrError("%r already exists in %r" % (new_name, self.id2path(new_parent_id)))
1355
1349
new_parent_idpath = self.get_idpath(new_parent_id)
1356
1350
if file_id in new_parent_idpath:
1357
raise errors.BzrError(
1358
"cannot move directory %r into a subdirectory of itself, %r"
1351
raise BzrError("cannot move directory %r into a subdirectory of itself, %r"
1359
1352
% (self.id2path(file_id), self.id2path(new_parent_id)))
1361
1354
file_ie = self._byid[file_id]
1490
1482
if entry.kind == 'directory':
1491
1483
directories_to_expand.add(entry.file_id)
1492
1484
interesting.add(entry.parent_id)
1493
children_of_parent_id.setdefault(entry.parent_id, set()
1494
).add(entry.file_id)
1485
children_of_parent_id.setdefault(entry.parent_id, []
1486
).append(entry.file_id)
1496
1488
# Now, interesting has all of the direct parents, but not the
1497
1489
# parents of those parents. It also may have some duplicates with
1505
1497
next_parents = set()
1506
1498
for entry in self._getitems(remaining_parents):
1507
1499
next_parents.add(entry.parent_id)
1508
children_of_parent_id.setdefault(entry.parent_id, set()
1509
).add(entry.file_id)
1500
children_of_parent_id.setdefault(entry.parent_id, []
1501
).append(entry.file_id)
1510
1502
# Remove any search tips we've already processed
1511
1503
remaining_parents = next_parents.difference(interesting)
1512
1504
interesting.update(remaining_parents)
1525
1517
for entry in self._getitems(next_file_ids):
1526
1518
if entry.kind == 'directory':
1527
1519
directories_to_expand.add(entry.file_id)
1528
children_of_parent_id.setdefault(entry.parent_id, set()
1529
).add(entry.file_id)
1520
children_of_parent_id.setdefault(entry.parent_id, []
1521
).append(entry.file_id)
1530
1522
return interesting, children_of_parent_id
1532
1524
def filter(self, specific_fileids):
1610
1602
self._fileid_to_entry_cache[result.file_id] = result
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())
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.
1970
1970
self._fileid_to_entry_cache[file_id] = ie
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():
1981
if file_id not in cache:
1982
ie = self._bytes_to_entry(entry)
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))
1996
parent_id, basename = key
1997
ie = cache[child_file_id]
1998
if parent_id == last_parent_id:
1999
parent_ie = last_parent_ie
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
2026
1973
def iter_changes(self, basis):
2027
1974
"""Generate a Tree.iter_changes change list between this and basis.
2383
2334
raise errors.InconsistentDelta(new_path, item[1],
2384
2335
"new_path with no entry")
2388
def mutable_inventory_from_tree(tree):
2389
"""Create a new inventory that has the same contents as a specified tree.
2391
:param tree: Revision tree to create inventory from
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())