17
17
"""Read in a bundle stream, and process it into a BundleReader object."""
19
from __future__ import absolute_import
22
from io import BytesIO
20
from cStringIO import StringIO
31
from . import apply_bundle
32
from ...errors import (
37
from ..inventory import (
43
from ...osutils import sha_string, sha_strings, pathjoin
44
from ...revision import Revision, NULL_REVISION
45
from ...sixish import (
48
from ..testament import StrictTestament
49
from ...trace import mutter, warning
54
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
57
45
class RevisionInfo(object):
58
46
"""Gets filled out for each revision object that is read.
61
48
def __init__(self, revision_id):
62
49
self.revision_id = revision_id
79
66
def as_revision(self):
80
67
rev = Revision(revision_id=self.revision_id,
81
committer=self.committer,
82
timestamp=float(self.timestamp),
83
timezone=int(self.timezone),
84
inventory_sha1=self.inventory_sha1,
85
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))
87
74
if self.parent_ids:
88
75
rev.parent_ids.extend(self.parent_ids)
286
271
so build up an inventory, and make sure the hashes match.
288
273
# Now we should have a complete inventory entry.
289
cs = serializer_v5.write_inventory_to_chunks(inv)
290
sha1 = sha_strings(cs)
274
s = serializer_v5.write_inventory_to_string(inv)
291
276
# Target revision is the last entry in the real_revisions list
292
277
rev = self.get_revision(revision_id)
293
278
if rev.revision_id != revision_id:
294
279
raise AssertionError()
295
280
if sha1 != rev.inventory_sha1:
296
with open(',,bogus-inv', 'wb') as f:
281
f = open(',,bogus-inv', 'wb')
298
286
warning('Inventory sha hash mismatch for revision %s. %s'
299
287
' != %s' % (revision_id, sha1, rev.inventory_sha1))
301
def _testament(self, revision, tree):
302
raise NotImplementedError(self._testament)
304
def _validate_revision(self, tree, revision_id):
289
def _validate_revision(self, inventory, revision_id):
305
290
"""Make sure all revision entries match their checksum."""
307
# 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
310
295
rev = self.get_revision(revision_id)
398
383
info = extra.split(' // ')
399
384
if len(info) <= 1:
400
385
raise BzrError('add action lines require the path and file id'
402
387
elif len(info) > 5:
403
388
raise BzrError('add action lines have fewer than 5 entries.'
406
391
if not info[1].startswith('file-id:'):
407
392
raise BzrError('The file-id should follow the path for an add'
409
394
# This will be Unicode because of how the stream is read. Turn it
410
395
# back into a utf8 file_id
411
file_id = cache_utf8.encode(info[1][8:])
396
file_id = osutils.safe_file_id(info[1][8:], warn=False)
413
398
bundle_tree.note_id(file_id, path, kind)
414
399
# this will be overridden in extra_info if executable is specified.
432
417
do_patch(path, lines, encoding)
434
419
valid_actions = {
440
425
for action_line, lines in \
441
self.get_revision_info(revision_id).tree_actions:
426
self.get_revision_info(revision_id).tree_actions:
442
427
first = action_line.find(' ')
444
429
raise BzrError('Bogus action line'
445
' (no opening space): %r' % action_line)
446
second = action_line.find(' ', first + 1)
430
' (no opening space): %r' % action_line)
431
second = action_line.find(' ', first+1)
448
433
raise BzrError('Bogus action line'
449
' (missing second space): %r' % action_line)
434
' (missing second space): %r' % action_line)
450
435
action = action_line[:first]
451
kind = action_line[first + 1:second]
436
kind = action_line[first+1:second]
452
437
if kind not in ('file', 'directory', 'symlink'):
453
438
raise BzrError('Bogus action line'
454
' (invalid object kind %r): %r' % (kind, action_line))
455
extra = action_line[second + 1:]
439
' (invalid object kind %r): %r' % (kind, action_line))
440
extra = action_line[second+1:]
457
442
if action not in valid_actions:
458
443
raise BzrError('Bogus action line'
459
' (unrecognized action): %r' % action_line)
444
' (unrecognized action): %r' % action_line)
460
445
valid_actions[action](kind, extra, lines)
462
447
def install_revisions(self, target_repo, stream_input=True):
479
464
class BundleTree(Tree):
481
465
def __init__(self, base_tree, revision_id):
482
466
self.base_tree = base_tree
483
self._renamed = {} # Mapping from old_path => new_path
484
self._renamed_r = {} # new_path => old_path
485
self._new_id = {} # new_path => new_id
486
self._new_id_r = {} # new_id => new_path
487
self._kinds = {} # new_path => kind
488
self._last_changed = {} # new_id => revision_id
489
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
490
474
self.patches = {}
491
self._targets = {} # new path => new symlink target
475
self._targets = {} # new path => new symlink target
492
476
self.deleted = []
477
self.contents_by_id = True
493
478
self.revision_id = revision_id
494
479
self._inventory = None
495
self._base_inter = InterTree.get(self.base_tree, self)
497
481
def __str__(self):
498
482
return pprint.pformat(self.__dict__)
510
494
"""Files that don't exist in base need a new id."""
511
495
self._new_id[new_path] = new_id
512
496
self._new_id_r[new_id] = new_path
513
self._kinds[new_path] = kind
497
self._kinds[new_id] = kind
515
499
def note_last_changed(self, file_id, revision_id):
516
500
if (file_id in self._last_changed
517
501
and self._last_changed[file_id] != revision_id):
518
502
raise BzrError('Mismatched last-changed revision for file_id {%s}'
519
': %s != %s' % (file_id,
520
self._last_changed[file_id],
503
': %s != %s' % (file_id,
504
self._last_changed[file_id],
522
506
self._last_changed[file_id] = revision_id
524
508
def note_patch(self, new_path, patch):
598
582
if old_path in self.deleted:
600
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)
602
def id2path(self, file_id, recurse='down'):
589
def id2path(self, file_id):
603
590
"""Return the new path in the target tree of the file with id file_id"""
604
591
path = self._new_id_r.get(file_id)
605
592
if path is not None:
607
old_path = self.base_tree.id2path(file_id, recurse)
594
old_path = self.base_tree.id2path(file_id)
608
595
if old_path is None:
609
raise NoSuchId(file_id, self)
610
597
if old_path in self.deleted:
611
raise NoSuchId(file_id, self)
612
new_path = self.new_path(old_path)
614
raise NoSuchId(file_id, self)
617
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):
618
614
"""Return a file-like object containing the new contents of the
619
615
file given by file_id.
622
618
in the text-store, so that the file contents would
625
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)
627
626
patch_original = None
629
patch_original = self.base_tree.get_file(old_path)
630
file_patch = self.patches.get(path)
627
file_patch = self.patches.get(self.id2path(file_id))
631
628
if file_patch is None:
632
629
if (patch_original is None and
633
self.kind(path) == 'directory'):
630
self.get_kind(file_id) == 'directory'):
635
632
if patch_original is None:
636
633
raise AssertionError("None: %s" % file_id)
637
634
return patch_original
639
if file_patch.startswith(b'\\'):
636
if file_patch.startswith('\\'):
640
637
raise ValueError(
641
638
'Malformed patch for %s, %r' % (file_id, file_patch))
642
639
return patched_file(file_patch, patch_original)
644
def get_symlink_target(self, path):
646
return self._targets[path]
648
old_path = self.old_path(path)
649
return self.base_tree.get_symlink_target(old_path)
651
def kind(self, path):
653
return self._kinds[path]
655
old_path = self.old_path(path)
656
return self.base_tree.kind(old_path)
658
def get_file_revision(self, path):
659
if path in self._last_changed:
660
return self._last_changed[path]
662
old_path = self.old_path(path)
663
return self.base_tree.get_file_revision(old_path)
665
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)
666
655
if path in self._executable:
667
656
return self._executable[path]
669
old_path = self.old_path(path)
670
return self.base_tree.is_executable(old_path)
658
return self.base_tree.inventory[file_id].executable
672
def get_last_changed(self, path):
660
def get_last_changed(self, file_id):
661
path = self.id2path(file_id)
673
662
if path in self._last_changed:
674
663
return self._last_changed[path]
675
old_path = self.old_path(path)
676
return self.base_tree.get_file_revision(old_path)
664
return self.base_tree.inventory[file_id].revision
678
def get_size_and_sha1(self, new_path):
666
def get_size_and_sha1(self, file_id):
679
667
"""Return the size and sha1 hash of the given file id.
680
668
If the file was not locally modified, this is extracted
681
669
from the base_tree. Rather than re-reading the file.
671
new_path = self.id2path(file_id)
683
672
if new_path is None:
684
673
return None, None
685
674
if new_path not in self.patches:
686
675
# If the entry does not have a patch, then the
687
676
# contents must be the same as in the base_tree
688
base_path = self.old_path(new_path)
689
text_size = self.base_tree.get_file_size(base_path)
690
text_sha1 = self.base_tree.get_file_sha1(base_path)
691
return text_size, text_sha1
692
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)
693
682
content = fileobj.read()
694
683
return len(content), sha_string(content)
699
688
This need to be called before ever accessing self.inventory
701
690
from os.path import dirname, basename
691
base_inv = self.base_tree.inventory
702
692
inv = Inventory(None, self.revision_id)
704
def add_entry(path, file_id):
694
def add_entry(file_id):
695
path = self.id2path(file_id)
708
701
parent_path = dirname(path)
709
702
parent_id = self.path2id(parent_path)
711
kind = self.kind(path)
712
revision_id = self.get_last_changed(path)
704
kind = self.get_kind(file_id)
705
revision_id = self.get_last_changed(file_id)
714
707
name = basename(path)
715
708
if kind == 'directory':
716
709
ie = InventoryDirectory(file_id, name, parent_id)
717
710
elif kind == 'file':
718
711
ie = InventoryFile(file_id, name, parent_id)
719
ie.executable = self.is_executable(path)
712
ie.executable = self.is_executable(file_id)
720
713
elif kind == 'symlink':
721
714
ie = InventoryLink(file_id, name, parent_id)
722
ie.symlink_target = self.get_symlink_target(path)
715
ie.symlink_target = self.get_symlink_target(file_id)
723
716
ie.revision = revision_id
726
ie.text_size, ie.text_sha1 = self.get_size_and_sha1(path)
727
if ie.text_size is None:
729
'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)
732
726
sorted_entries = self.sorted_path_id()
733
727
for path, file_id in sorted_entries:
734
add_entry(path, file_id)
742
736
# at that instant
743
737
inventory = property(_get_inventory)
745
root_inventory = property(_get_inventory)
747
def all_file_ids(self):
748
return {entry.file_id for path, entry in self.inventory.iter_entries()}
750
def all_versioned_paths(self):
751
return {path for path, entry in self.inventory.iter_entries()}
753
def list_files(self, include_root=False, from_dir=None, recursive=True):
754
# The only files returned by this are those from the version
759
from_dir_id = inv.path2id(from_dir)
760
if from_dir_id is None:
761
# Directory not versioned
763
entries = inv.iter_entries(from_dir=from_dir_id, recursive=recursive)
764
if inv.root is not None and not include_root and from_dir is None:
765
# skip the root for compatibility with the current apis.
767
for path, entry in entries:
768
yield path, 'V', entry.kind, entry
740
for path, entry in self.inventory.iter_entries():
770
743
def sorted_path_id(self):
772
for result in viewitems(self._new_id):
745
for result in self._new_id.iteritems():
773
746
paths.append(result)
774
for id in self.base_tree.all_file_ids():
776
path = self.id2path(id, recurse='none')
747
for id in self.base_tree:
748
path = self.id2path(id)
779
751
paths.append((path, id))