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
316
327
self.controlfile(f, 'w').write('')
317
328
mutter('created control directory in ' + self.base)
319
pack_xml(Inventory(gen_root_id()), self.controlfile('inventory','w'))
330
# if we want per-tree root ids then this is the place to set
331
# them; they're not needed for now and so ommitted for
333
pack_xml(Inventory(), self.controlfile('inventory','w'))
322
336
def _check_format(self):
657
671
from bzrlib.inventory import Inventory
658
672
from bzrlib.xml import unpack_xml
660
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]
663
682
def get_inventory_sha1(self, inventory_id):
664
683
"""Return the sha1 hash of the inventory entry
666
return sha_file(self.inventory_store[inventory_id])
685
return sha_file(self.get_inventory_xml(inventory_id))
669
688
def get_revision_inventory(self, revision_id):
758
def missing_revisions(self, other, stop_revision=None):
777
def missing_revisions(self, other, stop_revision=None, diverged_ok=False):
760
779
If self and other have not diverged, return a list of the revisions
761
780
present in other, but missing from self.
794
813
if stop_revision is None:
795
814
stop_revision = other_len
796
815
elif stop_revision > other_len:
797
raise NoSuchRevision(self, stop_revision)
816
raise bzrlib.errors.NoSuchRevision(self, stop_revision)
799
818
return other_history[self_len:stop_revision]
802
821
def update_revisions(self, other, stop_revision=None):
803
822
"""Pull in all new revisions from other branch.
805
>>> from bzrlib.commit import commit
806
>>> bzrlib.trace.silent = True
807
>>> br1 = ScratchBranch(files=['foo', 'bar'])
810
>>> commit(br1, "lala!", rev_id="REVISION-ID-1", verbose=False)
811
>>> br2 = ScratchBranch()
812
>>> br2.update_revisions(br1)
816
>>> br2.revision_history()
818
>>> br2.update_revisions(br1)
822
>>> br1.text_store.total_size() == br2.text_store.total_size()
825
from bzrlib.progress import ProgressBar
824
from bzrlib.fetch import greedy_fetch
826
pb = bzrlib.ui.ui_factory.progress_bar()
829
827
pb.update('comparing histories')
830
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)
841
def install_revisions(self, other, revision_ids, pb):
832
842
if hasattr(other.revision_store, "prefetch"):
833
843
other.revision_store.prefetch(revision_ids)
834
844
if hasattr(other.inventory_store, "prefetch"):
835
845
inventory_ids = [other.get_revision(r).inventory_id
836
846
for r in revision_ids]
837
847
other.inventory_store.prefetch(inventory_ids)
850
pb = bzrlib.ui.ui_factory.progress_bar()
840
853
needed_texts = set()
842
for rev_id in revision_ids:
844
pb.update('fetching revision', i, len(revision_ids))
845
rev = other.get_revision(rev_id)
857
for i, rev_id in enumerate(revision_ids):
858
pb.update('fetching revision', i+1, len(revision_ids))
860
rev = other.get_revision(rev_id)
861
except bzrlib.errors.NoSuchRevision:
846
865
revisions.append(rev)
847
866
inv = other.get_inventory(str(rev.inventory_id))
848
867
for key, entry in inv.iter_entries():
856
count = self.text_store.copy_multi(other.text_store, needed_texts)
857
print "Added %d texts." % count
875
count, cp_fail = self.text_store.copy_multi(other.text_store,
877
#print "Added %d texts." % count
858
878
inventory_ids = [ f.inventory_id for f in revisions ]
859
count = self.inventory_store.copy_multi(other.inventory_store,
861
print "Added %d inventories." % count
879
count, cp_fail = self.inventory_store.copy_multi(other.inventory_store,
881
#print "Added %d inventories." % count
862
882
revision_ids = [ f.revision_id for f in revisions]
863
count = self.revision_store.copy_multi(other.revision_store,
865
for revision_id in revision_ids:
866
self.append_revision(revision_id)
867
print "Added %d revisions." % count
884
count, cp_fail = self.revision_store.copy_multi(other.revision_store,
887
assert len(cp_fail) == 0
888
return count, failures
870
891
def commit(self, *args, **kw):
871
892
from bzrlib.commit import commit
872
893
commit(self, *args, **kw)
877
898
revno, info = self.get_revision_info(revision)
902
def revision_id_to_revno(self, revision_id):
903
"""Given a revision id, return its revno"""
904
history = self.revision_history()
906
return history.index(revision_id) + 1
908
raise bzrlib.errors.NoSuchRevision(self, revision_id)
880
911
def get_revision_info(self, revision):
881
912
"""Return (revno, revision id) for revision identifier.