79
79
InventoryDirectory('123', 'src', parent_id='TREE_ROOT')
80
80
>>> i.add(InventoryFile('2323', 'hello.c', parent_id='123'))
81
81
InventoryFile('2323', 'hello.c', parent_id='123')
82
>>> shouldbe = {0: 'src', 1: pathjoin('src','hello.c')}
83
>>> for ix, j in enumerate(i.iter_entries()):
84
... print (j[0] == shouldbe[ix], j[1])
82
>>> for j in i.iter_entries():
86
(True, InventoryDirectory('123', 'src', parent_id='TREE_ROOT'))
87
(True, InventoryFile('2323', 'hello.c', parent_id='123'))
85
('src', InventoryDirectory('123', 'src', parent_id='TREE_ROOT'))
86
('src/hello.c', InventoryFile('2323', 'hello.c', parent_id='123'))
88
87
>>> i.add(InventoryFile('2323', 'bye.c', '123'))
89
88
Traceback (most recent call last):
112
111
src/wibble/wibble.c
113
>>> i.id2path('2326')
112
>>> i.id2path('2326').replace('\\\\', '/')
114
113
'src/wibble/wibble.c'
117
# Constants returned by describe_change()
119
# TODO: These should probably move to some kind of FileChangeDescription
120
# class; that's like what's inside a TreeDelta but we want to be able to
121
# generate them just for one file at a time.
123
MODIFIED_AND_RENAMED = 'modified and renamed'
125
116
__slots__ = ['text_sha1', 'text_size', 'file_id', 'name', 'kind',
126
117
'text_id', 'parent_id', 'children', 'executable',
129
120
def _add_text_to_weave(self, new_lines, parents, weave_store, transaction):
130
versionedfile = weave_store.get_weave_or_empty(self.file_id,
132
versionedfile.add_lines(self.revision, parents, new_lines)
133
versionedfile.clear_cache()
121
weave_store.add_text(self.file_id, self.revision, new_lines, parents,
135
124
def detect_changes(self, old_entry):
136
125
"""Return a (text_modified, meta_modified) from this to old_entry.
172
158
This is a map containing the file revisions in all parents
173
159
for which the file exists, and its revision is not a parent of
174
160
any other. If the file is new, the set will be empty.
176
:param versioned_file_store: A store where ancestry data on this
177
file id can be queried.
178
:param transaction: The transaction that queries to the versioned
179
file store should be completed under.
180
:param entry_vf: The entry versioned file, if its already available.
182
162
def get_ancestors(weave, entry):
183
return set(weave.get_ancestry(entry.revision))
184
# revision:ie mapping for each ie found in previous_inventories.
186
# revision:ie mapping with one revision for each head.
163
return set(map(weave.idx_to_name,
164
weave.inclusions([weave.lookup(entry.revision)])))
188
# revision: ancestor list for each head
189
166
head_ancestors = {}
190
# identify candidate head revision ids.
191
167
for inv in previous_inventories:
192
168
if self.file_id in inv:
193
169
ie = inv[self.file_id]
194
170
assert ie.file_id == self.file_id
195
if ie.revision in candidates:
196
# same revision value in two different inventories:
197
# correct possible inconsistencies:
198
# * there was a bug in revision updates with 'x' bit
171
if ie.revision in heads:
172
# fixup logic, there was a bug in revision updates.
173
# with x bit support.
201
if candidates[ie.revision].executable != ie.executable:
202
candidates[ie.revision].executable = False
175
if heads[ie.revision].executable != ie.executable:
176
heads[ie.revision].executable = False
203
177
ie.executable = False
204
178
except AttributeError:
206
# must now be the same.
207
assert candidates[ie.revision] == ie
180
assert heads[ie.revision] == ie
209
# add this revision as a candidate.
210
candidates[ie.revision] = ie
212
# common case optimisation
213
if len(candidates) == 1:
214
# if there is only one candidate revision found
215
# then we can opening the versioned file to access ancestry:
216
# there cannot be any ancestors to eliminate when there is
217
# only one revision available.
218
heads[ie.revision] = ie
221
# eliminate ancestors amongst the available candidates:
222
# heads are those that are not an ancestor of any other candidate
223
# - this provides convergence at a per-file level.
224
for ie in candidates.values():
225
# may be an ancestor of a known head:
226
already_present = 0 != len(
227
[head for head in heads
228
if ie.revision in head_ancestors[head]])
230
# an ancestor of an analyzed candidate.
232
# not an ancestor of a known head:
233
# load the versioned file for this file id if needed
235
entry_vf = versioned_file_store.get_weave_or_empty(
236
self.file_id, transaction)
237
ancestors = get_ancestors(entry_vf, ie)
238
# may knock something else out:
239
check_heads = list(heads.keys())
240
for head in check_heads:
241
if head in ancestors:
242
# this previously discovered 'head' is not
243
# really a head - its an ancestor of the newly
246
head_ancestors[ie.revision] = ancestors
247
heads[ie.revision] = ie
182
# may want to add it.
183
# may already be covered:
184
already_present = 0 != len(
185
[head for head in heads
186
if ie.revision in head_ancestors[head]])
188
# an ancestor of a known head.
191
ancestors = get_ancestors(entry_weave, ie)
192
# may knock something else out:
193
check_heads = list(heads.keys())
194
for head in check_heads:
195
if head in ancestors:
196
# this head is not really a head
198
head_ancestors[ie.revision] = ancestors
199
heads[ie.revision] = ie
250
202
def get_tar_item(self, root, dp, now, tree):
251
203
"""Get a tarfile item and a file stream for its content."""
252
item = tarfile.TarInfo(pathjoin(root, dp))
204
item = tarfile.TarInfo(os.path.join(root, dp))
253
205
# TODO: would be cool to actually set it to the timestamp of the
254
206
# revision it was last changed
315
267
This is a template method - implement _put_on_disk in subclasses.
317
fullpath = pathjoin(dest, dp)
269
fullpath = appendpath(dest, dp)
318
270
self._put_on_disk(fullpath, tree)
319
mutter(" export {%s} kind %s to %s", self.file_id,
271
mutter(" export {%s} kind %s to %s" % (self.file_id, self.kind, fullpath))
322
273
def _put_on_disk(self, fullpath, tree):
323
274
"""Put this entry onto disk at fullpath, from tree tree."""
357
300
raise BzrCheckError('unknown entry kind %r in revision {%s}' %
358
301
(self.kind, rev_id))
361
305
"""Clone this inventory entry."""
362
306
raise NotImplementedError
365
def describe_change(old_entry, new_entry):
366
"""Describe the change between old_entry and this.
368
This smells of being an InterInventoryEntry situation, but as its
369
the first one, we're making it a static method for now.
371
An entry with a different parent, or different name is considered
372
to be renamed. Reparenting is an internal detail.
373
Note that renaming the parent does not trigger a rename for the
376
# TODO: Perhaps return an object rather than just a string
377
if old_entry is new_entry:
378
# also the case of both being None
380
elif old_entry is None:
308
def _get_snapshot_change(self, previous_entries):
309
if len(previous_entries) > 1:
311
elif len(previous_entries) == 0:
382
elif new_entry is None:
384
text_modified, meta_modified = new_entry.detect_changes(old_entry)
385
if text_modified or meta_modified:
389
# TODO 20060511 (mbp, rbc) factor out 'detect_rename' here.
390
if old_entry.parent_id != new_entry.parent_id:
392
elif old_entry.name != new_entry.name:
396
if renamed and not modified:
397
return InventoryEntry.RENAMED
398
if modified and not renamed:
400
if modified and renamed:
401
return InventoryEntry.MODIFIED_AND_RENAMED
314
return 'modified/renamed/reparented'
404
316
def __repr__(self):
405
317
return ("%s(%r, %r, parent_id=%r)"
424
336
mutter("found unchanged entry")
425
337
self.revision = parent_ie.revision
426
338
return "unchanged"
427
return self._snapshot_into_revision(revision, previous_entries,
428
work_tree, weave_store, transaction)
430
def _snapshot_into_revision(self, revision, previous_entries, work_tree,
431
weave_store, transaction):
432
"""Record this revision unconditionally into a store.
434
The entry's last-changed revision property (`revision`) is updated to
435
that of the new revision.
437
:param revision: id of the new revision that is being recorded.
439
:returns: String description of the commit (e.g. "merged", "modified"), etc.
441
mutter('new revision {%s} for {%s}', revision, self.file_id)
339
return self.snapshot_revision(revision, previous_entries,
340
work_tree, weave_store, transaction)
342
def snapshot_revision(self, revision, previous_entries, work_tree,
343
weave_store, transaction):
344
"""Record this revision unconditionally."""
345
mutter('new revision for {%s}', self.file_id)
442
346
self.revision = revision
347
change = self._get_snapshot_change(previous_entries)
443
348
self._snapshot_text(previous_entries, work_tree, weave_store,
446
352
def _snapshot_text(self, file_parents, work_tree, weave_store, transaction):
447
353
"""Record the 'text' of this entry, whatever form that takes.
565
468
class InventoryFile(InventoryEntry):
566
469
"""A file in an inventory."""
568
def _check(self, checker, tree_revision_id, tree):
471
def _check(self, checker, rev_id, tree):
569
472
"""See InventoryEntry._check"""
570
t = (self.file_id, self.revision)
473
revision = self.revision
474
t = (self.file_id, revision)
571
475
if t in checker.checked_texts:
572
prev_sha = checker.checked_texts[t]
476
prev_sha = checker.checked_texts[t]
573
477
if prev_sha != self.text_sha1:
574
478
raise BzrCheckError('mismatched sha1 on {%s} in {%s}' %
575
(self.file_id, tree_revision_id))
479
(self.file_id, rev_id))
577
481
checker.repeated_text_cnt += 1
580
if self.file_id not in checker.checked_weaves:
581
mutter('check weave {%s}', self.file_id)
582
w = tree.get_weave(self.file_id)
583
# Not passing a progress bar, because it creates a new
584
# progress, which overwrites the current progress,
585
# and doesn't look nice
587
checker.checked_weaves[self.file_id] = True
589
w = tree.get_weave(self.file_id)
591
mutter('check version {%s} of {%s}', tree_revision_id, self.file_id)
592
checker.checked_text_cnt += 1
593
# We can't check the length, because Weave doesn't store that
594
# information, and the whole point of looking at the weave's
595
# sha1sum is that we don't have to extract the text.
596
if self.text_sha1 != w.get_sha1(self.revision):
597
raise BzrCheckError('text {%s} version {%s} wrong sha1'
598
% (self.file_id, self.revision))
483
mutter('check version {%s} of {%s}', rev_id, self.file_id)
484
file_lines = tree.get_file_lines(self.file_id)
485
checker.checked_text_cnt += 1
486
if self.text_size != sum(map(len, file_lines)):
487
raise BzrCheckError('text {%s} wrong size' % self.text_id)
488
if self.text_sha1 != sha_strings(file_lines):
489
raise BzrCheckError('text {%s} wrong sha1' % self.text_id)
599
490
checker.checked_texts[t] = self.text_sha1
618
509
def _diff(self, text_diff, from_label, tree, to_label, to_entry, to_tree,
619
510
output_to, reverse=False):
620
511
"""See InventoryEntry._diff."""
622
from_text = tree.get_file(self.file_id).readlines()
624
to_text = to_tree.get_file(to_entry.file_id).readlines()
628
text_diff(from_label, from_text,
629
to_label, to_text, output_to)
631
text_diff(to_label, to_text,
632
from_label, from_text, output_to)
635
label_pair = (to_label, from_label)
637
label_pair = (from_label, to_label)
638
print >> output_to, "Binary files %s and %s differ" % label_pair
512
from_text = tree.get_file(self.file_id).readlines()
514
to_text = to_tree.get_file(to_entry.file_id).readlines()
518
text_diff(from_label, from_text,
519
to_label, to_text, output_to)
521
text_diff(to_label, to_text,
522
from_label, from_text, output_to)
640
524
def has_text(self):
641
525
"""See InventoryEntry.has_text."""
671
555
self.text_sha1 = work_tree.get_file_sha1(self.file_id)
672
556
self.executable = work_tree.is_executable(self.file_id)
674
def _forget_tree_state(self):
675
self.text_sha1 = None
676
self.executable = None
678
def _snapshot_text(self, file_parents, work_tree, versionedfile_store, transaction):
558
def _snapshot_text(self, file_parents, work_tree, weave_store, transaction):
679
559
"""See InventoryEntry._snapshot_text."""
680
mutter('storing text of file {%s} in revision {%s} into %r',
681
self.file_id, self.revision, versionedfile_store)
560
mutter('storing file {%s} in revision {%s}',
561
self.file_id, self.revision)
682
562
# special case to avoid diffing on renames or
684
564
if (len(file_parents) == 1
685
565
and self.text_sha1 == file_parents.values()[0].text_sha1
686
566
and self.text_size == file_parents.values()[0].text_size):
687
567
previous_ie = file_parents.values()[0]
688
versionedfile = versionedfile_store.get_weave(self.file_id, transaction)
689
versionedfile.clone_text(self.revision, previous_ie.revision, file_parents.keys())
568
weave_store.add_identical_text(
569
self.file_id, previous_ie.revision,
570
self.revision, file_parents, transaction)
691
572
new_lines = work_tree.get_file(self.file_id).readlines()
692
self._add_text_to_weave(new_lines, file_parents.keys(), versionedfile_store,
573
self._add_text_to_weave(new_lines, file_parents, weave_store,
694
575
self.text_sha1 = sha_strings(new_lines)
695
576
self.text_size = sum(map(len, new_lines))
842
720
The inventory is created with a default root directory, with
845
# We are letting Branch.create() create a unique inventory
723
# We are letting Branch.initialize() create a unique inventory
846
724
# root id. Rather than generating a random one here.
847
725
#if root_id is None:
848
726
# root_id = bzrlib.branch.gen_file_id('TREE_ROOT')
849
727
self.root = RootEntry(root_id)
850
self.revision_id = revision_id
851
728
self._byid = {self.root.file_id: self.root}
855
# TODO: jam 20051218 Should copy also copy the revision_id?
856
732
other = Inventory(self.root.file_id)
857
733
# copy recursively so we know directories will be added before
858
734
# their children. There are more efficient ways than this...
987
863
if parent.children.has_key(entry.name):
988
864
raise BzrError("%s is already versioned" %
989
pathjoin(self.id2path(parent.file_id), entry.name))
865
appendpath(self.id2path(parent.file_id), entry.name))
991
867
self._byid[entry.file_id] = entry
992
868
parent.children[entry.name] = entry
996
def add_path(self, relpath, kind, file_id=None, parent_id=None):
872
def add_path(self, relpath, kind, file_id=None):
997
873
"""Add entry from a path.
999
875
The immediate parent must already be versioned.
1001
877
Returns the new entry object."""
878
from bzrlib.branch import gen_file_id
1003
880
parts = bzrlib.osutils.splitpath(relpath)
1005
881
if len(parts) == 0:
1007
file_id = bzrlib.workingtree.gen_root_id()
1008
self.root = RootEntry(file_id)
1009
self._byid = {self.root.file_id: self.root}
882
raise BzrError("cannot re-add root of inventory")
885
file_id = gen_file_id(relpath)
887
parent_path = parts[:-1]
888
parent_id = self.path2id(parent_path)
889
if parent_id == None:
890
raise NotVersionedError(path=parent_path)
891
if kind == 'directory':
892
ie = InventoryDirectory(file_id, parts[-1], parent_id)
894
ie = InventoryFile(file_id, parts[-1], parent_id)
895
elif kind == 'symlink':
896
ie = InventoryLink(file_id, parts[-1], parent_id)
1012
parent_path = parts[:-1]
1013
parent_id = self.path2id(parent_path)
1014
if parent_id == None:
1015
raise NotVersionedError(path=parent_path)
1016
ie = make_entry(kind, parts[-1], parent_id, file_id)
898
raise BzrError("unknown kind %r" % kind)
1017
899
return self.add(ie)
1019
902
def __delitem__(self, file_id):
1020
903
"""Remove entry by id.
1090
969
root directory as depth 1.
1093
for parent in self._iter_file_id_parents(file_id):
1094
p.insert(0, parent.file_id)
972
while file_id != None:
974
ie = self._byid[file_id]
976
raise BzrError("file_id {%s} not found in inventory" % file_id)
977
p.insert(0, ie.file_id)
978
file_id = ie.parent_id
1097
982
def id2path(self, file_id):
1098
"""Return as a string the path to file_id.
983
"""Return as a list the path to file_id.
1100
985
>>> i = Inventory()
1101
986
>>> e = i.add(InventoryDirectory('src-id', 'src', ROOT_ID))
1102
987
>>> e = i.add(InventoryFile('foo-id', 'foo.c', parent_id='src-id'))
1103
>>> print i.id2path('foo-id')
988
>>> print i.id2path('foo-id').replace(os.sep, '/')
1106
991
# get all names, skipping root
1107
return '/'.join(reversed(
1108
[parent.name for parent in
1109
self._iter_file_id_parents(file_id)][:-1]))
992
p = [self._byid[fid].name for fid in self.get_idpath(file_id)[1:]]
993
return os.sep.join(p)
1111
997
def path2id(self, name):
1112
998
"""Walk down through directories to return entry of last component.
1176
1062
file_ie.parent_id = new_parent_id
1179
def make_entry(kind, name, parent_id, file_id=None):
1180
"""Create an inventory entry.
1182
:param kind: the type of inventory entry to create.
1183
:param name: the basename of the entry.
1184
:param parent_id: the parent_id of the entry.
1185
:param file_id: the file_id to use. if None, one will be created.
1188
file_id = bzrlib.workingtree.gen_file_id(name)
1189
if kind == 'directory':
1190
return InventoryDirectory(file_id, name, parent_id)
1191
elif kind == 'file':
1192
return InventoryFile(file_id, name, parent_id)
1193
elif kind == 'symlink':
1194
return InventoryLink(file_id, name, parent_id)
1196
raise BzrError("unknown kind %r" % kind)
1200
1067
_NAME_RE = None