17
17
"""Read in a bundle stream, and process it into a BundleReader object."""
20
from io import BytesIO
20
from cStringIO import StringIO
29
from . import apply_bundle
30
from ...errors import (
35
from ..inventory import (
41
from ..inventorytree import InventoryTree
42
from ...osutils import sha_string, sha_strings, pathjoin
43
from ...revision import Revision, NULL_REVISION
44
from ..testament import StrictTestament
45
from ...trace import mutter, warning
50
from ..xml5 import serializer_v5
29
from bzrlib.bundle import apply_bundle
30
from bzrlib.errors import (TestamentMismatch, BzrError,
31
MalformedHeader, MalformedPatches, NotABundle)
32
from bzrlib.inventory import (Inventory, InventoryEntry,
33
InventoryDirectory, InventoryFile,
35
from bzrlib.osutils import sha_file, sha_string, pathjoin
36
from bzrlib.revision import Revision, NULL_REVISION
37
from bzrlib.testament import StrictTestament
38
from bzrlib.trace import mutter, warning
39
import bzrlib.transport
40
from bzrlib.tree import Tree
41
import bzrlib.urlutils
42
from bzrlib.xml5 import serializer_v5
53
45
class RevisionInfo(object):
54
46
"""Gets filled out for each revision object that is read.
57
48
def __init__(self, revision_id):
58
49
self.revision_id = revision_id
75
66
def as_revision(self):
76
67
rev = Revision(revision_id=self.revision_id,
77
committer=self.committer,
78
timestamp=float(self.timestamp),
79
timezone=int(self.timezone),
80
inventory_sha1=self.inventory_sha1,
81
message='\n'.join(self.message))
68
committer=self.committer,
69
timestamp=float(self.timestamp),
70
timezone=int(self.timezone),
71
inventory_sha1=self.inventory_sha1,
72
message='\n'.join(self.message))
83
74
if self.parent_ids:
84
75
rev.parent_ids.extend(self.parent_ids)
282
271
so build up an inventory, and make sure the hashes match.
284
273
# Now we should have a complete inventory entry.
285
cs = serializer_v5.write_inventory_to_chunks(inv)
286
sha1 = sha_strings(cs)
274
s = serializer_v5.write_inventory_to_string(inv)
287
276
# Target revision is the last entry in the real_revisions list
288
277
rev = self.get_revision(revision_id)
289
278
if rev.revision_id != revision_id:
290
279
raise AssertionError()
291
280
if sha1 != rev.inventory_sha1:
292
with open(',,bogus-inv', 'wb') as f:
281
f = open(',,bogus-inv', 'wb')
294
286
warning('Inventory sha hash mismatch for revision %s. %s'
295
287
' != %s' % (revision_id, sha1, rev.inventory_sha1))
297
def _testament(self, revision, tree):
298
raise NotImplementedError(self._testament)
300
def _validate_revision(self, tree, revision_id):
289
def _validate_revision(self, inventory, revision_id):
301
290
"""Make sure all revision entries match their checksum."""
303
# This is a mapping from each revision id to its sha hash
292
# This is a mapping from each revision id to it's sha hash
306
295
rev = self.get_revision(revision_id)
394
383
info = extra.split(' // ')
395
384
if len(info) <= 1:
396
385
raise BzrError('add action lines require the path and file id'
398
387
elif len(info) > 5:
399
388
raise BzrError('add action lines have fewer than 5 entries.'
402
391
if not info[1].startswith('file-id:'):
403
392
raise BzrError('The file-id should follow the path for an add'
405
394
# This will be Unicode because of how the stream is read. Turn it
406
395
# back into a utf8 file_id
407
file_id = cache_utf8.encode(info[1][8:])
396
file_id = osutils.safe_file_id(info[1][8:], warn=False)
409
398
bundle_tree.note_id(file_id, path, kind)
410
399
# this will be overridden in extra_info if executable is specified.
428
417
do_patch(path, lines, encoding)
430
419
valid_actions = {
436
425
for action_line, lines in \
437
self.get_revision_info(revision_id).tree_actions:
426
self.get_revision_info(revision_id).tree_actions:
438
427
first = action_line.find(' ')
440
429
raise BzrError('Bogus action line'
441
' (no opening space): %r' % action_line)
442
second = action_line.find(' ', first + 1)
430
' (no opening space): %r' % action_line)
431
second = action_line.find(' ', first+1)
444
433
raise BzrError('Bogus action line'
445
' (missing second space): %r' % action_line)
434
' (missing second space): %r' % action_line)
446
435
action = action_line[:first]
447
kind = action_line[first + 1:second]
436
kind = action_line[first+1:second]
448
437
if kind not in ('file', 'directory', 'symlink'):
449
438
raise BzrError('Bogus action line'
450
' (invalid object kind %r): %r' % (kind, action_line))
451
extra = action_line[second + 1:]
439
' (invalid object kind %r): %r' % (kind, action_line))
440
extra = action_line[second+1:]
453
442
if action not in valid_actions:
454
443
raise BzrError('Bogus action line'
455
' (unrecognized action): %r' % action_line)
444
' (unrecognized action): %r' % action_line)
456
445
valid_actions[action](kind, extra, lines)
458
447
def install_revisions(self, target_repo, stream_input=True):
472
461
return None, self.target, 'inapplicable'
475
class BundleTree(InventoryTree):
464
class BundleTree(Tree):
477
465
def __init__(self, base_tree, revision_id):
478
466
self.base_tree = base_tree
479
self._renamed = {} # Mapping from old_path => new_path
480
self._renamed_r = {} # new_path => old_path
481
self._new_id = {} # new_path => new_id
482
self._new_id_r = {} # new_id => new_path
483
self._kinds = {} # new_path => kind
484
self._last_changed = {} # new_id => revision_id
485
self._executable = {} # new_id => executable value
467
self._renamed = {} # Mapping from old_path => new_path
468
self._renamed_r = {} # new_path => old_path
469
self._new_id = {} # new_path => new_id
470
self._new_id_r = {} # new_id => new_path
471
self._kinds = {} # new_id => kind
472
self._last_changed = {} # new_id => revision_id
473
self._executable = {} # new_id => executable value
486
474
self.patches = {}
487
self._targets = {} # new path => new symlink target
475
self._targets = {} # new path => new symlink target
488
476
self.deleted = []
477
self.contents_by_id = True
489
478
self.revision_id = revision_id
490
479
self._inventory = None
491
self._base_inter = InterTree.get(self.base_tree, self)
493
481
def __str__(self):
494
482
return pprint.pformat(self.__dict__)
506
494
"""Files that don't exist in base need a new id."""
507
495
self._new_id[new_path] = new_id
508
496
self._new_id_r[new_id] = new_path
509
self._kinds[new_path] = kind
497
self._kinds[new_id] = kind
511
499
def note_last_changed(self, file_id, revision_id):
512
500
if (file_id in self._last_changed
513
501
and self._last_changed[file_id] != revision_id):
514
502
raise BzrError('Mismatched last-changed revision for file_id {%s}'
515
': %s != %s' % (file_id,
516
self._last_changed[file_id],
503
': %s != %s' % (file_id,
504
self._last_changed[file_id],
518
506
self._last_changed[file_id] = revision_id
520
508
def note_patch(self, new_path, patch):
594
582
if old_path in self.deleted:
596
return self.base_tree.path2id(old_path)
584
if getattr(self.base_tree, 'path2id', None) is not None:
585
return self.base_tree.path2id(old_path)
587
return self.base_tree.inventory.path2id(old_path)
598
def id2path(self, file_id, recurse='down'):
589
def id2path(self, file_id):
599
590
"""Return the new path in the target tree of the file with id file_id"""
600
591
path = self._new_id_r.get(file_id)
601
592
if path is not None:
603
old_path = self.base_tree.id2path(file_id, recurse)
594
old_path = self.base_tree.id2path(file_id)
604
595
if old_path is None:
605
raise NoSuchId(file_id, self)
606
597
if old_path in self.deleted:
607
raise NoSuchId(file_id, self)
608
new_path = self.new_path(old_path)
610
raise NoSuchId(file_id, self)
613
def get_file(self, path):
599
return self.new_path(old_path)
601
def old_contents_id(self, file_id):
602
"""Return the id in the base_tree for the given file_id.
603
Return None if the file did not exist in base.
605
if self.contents_by_id:
606
if self.base_tree.has_id(file_id):
610
new_path = self.id2path(file_id)
611
return self.base_tree.path2id(new_path)
613
def get_file(self, file_id):
614
614
"""Return a file-like object containing the new contents of the
615
615
file given by file_id.
618
618
in the text-store, so that the file contents would
621
old_path = self._base_inter.find_source_path(path)
621
base_id = self.old_contents_id(file_id)
622
if (base_id is not None and
623
base_id != self.base_tree.inventory.root.file_id):
624
patch_original = self.base_tree.get_file(base_id)
623
626
patch_original = None
625
patch_original = self.base_tree.get_file(old_path)
626
file_patch = self.patches.get(path)
627
file_patch = self.patches.get(self.id2path(file_id))
627
628
if file_patch is None:
628
629
if (patch_original is None and
629
self.kind(path) == 'directory'):
630
self.get_kind(file_id) == 'directory'):
631
632
if patch_original is None:
632
633
raise AssertionError("None: %s" % file_id)
633
634
return patch_original
635
if file_patch.startswith(b'\\'):
636
if file_patch.startswith('\\'):
636
637
raise ValueError(
637
638
'Malformed patch for %s, %r' % (file_id, file_patch))
638
639
return patched_file(file_patch, patch_original)
640
def get_symlink_target(self, path):
642
return self._targets[path]
644
old_path = self.old_path(path)
645
return self.base_tree.get_symlink_target(old_path)
647
def kind(self, path):
649
return self._kinds[path]
651
old_path = self.old_path(path)
652
return self.base_tree.kind(old_path)
654
def get_file_revision(self, path):
655
if path in self._last_changed:
656
return self._last_changed[path]
658
old_path = self.old_path(path)
659
return self.base_tree.get_file_revision(old_path)
661
def is_executable(self, path):
641
def get_symlink_target(self, file_id):
642
new_path = self.id2path(file_id)
644
return self._targets[new_path]
646
return self.base_tree.get_symlink_target(file_id)
648
def get_kind(self, file_id):
649
if file_id in self._kinds:
650
return self._kinds[file_id]
651
return self.base_tree.inventory[file_id].kind
653
def is_executable(self, file_id):
654
path = self.id2path(file_id)
662
655
if path in self._executable:
663
656
return self._executable[path]
665
old_path = self.old_path(path)
666
return self.base_tree.is_executable(old_path)
658
return self.base_tree.inventory[file_id].executable
668
def get_last_changed(self, path):
660
def get_last_changed(self, file_id):
661
path = self.id2path(file_id)
669
662
if path in self._last_changed:
670
663
return self._last_changed[path]
671
old_path = self.old_path(path)
672
return self.base_tree.get_file_revision(old_path)
664
return self.base_tree.inventory[file_id].revision
674
def get_size_and_sha1(self, new_path):
666
def get_size_and_sha1(self, file_id):
675
667
"""Return the size and sha1 hash of the given file id.
676
668
If the file was not locally modified, this is extracted
677
669
from the base_tree. Rather than re-reading the file.
671
new_path = self.id2path(file_id)
679
672
if new_path is None:
680
673
return None, None
681
674
if new_path not in self.patches:
682
675
# If the entry does not have a patch, then the
683
676
# contents must be the same as in the base_tree
684
base_path = self.old_path(new_path)
685
text_size = self.base_tree.get_file_size(base_path)
686
text_sha1 = self.base_tree.get_file_sha1(base_path)
687
return text_size, text_sha1
688
fileobj = self.get_file(new_path)
677
ie = self.base_tree.inventory[file_id]
678
if ie.text_size is None:
679
return ie.text_size, ie.text_sha1
680
return int(ie.text_size), ie.text_sha1
681
fileobj = self.get_file(file_id)
689
682
content = fileobj.read()
690
683
return len(content), sha_string(content)
695
688
This need to be called before ever accessing self.inventory
697
690
from os.path import dirname, basename
691
base_inv = self.base_tree.inventory
698
692
inv = Inventory(None, self.revision_id)
700
def add_entry(path, file_id):
694
def add_entry(file_id):
695
path = self.id2path(file_id)
704
701
parent_path = dirname(path)
705
702
parent_id = self.path2id(parent_path)
707
kind = self.kind(path)
708
revision_id = self.get_last_changed(path)
704
kind = self.get_kind(file_id)
705
revision_id = self.get_last_changed(file_id)
710
707
name = basename(path)
711
708
if kind == 'directory':
712
709
ie = InventoryDirectory(file_id, name, parent_id)
713
710
elif kind == 'file':
714
711
ie = InventoryFile(file_id, name, parent_id)
715
ie.executable = self.is_executable(path)
712
ie.executable = self.is_executable(file_id)
716
713
elif kind == 'symlink':
717
714
ie = InventoryLink(file_id, name, parent_id)
718
ie.symlink_target = self.get_symlink_target(path)
715
ie.symlink_target = self.get_symlink_target(file_id)
719
716
ie.revision = revision_id
722
ie.text_size, ie.text_sha1 = self.get_size_and_sha1(path)
723
if ie.text_size is None:
725
'Got a text_size of None for file_id %r' % file_id)
718
if kind in ('directory', 'symlink'):
719
ie.text_size, ie.text_sha1 = None, None
721
ie.text_size, ie.text_sha1 = self.get_size_and_sha1(file_id)
722
if (ie.text_size is None) and (kind == 'file'):
723
raise BzrError('Got a text_size of None for file_id %r' % file_id)
728
726
sorted_entries = self.sorted_path_id()
729
727
for path, file_id in sorted_entries:
730
add_entry(path, file_id)
738
736
# at that instant
739
737
inventory = property(_get_inventory)
741
root_inventory = property(_get_inventory)
743
def all_file_ids(self):
744
return {entry.file_id for path, entry in self.inventory.iter_entries()}
746
def all_versioned_paths(self):
747
return {path for path, entry in self.inventory.iter_entries()}
749
def list_files(self, include_root=False, from_dir=None, recursive=True):
750
# The only files returned by this are those from the version
755
from_dir_id = inv.path2id(from_dir)
756
if from_dir_id is None:
757
# Directory not versioned
759
entries = inv.iter_entries(from_dir=from_dir_id, recursive=recursive)
760
if inv.root is not None and not include_root and from_dir is None:
761
# skip the root for compatibility with the current apis.
763
for path, entry in entries:
764
yield path, 'V', entry.kind, entry
740
for path, entry in self.inventory.iter_entries():
766
743
def sorted_path_id(self):
768
for result in self._new_id.items():
745
for result in self._new_id.iteritems():
769
746
paths.append(result)
770
for id in self.base_tree.all_file_ids():
772
path = self.id2path(id, recurse='none')
747
for id in self.base_tree:
748
path = self.id2path(id)
775
751
paths.append((path, id))