33
31
lazy_import(globals(), """
39
39
from bzrlib import (
48
from bzrlib.errors import (
52
from bzrlib.symbol_versioning import deprecated_in, deprecated_method
53
from bzrlib.trace import mutter
52
54
from bzrlib.static_tuple import StaticTuple
53
from bzrlib.symbol_versioning import (
59
57
class InventoryEntry(object):
106
104
InventoryDirectory('2325', 'wibble', parent_id='123', revision=None)
107
105
>>> i.path2id('src/wibble')
109
109
>>> i.add(InventoryFile('2326', 'wibble.c', '2325'))
110
110
InventoryFile('2326', 'wibble.c', parent_id='2325', sha1=None, len=None, revision=None)
131
131
RENAMED = 'renamed'
132
132
MODIFIED_AND_RENAMED = 'modified and renamed'
134
__slots__ = ['file_id', 'revision', 'parent_id', 'name']
136
# Attributes that all InventoryEntry instances are expected to have, but
137
# that don't vary for all kinds of entry. (e.g. symlink_target is only
138
# relevant to InventoryLink, so there's no reason to make every
139
# InventoryFile instance allocate space to hold a value for it.)
140
# Attributes that only vary for files: executable, text_sha1, text_size,
146
# Attributes that only vary for symlinks: symlink_target
147
symlink_target = None
148
# Attributes that only vary for tree-references: reference_revision
149
reference_revision = None
152
136
def detect_changes(self, old_entry):
153
137
"""Return a (text_modified, meta_modified) from this to old_entry.
192
176
candidates[ie.revision] = ie
193
177
return candidates
179
@deprecated_method(deprecated_in((1, 6, 0)))
180
def get_tar_item(self, root, dp, now, tree):
181
"""Get a tarfile item and a file stream for its content."""
182
item = tarfile.TarInfo(osutils.pathjoin(root, dp).encode('utf8'))
183
# TODO: would be cool to actually set it to the timestamp of the
184
# revision it was last changed
186
fileobj = self._put_in_tar(item, tree)
195
189
def has_text(self):
196
190
"""Return true if the object this entry represents has textual data.
206
def __init__(self, file_id, name, parent_id):
200
def __init__(self, file_id, name, parent_id, text_id=None):
207
201
"""Create an InventoryEntry
209
203
The filename must be a single component, relative to the
221
215
if '/' in name or '\\' in name:
222
216
raise errors.InvalidEntryName(name=name)
217
self.executable = False
219
self.text_sha1 = None
220
self.text_size = None
223
221
self.file_id = file_id
223
self.text_id = text_id
226
224
self.parent_id = parent_id
225
self.symlink_target = None
226
self.reference_revision = None
228
228
def kind_character(self):
229
229
"""Return a short kind indicator useful for appending to names."""
230
raise errors.BzrError('unknown kind %r' % self.kind)
230
raise BzrError('unknown kind %r' % self.kind)
232
232
known_kinds = ('file', 'directory', 'symlink')
234
def _put_in_tar(self, item, tree):
235
"""populate item for stashing in a tar, and return the content stream.
237
If no content is available, return None.
239
raise BzrError("don't know how to export {%s} of kind %r" %
240
(self.file_id, self.kind))
242
@deprecated_method(deprecated_in((1, 6, 0)))
243
def put_on_disk(self, dest, dp, tree):
244
"""Create a representation of self on disk in the prefix dest.
246
This is a template method - implement _put_on_disk in subclasses.
248
fullpath = osutils.pathjoin(dest, dp)
249
self._put_on_disk(fullpath, tree)
250
# mutter(" export {%s} kind %s to %s", self.file_id,
251
# self.kind, fullpath)
253
def _put_on_disk(self, fullpath, tree):
254
"""Put this entry onto disk at fullpath, from tree tree."""
255
raise BzrError("don't know how to export {%s} of kind %r" % (self.file_id, self.kind))
234
257
def sorted_children(self):
235
258
return sorted(self.children.items())
254
277
if self.parent_id is not None:
255
278
if not inv.has_id(self.parent_id):
256
raise errors.BzrCheckError(
257
'missing parent {%s} in inventory for revision {%s}' % (
258
self.parent_id, rev_id))
279
raise BzrCheckError('missing parent {%s} in inventory for revision {%s}'
280
% (self.parent_id, rev_id))
259
281
checker._add_entry_to_text_key_references(inv, self)
260
282
self._check(checker, rev_id)
400
class RootEntry(InventoryEntry):
402
__slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
403
'text_id', 'parent_id', 'children', 'executable',
404
'revision', 'symlink_target', 'reference_revision']
406
def _check(self, checker, rev_id):
407
"""See InventoryEntry._check"""
409
def __init__(self, file_id):
410
self.file_id = file_id
412
self.kind = 'directory'
413
self.parent_id = None
416
symbol_versioning.warn('RootEntry is deprecated as of bzr 0.10.'
417
' Please use InventoryDirectory instead.',
418
DeprecationWarning, stacklevel=2)
420
def __eq__(self, other):
421
if not isinstance(other, RootEntry):
422
return NotImplemented
424
return (self.file_id == other.file_id) \
425
and (self.children == other.children)
378
428
class InventoryDirectory(InventoryEntry):
379
429
"""A directory in an inventory."""
381
__slots__ = ['children']
431
__slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
432
'text_id', 'parent_id', 'children', 'executable',
433
'revision', 'symlink_target', 'reference_revision']
385
435
def _check(self, checker, rev_id):
386
436
"""See InventoryEntry._check"""
437
if (self.text_sha1 is not None or self.text_size is not None or
438
self.text_id is not None):
439
checker._report_items.append('directory {%s} has text in revision {%s}'
440
% (self.file_id, rev_id))
387
441
# In non rich root repositories we do not expect a file graph for the
389
443
if self.name == '' and not checker.rich_roots:
405
459
def __init__(self, file_id, name, parent_id):
406
460
super(InventoryDirectory, self).__init__(file_id, name, parent_id)
407
461
self.children = {}
462
self.kind = 'directory'
409
464
def kind_character(self):
410
465
"""See InventoryEntry.kind_character."""
468
def _put_in_tar(self, item, tree):
469
"""See InventoryEntry._put_in_tar."""
470
item.type = tarfile.DIRTYPE
477
def _put_on_disk(self, fullpath, tree):
478
"""See InventoryEntry._put_on_disk."""
414
482
class InventoryFile(InventoryEntry):
415
483
"""A file in an inventory."""
417
__slots__ = ['text_sha1', 'text_size', 'text_id', 'executable']
421
def __init__(self, file_id, name, parent_id):
422
super(InventoryFile, self).__init__(file_id, name, parent_id)
423
self.text_sha1 = None
424
self.text_size = None
426
self.executable = False
485
__slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
486
'text_id', 'parent_id', 'children', 'executable',
487
'revision', 'symlink_target', 'reference_revision']
428
489
def _check(self, checker, tree_revision_id):
429
490
"""See InventoryEntry._check"""
472
533
"""See InventoryEntry.has_text."""
536
def __init__(self, file_id, name, parent_id):
537
super(InventoryFile, self).__init__(file_id, name, parent_id)
475
540
def kind_character(self):
476
541
"""See InventoryEntry.kind_character."""
544
def _put_in_tar(self, item, tree):
545
"""See InventoryEntry._put_in_tar."""
546
item.type = tarfile.REGTYPE
547
fileobj = tree.get_file(self.file_id)
548
item.size = self.text_size
549
if tree.is_executable(self.file_id):
555
def _put_on_disk(self, fullpath, tree):
556
"""See InventoryEntry._put_on_disk."""
557
osutils.pumpfile(tree.get_file(self.file_id), file(fullpath, 'wb'))
558
if tree.is_executable(self.file_id):
559
os.chmod(fullpath, 0755)
479
561
def _read_tree_state(self, path, work_tree):
480
562
"""See InventoryEntry._read_tree_state."""
481
563
self.text_sha1 = work_tree.get_file_sha1(self.file_id, path=path)
513
595
class InventoryLink(InventoryEntry):
514
596
"""A file in an inventory."""
516
__slots__ = ['symlink_target']
520
def __init__(self, file_id, name, parent_id):
521
super(InventoryLink, self).__init__(file_id, name, parent_id)
522
self.symlink_target = None
598
__slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
599
'text_id', 'parent_id', 'children', 'executable',
600
'revision', 'symlink_target', 'reference_revision']
524
602
def _check(self, checker, tree_revision_id):
525
603
"""See InventoryEntry._check"""
604
if self.text_sha1 is not None or self.text_size is not None or self.text_id is not None:
605
checker._report_items.append(
606
'symlink {%s} has text in revision {%s}'
607
% (self.file_id, tree_revision_id))
526
608
if self.symlink_target is None:
527
609
checker._report_items.append(
528
610
'symlink {%s} has no target in revision {%s}'
566
648
differ = DiffSymlink(old_tree, new_tree, output_to)
567
649
return differ.diff_symlink(old_target, new_target)
651
def __init__(self, file_id, name, parent_id):
652
super(InventoryLink, self).__init__(file_id, name, parent_id)
653
self.kind = 'symlink'
569
655
def kind_character(self):
570
656
"""See InventoryEntry.kind_character."""
659
def _put_in_tar(self, item, tree):
660
"""See InventoryEntry._put_in_tar."""
661
item.type = tarfile.SYMTYPE
665
item.linkname = self.symlink_target
668
def _put_on_disk(self, fullpath, tree):
669
"""See InventoryEntry._put_on_disk."""
671
os.symlink(self.symlink_target, fullpath)
673
raise BzrError("Failed to create symlink %r -> %r, error: %s" % (fullpath, self.symlink_target, e))
573
675
def _read_tree_state(self, path, work_tree):
574
676
"""See InventoryEntry._read_tree_state."""
575
677
self.symlink_target = work_tree.get_symlink_target(self.file_id)
633
733
inserted, other than through the Inventory API.
736
def __contains__(self, file_id):
737
"""True if this entry contains a file with given id.
739
>>> inv = Inventory()
740
>>> inv.add(InventoryFile('123', 'foo.c', ROOT_ID))
741
InventoryFile('123', 'foo.c', parent_id='TREE_ROOT', sha1=None, len=None, revision=None)
747
Note that this method along with __iter__ are not encouraged for use as
748
they are less clear than specific query methods - they may be rmeoved
751
return self.has_id(file_id)
636
753
def has_filename(self, filename):
637
754
return bool(self.path2id(filename))
705
822
# if we finished all children, pop it off the stack
708
def _preload_cache(self):
709
"""Populate any caches, we are about to access all items.
711
The default implementation does nothing, because CommonInventory doesn't
716
825
def iter_entries_by_dir(self, from_dir=None, specific_file_ids=None,
717
826
yield_parents=False):
718
827
"""Iterate over the entries in a directory first order.
731
840
specific_file_ids = set(specific_file_ids)
732
841
# TODO? Perhaps this should return the from_dir so that the root is
733
842
# yielded? or maybe an option?
734
if from_dir is None and specific_file_ids is None:
735
# They are iterating from the root, and have not specified any
736
# specific entries to look at. All current callers fully consume the
737
# iterator, so we can safely assume we are accessing all entries
738
self._preload_cache()
739
843
if from_dir is None:
740
844
if self.root is None:
809
913
file_id, self[file_id]))
916
def _get_mutable_inventory(self):
917
"""Returns a mutable copy of the object.
919
Some inventories are immutable, yet working trees, for example, needs
920
to mutate exisiting inventories instead of creating a new one.
922
raise NotImplementedError(self._get_mutable_inventory)
812
924
def make_entry(self, kind, name, parent_id, file_id=None):
813
925
"""Simple thunk to bzrlib.inventory.make_entry."""
814
926
return make_entry(kind, name, parent_id, file_id)
828
940
if ie.kind == 'directory':
829
941
descend(ie, child_path)
831
if self.root is not None:
832
descend(self.root, u'')
943
descend(self.root, u'')
946
def directories(self):
947
"""Return (path, entry) pairs for all directories, including the root.
950
def descend(parent_ie, parent_path):
951
accum.append((parent_path, parent_ie))
953
kids = [(ie.name, ie) for ie in parent_ie.children.itervalues() if ie.kind == 'directory']
956
for name, child_ie in kids:
957
child_path = osutils.pathjoin(parent_path, name)
958
descend(child_ie, child_path)
959
descend(self.root, u'')
835
962
def path2id(self, relpath):
1141
1272
def _add_child(self, entry):
1142
1273
"""Add an entry to the inventory, without adding it to its parent"""
1143
1274
if entry.file_id in self._byid:
1144
raise errors.BzrError(
1145
"inventory already contains entry with id {%s}" %
1275
raise BzrError("inventory already contains entry with id {%s}" %
1147
1277
self._byid[entry.file_id] = entry
1148
1278
for child in getattr(entry, 'children', {}).itervalues():
1149
1279
self._add_child(child)
1314
1447
new_name = ensure_normalized_name(new_name)
1315
1448
if not is_valid_name(new_name):
1316
raise errors.BzrError("not an acceptable filename: %r" % new_name)
1449
raise BzrError("not an acceptable filename: %r" % new_name)
1318
1451
new_parent = self._byid[new_parent_id]
1319
1452
if new_name in new_parent.children:
1320
raise errors.BzrError("%r already exists in %r" %
1321
(new_name, self.id2path(new_parent_id)))
1453
raise BzrError("%r already exists in %r" % (new_name, self.id2path(new_parent_id)))
1323
1455
new_parent_idpath = self.get_idpath(new_parent_id)
1324
1456
if file_id in new_parent_idpath:
1325
raise errors.BzrError(
1326
"cannot move directory %r into a subdirectory of itself, %r"
1457
raise BzrError("cannot move directory %r into a subdirectory of itself, %r"
1327
1458
% (self.id2path(file_id), self.id2path(new_parent_id)))
1329
1460
file_ie = self._byid[file_id]
1458
1588
if entry.kind == 'directory':
1459
1589
directories_to_expand.add(entry.file_id)
1460
1590
interesting.add(entry.parent_id)
1461
children_of_parent_id.setdefault(entry.parent_id, set()
1462
).add(entry.file_id)
1591
children_of_parent_id.setdefault(entry.parent_id, []
1592
).append(entry.file_id)
1464
1594
# Now, interesting has all of the direct parents, but not the
1465
1595
# parents of those parents. It also may have some duplicates with
1473
1603
next_parents = set()
1474
1604
for entry in self._getitems(remaining_parents):
1475
1605
next_parents.add(entry.parent_id)
1476
children_of_parent_id.setdefault(entry.parent_id, set()
1477
).add(entry.file_id)
1606
children_of_parent_id.setdefault(entry.parent_id, []
1607
).append(entry.file_id)
1478
1608
# Remove any search tips we've already processed
1479
1609
remaining_parents = next_parents.difference(interesting)
1480
1610
interesting.update(remaining_parents)
1493
1623
for entry in self._getitems(next_file_ids):
1494
1624
if entry.kind == 'directory':
1495
1625
directories_to_expand.add(entry.file_id)
1496
children_of_parent_id.setdefault(entry.parent_id, set()
1497
).add(entry.file_id)
1626
children_of_parent_id.setdefault(entry.parent_id, []
1627
).append(entry.file_id)
1498
1628
return interesting, children_of_parent_id
1500
1630
def filter(self, specific_fileids):
1522
1652
# parent_to_children with at least the tree root.)
1524
1654
cache = self._fileid_to_entry_cache
1525
remaining_children = collections.deque(parent_to_children[self.root_id])
1656
remaining_children = collections.deque(parent_to_children[self.root_id])
1658
import pdb; pdb.set_trace()
1526
1660
while remaining_children:
1527
1661
file_id = remaining_children.popleft()
1528
1662
ie = cache[file_id]
1578
1712
self._fileid_to_entry_cache[result.file_id] = result
1715
def _get_mutable_inventory(self):
1716
"""See CommonInventory._get_mutable_inventory."""
1717
entries = self.iter_entries()
1718
inv = Inventory(None, self.revision_id)
1719
for path, inv_entry in entries:
1720
inv.add(inv_entry.copy())
1581
1723
def create_by_apply_delta(self, inventory_delta, new_revision_id,
1582
1724
propagate_caches=False):
1583
1725
"""Create a new CHKInventory by applying inventory_delta to this one.
1938
2080
self._fileid_to_entry_cache[file_id] = ie
1941
def _preload_cache(self):
1942
"""Make sure all file-ids are in _fileid_to_entry_cache"""
1943
if self._fully_cached:
1944
return # No need to do it again
1945
# The optimal sort order is to use iteritems() directly
1946
cache = self._fileid_to_entry_cache
1947
for key, entry in self.id_to_entry.iteritems():
1949
if file_id not in cache:
1950
ie = self._bytes_to_entry(entry)
1954
last_parent_id = last_parent_ie = None
1955
pid_items = self.parent_id_basename_to_file_id.iteritems()
1956
for key, child_file_id in pid_items:
1957
if key == ('', ''): # This is the root
1958
if child_file_id != self.root_id:
1959
raise ValueError('Data inconsistency detected.'
1960
' We expected data with key ("","") to match'
1961
' the root id, but %s != %s'
1962
% (child_file_id, self.root_id))
1964
parent_id, basename = key
1965
ie = cache[child_file_id]
1966
if parent_id == last_parent_id:
1967
parent_ie = last_parent_ie
1969
parent_ie = cache[parent_id]
1970
if parent_ie.kind != 'directory':
1971
raise ValueError('Data inconsistency detected.'
1972
' An entry in the parent_id_basename_to_file_id map'
1973
' has parent_id {%s} but the kind of that object'
1974
' is %r not "directory"' % (parent_id, parent_ie.kind))
1975
if parent_ie._children is None:
1976
parent_ie._children = {}
1977
basename = basename.decode('utf-8')
1978
if basename in parent_ie._children:
1979
existing_ie = parent_ie._children[basename]
1980
if existing_ie != ie:
1981
raise ValueError('Data inconsistency detected.'
1982
' Two entries with basename %r were found'
1983
' in the parent entry {%s}'
1984
% (basename, parent_id))
1985
if basename != ie.name:
1986
raise ValueError('Data inconsistency detected.'
1987
' In the parent_id_basename_to_file_id map, file_id'
1988
' {%s} is listed as having basename %r, but in the'
1989
' id_to_entry map it is %r'
1990
% (child_file_id, basename, ie.name))
1991
parent_ie._children[basename] = ie
1992
self._fully_cached = True
1994
2083
def iter_changes(self, basis):
1995
2084
"""Generate a Tree.iter_changes change list between this and basis.
2093
2182
def path2id(self, relpath):
2094
2183
"""See CommonInventory.path2id()."""
2095
2184
# TODO: perhaps support negative hits?
2096
if isinstance(relpath, basestring):
2097
names = osutils.splitpath(relpath)
2102
relpath = osutils.pathjoin(*relpath)
2103
2185
result = self._path_to_fileid_cache.get(relpath, None)
2104
2186
if result is not None:
2188
if isinstance(relpath, basestring):
2189
names = osutils.splitpath(relpath)
2106
2192
current_id = self.root_id
2107
2193
if current_id is None:
2159
2245
class CHKInventoryDirectory(InventoryDirectory):
2160
2246
"""A directory in an inventory."""
2162
__slots__ = ['_children', '_chk_inventory']
2248
__slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
2249
'text_id', 'parent_id', '_children', 'executable',
2250
'revision', 'symlink_target', 'reference_revision',
2164
2253
def __init__(self, file_id, name, parent_id, chk_inventory):
2165
2254
# Don't call InventoryDirectory.__init__ - it isn't right for this
2167
2256
InventoryEntry.__init__(self, file_id, name, parent_id)
2168
2257
self._children = None
2258
self.kind = 'directory'
2169
2259
self._chk_inventory = chk_inventory
2354
2448
raise errors.InconsistentDelta(new_path, item[1],
2355
2449
"new_path with no entry")
2359
def mutable_inventory_from_tree(tree):
2360
"""Create a new inventory that has the same contents as a specified tree.
2362
:param tree: Revision tree to create inventory from
2364
entries = tree.iter_entries_by_dir()
2365
inv = Inventory(None, tree.get_revision_id())
2366
for path, inv_entry in entries:
2367
inv.add(inv_entry.copy())