26
26
from inventory import InventoryEntry, Inventory
27
27
from osutils import isdir, quotefn, isfile, uuid, sha_file, username, \
28
28
format_date, compact_date, pumpfile, user_email, rand_bytes, splitpath, \
29
joinpath, sha_file, sha_string, file_kind, local_time_offset, appendpath
29
joinpath, sha_string, file_kind, local_time_offset, appendpath
30
30
from store import ImmutableStore
31
31
from revision import Revision
32
32
from errors import BzrError
542
529
def get_revision(self, revision_id):
543
530
"""Return the Revision object for a named revision"""
544
if not revision_id or not isinstance(revision_id, basestring):
545
raise ValueError('invalid revision-id: %r' % revision_id)
546
531
r = Revision.read_xml(self.revision_store[revision_id])
547
532
assert r.revision_id == revision_id
550
def get_revision_sha1(self, revision_id):
551
"""Hash the stored value of a revision, and return it."""
552
# In the future, revision entries will be signed. At that
553
# point, it is probably best *not* to include the signature
554
# in the revision hash. Because that lets you re-sign
555
# the revision, (add signatures/remove signatures) and still
556
# have all hash pointers stay consistent.
557
# But for now, just hash the contents.
558
return sha_file(self.revision_store[revision_id])
561
536
def get_inventory(self, inventory_id):
562
537
"""Get Inventory object by hash.
683
def missing_revisions(self, other, stop_revision=None):
685
If self and other have not diverged, return a list of the revisions
686
present in other, but missing from self.
688
>>> from bzrlib.commit import commit
689
>>> bzrlib.trace.silent = True
690
>>> br1 = ScratchBranch()
691
>>> br2 = ScratchBranch()
692
>>> br1.missing_revisions(br2)
694
>>> commit(br2, "lala!", rev_id="REVISION-ID-1")
695
>>> br1.missing_revisions(br2)
697
>>> br2.missing_revisions(br1)
699
>>> commit(br1, "lala!", rev_id="REVISION-ID-1")
700
>>> br1.missing_revisions(br2)
702
>>> commit(br2, "lala!", rev_id="REVISION-ID-2A")
703
>>> br1.missing_revisions(br2)
705
>>> commit(br1, "lala!", rev_id="REVISION-ID-2B")
706
>>> br1.missing_revisions(br2)
707
Traceback (most recent call last):
708
DivergedBranches: These branches have diverged.
710
self_history = self.revision_history()
711
self_len = len(self_history)
712
other_history = other.revision_history()
713
other_len = len(other_history)
714
common_index = min(self_len, other_len) -1
715
if common_index >= 0 and \
716
self_history[common_index] != other_history[common_index]:
717
raise DivergedBranches(self, other)
719
if stop_revision is None:
720
stop_revision = other_len
721
elif stop_revision > other_len:
722
raise NoSuchRevision(self, stop_revision)
724
return other_history[self_len:stop_revision]
727
def update_revisions(self, other, stop_revision=None):
728
"""Pull in all new revisions from other branch.
730
>>> from bzrlib.commit import commit
731
>>> bzrlib.trace.silent = True
732
>>> br1 = ScratchBranch(files=['foo', 'bar'])
735
>>> commit(br1, "lala!", rev_id="REVISION-ID-1", verbose=False)
736
>>> br2 = ScratchBranch()
737
>>> br2.update_revisions(br1)
741
>>> br2.revision_history()
743
>>> br2.update_revisions(br1)
747
>>> br1.text_store.total_size() == br2.text_store.total_size()
750
from bzrlib.progress import ProgressBar
754
pb.update('comparing histories')
755
revision_ids = self.missing_revisions(other, stop_revision)
757
needed_texts = sets.Set()
759
for rev_id in revision_ids:
761
pb.update('fetching revision', i, len(revision_ids))
762
rev = other.get_revision(rev_id)
763
revisions.append(rev)
764
inv = other.get_inventory(str(rev.inventory_id))
765
for key, entry in inv.iter_entries():
766
if entry.text_id is None:
768
if entry.text_id not in self.text_store:
769
needed_texts.add(entry.text_id)
773
count = self.text_store.copy_multi(other.text_store, needed_texts)
774
print "Added %d texts." % count
775
inventory_ids = [ f.inventory_id for f in revisions ]
776
count = self.inventory_store.copy_multi(other.inventory_store,
778
print "Added %d inventories." % count
779
revision_ids = [ f.revision_id for f in revisions]
780
count = self.revision_store.copy_multi(other.revision_store,
782
for revision_id in revision_ids:
783
self.append_revision(revision_id)
784
print "Added %d revisions." % count
787
653
def commit(self, *args, **kw):
789
655
from bzrlib.commit import commit