17
17
"""Read in a bundle stream, and process it into a BundleReader object."""
19
from __future__ import absolute_import
20
from cStringIO import StringIO
22
from io import BytesIO
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
31
from . import apply_bundle
32
from ..errors import (
36
from ..bzr.inventory import (
42
from ..osutils import sha_string, pathjoin
43
from ..revision import Revision, NULL_REVISION
44
from ..sixish import (
47
from ..testament import StrictTestament
48
from ..trace import mutter, warning
49
from ..tree import Tree
50
from ..bzr.xml5 import serializer_v5
45
53
class RevisionInfo(object):
46
54
"""Gets filled out for each revision object that is read.
48
57
def __init__(self, revision_id):
49
58
self.revision_id = revision_id
278
287
if rev.revision_id != revision_id:
279
288
raise AssertionError()
280
289
if sha1 != rev.inventory_sha1:
281
open(',,bogus-inv', 'wb').write(s)
290
with open(',,bogus-inv', 'wb') as f:
282
292
warning('Inventory sha hash mismatch for revision %s. %s'
283
293
' != %s' % (revision_id, sha1, rev.inventory_sha1))
285
def _validate_revision(self, inventory, revision_id):
295
def _validate_revision(self, tree, revision_id):
286
296
"""Make sure all revision entries match their checksum."""
288
# This is a mapping from each revision id to it's sha hash
298
# This is a mapping from each revision id to its sha hash
291
301
rev = self.get_revision(revision_id)
614
624
in the text-store, so that the file contents would
628
file_id = self.path2id(path)
617
629
base_id = self.old_contents_id(file_id)
618
630
if (base_id is not None and
619
base_id != self.base_tree.inventory.root.file_id):
620
patch_original = self.base_tree.get_file(base_id)
631
base_id != self.base_tree.get_root_id()):
632
old_path = self.old_path(path)
633
patch_original = self.base_tree.get_file(
622
636
patch_original = None
623
file_patch = self.patches.get(self.id2path(file_id))
637
file_patch = self.patches.get(path)
624
638
if file_patch is None:
625
639
if (patch_original is None and
626
self.get_kind(file_id) == 'directory'):
640
self.kind(path, file_id) == 'directory'):
628
642
if patch_original is None:
629
643
raise AssertionError("None: %s" % file_id)
630
644
return patch_original
632
if file_patch.startswith('\\'):
646
if file_patch.startswith(b'\\'):
633
647
raise ValueError(
634
648
'Malformed patch for %s, %r' % (file_id, file_patch))
635
649
return patched_file(file_patch, patch_original)
637
def get_symlink_target(self, file_id):
638
new_path = self.id2path(file_id)
640
return self._targets[new_path]
642
return self.base_tree.get_symlink_target(file_id)
644
def get_kind(self, file_id):
645
if file_id in self._kinds:
646
return self._kinds[file_id]
647
return self.base_tree.inventory[file_id].kind
649
def is_executable(self, file_id):
650
path = self.id2path(file_id)
651
def get_symlink_target(self, path, file_id=None):
653
return self._targets[path]
655
old_path = self.old_path(path)
656
return self.base_tree.get_symlink_target(old_path, file_id)
658
def kind(self, path, file_id=None):
660
return self._kinds[path]
662
old_path = self.old_path(path)
663
return self.base_tree.kind(old_path, file_id)
665
def get_file_revision(self, path, file_id=None):
666
if path in self._last_changed:
667
return self._last_changed[path]
669
old_path = self.old_path(path)
670
return self.base_tree.get_file_revision(old_path, file_id)
672
def is_executable(self, path, file_id=None):
651
673
if path in self._executable:
652
674
return self._executable[path]
654
return self.base_tree.inventory[file_id].executable
676
old_path = self.old_path(path)
677
return self.base_tree.is_executable(old_path, file_id)
656
def get_last_changed(self, file_id):
657
path = self.id2path(file_id)
679
def get_last_changed(self, path, file_id=None):
658
680
if path in self._last_changed:
659
681
return self._last_changed[path]
660
return self.base_tree.inventory[file_id].revision
682
old_path = self.old_path(path)
683
return self.base_tree.get_file_revision(old_path, file_id)
662
def get_size_and_sha1(self, file_id):
685
def get_size_and_sha1(self, new_path, file_id=None):
663
686
"""Return the size and sha1 hash of the given file id.
664
687
If the file was not locally modified, this is extracted
665
688
from the base_tree. Rather than re-reading the file.
667
new_path = self.id2path(file_id)
668
690
if new_path is None:
669
691
return None, None
670
692
if new_path not in self.patches:
671
693
# If the entry does not have a patch, then the
672
694
# contents must be the same as in the base_tree
673
ie = self.base_tree.inventory[file_id]
674
if ie.text_size is None:
675
return ie.text_size, ie.text_sha1
676
return int(ie.text_size), ie.text_sha1
677
fileobj = self.get_file(file_id)
695
base_path = self.old_path(new_path)
696
text_size = self.base_tree.get_file_size(base_path, file_id)
697
text_sha1 = self.base_tree.get_file_sha1(base_path, file_id)
698
return text_size, text_sha1
699
fileobj = self.get_file(new_path, file_id)
678
700
content = fileobj.read()
679
701
return len(content), sha_string(content)
684
706
This need to be called before ever accessing self.inventory
686
708
from os.path import dirname, basename
687
base_inv = self.base_tree.inventory
688
709
inv = Inventory(None, self.revision_id)
690
def add_entry(file_id):
691
path = self.id2path(file_id)
711
def add_entry(path, file_id):
697
715
parent_path = dirname(path)
698
716
parent_id = self.path2id(parent_path)
700
kind = self.get_kind(file_id)
701
revision_id = self.get_last_changed(file_id)
718
kind = self.kind(path, file_id)
719
revision_id = self.get_last_changed(path, file_id)
703
721
name = basename(path)
704
722
if kind == 'directory':
705
723
ie = InventoryDirectory(file_id, name, parent_id)
706
724
elif kind == 'file':
707
725
ie = InventoryFile(file_id, name, parent_id)
708
ie.executable = self.is_executable(file_id)
726
ie.executable = self.is_executable(path, file_id)
709
727
elif kind == 'symlink':
710
728
ie = InventoryLink(file_id, name, parent_id)
711
ie.symlink_target = self.get_symlink_target(file_id)
729
ie.symlink_target = self.get_symlink_target(path, file_id)
712
730
ie.revision = revision_id
714
if kind in ('directory', 'symlink'):
715
ie.text_size, ie.text_sha1 = None, None
717
ie.text_size, ie.text_sha1 = self.get_size_and_sha1(file_id)
718
if (ie.text_size is None) and (kind == 'file'):
719
raise BzrError('Got a text_size of None for file_id %r' % file_id)
733
ie.text_size, ie.text_sha1 = self.get_size_and_sha1(
735
if ie.text_size is None:
737
'Got a text_size of None for file_id %r' % file_id)
722
740
sorted_entries = self.sorted_path_id()
723
741
for path, file_id in sorted_entries:
742
add_entry(path, file_id)
732
750
# at that instant
733
751
inventory = property(_get_inventory)
736
for path, entry in self.inventory.iter_entries():
753
root_inventory = property(_get_inventory)
755
def all_file_ids(self):
756
return {entry.file_id for path, entry in self.inventory.iter_entries()}
758
def all_versioned_paths(self):
759
return {path for path, entry in self.inventory.iter_entries()}
761
def list_files(self, include_root=False, from_dir=None, recursive=True):
762
# The only files returned by this are those from the version
767
from_dir_id = inv.path2id(from_dir)
768
if from_dir_id is None:
769
# Directory not versioned
771
entries = inv.iter_entries(from_dir=from_dir_id, recursive=recursive)
772
if inv.root is not None and not include_root and from_dir is None:
773
# skip the root for compatability with the current apis.
775
for path, entry in entries:
776
yield path, 'V', entry.kind, entry.file_id, entry
739
778
def sorted_path_id(self):
741
for result in self._new_id.iteritems():
780
for result in viewitems(self._new_id):
742
781
paths.append(result)
743
for id in self.base_tree:
782
for id in self.base_tree.all_file_ids():
744
783
path = self.id2path(id)