78
78
>>> i.add(InventoryDirectory('123', 'src', ROOT_ID))
79
79
InventoryDirectory('123', 'src', parent_id='TREE_ROOT')
80
>>> i.add(InventoryFile('2323', 'hello.c', parent_id='123'))
81
InventoryFile('2323', 'hello.c', parent_id='123')
80
>>> i.add(InventoryEntry('2323', 'hello.c', 'file', parent_id='123'))
81
InventoryEntry('2323', 'hello.c', kind='file', parent_id='123')
82
82
>>> for j in i.iter_entries():
85
85
('src', InventoryDirectory('123', 'src', parent_id='TREE_ROOT'))
86
('src/hello.c', InventoryFile('2323', 'hello.c', parent_id='123'))
87
>>> i.add(InventoryFile('2323', 'bye.c', '123'))
86
('src/hello.c', InventoryEntry('2323', 'hello.c', kind='file', parent_id='123'))
87
>>> i.add(InventoryEntry('2323', 'bye.c', 'file', '123'))
88
88
Traceback (most recent call last):
90
90
BzrError: inventory already contains entry with id {2323}
91
>>> i.add(InventoryFile('2324', 'bye.c', '123'))
92
InventoryFile('2324', 'bye.c', parent_id='123')
91
>>> i.add(InventoryEntry('2324', 'bye.c', 'file', '123'))
92
InventoryEntry('2324', 'bye.c', kind='file', parent_id='123')
93
93
>>> i.add(InventoryDirectory('2325', 'wibble', '123'))
94
94
InventoryDirectory('2325', 'wibble', parent_id='123')
95
95
>>> i.path2id('src/wibble')
99
>>> i.add(InventoryFile('2326', 'wibble.c', '2325'))
100
InventoryFile('2326', 'wibble.c', parent_id='2325')
99
>>> i.add(InventoryEntry('2326', 'wibble.c', 'file', '2325'))
100
InventoryEntry('2326', 'wibble.c', kind='file', parent_id='2325')
102
InventoryFile('2326', 'wibble.c', parent_id='2325')
102
InventoryEntry('2326', 'wibble.c', kind='file', parent_id='2325')
103
103
>>> for path, entry in i.iter_entries():
104
104
... print path.replace('\\\\', '/') # for win32 os.sep
105
105
... assert i.path2id(path)
143
155
assert self.kind == to_entry.kind
144
156
to_entry._read_tree_state(to_tree.id2path(to_entry.file_id),
146
self._diff(text_diff, from_label, tree, to_label, to_entry, to_tree,
149
def _diff(self, text_diff, from_label, tree, to_label, to_entry, to_tree,
150
output_to, reverse=False):
151
"""Perform a diff between two entries of the same kind."""
153
def find_previous_heads(self, previous_inventories, entry_weave):
154
"""Return the revisions and entries that directly preceed this.
156
Returned as a map from revision to inventory entry.
158
This is a map containing the file revisions in all parents
159
for which the file exists, and its revision is not a parent of
160
any other. If the file is new, the set will be empty.
162
def get_ancestors(weave, entry):
163
return set(map(weave.idx_to_name,
164
weave.inclusions([weave.lookup(entry.revision)])))
167
for inv in previous_inventories:
168
if self.file_id in inv:
169
ie = inv[self.file_id]
170
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.
175
if heads[ie.revision].executable != ie.executable:
176
heads[ie.revision].executable = False
177
ie.executable = False
178
except AttributeError:
180
assert heads[ie.revision] == ie
158
if self.kind == 'file':
159
from_text = tree.get_file(self.file_id).readlines()
161
to_text = to_tree.get_file(to_entry.file_id).readlines()
165
text_diff(from_label, from_text,
166
to_label, to_text, output_to)
168
text_diff(to_label, to_text,
169
from_label, from_text, output_to)
170
elif self.kind == 'symlink':
171
from_text = self.symlink_target
172
if to_entry is not None:
173
to_text = to_entry.symlink_target
178
print >>output_to, '=== target changed %r => %r' % (from_text, to_text)
181
print >>output_to, '=== target was %r' % self.symlink_target
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
183
print >>output_to, '=== target is %r' % self.symlink_target
202
185
def get_tar_item(self, root, dp, now, tree):
203
186
"""Get a tarfile item and a file stream for its content."""
212
195
"""Return true if the object this entry represents has textual data.
214
197
Note that textual data includes binary content.
216
Also note that all entries get weave files created for them.
217
This attribute is primarily used when upgrading from old trees that
218
did not have the weave index for all inventory entries.
199
if self.kind =='file':
222
def __init__(self, file_id, name, parent_id, text_id=None):
204
def __init__(self, file_id, name, kind, parent_id, text_id=None):
223
205
"""Create an InventoryEntry
225
207
The filename must be a single component, relative to the
226
208
parent directory; it cannot be a whole path or relative name.
228
>>> e = InventoryFile('123', 'hello.c', ROOT_ID)
210
>>> e = InventoryEntry('123', 'hello.c', 'file', ROOT_ID)
233
>>> e = InventoryFile('123', 'src/hello.c', ROOT_ID)
215
>>> e = InventoryEntry('123', 'src/hello.c', 'file', ROOT_ID)
234
216
Traceback (most recent call last):
235
InvalidEntryName: Invalid entry name: src/hello.c
217
BzrCheckError: InventoryEntry name 'src/hello.c' is invalid
237
219
assert isinstance(name, basestring), name
238
220
if '/' in name or '\\' in name:
239
raise InvalidEntryName(name=name)
221
raise BzrCheckError('InventoryEntry name %r is invalid' % name)
240
223
self.executable = False
241
224
self.revision = None
242
225
self.text_sha1 = None
243
226
self.text_size = None
244
227
self.file_id = file_id
246
230
self.text_id = text_id
247
231
self.parent_id = parent_id
248
232
self.symlink_target = None
233
if kind not in ('directory', 'file', 'symlink'):
234
raise BzrError("unhandled entry kind %r" % kind)
250
236
def kind_character(self):
251
237
"""Return a short kind indicator useful for appending to names."""
252
raise BzrError('unknown kind %r' % self.kind)
238
if self.kind == 'file':
240
if self.kind == 'symlink':
242
raise RuntimeError('unreachable code')
254
244
known_kinds = ('file', 'directory', 'symlink', 'root_directory')
246
def __new__(cls, file_id, name, kind, parent_id, text_id=None):
247
"""Factory method to return the appropriate concrete class."""
248
return object.__new__(InventoryEntry, file_id, name, kind, parent_id,
256
251
def _put_in_tar(self, item, tree):
257
252
"""populate item for stashing in a tar, and return the content stream.
259
254
If no content is available, return None.
261
raise BzrError("don't know how to export {%s} of kind %r" %
262
(self.file_id, self.kind))
256
if self.kind == 'file':
257
item.type = tarfile.REGTYPE
258
fileobj = tree.get_file(self.file_id)
259
item.size = self.text_size
260
if tree.is_executable(self.file_id):
264
elif self.kind == 'symlink':
265
iterm.type = tarfile.SYMTYPE
269
item.linkname = self.symlink_target
271
raise BzrError("don't know how to export {%s} of kind %r" %
272
(self.file_id, self.kind))
264
275
def put_on_disk(self, dest, dp, tree):
265
276
"""Create a representation of self on disk in the prefix dest.
298
319
def _check(self, checker, rev_id, tree):
299
320
"""Check this inventory entry for kind specific errors."""
300
raise BzrCheckError('unknown entry kind %r in revision {%s}' %
321
if self.kind == 'file':
322
revision = self.revision
323
t = (self.file_id, revision)
324
if t in checker.checked_texts:
325
prev_sha = checker.checked_texts[t]
326
if prev_sha != self.text_sha1:
327
raise BzrCheckError('mismatched sha1 on {%s} in {%s}' %
328
(self.file_id, rev_id))
330
checker.repeated_text_cnt += 1
332
mutter('check version {%s} of {%s}', rev_id, self.file_id)
333
file_lines = tree.get_file_lines(self.file_id)
334
checker.checked_text_cnt += 1
335
if self.text_size != sum(map(len, file_lines)):
336
raise BzrCheckError('text {%s} wrong size' % self.text_id)
337
if self.text_sha1 != sha_strings(file_lines):
338
raise BzrCheckError('text {%s} wrong sha1' % self.text_id)
339
checker.checked_texts[t] = self.text_sha1
340
elif self.kind == 'symlink':
341
if self.text_sha1 != None or self.text_size != None or self.text_id != None:
342
raise BzrCheckError('symlink {%s} has text in revision {%s}'
343
% (self.file_id, rev_id))
344
if self.symlink_target == None:
345
raise BzrCheckError('symlink {%s} has no target in revision {%s}'
346
% (self.file_id, rev_id))
348
raise BzrCheckError('unknown entry kind %r in revision {%s}' %
305
"""Clone this inventory entry."""
306
raise NotImplementedError
353
other = InventoryEntry(self.file_id, self.name, self.kind,
355
other.executable = self.executable
356
other.text_id = self.text_id
357
other.text_sha1 = self.text_sha1
358
other.text_size = self.text_size
359
other.symlink_target = self.symlink_target
360
other.revision = self.revision
361
# note that children are *not* copied; they're pulled across when
308
365
def _get_snapshot_change(self, previous_entries):
309
366
if len(previous_entries) > 1:
332
390
if len(previous_entries) == 1:
333
391
# cannot be unchanged unless there is only one parent file rev.
334
392
parent_ie = previous_entries.values()[0]
335
if self._unchanged(parent_ie):
393
if self._unchanged(path, parent_ie, work_tree):
336
394
mutter("found unchanged entry")
337
395
self.revision = parent_ie.revision
338
396
return "unchanged"
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
397
mutter('new revision for {%s}', self.file_id)
346
398
self.revision = revision
347
399
change = self._get_snapshot_change(previous_entries)
348
self._snapshot_text(previous_entries, work_tree, weave_store,
400
if self.kind != 'file':
402
self._snapshot_text(previous_entries, work_tree, weave_store)
352
def _snapshot_text(self, file_parents, work_tree, weave_store, transaction):
353
"""Record the 'text' of this entry, whatever form that takes.
355
This default implementation simply adds an empty text.
405
def _snapshot_text(self, file_parents, work_tree, weave_store):
357
406
mutter('storing file {%s} in revision {%s}',
358
407
self.file_id, self.revision)
359
self._add_text_to_weave([], file_parents, weave_store, transaction)
408
# special case to avoid diffing on renames or
410
if (len(file_parents) == 1
411
and self.text_sha1 == file_parents.values()[0].text_sha1
412
and self.text_size == file_parents.values()[0].text_size):
413
previous_ie = file_parents.values()[0]
414
weave_store.add_identical_text(
415
self.file_id, previous_ie.revision,
416
self.revision, file_parents)
418
new_lines = work_tree.get_file(self.file_id).readlines()
419
self._add_text_to_weave(new_lines, file_parents, weave_store)
420
self.text_sha1 = sha_strings(new_lines)
421
self.text_size = sum(map(len, new_lines))
361
423
def __eq__(self, other):
362
424
if not isinstance(other, InventoryEntry):
464
541
"""See InventoryEntry._put_on_disk."""
465
542
os.mkdir(fullpath)
468
class InventoryFile(InventoryEntry):
469
"""A file in an inventory."""
471
def _check(self, checker, rev_id, tree):
472
"""See InventoryEntry._check"""
473
revision = self.revision
474
t = (self.file_id, revision)
475
if t in checker.checked_texts:
476
prev_sha = checker.checked_texts[t]
477
if prev_sha != self.text_sha1:
478
raise BzrCheckError('mismatched sha1 on {%s} in {%s}' %
479
(self.file_id, rev_id))
481
checker.repeated_text_cnt += 1
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)
490
checker.checked_texts[t] = self.text_sha1
493
other = InventoryFile(self.file_id, self.name, self.parent_id)
494
other.executable = self.executable
495
other.text_id = self.text_id
496
other.text_sha1 = self.text_sha1
497
other.text_size = self.text_size
498
other.revision = self.revision
501
def detect_changes(self, old_entry):
502
"""See InventoryEntry.detect_changes."""
503
assert self.text_sha1 != None
504
assert old_entry.text_sha1 != None
505
text_modified = (self.text_sha1 != old_entry.text_sha1)
506
meta_modified = (self.executable != old_entry.executable)
507
return text_modified, meta_modified
509
def _diff(self, text_diff, from_label, tree, to_label, to_entry, to_tree,
510
output_to, reverse=False):
511
"""See InventoryEntry._diff."""
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)
525
"""See InventoryEntry.has_text."""
528
def __init__(self, file_id, name, parent_id):
529
super(InventoryFile, self).__init__(file_id, name, parent_id)
532
def kind_character(self):
533
"""See InventoryEntry.kind_character."""
536
def _put_in_tar(self, item, tree):
537
"""See InventoryEntry._put_in_tar."""
538
item.type = tarfile.REGTYPE
539
fileobj = tree.get_file(self.file_id)
540
item.size = self.text_size
541
if tree.is_executable(self.file_id):
547
def _put_on_disk(self, fullpath, tree):
548
"""See InventoryEntry._put_on_disk."""
549
pumpfile(tree.get_file(self.file_id), file(fullpath, 'wb'))
550
if tree.is_executable(self.file_id):
551
os.chmod(fullpath, 0755)
553
def _read_tree_state(self, path, work_tree):
554
"""See InventoryEntry._read_tree_state."""
555
self.text_sha1 = work_tree.get_file_sha1(self.file_id)
556
self.executable = work_tree.is_executable(self.file_id)
558
def _snapshot_text(self, file_parents, work_tree, weave_store, transaction):
559
"""See InventoryEntry._snapshot_text."""
560
mutter('storing file {%s} in revision {%s}',
561
self.file_id, self.revision)
562
# special case to avoid diffing on renames or
564
if (len(file_parents) == 1
565
and self.text_sha1 == file_parents.values()[0].text_sha1
566
and self.text_size == file_parents.values()[0].text_size):
567
previous_ie = file_parents.values()[0]
568
weave_store.add_identical_text(
569
self.file_id, previous_ie.revision,
570
self.revision, file_parents, transaction)
572
new_lines = work_tree.get_file(self.file_id).readlines()
573
self._add_text_to_weave(new_lines, file_parents, weave_store,
575
self.text_sha1 = sha_strings(new_lines)
576
self.text_size = sum(map(len, new_lines))
579
def _unchanged(self, previous_ie):
580
"""See InventoryEntry._unchanged."""
581
compatible = super(InventoryFile, self)._unchanged(previous_ie)
582
if self.text_sha1 != previous_ie.text_sha1:
585
# FIXME: 20050930 probe for the text size when getting sha1
586
# in _read_tree_state
587
self.text_size = previous_ie.text_size
588
if self.executable != previous_ie.executable:
593
class InventoryLink(InventoryEntry):
594
"""A file in an inventory."""
596
__slots__ = ['symlink_target']
598
def _check(self, checker, rev_id, tree):
599
"""See InventoryEntry._check"""
600
if self.text_sha1 != None or self.text_size != None or self.text_id != None:
601
raise BzrCheckError('symlink {%s} has text in revision {%s}'
602
% (self.file_id, rev_id))
603
if self.symlink_target == None:
604
raise BzrCheckError('symlink {%s} has no target in revision {%s}'
605
% (self.file_id, rev_id))
608
other = InventoryLink(self.file_id, self.name, self.parent_id)
609
other.symlink_target = self.symlink_target
610
other.revision = self.revision
613
def detect_changes(self, old_entry):
614
"""See InventoryEntry.detect_changes."""
615
# FIXME: which _modified field should we use ? RBC 20051003
616
text_modified = (self.symlink_target != old_entry.symlink_target)
618
mutter(" symlink target changed")
619
meta_modified = False
620
return text_modified, meta_modified
622
def _diff(self, text_diff, from_label, tree, to_label, to_entry, to_tree,
623
output_to, reverse=False):
624
"""See InventoryEntry._diff."""
625
from_text = self.symlink_target
626
if to_entry is not None:
627
to_text = to_entry.symlink_target
632
print >>output_to, '=== target changed %r => %r' % (from_text, to_text)
635
print >>output_to, '=== target was %r' % self.symlink_target
637
print >>output_to, '=== target is %r' % self.symlink_target
639
def __init__(self, file_id, name, parent_id):
640
super(InventoryLink, self).__init__(file_id, name, parent_id)
641
self.kind = 'symlink'
643
def kind_character(self):
644
"""See InventoryEntry.kind_character."""
647
def _put_in_tar(self, item, tree):
648
"""See InventoryEntry._put_in_tar."""
649
iterm.type = tarfile.SYMTYPE
653
item.linkname = self.symlink_target
656
def _put_on_disk(self, fullpath, tree):
657
"""See InventoryEntry._put_on_disk."""
659
os.symlink(self.symlink_target, fullpath)
661
raise BzrError("Failed to create symlink %r -> %r, error: %s" % (fullpath, self.symlink_target, e))
663
def _read_tree_state(self, path, work_tree):
664
"""See InventoryEntry._read_tree_state."""
665
self.symlink_target = work_tree.get_symlink_target(self.file_id)
667
def _unchanged(self, previous_ie):
668
"""See InventoryEntry._unchanged."""
669
compatible = super(InventoryLink, self)._unchanged(previous_ie)
670
if self.symlink_target != previous_ie.symlink_target:
545
return ("%s(%r, %r, parent_id=%r)"
546
% (self.__class__.__name__,
675
552
class Inventory(object):