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):
278
286
if rev.revision_id != revision_id:
279
287
raise AssertionError()
280
288
if sha1 != rev.inventory_sha1:
281
open(',,bogus-inv', 'wb').write(s)
289
with open(',,bogus-inv', 'wb') as f:
282
291
warning('Inventory sha hash mismatch for revision %s. %s'
283
292
' != %s' % (revision_id, sha1, rev.inventory_sha1))
285
def _validate_revision(self, inventory, revision_id):
294
def _validate_revision(self, tree, revision_id):
286
295
"""Make sure all revision entries match their checksum."""
288
# This is a mapping from each revision id to it's sha hash
297
# This is a mapping from each revision id to its sha hash
291
300
rev = self.get_revision(revision_id)
614
623
in the text-store, so that the file contents would
627
file_id = self.path2id(path)
617
628
base_id = self.old_contents_id(file_id)
618
629
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)
630
base_id != self.base_tree.get_root_id()):
631
old_path = self.old_path(path)
632
patch_original = self.base_tree.get_file(
622
635
patch_original = None
623
file_patch = self.patches.get(self.id2path(file_id))
636
file_patch = self.patches.get(path)
624
637
if file_patch is None:
625
638
if (patch_original is None and
626
self.get_kind(file_id) == 'directory'):
639
self.kind(path, file_id) == 'directory'):
628
641
if patch_original is None:
629
642
raise AssertionError("None: %s" % file_id)
630
643
return patch_original
634
647
'Malformed patch for %s, %r' % (file_id, file_patch))
635
648
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)
650
def get_symlink_target(self, path, file_id=None):
652
return self._targets[path]
654
old_path = self.old_path(path)
655
return self.base_tree.get_symlink_target(old_path, file_id)
657
def kind(self, path, file_id=None):
659
return self._kinds[path]
661
old_path = self.old_path(path)
662
return self.base_tree.kind(old_path, file_id)
664
def get_file_revision(self, path, file_id=None):
665
if path in self._last_changed:
666
return self._last_changed[path]
668
old_path = self.old_path(path)
669
return self.base_tree.get_file_revision(old_path, file_id)
671
def is_executable(self, path, file_id=None):
651
672
if path in self._executable:
652
673
return self._executable[path]
654
return self.base_tree.inventory[file_id].executable
675
old_path = self.old_path(path)
676
return self.base_tree.is_executable(old_path, file_id)
656
def get_last_changed(self, file_id):
657
path = self.id2path(file_id)
678
def get_last_changed(self, path, file_id=None):
658
679
if path in self._last_changed:
659
680
return self._last_changed[path]
660
return self.base_tree.inventory[file_id].revision
681
old_path = self.old_path(path)
682
return self.base_tree.get_file_revision(old_path, file_id)
662
def get_size_and_sha1(self, file_id):
684
def get_size_and_sha1(self, new_path, file_id=None):
663
685
"""Return the size and sha1 hash of the given file id.
664
686
If the file was not locally modified, this is extracted
665
687
from the base_tree. Rather than re-reading the file.
667
new_path = self.id2path(file_id)
668
689
if new_path is None:
669
690
return None, None
670
691
if new_path not in self.patches:
671
692
# If the entry does not have a patch, then the
672
693
# 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)
694
base_path = self.old_path(new_path)
695
text_size = self.base_tree.get_file_size(base_path, file_id)
696
text_sha1 = self.base_tree.get_file_sha1(base_path, file_id)
697
return text_size, text_sha1
698
fileobj = self.get_file(new_path, file_id)
678
699
content = fileobj.read()
679
700
return len(content), sha_string(content)
684
705
This need to be called before ever accessing self.inventory
686
707
from os.path import dirname, basename
687
base_inv = self.base_tree.inventory
688
708
inv = Inventory(None, self.revision_id)
690
def add_entry(file_id):
691
path = self.id2path(file_id)
710
def add_entry(path, file_id):
697
714
parent_path = dirname(path)
698
715
parent_id = self.path2id(parent_path)
700
kind = self.get_kind(file_id)
701
revision_id = self.get_last_changed(file_id)
717
kind = self.kind(path, file_id)
718
revision_id = self.get_last_changed(path, file_id)
703
720
name = basename(path)
704
721
if kind == 'directory':
705
722
ie = InventoryDirectory(file_id, name, parent_id)
706
723
elif kind == 'file':
707
724
ie = InventoryFile(file_id, name, parent_id)
708
ie.executable = self.is_executable(file_id)
725
ie.executable = self.is_executable(path, file_id)
709
726
elif kind == 'symlink':
710
727
ie = InventoryLink(file_id, name, parent_id)
711
ie.symlink_target = self.get_symlink_target(file_id)
728
ie.symlink_target = self.get_symlink_target(path, file_id)
712
729
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)
732
ie.text_size, ie.text_sha1 = self.get_size_and_sha1(
734
if ie.text_size is None:
736
'Got a text_size of None for file_id %r' % file_id)
722
739
sorted_entries = self.sorted_path_id()
723
740
for path, file_id in sorted_entries:
741
add_entry(path, file_id)
732
749
# at that instant
733
750
inventory = property(_get_inventory)
736
for path, entry in self.inventory.iter_entries():
752
root_inventory = property(_get_inventory)
754
def all_file_ids(self):
755
return {entry.file_id for path, entry in self.inventory.iter_entries()}
757
def all_versioned_paths(self):
758
return {path for path, entry in self.inventory.iter_entries()}
760
def list_files(self, include_root=False, from_dir=None, recursive=True):
761
# The only files returned by this are those from the version
766
from_dir_id = inv.path2id(from_dir)
767
if from_dir_id is None:
768
# Directory not versioned
770
entries = inv.iter_entries(from_dir=from_dir_id, recursive=recursive)
771
if inv.root is not None and not include_root and from_dir is None:
772
# skip the root for compatability with the current apis.
774
for path, entry in entries:
775
yield path, 'V', entry.kind, entry.file_id, entry
739
777
def sorted_path_id(self):
741
for result in self._new_id.iteritems():
779
for result in viewitems(self._new_id):
742
780
paths.append(result)
743
for id in self.base_tree:
781
for id in self.base_tree.all_file_ids():
744
782
path = self.id2path(id)
752
790
def patched_file(file_patch, original):
753
791
"""Produce a file-like object with the patched version of a text"""
754
from bzrlib.patches import iter_patched
755
from bzrlib.iterablefile import IterableFile
792
from breezy.patches import iter_patched
793
from breezy.iterablefile import IterableFile
794
if file_patch == b"":
757
795
return IterableFile(())
758
796
# string.splitlines(True) also splits on '\r', but the iter_patched code
759
797
# only expects to iterate over '\n' style lines
760
798
return IterableFile(iter_patched(original,
761
StringIO(file_patch).readlines()))
799
BytesIO(file_patch).readlines()))