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.
26
from __future__ import absolute_import
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"
30
from bzrlib.lazy_import import lazy_import
32
from .lazy_import import lazy_import
31
33
lazy_import(globals(), """
48
from bzrlib.errors import (
52
from bzrlib.symbol_versioning import deprecated_in, deprecated_method
53
from bzrlib.trace import mutter
54
from bzrlib.static_tuple import StaticTuple
52
from .static_tuple import StaticTuple
57
55
class InventoryEntry(object):
104
102
InventoryDirectory('2325', 'wibble', parent_id='123', revision=None)
105
103
>>> i.path2id('src/wibble')
109
105
>>> i.add(InventoryFile('2326', 'wibble.c', '2325'))
110
106
InventoryFile('2326', 'wibble.c', parent_id='2325', sha1=None, len=None, revision=None)
131
127
RENAMED = 'renamed'
132
128
MODIFIED_AND_RENAMED = 'modified and renamed'
130
__slots__ = ['file_id', 'revision', 'parent_id', 'name']
132
# Attributes that all InventoryEntry instances are expected to have, but
133
# that don't vary for all kinds of entry. (e.g. symlink_target is only
134
# relevant to InventoryLink, so there's no reason to make every
135
# InventoryFile instance allocate space to hold a value for it.)
136
# Attributes that only vary for files: executable, text_sha1, text_size,
142
# Attributes that only vary for symlinks: symlink_target
143
symlink_target = None
144
# Attributes that only vary for tree-references: reference_revision
145
reference_revision = None
136
148
def detect_changes(self, old_entry):
137
149
"""Return a (text_modified, meta_modified) from this to old_entry.
176
188
candidates[ie.revision] = ie
177
189
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)
189
191
def has_text(self):
190
192
"""Return true if the object this entry represents has textual data.
200
def __init__(self, file_id, name, parent_id, text_id=None):
202
def __init__(self, file_id, name, parent_id):
201
203
"""Create an InventoryEntry
203
205
The filename must be a single component, relative to the
215
217
if '/' in name or '\\' in name:
216
218
raise errors.InvalidEntryName(name=name)
217
self.executable = False
219
self.file_id = file_id
218
220
self.revision = None
219
self.text_sha1 = None
220
self.text_size = None
221
self.file_id = file_id
223
self.text_id = text_id
224
222
self.parent_id = parent_id
225
self.symlink_target = None
226
self.reference_revision = None
228
224
def kind_character(self):
229
225
"""Return a short kind indicator useful for appending to names."""
230
raise BzrError('unknown kind %r' % self.kind)
226
raise errors.BzrError('unknown kind %r' % self.kind)
232
228
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))
257
230
def sorted_children(self):
258
231
return sorted(self.children.items())
277
250
if self.parent_id is not None:
278
251
if not inv.has_id(self.parent_id):
279
raise BzrCheckError('missing parent {%s} in inventory for revision {%s}'
280
% (self.parent_id, rev_id))
252
raise errors.BzrCheckError(
253
'missing parent {%s} in inventory for revision {%s}' % (
254
self.parent_id, rev_id))
281
255
checker._add_entry_to_text_key_references(inv, self)
282
256
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)
428
374
class InventoryDirectory(InventoryEntry):
429
375
"""A directory in an inventory."""
431
__slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
432
'text_id', 'parent_id', 'children', 'executable',
433
'revision', 'symlink_target', 'reference_revision']
377
__slots__ = ['children']
435
381
def _check(self, checker, rev_id):
436
382
"""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))
441
383
# In non rich root repositories we do not expect a file graph for the
443
385
if self.name == '' and not checker.rich_roots:
459
401
def __init__(self, file_id, name, parent_id):
460
402
super(InventoryDirectory, self).__init__(file_id, name, parent_id)
461
403
self.children = {}
462
self.kind = 'directory'
464
405
def kind_character(self):
465
406
"""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."""
482
410
class InventoryFile(InventoryEntry):
483
411
"""A file in an inventory."""
485
__slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
486
'text_id', 'parent_id', 'children', 'executable',
487
'revision', 'symlink_target', 'reference_revision']
413
__slots__ = ['text_sha1', 'text_size', 'text_id', 'executable']
417
def __init__(self, file_id, name, parent_id):
418
super(InventoryFile, self).__init__(file_id, name, parent_id)
419
self.text_sha1 = None
420
self.text_size = None
422
self.executable = False
489
424
def _check(self, checker, tree_revision_id):
490
425
"""See InventoryEntry._check"""
515
450
def _diff(self, text_diff, from_label, tree, to_label, to_entry, to_tree,
516
451
output_to, reverse=False):
517
452
"""See InventoryEntry._diff."""
518
from bzrlib.diff import DiffText
453
from breezy.diff import DiffText
519
454
from_file_id = self.file_id
521
456
to_file_id = to_entry.file_id
533
468
"""See InventoryEntry.has_text."""
536
def __init__(self, file_id, name, parent_id):
537
super(InventoryFile, self).__init__(file_id, name, parent_id)
540
471
def kind_character(self):
541
472
"""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)
561
475
def _read_tree_state(self, path, work_tree):
562
476
"""See InventoryEntry._read_tree_state."""
563
477
self.text_sha1 = work_tree.get_file_sha1(self.file_id, path=path)
595
509
class InventoryLink(InventoryEntry):
596
510
"""A file in an inventory."""
598
__slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
599
'text_id', 'parent_id', 'children', 'executable',
600
'revision', 'symlink_target', 'reference_revision']
512
__slots__ = ['symlink_target']
516
def __init__(self, file_id, name, parent_id):
517
super(InventoryLink, self).__init__(file_id, name, parent_id)
518
self.symlink_target = None
602
520
def _check(self, checker, tree_revision_id):
603
521
"""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))
608
522
if self.symlink_target is None:
609
523
checker._report_items.append(
610
524
'symlink {%s} has no target in revision {%s}'
625
539
# FIXME: which _modified field should we use ? RBC 20051003
626
540
text_modified = (self.symlink_target != old_entry.symlink_target)
627
541
if text_modified:
628
mutter(" symlink target changed")
542
trace.mutter(" symlink target changed")
629
543
meta_modified = False
630
544
return text_modified, meta_modified
632
546
def _diff(self, text_diff, from_label, tree, to_label, to_entry, to_tree,
633
547
output_to, reverse=False):
634
548
"""See InventoryEntry._diff."""
635
from bzrlib.diff import DiffSymlink
549
from breezy.diff import DiffSymlink
636
550
old_target = self.symlink_target
637
551
if to_entry is not None:
638
552
new_target = to_entry.symlink_target
648
562
differ = DiffSymlink(old_tree, new_tree, output_to)
649
563
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'
655
565
def kind_character(self):
656
566
"""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))
675
569
def _read_tree_state(self, path, work_tree):
676
570
"""See InventoryEntry._read_tree_state."""
677
571
self.symlink_target = work_tree.get_symlink_target(self.file_id)
733
629
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)
753
632
def has_filename(self, filename):
754
633
return bool(self.path2id(filename))
814
692
# But do this child first
815
new_children = ie.children.items()
693
new_children = sorted(ie.children.items())
817
694
new_children = collections.deque(new_children)
818
695
stack.append((path, new_children))
819
696
# Break out of inner loop, so that we start outer loop with child
822
699
# if we finished all children, pop it off the stack
702
def _preload_cache(self):
703
"""Populate any caches, we are about to access all items.
705
The default implementation does nothing, because CommonInventory doesn't
825
710
def iter_entries_by_dir(self, from_dir=None, specific_file_ids=None,
826
711
yield_parents=False):
827
712
"""Iterate over the entries in a directory first order.
840
725
specific_file_ids = set(specific_file_ids)
841
726
# TODO? Perhaps this should return the from_dir so that the root is
842
727
# yielded? or maybe an option?
728
if from_dir is None and specific_file_ids is None:
729
# They are iterating from the root, and have not specified any
730
# specific entries to look at. All current callers fully consume the
731
# iterator, so we can safely assume we are accessing all entries
732
self._preload_cache()
843
733
if from_dir is None:
844
734
if self.root is None:
913
803
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)
924
806
def make_entry(self, kind, name, parent_id, file_id=None):
925
"""Simple thunk to bzrlib.inventory.make_entry."""
807
"""Simple thunk to breezy.inventory.make_entry."""
926
808
return make_entry(kind, name, parent_id, file_id)
928
810
def entries(self):
934
816
def descend(dir_ie, dir_path):
935
kids = dir_ie.children.items()
817
kids = sorted(dir_ie.children.items())
937
818
for name, ie in kids:
938
819
child_path = osutils.pathjoin(dir_path, name)
939
820
accum.append((child_path, ie))
940
821
if ie.kind == 'directory':
941
822
descend(ie, child_path)
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'')
824
if self.root is not None:
825
descend(self.root, u'')
962
828
def path2id(self, relpath):
1272
1134
def _add_child(self, entry):
1273
1135
"""Add an entry to the inventory, without adding it to its parent"""
1274
1136
if entry.file_id in self._byid:
1275
raise BzrError("inventory already contains entry with id {%s}" %
1137
raise errors.BzrError(
1138
"inventory already contains entry with id {%s}" %
1277
1140
self._byid[entry.file_id] = entry
1278
1141
for child in getattr(entry, 'children', {}).itervalues():
1279
1142
self._add_child(child)
1447
1307
new_name = ensure_normalized_name(new_name)
1448
1308
if not is_valid_name(new_name):
1449
raise BzrError("not an acceptable filename: %r" % new_name)
1309
raise errors.BzrError("not an acceptable filename: %r" % new_name)
1451
1311
new_parent = self._byid[new_parent_id]
1452
1312
if new_name in new_parent.children:
1453
raise BzrError("%r already exists in %r" % (new_name, self.id2path(new_parent_id)))
1313
raise errors.BzrError("%r already exists in %r" %
1314
(new_name, self.id2path(new_parent_id)))
1455
1316
new_parent_idpath = self.get_idpath(new_parent_id)
1456
1317
if file_id in new_parent_idpath:
1457
raise BzrError("cannot move directory %r into a subdirectory of itself, %r"
1318
raise errors.BzrError(
1319
"cannot move directory %r into a subdirectory of itself, %r"
1458
1320
% (self.id2path(file_id), self.id2path(new_parent_id)))
1460
1322
file_ie = self._byid[file_id]
1588
1451
if entry.kind == 'directory':
1589
1452
directories_to_expand.add(entry.file_id)
1590
1453
interesting.add(entry.parent_id)
1591
children_of_parent_id.setdefault(entry.parent_id, []
1592
).append(entry.file_id)
1454
children_of_parent_id.setdefault(entry.parent_id, set()
1455
).add(entry.file_id)
1594
1457
# Now, interesting has all of the direct parents, but not the
1595
1458
# parents of those parents. It also may have some duplicates with
1603
1466
next_parents = set()
1604
1467
for entry in self._getitems(remaining_parents):
1605
1468
next_parents.add(entry.parent_id)
1606
children_of_parent_id.setdefault(entry.parent_id, []
1607
).append(entry.file_id)
1469
children_of_parent_id.setdefault(entry.parent_id, set()
1470
).add(entry.file_id)
1608
1471
# Remove any search tips we've already processed
1609
1472
remaining_parents = next_parents.difference(interesting)
1610
1473
interesting.update(remaining_parents)
1617
1480
keys = [StaticTuple(f,).intern() for f in directories_to_expand]
1618
1481
directories_to_expand = set()
1619
1482
items = self.parent_id_basename_to_file_id.iteritems(keys)
1620
next_file_ids = set([item[1] for item in items])
1483
next_file_ids = {item[1] for item in items}
1621
1484
next_file_ids = next_file_ids.difference(interesting)
1622
1485
interesting.update(next_file_ids)
1623
1486
for entry in self._getitems(next_file_ids):
1624
1487
if entry.kind == 'directory':
1625
1488
directories_to_expand.add(entry.file_id)
1626
children_of_parent_id.setdefault(entry.parent_id, []
1627
).append(entry.file_id)
1489
children_of_parent_id.setdefault(entry.parent_id, set()
1490
).add(entry.file_id)
1628
1491
return interesting, children_of_parent_id
1630
1493
def filter(self, specific_fileids):
1652
1515
# parent_to_children with at least the tree root.)
1654
1517
cache = self._fileid_to_entry_cache
1656
remaining_children = collections.deque(parent_to_children[self.root_id])
1658
import pdb; pdb.set_trace()
1518
remaining_children = collections.deque(parent_to_children[self.root_id])
1660
1519
while remaining_children:
1661
1520
file_id = remaining_children.popleft()
1662
1521
ie = cache[file_id]
1712
1571
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())
1723
1574
def create_by_apply_delta(self, inventory_delta, new_revision_id,
1724
1575
propagate_caches=False):
1725
1576
"""Create a new CHKInventory by applying inventory_delta to this one.
2080
1931
self._fileid_to_entry_cache[file_id] = ie
1934
def _preload_cache(self):
1935
"""Make sure all file-ids are in _fileid_to_entry_cache"""
1936
if self._fully_cached:
1937
return # No need to do it again
1938
# The optimal sort order is to use iteritems() directly
1939
cache = self._fileid_to_entry_cache
1940
for key, entry in self.id_to_entry.iteritems():
1942
if file_id not in cache:
1943
ie = self._bytes_to_entry(entry)
1947
last_parent_id = last_parent_ie = None
1948
pid_items = self.parent_id_basename_to_file_id.iteritems()
1949
for key, child_file_id in pid_items:
1950
if key == ('', ''): # This is the root
1951
if child_file_id != self.root_id:
1952
raise ValueError('Data inconsistency detected.'
1953
' We expected data with key ("","") to match'
1954
' the root id, but %s != %s'
1955
% (child_file_id, self.root_id))
1957
parent_id, basename = key
1958
ie = cache[child_file_id]
1959
if parent_id == last_parent_id:
1960
parent_ie = last_parent_ie
1962
parent_ie = cache[parent_id]
1963
if parent_ie.kind != 'directory':
1964
raise ValueError('Data inconsistency detected.'
1965
' An entry in the parent_id_basename_to_file_id map'
1966
' has parent_id {%s} but the kind of that object'
1967
' is %r not "directory"' % (parent_id, parent_ie.kind))
1968
if parent_ie._children is None:
1969
parent_ie._children = {}
1970
basename = basename.decode('utf-8')
1971
if basename in parent_ie._children:
1972
existing_ie = parent_ie._children[basename]
1973
if existing_ie != ie:
1974
raise ValueError('Data inconsistency detected.'
1975
' Two entries with basename %r were found'
1976
' in the parent entry {%s}'
1977
% (basename, parent_id))
1978
if basename != ie.name:
1979
raise ValueError('Data inconsistency detected.'
1980
' In the parent_id_basename_to_file_id map, file_id'
1981
' {%s} is listed as having basename %r, but in the'
1982
' id_to_entry map it is %r'
1983
% (child_file_id, basename, ie.name))
1984
parent_ie._children[basename] = ie
1985
self._fully_cached = True
2083
1987
def iter_changes(self, basis):
2084
1988
"""Generate a Tree.iter_changes change list between this and basis.
2160
2064
def _make_delta(self, old):
2161
2065
"""Make an inventory delta from two inventories."""
2162
if type(old) != CHKInventory:
2066
if not isinstance(old, CHKInventory):
2163
2067
return CommonInventory._make_delta(self, old)
2165
2069
for key, old_value, self_value in \
2182
2086
def path2id(self, relpath):
2183
2087
"""See CommonInventory.path2id()."""
2184
2088
# TODO: perhaps support negative hits?
2089
if isinstance(relpath, basestring):
2090
names = osutils.splitpath(relpath)
2095
relpath = osutils.pathjoin(*relpath)
2185
2096
result = self._path_to_fileid_cache.get(relpath, None)
2186
2097
if result is not None:
2188
if isinstance(relpath, basestring):
2189
names = osutils.splitpath(relpath)
2192
2099
current_id = self.root_id
2193
2100
if current_id is None:
2245
2152
class CHKInventoryDirectory(InventoryDirectory):
2246
2153
"""A directory in an inventory."""
2248
__slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
2249
'text_id', 'parent_id', '_children', 'executable',
2250
'revision', 'symlink_target', 'reference_revision',
2155
__slots__ = ['_children', '_chk_inventory']
2253
2157
def __init__(self, file_id, name, parent_id, chk_inventory):
2254
2158
# Don't call InventoryDirectory.__init__ - it isn't right for this
2256
2160
InventoryEntry.__init__(self, file_id, name, parent_id)
2257
2161
self._children = None
2258
self.kind = 'directory'
2259
2162
self._chk_inventory = chk_inventory
2331
2234
accessed on this platform by the normalized path.
2332
2235
:return: The NFC normalised version of name.
2334
#------- This has been copied to bzrlib.dirstate.DirState.add, please
2237
#------- This has been copied to breezy.dirstate.DirState.add, please
2335
2238
# keep them synchronised.
2336
2239
# we dont import normalized_filename directly because we want to be
2337
2240
# able to change the implementation at runtime for tests.
2413
2312
if item[2] is None:
2414
2313
raise errors.InconsistentDelta(item[0] or item[1], item[2],
2415
2314
"entry with file_id None %r" % entry)
2416
if type(item[2]) != str:
2315
if not isinstance(item[2], str):
2417
2316
raise errors.InconsistentDelta(item[0] or item[1], item[2],
2418
2317
"entry with non bytes file_id %r" % entry)
2448
2347
raise errors.InconsistentDelta(new_path, item[1],
2449
2348
"new_path with no entry")
2352
def mutable_inventory_from_tree(tree):
2353
"""Create a new inventory that has the same contents as a specified tree.
2355
:param tree: Revision tree to create inventory from
2357
entries = tree.iter_entries_by_dir()
2358
inv = Inventory(None, tree.get_revision_id())
2359
for path, inv_entry in entries:
2360
inv.add(inv_entry.copy())