21
from warnings import warn
22
25
from bzrlib.trace import mutter, note
23
26
from bzrlib.osutils import isdir, quotefn, compact_date, rand_bytes, \
25
28
sha_file, appendpath, file_kind
26
from bzrlib.errors import BzrError, InvalidRevisionNumber, InvalidRevisionId
30
from bzrlib.errors import (BzrError, InvalidRevisionNumber, InvalidRevisionId,
31
NoSuchRevision, HistoryMissing, NotBranchError,
28
33
from bzrlib.textui import show_status
29
from bzrlib.revision import Revision
30
from bzrlib.xml import unpack_xml
34
from bzrlib.revision import Revision, validate_revision_id
31
35
from bzrlib.delta import compare_trees
32
36
from bzrlib.tree import EmptyTree, RevisionTree
34
BZR_BRANCH_FORMAT = "Bazaar-NG branch, format 0.0.4\n"
37
from bzrlib.inventory import Inventory
38
from bzrlib.weavestore import WeaveStore
39
from bzrlib.store import ImmutableStore
44
BZR_BRANCH_FORMAT_4 = "Bazaar-NG branch, format 0.0.4\n"
45
BZR_BRANCH_FORMAT_5 = "Bazaar-NG branch, format 5\n"
35
46
## TODO: Maybe include checks for common corruption of newlines, etc?
38
49
# TODO: Some operations like log might retrieve the same revisions
39
50
# repeatedly to calculate deltas. We could perhaps have a weakref
40
# cache in memory to make this faster.
51
# cache in memory to make this faster. In general anything can be
52
# cached in memory between lock and unlock operations.
54
# TODO: please move the revision-string syntax stuff out of the branch
55
# object; it's clutter
43
58
def find_branch(f, **args):
184
208
self.base = os.path.realpath(base)
185
209
if not isdir(self.controlfilename('.')):
186
from errors import NotBranchError
187
raise NotBranchError("not a bzr branch: %s" % quotefn(base),
188
['use "bzr init" to initialize a new working tree',
189
'current bzr can only operate from top-of-tree'])
192
self.text_store = ImmutableStore(self.controlfilename('text-store'))
193
self.revision_store = ImmutableStore(self.controlfilename('revision-store'))
194
self.inventory_store = ImmutableStore(self.controlfilename('inventory-store'))
210
raise NotBranchError('not a bzr branch: %s' % quotefn(base),
211
['use "bzr init" to initialize a '
213
self._check_format(relax_version_check)
214
cfn = self.controlfilename
215
if self._branch_format == 4:
216
self.inventory_store = ImmutableStore(cfn('inventory-store'))
217
self.text_store = ImmutableStore(cfn('text-store'))
218
elif self._branch_format == 5:
219
self.control_weaves = WeaveStore(cfn([]))
220
self.weave_store = WeaveStore(cfn('weaves'))
221
self.revision_store = ImmutableStore(cfn('revision-store'))
197
224
def __str__(self):
297
314
raise BzrError("invalid controlfile mode %r" % mode)
301
316
def _make_control(self):
302
from bzrlib.inventory import Inventory
303
from bzrlib.xml import pack_xml
305
317
os.mkdir(self.controlfilename([]))
306
318
self.controlfile('README', 'w').write(
307
319
"This is a Bazaar-NG control directory.\n"
308
320
"Do not change any files in this directory.\n")
309
self.controlfile('branch-format', 'w').write(BZR_BRANCH_FORMAT)
310
for d in ('text-store', 'inventory-store', 'revision-store'):
321
self.controlfile('branch-format', 'w').write(BZR_BRANCH_FORMAT_5)
322
for d in ('text-store', 'revision-store',
311
324
os.mkdir(self.controlfilename(d))
312
for f in ('revision-history', 'merged-patches',
313
'pending-merged-patches', 'branch-name',
325
for f in ('revision-history',
315
328
'pending-merges'):
316
329
self.controlfile(f, 'w').write('')
317
330
mutter('created control directory in ' + self.base)
319
pack_xml(Inventory(gen_root_id()), self.controlfile('inventory','w'))
322
def _check_format(self):
332
# if we want per-tree root ids then this is the place to set
333
# them; they're not needed for now and so ommitted for
335
f = self.controlfile('inventory','w')
336
bzrlib.xml5.serializer_v5.write_inventory(Inventory(), f)
340
def _check_format(self, relax_version_check):
323
341
"""Check this branch format is supported.
325
The current tool only supports the current unstable format.
343
The format level is stored, as an integer, in
344
self._branch_format for code that needs to check it later.
327
346
In the future, we might need different in-memory Branch
328
347
classes to support downlevel branches. But not yet.
330
# This ignores newlines so that we can open branches created
331
# on Windows from Linux and so on. I think it might be better
332
# to always make all internal files in unix format.
333
fmt = self.controlfile('branch-format', 'r').read()
334
fmt.replace('\r\n', '')
335
if fmt != BZR_BRANCH_FORMAT:
336
raise BzrError('sorry, branch format %r not supported' % fmt,
337
['use a different bzr version',
338
'or remove the .bzr directory and "bzr init" again'])
350
fmt = self.controlfile('branch-format', 'r').read()
352
if e.errno == errno.ENOENT:
353
raise NotBranchError(self.base)
357
if fmt == BZR_BRANCH_FORMAT_5:
358
self._branch_format = 5
359
elif fmt == BZR_BRANCH_FORMAT_4:
360
self._branch_format = 4
362
if (not relax_version_check
363
and self._branch_format != 5):
364
raise BzrError('sorry, branch format "%s" not supported; '
365
'use a different bzr version, '
366
'or run "bzr upgrade"'
367
% fmt.rstrip('\n\r'))
340
370
def get_root_id(self):
341
371
"""Return the id of this branches root"""
595
618
return self.revision_store[revision_id]
596
619
except IndexError:
597
raise bzrlib.errors.NoSuchRevision(revision_id)
620
raise bzrlib.errors.NoSuchRevision(self, revision_id)
625
def get_revision_xml(self, revision_id):
626
return self.get_revision_xml_file(revision_id).read()
602
629
def get_revision(self, revision_id):
603
630
"""Return the Revision object for a named revision"""
604
xml_file = self.get_revision_xml(revision_id)
631
xml_file = self.get_revision_xml_file(revision_id)
607
r = unpack_xml(Revision, xml_file)
634
r = bzrlib.xml5.serializer_v5.read_revision(xml_file)
608
635
except SyntaxError, e:
609
636
raise bzrlib.errors.BzrError('failed to unpack revision_xml',
640
667
def get_revision_sha1(self, revision_id):
641
668
"""Hash the stored value of a revision, and return it."""
642
# In the future, revision entries will be signed. At that
643
# point, it is probably best *not* to include the signature
644
# in the revision hash. Because that lets you re-sign
645
# the revision, (add signatures/remove signatures) and still
646
# have all hash pointers stay consistent.
647
# But for now, just hash the contents.
648
return bzrlib.osutils.sha_file(self.get_revision_xml(revision_id))
651
def get_inventory(self, inventory_id):
652
"""Get Inventory object by hash.
654
TODO: Perhaps for this and similar methods, take a revision
655
parameter which can be either an integer revno or a
657
from bzrlib.inventory import Inventory
658
from bzrlib.xml import unpack_xml
660
return unpack_xml(Inventory, self.inventory_store[inventory_id])
663
def get_inventory_sha1(self, inventory_id):
669
return bzrlib.osutils.sha_file(self.get_revision_xml_file(revision_id))
672
def _get_ancestry_weave(self):
673
return self.control_weaves.get_weave('ancestry')
676
def get_ancestry(self, revision_id):
677
"""Return a list of revision-ids integrated by a revision.
680
w = self._get_ancestry_weave()
681
return [l[:-1] for l in w.get_iter(w.lookup(revision_id))]
684
def get_inventory_weave(self):
685
return self.control_weaves.get_weave('inventory')
688
def get_inventory(self, revision_id):
689
"""Get Inventory object by hash."""
690
xml = self.get_inventory_xml(revision_id)
691
return bzrlib.xml5.serializer_v5.read_inventory_from_string(xml)
694
def get_inventory_xml(self, revision_id):
695
"""Get inventory XML as a file object."""
697
assert isinstance(revision_id, basestring), type(revision_id)
698
iw = self.get_inventory_weave()
699
return iw.get_text(iw.lookup(revision_id))
701
raise bzrlib.errors.HistoryMissing(self, 'inventory', revision_id)
704
def get_inventory_sha1(self, revision_id):
664
705
"""Return the sha1 hash of the inventory entry
666
return sha_file(self.inventory_store[inventory_id])
707
return self.get_revision(revision_id).inventory_sha1
669
710
def get_revision_inventory(self, revision_id):
670
711
"""Return inventory of a past revision."""
671
# bzr 0.0.6 imposes the constraint that the inventory_id
712
# TODO: Unify this with get_inventory()
713
# bzr 0.0.6 and later imposes the constraint that the inventory_id
672
714
# must be the same as its revision, so this is trivial.
673
715
if revision_id == None:
674
from bzrlib.inventory import Inventory
675
716
return Inventory(self.get_root_id())
677
718
return self.get_inventory(revision_id)
680
721
def revision_history(self):
681
"""Return sequence of revision hashes on to this branch.
683
>>> ScratchBranch().revision_history()
722
"""Return sequence of revision hashes on to this branch."""
688
725
return [l.rstrip('\r\n') for l in
697
734
>>> sb = ScratchBranch(files=['foo', 'foo~'])
698
735
>>> sb.common_ancestor(sb) == (None, None)
700
>>> commit.commit(sb, "Committing first revision", verbose=False)
737
>>> commit.commit(sb, "Committing first revision")
701
738
>>> sb.common_ancestor(sb)[0]
703
740
>>> clone = sb.clone()
704
>>> commit.commit(sb, "Committing second revision", verbose=False)
741
>>> commit.commit(sb, "Committing second revision")
705
742
>>> sb.common_ancestor(sb)[0]
707
744
>>> sb.common_ancestor(clone)[0]
709
>>> commit.commit(clone, "Committing divergent second revision",
746
>>> commit.commit(clone, "Committing divergent second revision")
711
747
>>> sb.common_ancestor(clone)[0]
713
749
>>> sb.common_ancestor(clone) == clone.common_ancestor(sb)
794
834
if stop_revision is None:
795
835
stop_revision = other_len
796
elif stop_revision > other_len:
797
raise NoSuchRevision(self, stop_revision)
837
assert isinstance(stop_revision, int)
838
if stop_revision > other_len:
839
raise bzrlib.errors.NoSuchRevision(self, stop_revision)
799
841
return other_history[self_len:stop_revision]
802
def update_revisions(self, other, stop_revision=None):
803
"""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()
844
def update_revisions(self, other, stop_revno=None):
845
"""Pull in new perfect-fit revisions.
825
from bzrlib.progress import ProgressBar
829
pb.update('comparing histories')
830
revision_ids = self.missing_revisions(other, stop_revision)
832
if hasattr(other.revision_store, "prefetch"):
833
other.revision_store.prefetch(revision_ids)
834
if hasattr(other.inventory_store, "prefetch"):
835
inventory_ids = [other.get_revision(r).inventory_id
836
for r in revision_ids]
837
other.inventory_store.prefetch(inventory_ids)
842
for rev_id in revision_ids:
844
pb.update('fetching revision', i, len(revision_ids))
845
rev = other.get_revision(rev_id)
846
revisions.append(rev)
847
inv = other.get_inventory(str(rev.inventory_id))
848
for key, entry in inv.iter_entries():
849
if entry.text_id is None:
851
if entry.text_id not in self.text_store:
852
needed_texts.add(entry.text_id)
856
count = self.text_store.copy_multi(other.text_store, needed_texts)
857
print "Added %d texts." % count
858
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
862
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
847
from bzrlib.fetch import greedy_fetch
850
stop_revision = other.lookup_revision(stop_revno)
853
greedy_fetch(to_branch=self, from_branch=other,
854
revision=stop_revision)
856
pullable_revs = self.missing_revisions(other, stop_revision)
859
greedy_fetch(to_branch=self,
861
revision=pullable_revs[-1])
862
self.append_revision(*pullable_revs)
870
865
def commit(self, *args, **kw):
871
from bzrlib.commit import commit
872
commit(self, *args, **kw)
866
from bzrlib.commit import Commit
867
Commit().commit(self, *args, **kw)
875
870
def lookup_revision(self, revision):
876
871
"""Return the revision identifier for a given revision information."""
877
revno, info = self.get_revision_info(revision)
872
revno, info = self._get_revision_info(revision)
876
def revision_id_to_revno(self, revision_id):
877
"""Given a revision id, return its revno"""
878
history = self.revision_history()
880
return history.index(revision_id) + 1
882
raise bzrlib.errors.NoSuchRevision(self, revision_id)
880
885
def get_revision_info(self, revision):
881
886
"""Return (revno, revision id) for revision identifier.
885
890
revision can also be a string, in which case it is parsed for something like
886
891
'date:' or 'revid:' etc.
893
revno, rev_id = self._get_revision_info(revision)
895
raise bzrlib.errors.NoSuchRevision(self, revision)
898
def get_rev_id(self, revno, history=None):
899
"""Find the revision id of the specified revno."""
903
history = self.revision_history()
904
elif revno <= 0 or revno > len(history):
905
raise bzrlib.errors.NoSuchRevision(self, revno)
906
return history[revno - 1]
908
def _get_revision_info(self, revision):
909
"""Return (revno, revision id) for revision specifier.
911
revision can be an integer, in which case it is assumed to be revno
912
(though this will translate negative values into positive ones)
913
revision can also be a string, in which case it is parsed for something
914
like 'date:' or 'revid:' etc.
916
A revid is always returned. If it is None, the specifier referred to
917
the null revision. If the revid does not occur in the revision
918
history, revno will be None.
888
921
if revision is None:
895
928
revs = self.revision_history()
896
929
if isinstance(revision, int):
899
# Mabye we should do this first, but we don't need it if revision == 0
901
931
revno = len(revs) + revision + 1
934
rev_id = self.get_rev_id(revno, revs)
904
935
elif isinstance(revision, basestring):
905
936
for prefix, func in Branch.REVISION_NAMESPACES.iteritems():
906
937
if revision.startswith(prefix):
907
revno = func(self, revs, revision)
938
result = func(self, revs, revision)
940
revno, rev_id = result
943
rev_id = self.get_rev_id(revno, revs)
910
raise BzrError('No namespace registered for string: %r' % revision)
946
raise BzrError('No namespace registered for string: %r' %
949
raise TypeError('Unhandled revision type %s' % revision)
912
if revno is None or revno <= 0 or revno > len(revs):
913
raise BzrError("no such revision %s" % revision)
914
return revno, revs[revno-1]
953
raise bzrlib.errors.NoSuchRevision(self, revision)
916
956
def _namespace_revno(self, revs, revision):
917
957
"""Lookup a revision by revision number"""
918
958
assert revision.startswith('revno:')
920
return int(revision[6:])
960
return (int(revision[6:]),)
921
961
except ValueError:
923
963
REVISION_NAMESPACES['revno:'] = _namespace_revno
925
965
def _namespace_revid(self, revs, revision):
926
966
assert revision.startswith('revid:')
967
rev_id = revision[len('revid:'):]
928
return revs.index(revision[6:]) + 1
969
return revs.index(rev_id) + 1, rev_id
929
970
except ValueError:
931
972
REVISION_NAMESPACES['revid:'] = _namespace_revid
933
974
def _namespace_last(self, revs, revision):
1306
def get_parent(self):
1307
"""Return the parent location of the branch.
1309
This is the default location for push/pull/missing. The usual
1310
pattern is that the user can override it by specifying a
1314
_locs = ['parent', 'pull', 'x-pull']
1317
return self.controlfile(l, 'r').read().strip('\n')
1319
if e.errno != errno.ENOENT:
1324
def set_parent(self, url):
1325
# TODO: Maybe delete old location files?
1326
from bzrlib.atomicfile import AtomicFile
1329
f = AtomicFile(self.controlfilename('parent'))
1338
def check_revno(self, revno):
1340
Check whether a revno corresponds to any revision.
1341
Zero (the NULL revision) is considered valid.
1344
self.check_real_revno(revno)
1346
def check_real_revno(self, revno):
1348
Check whether a revno corresponds to a real revision.
1349
Zero (the NULL revision) is considered invalid
1351
if revno < 1 or revno > self.revno():
1352
raise InvalidRevisionNumber(revno)
1268
1357
class ScratchBranch(Branch):
1269
1358
"""Special test class: a branch that cleans up after itself.
1386
1477
"""Return a new tree-root file id."""
1387
1478
return gen_file_id('TREE_ROOT')
1481
def pull_loc(branch):
1482
# TODO: Should perhaps just make attribute be 'base' in
1483
# RemoteBranch and Branch?
1484
if hasattr(branch, "baseurl"):
1485
return branch.baseurl
1490
def copy_branch(branch_from, to_location, revision=None):
1491
"""Copy branch_from into the existing directory to_location.
1494
If not None, only revisions up to this point will be copied.
1495
The head of the new branch will be that revision. Can be a
1499
The name of a local directory that exists but is empty.
1501
# TODO: This could be done *much* more efficiently by just copying
1502
# all the whole weaves and revisions, rather than getting one
1503
# revision at a time.
1504
from bzrlib.merge import merge
1505
from bzrlib.branch import Branch
1507
assert isinstance(branch_from, Branch)
1508
assert isinstance(to_location, basestring)
1510
br_to = Branch(to_location, init=True)
1511
br_to.set_root_id(branch_from.get_root_id())
1512
if revision is None:
1515
revno, rev_id = branch_from.get_revision_info(revision)
1516
br_to.update_revisions(branch_from, stop_revno=revno)
1517
merge((to_location, -1), (to_location, 0), this_dir=to_location,
1518
check_clean=False, ignore_zero=True)
1520
from_location = pull_loc(branch_from)
1521
br_to.set_parent(pull_loc(branch_from))