23
23
from bzrlib.osutils import isdir, quotefn, compact_date, rand_bytes, \
25
25
sha_file, appendpath, file_kind
26
27
from bzrlib.errors import BzrError, InvalidRevisionNumber, InvalidRevisionId
27
28
import bzrlib.errors
28
29
from bzrlib.textui import show_status
120
128
head, tail = os.path.split(f)
122
130
# reached the root, whatever that may be
123
raise BzrError('%r is not in a branch' % orig_f)
131
raise bzrlib.errors.NotBranchError('%s is not in a branch' % orig_f)
136
# XXX: move into bzrlib.errors; subclass BzrError
126
137
class DivergedBranches(Exception):
127
138
def __init__(self, branch1, branch2):
128
139
self.branch1 = branch1
660
671
from bzrlib.inventory import Inventory
661
672
from bzrlib.xml import unpack_xml
663
return unpack_xml(Inventory, self.inventory_store[inventory_id])
674
return unpack_xml(Inventory, self.get_inventory_xml(inventory_id))
677
def get_inventory_xml(self, inventory_id):
678
"""Get inventory XML as a file object."""
679
return self.inventory_store[inventory_id]
666
682
def get_inventory_sha1(self, inventory_id):
667
683
"""Return the sha1 hash of the inventory entry
669
return sha_file(self.inventory_store[inventory_id])
685
return sha_file(self.get_inventory_xml(inventory_id))
672
688
def get_revision_inventory(self, revision_id):
761
def missing_revisions(self, other, stop_revision=None):
777
def missing_revisions(self, other, stop_revision=None, diverged_ok=False):
763
779
If self and other have not diverged, return a list of the revisions
764
780
present in other, but missing from self.
797
813
if stop_revision is None:
798
814
stop_revision = other_len
799
815
elif stop_revision > other_len:
800
raise NoSuchRevision(self, stop_revision)
816
raise bzrlib.errors.NoSuchRevision(self, stop_revision)
802
818
return other_history[self_len:stop_revision]
805
821
def update_revisions(self, other, stop_revision=None):
806
822
"""Pull in all new revisions from other branch.
808
>>> from bzrlib.commit import commit
809
>>> bzrlib.trace.silent = True
810
>>> br1 = ScratchBranch(files=['foo', 'bar'])
813
>>> commit(br1, "lala!", rev_id="REVISION-ID-1", verbose=False)
814
>>> br2 = ScratchBranch()
815
>>> br2.update_revisions(br1)
819
>>> br2.revision_history()
821
>>> br2.update_revisions(br1)
825
>>> br1.text_store.total_size() == br2.text_store.total_size()
828
from bzrlib.progress import ProgressBar
824
from bzrlib.fetch import greedy_fetch
826
pb = bzrlib.ui.ui_factory.progress_bar()
832
827
pb.update('comparing histories')
833
829
revision_ids = self.missing_revisions(other, stop_revision)
831
if len(revision_ids) > 0:
832
count = greedy_fetch(self, other, revision_ids[-1], pb)[0]
835
self.append_revision(*revision_ids)
836
## note("Added %d revisions." % count)
839
def install_revisions(self, other, revision_ids, pb):
835
840
if hasattr(other.revision_store, "prefetch"):
836
841
other.revision_store.prefetch(revision_ids)
837
842
if hasattr(other.inventory_store, "prefetch"):
838
843
inventory_ids = [other.get_revision(r).inventory_id
839
844
for r in revision_ids]
840
845
other.inventory_store.prefetch(inventory_ids)
848
pb = bzrlib.ui.ui_factory.progress_bar()
843
851
needed_texts = set()
845
for rev_id in revision_ids:
847
pb.update('fetching revision', i, len(revision_ids))
848
rev = other.get_revision(rev_id)
855
for i, rev_id in enumerate(revision_ids):
856
pb.update('fetching revision', i+1, len(revision_ids))
858
rev = other.get_revision(rev_id)
859
except bzrlib.errors.NoSuchRevision:
849
863
revisions.append(rev)
850
864
inv = other.get_inventory(str(rev.inventory_id))
851
865
for key, entry in inv.iter_entries():
859
count = self.text_store.copy_multi(other.text_store, needed_texts)
873
count, cp_fail = self.text_store.copy_multi(other.text_store,
860
875
print "Added %d texts." % count
861
876
inventory_ids = [ f.inventory_id for f in revisions ]
862
count = self.inventory_store.copy_multi(other.inventory_store,
877
count, cp_fail = self.inventory_store.copy_multi(other.inventory_store,
864
879
print "Added %d inventories." % count
865
880
revision_ids = [ f.revision_id for f in revisions]
866
count = self.revision_store.copy_multi(other.revision_store,
868
for revision_id in revision_ids:
869
self.append_revision(revision_id)
870
print "Added %d revisions." % count
882
count, cp_fail = self.revision_store.copy_multi(other.revision_store,
885
assert len(cp_fail) == 0
886
return count, failures
873
889
def commit(self, *args, **kw):
874
890
from bzrlib.commit import commit
875
891
commit(self, *args, **kw)
880
896
revno, info = self.get_revision_info(revision)
900
def revision_id_to_revno(self, revision_id):
901
"""Given a revision id, return its revno"""
902
history = self.revision_history()
904
return history.index(revision_id) + 1
906
raise bzrlib.errors.NoSuchRevision(self, revision_id)
883
909
def get_revision_info(self, revision):
884
910
"""Return (revno, revision id) for revision identifier.