38
38
from bzrlib.osutils import (pumpfile, quotefn, splitpath, joinpath,
39
39
pathjoin, sha_strings)
40
from bzrlib.errors import (NotVersionedError, InvalidEntryName,
41
BzrError, BzrCheckError, BinaryFile)
40
42
from bzrlib.trace import mutter
41
from bzrlib.errors import (NotVersionedError, InvalidEntryName,
42
BzrError, BzrCheckError)
45
45
class InventoryEntry(object):
121
121
def _add_text_to_weave(self, new_lines, parents, weave_store, transaction):
122
versionedfile = weave_store.get_weave(self.file_id, transaction)
122
versionedfile = weave_store.get_weave_or_empty(self.file_id,
123
124
versionedfile.add_lines(self.revision, parents, new_lines)
125
versionedfile.clear_cache()
125
127
def detect_changes(self, old_entry):
126
128
"""Return a (text_modified, meta_modified) from this to old_entry.
159
164
This is a map containing the file revisions in all parents
160
165
for which the file exists, and its revision is not a parent of
161
166
any other. If the file is new, the set will be empty.
168
:param versioned_file_store: A store where ancestry data on this
169
file id can be queried.
170
:param transaction: The transaction that queries to the versioned
171
file store should be completed under.
172
:param entry_vf: The entry versioned file, if its already available.
163
174
def get_ancestors(weave, entry):
164
175
return set(weave.get_ancestry(entry.revision))
176
# revision:ie mapping for each ie found in previous_inventories.
178
# revision:ie mapping with one revision for each head.
180
# revision: ancestor list for each head
166
181
head_ancestors = {}
182
# identify candidate head revision ids.
167
183
for inv in previous_inventories:
168
184
if self.file_id in inv:
169
185
ie = inv[self.file_id]
170
186
assert ie.file_id == self.file_id
171
if ie.revision in heads:
172
# fixup logic, there was a bug in revision updates.
173
# with x bit support.
187
if ie.revision in candidates:
188
# same revision value in two different inventories:
189
# correct possible inconsistencies:
190
# * there was a bug in revision updates with 'x' bit
175
if heads[ie.revision].executable != ie.executable:
176
heads[ie.revision].executable = False
193
if candidates[ie.revision].executable != ie.executable:
194
candidates[ie.revision].executable = False
177
195
ie.executable = False
178
196
except AttributeError:
180
assert heads[ie.revision] == ie
198
# must now be the same.
199
assert candidates[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
201
# add this revision as a candidate.
202
candidates[ie.revision] = ie
204
# common case optimisation
205
if len(candidates) == 1:
206
# if there is only one candidate revision found
207
# then we can opening the versioned file to access ancestry:
208
# there cannot be any ancestors to eliminate when there is
209
# only one revision available.
210
heads[ie.revision] = ie
213
# eliminate ancestors amongst the available candidates:
214
# heads are those that are not an ancestor of any other candidate
215
# - this provides convergence at a per-file level.
216
for ie in candidates.values():
217
# may be an ancestor of a known head:
218
already_present = 0 != len(
219
[head for head in heads
220
if ie.revision in head_ancestors[head]])
222
# an ancestor of an analyzed candidate.
224
# not an ancestor of a known head:
225
# load the versioned file for this file id if needed
227
entry_vf = versioned_file_store.get_weave_or_empty(
228
self.file_id, transaction)
229
ancestors = get_ancestors(entry_vf, ie)
230
# may knock something else out:
231
check_heads = list(heads.keys())
232
for head in check_heads:
233
if head in ancestors:
234
# this previously discovered 'head' is not
235
# really a head - its an ancestor of the newly
238
head_ancestors[ie.revision] = ancestors
239
heads[ie.revision] = ie
202
242
def get_tar_item(self, root, dp, now, tree):
301
349
raise BzrCheckError('unknown entry kind %r in revision {%s}' %
302
350
(self.kind, rev_id))
306
353
"""Clone this inventory entry."""
307
354
raise NotImplementedError
309
def _get_snapshot_change(self, previous_entries):
356
def _describe_snapshot_change(self, previous_entries):
357
"""Describe how this entry will have changed in a new commit.
359
:param previous_entries: Dictionary from revision_id to inventory entry.
361
:returns: One-word description: "merged", "added", "renamed", "modified".
363
# XXX: This assumes that the file *has* changed -- it should probably
364
# be fused with whatever does that detection. Why not just a single
365
# thing to compare the entries?
367
# TODO: Return some kind of object describing all the possible
368
# dimensions that can change, not just a string. That can then give
369
# both old and new names for renames, etc.
310
371
if len(previous_entries) > 1:
312
373
elif len(previous_entries) == 0:
315
return 'modified/renamed/reparented'
375
the_parent, = previous_entries.values()
376
if self.parent_id != the_parent.parent_id:
377
# actually, moved to another directory
379
elif self.name != the_parent.name:
317
383
def __repr__(self):
318
384
return ("%s(%r, %r, parent_id=%r)"
337
403
mutter("found unchanged entry")
338
404
self.revision = parent_ie.revision
339
405
return "unchanged"
340
return self.snapshot_revision(revision, previous_entries,
341
work_tree, weave_store, transaction)
343
def snapshot_revision(self, revision, previous_entries, work_tree,
344
weave_store, transaction):
345
"""Record this revision unconditionally."""
346
mutter('new revision for {%s}', self.file_id)
406
return self._snapshot_into_revision(revision, previous_entries,
407
work_tree, weave_store, transaction)
409
def _snapshot_into_revision(self, revision, previous_entries, work_tree,
410
weave_store, transaction):
411
"""Record this revision unconditionally into a store.
413
The entry's last-changed revision property (`revision`) is updated to
414
that of the new revision.
416
:param revision: id of the new revision that is being recorded.
418
:returns: String description of the commit (e.g. "merged", "modified"), etc.
420
mutter('new revision {%s} for {%s}', revision, self.file_id)
347
421
self.revision = revision
348
change = self._get_snapshot_change(previous_entries)
422
change = self._describe_snapshot_change(previous_entries)
349
423
self._snapshot_text(previous_entries, work_tree, weave_store,
472
546
class InventoryFile(InventoryEntry):
473
547
"""A file in an inventory."""
475
def _check(self, checker, rev_id, tree):
549
def _check(self, checker, tree_revision_id, tree):
476
550
"""See InventoryEntry._check"""
477
revision = self.revision
478
t = (self.file_id, revision)
551
t = (self.file_id, self.revision)
479
552
if t in checker.checked_texts:
480
prev_sha = checker.checked_texts[t]
553
prev_sha = checker.checked_texts[t]
481
554
if prev_sha != self.text_sha1:
482
555
raise BzrCheckError('mismatched sha1 on {%s} in {%s}' %
483
(self.file_id, rev_id))
556
(self.file_id, tree_revision_id))
485
558
checker.repeated_text_cnt += 1
497
570
w = tree.get_weave(self.file_id)
499
mutter('check version {%s} of {%s}', rev_id, self.file_id)
500
checker.checked_text_cnt += 1
572
mutter('check version {%s} of {%s}', tree_revision_id, self.file_id)
573
checker.checked_text_cnt += 1
501
574
# We can't check the length, because Weave doesn't store that
502
575
# information, and the whole point of looking at the weave's
503
576
# sha1sum is that we don't have to extract the text.
526
599
def _diff(self, text_diff, from_label, tree, to_label, to_entry, to_tree,
527
600
output_to, reverse=False):
528
601
"""See InventoryEntry._diff."""
529
from_text = tree.get_file(self.file_id).readlines()
531
to_text = to_tree.get_file(to_entry.file_id).readlines()
535
text_diff(from_label, from_text,
536
to_label, to_text, output_to)
538
text_diff(to_label, to_text,
539
from_label, from_text, output_to)
603
from_text = tree.get_file(self.file_id).readlines()
605
to_text = to_tree.get_file(to_entry.file_id).readlines()
609
text_diff(from_label, from_text,
610
to_label, to_text, output_to)
612
text_diff(to_label, to_text,
613
from_label, from_text, output_to)
616
label_pair = (to_label, from_label)
618
label_pair = (from_label, to_label)
619
print >> output_to, "Binary files %s and %s differ" % label_pair
541
621
def has_text(self):
542
622
"""See InventoryEntry.has_text."""