15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
from cStringIO import StringIO
21
23
from bzrlib.trace import mutter, note
22
from bzrlib.osutils import isdir, quotefn, compact_date, rand_bytes, splitpath, \
24
from bzrlib.osutils import isdir, quotefn, compact_date, rand_bytes, \
23
26
sha_file, appendpath, file_kind
24
from bzrlib.errors import BzrError
26
BZR_BRANCH_FORMAT = "Bazaar-NG branch, format 0.0.4\n"
28
from bzrlib.errors import (BzrError, InvalidRevisionNumber, InvalidRevisionId,
29
NoSuchRevision, HistoryMissing, NotBranchError,
31
from bzrlib.textui import show_status
32
from bzrlib.revision import Revision, validate_revision_id
33
from bzrlib.delta import compare_trees
34
from bzrlib.tree import EmptyTree, RevisionTree
35
from bzrlib.inventory import Inventory
36
from bzrlib.weavestore import WeaveStore
37
from bzrlib.store import ImmutableStore
42
BZR_BRANCH_FORMAT_4 = "Bazaar-NG branch, format 0.0.4\n"
43
BZR_BRANCH_FORMAT_5 = "Bazaar-NG branch, format 5\n"
27
44
## TODO: Maybe include checks for common corruption of newlines, etc?
47
# TODO: Some operations like log might retrieve the same revisions
48
# repeatedly to calculate deltas. We could perhaps have a weakref
49
# cache in memory to make this faster. In general anything can be
50
# cached in memory between lock and unlock operations.
52
# TODO: please move the revision-string syntax stuff out of the branch
53
# object; it's clutter
31
56
def find_branch(f, **args):
32
57
if f and (f.startswith('http://') or f.startswith('https://')):
180
206
self.base = os.path.realpath(base)
181
207
if not isdir(self.controlfilename('.')):
182
from errors import NotBranchError
183
raise NotBranchError("not a bzr branch: %s" % quotefn(base),
184
['use "bzr init" to initialize a new working tree',
185
'current bzr can only operate from top-of-tree'])
188
self.text_store = ImmutableStore(self.controlfilename('text-store'))
189
self.revision_store = ImmutableStore(self.controlfilename('revision-store'))
190
self.inventory_store = ImmutableStore(self.controlfilename('inventory-store'))
208
raise NotBranchError('not a bzr branch: %s' % quotefn(base),
209
['use "bzr init" to initialize a '
211
self._check_format(relax_version_check)
212
cfn = self.controlfilename
213
if self._branch_format == 4:
214
self.inventory_store = ImmutableStore(cfn('inventory-store'))
215
self.text_store = ImmutableStore(cfn('text-store'))
216
elif self._branch_format == 5:
217
self.control_weaves = WeaveStore(cfn([]))
218
self.weave_store = WeaveStore(cfn('weaves'))
219
self.revision_store = ImmutableStore(cfn('revision-store'))
193
222
def __str__(self):
293
313
raise BzrError("invalid controlfile mode %r" % mode)
297
315
def _make_control(self):
298
from bzrlib.inventory import Inventory
299
from bzrlib.xml import pack_xml
301
316
os.mkdir(self.controlfilename([]))
302
317
self.controlfile('README', 'w').write(
303
318
"This is a Bazaar-NG control directory.\n"
304
319
"Do not change any files in this directory.\n")
305
self.controlfile('branch-format', 'w').write(BZR_BRANCH_FORMAT)
306
for d in ('text-store', 'inventory-store', 'revision-store'):
320
self.controlfile('branch-format', 'w').write(BZR_BRANCH_FORMAT_5)
321
for d in ('text-store', 'revision-store',
307
323
os.mkdir(self.controlfilename(d))
308
for f in ('revision-history', 'merged-patches',
309
'pending-merged-patches', 'branch-name',
324
for f in ('revision-history',
311
327
'pending-merges'):
312
328
self.controlfile(f, 'w').write('')
313
329
mutter('created control directory in ' + self.base)
315
pack_xml(Inventory(gen_root_id()), self.controlfile('inventory','w'))
318
def _check_format(self):
331
# if we want per-tree root ids then this is the place to set
332
# them; they're not needed for now and so ommitted for
334
f = self.controlfile('inventory','w')
335
bzrlib.xml5.serializer_v5.write_inventory(Inventory(), f)
339
def _check_format(self, relax_version_check):
319
340
"""Check this branch format is supported.
321
The current tool only supports the current unstable format.
342
The format level is stored, as an integer, in
343
self._branch_format for code that needs to check it later.
323
345
In the future, we might need different in-memory Branch
324
346
classes to support downlevel branches. But not yet.
326
# This ignores newlines so that we can open branches created
327
# on Windows from Linux and so on. I think it might be better
328
# to always make all internal files in unix format.
329
348
fmt = self.controlfile('branch-format', 'r').read()
330
fmt.replace('\r\n', '')
331
if fmt != BZR_BRANCH_FORMAT:
332
raise BzrError('sorry, branch format %r not supported' % fmt,
333
['use a different bzr version',
334
'or remove the .bzr directory and "bzr init" again'])
349
if fmt == BZR_BRANCH_FORMAT_5:
350
self._branch_format = 5
351
elif fmt == BZR_BRANCH_FORMAT_4:
352
self._branch_format = 4
354
if (not relax_version_check
355
and self._branch_format != 5):
356
raise BzrError('sorry, branch format "%s" not supported; '
357
'use a different bzr version, '
358
'or run "bzr upgrade"'
359
% fmt.rstrip('\n\r'))
336
362
def get_root_id(self):
337
363
"""Return the id of this branches root"""
594
def has_revision(self, revision_id):
595
"""True if this branch has a copy of the revision.
597
This does not necessarily imply the revision is merge
598
or on the mainline."""
599
return revision_id in self.revision_store
602
def get_revision_xml_file(self, revision_id):
603
"""Return XML file object for revision object."""
604
if not revision_id or not isinstance(revision_id, basestring):
605
raise InvalidRevisionId(revision_id)
610
return self.revision_store[revision_id]
612
raise bzrlib.errors.NoSuchRevision(self, revision_id)
617
def get_revision_xml(self, revision_id):
618
return self.get_revision_xml_file(revision_id).read()
585
621
def get_revision(self, revision_id):
586
622
"""Return the Revision object for a named revision"""
587
from bzrlib.revision import Revision
588
from bzrlib.xml import unpack_xml
623
xml_file = self.get_revision_xml_file(revision_id)
592
if not revision_id or not isinstance(revision_id, basestring):
593
raise ValueError('invalid revision-id: %r' % revision_id)
594
r = unpack_xml(Revision, self.revision_store[revision_id])
626
r = bzrlib.xml5.serializer_v5.read_revision(xml_file)
627
except SyntaxError, e:
628
raise bzrlib.errors.BzrError('failed to unpack revision_xml',
598
632
assert r.revision_id == revision_id
636
def get_revision_delta(self, revno):
637
"""Return the delta for one revision.
639
The delta is relative to its mainline predecessor, or the
640
empty tree for revision 1.
642
assert isinstance(revno, int)
643
rh = self.revision_history()
644
if not (1 <= revno <= len(rh)):
645
raise InvalidRevisionNumber(revno)
647
# revno is 1-based; list is 0-based
649
new_tree = self.revision_tree(rh[revno-1])
651
old_tree = EmptyTree()
653
old_tree = self.revision_tree(rh[revno-2])
655
return compare_trees(old_tree, new_tree)
602
659
def get_revision_sha1(self, revision_id):
603
660
"""Hash the stored value of a revision, and return it."""
604
# In the future, revision entries will be signed. At that
605
# point, it is probably best *not* to include the signature
606
# in the revision hash. Because that lets you re-sign
607
# the revision, (add signatures/remove signatures) and still
608
# have all hash pointers stay consistent.
609
# But for now, just hash the contents.
610
return sha_file(self.revision_store[revision_id])
613
def get_inventory(self, inventory_id):
614
"""Get Inventory object by hash.
616
TODO: Perhaps for this and similar methods, take a revision
617
parameter which can be either an integer revno or a
619
from bzrlib.inventory import Inventory
620
from bzrlib.xml import unpack_xml
622
return unpack_xml(Inventory, self.inventory_store[inventory_id])
625
def get_inventory_sha1(self, inventory_id):
661
return bzrlib.osutils.sha_file(self.get_revision_xml_file(revision_id))
664
def _get_ancestry_weave(self):
665
return self.control_weaves.get_weave('ancestry')
668
def get_ancestry(self, revision_id):
669
"""Return a list of revision-ids integrated by a revision.
672
w = self._get_ancestry_weave()
673
return [l[:-1] for l in w.get_iter(w.lookup(revision_id))]
676
def get_inventory_weave(self):
677
return self.control_weaves.get_weave('inventory')
680
def get_inventory(self, revision_id):
681
"""Get Inventory object by hash."""
682
# FIXME: The text gets passed around a lot coming from the weave.
683
f = StringIO(self.get_inventory_xml(revision_id))
684
return bzrlib.xml5.serializer_v5.read_inventory(f)
687
def get_inventory_xml(self, revision_id):
688
"""Get inventory XML as a file object."""
690
assert isinstance(revision_id, basestring), type(revision_id)
691
iw = self.get_inventory_weave()
692
return iw.get_text(iw.lookup(revision_id))
694
raise bzrlib.errors.HistoryMissing(self, 'inventory', revision_id)
697
def get_inventory_sha1(self, revision_id):
626
698
"""Return the sha1 hash of the inventory entry
628
return sha_file(self.inventory_store[inventory_id])
700
return self.get_revision(revision_id).inventory_sha1
631
703
def get_revision_inventory(self, revision_id):
632
704
"""Return inventory of a past revision."""
633
# bzr 0.0.6 imposes the constraint that the inventory_id
705
# bzr 0.0.6 and later imposes the constraint that the inventory_id
634
706
# must be the same as its revision, so this is trivial.
635
707
if revision_id == None:
636
from bzrlib.inventory import Inventory
637
708
return Inventory(self.get_root_id())
639
710
return self.get_inventory(revision_id)
642
713
def revision_history(self):
643
"""Return sequence of revision hashes on to this branch.
645
>>> ScratchBranch().revision_history()
714
"""Return sequence of revision hashes on to this branch."""
650
717
return [l.rstrip('\r\n') for l in
659
726
>>> sb = ScratchBranch(files=['foo', 'foo~'])
660
727
>>> sb.common_ancestor(sb) == (None, None)
662
>>> commit.commit(sb, "Committing first revision", verbose=False)
729
>>> commit.commit(sb, "Committing first revision")
663
730
>>> sb.common_ancestor(sb)[0]
665
732
>>> clone = sb.clone()
666
>>> commit.commit(sb, "Committing second revision", verbose=False)
733
>>> commit.commit(sb, "Committing second revision")
667
734
>>> sb.common_ancestor(sb)[0]
669
736
>>> sb.common_ancestor(clone)[0]
671
>>> commit.commit(clone, "Committing divergent second revision",
738
>>> commit.commit(clone, "Committing divergent second revision")
673
739
>>> sb.common_ancestor(clone)[0]
675
741
>>> sb.common_ancestor(clone) == clone.common_ancestor(sb)
777
826
if stop_revision is None:
778
827
stop_revision = other_len
779
elif stop_revision > other_len:
780
raise NoSuchRevision(self, stop_revision)
829
assert isinstance(stop_revision, int)
830
if stop_revision > other_len:
831
raise bzrlib.errors.NoSuchRevision(self, stop_revision)
782
833
return other_history[self_len:stop_revision]
785
def update_revisions(self, other, stop_revision=None):
786
"""Pull in all new revisions from other branch.
788
>>> from bzrlib.commit import commit
789
>>> bzrlib.trace.silent = True
790
>>> br1 = ScratchBranch(files=['foo', 'bar'])
793
>>> commit(br1, "lala!", rev_id="REVISION-ID-1", verbose=False)
794
>>> br2 = ScratchBranch()
795
>>> br2.update_revisions(br1)
799
>>> br2.revision_history()
801
>>> br2.update_revisions(br1)
805
>>> br1.text_store.total_size() == br2.text_store.total_size()
836
def update_revisions(self, other, stop_revno=None):
837
"""Pull in new perfect-fit revisions.
808
from bzrlib.progress import ProgressBar
812
pb.update('comparing histories')
813
revision_ids = self.missing_revisions(other, stop_revision)
815
if hasattr(other.revision_store, "prefetch"):
816
other.revision_store.prefetch(revision_ids)
817
if hasattr(other.inventory_store, "prefetch"):
818
inventory_ids = [other.get_revision(r).inventory_id
819
for r in revision_ids]
820
other.inventory_store.prefetch(inventory_ids)
825
for rev_id in revision_ids:
827
pb.update('fetching revision', i, len(revision_ids))
828
rev = other.get_revision(rev_id)
829
revisions.append(rev)
830
inv = other.get_inventory(str(rev.inventory_id))
831
for key, entry in inv.iter_entries():
832
if entry.text_id is None:
834
if entry.text_id not in self.text_store:
835
needed_texts.add(entry.text_id)
839
count = self.text_store.copy_multi(other.text_store, needed_texts)
840
print "Added %d texts." % count
841
inventory_ids = [ f.inventory_id for f in revisions ]
842
count = self.inventory_store.copy_multi(other.inventory_store,
844
print "Added %d inventories." % count
845
revision_ids = [ f.revision_id for f in revisions]
846
count = self.revision_store.copy_multi(other.revision_store,
848
for revision_id in revision_ids:
849
self.append_revision(revision_id)
850
print "Added %d revisions." % count
839
from bzrlib.fetch import greedy_fetch
842
stop_revision = other.lookup_revision(stop_revno)
845
greedy_fetch(to_branch=self, from_branch=other,
846
revision=stop_revision)
848
pullable_revs = self.missing_revisions(other, stop_revision)
851
greedy_fetch(to_branch=self,
853
revision=pullable_revs[-1])
854
self.append_revision(*pullable_revs)
853
857
def commit(self, *args, **kw):
854
from bzrlib.commit import commit
855
commit(self, *args, **kw)
858
from bzrlib.commit import Commit
859
Commit().commit(self, *args, **kw)
858
862
def lookup_revision(self, revision):
859
863
"""Return the revision identifier for a given revision information."""
860
revno, info = self.get_revision_info(revision)
864
revno, info = self._get_revision_info(revision)
868
def revision_id_to_revno(self, revision_id):
869
"""Given a revision id, return its revno"""
870
history = self.revision_history()
872
return history.index(revision_id) + 1
874
raise bzrlib.errors.NoSuchRevision(self, revision_id)
863
877
def get_revision_info(self, revision):
864
878
"""Return (revno, revision id) for revision identifier.
868
882
revision can also be a string, in which case it is parsed for something like
869
883
'date:' or 'revid:' etc.
885
revno, rev_id = self._get_revision_info(revision)
887
raise bzrlib.errors.NoSuchRevision(self, revision)
890
def get_rev_id(self, revno, history=None):
891
"""Find the revision id of the specified revno."""
895
history = self.revision_history()
896
elif revno <= 0 or revno > len(history):
897
raise bzrlib.errors.NoSuchRevision(self, revno)
898
return history[revno - 1]
900
def _get_revision_info(self, revision):
901
"""Return (revno, revision id) for revision specifier.
903
revision can be an integer, in which case it is assumed to be revno
904
(though this will translate negative values into positive ones)
905
revision can also be a string, in which case it is parsed for something
906
like 'date:' or 'revid:' etc.
908
A revid is always returned. If it is None, the specifier referred to
909
the null revision. If the revid does not occur in the revision
910
history, revno will be None.
871
913
if revision is None:
878
920
revs = self.revision_history()
879
921
if isinstance(revision, int):
882
# Mabye we should do this first, but we don't need it if revision == 0
884
923
revno = len(revs) + revision + 1
926
rev_id = self.get_rev_id(revno, revs)
887
927
elif isinstance(revision, basestring):
888
928
for prefix, func in Branch.REVISION_NAMESPACES.iteritems():
889
929
if revision.startswith(prefix):
890
revno = func(self, revs, revision)
930
result = func(self, revs, revision)
932
revno, rev_id = result
935
rev_id = self.get_rev_id(revno, revs)
893
raise BzrError('No namespace registered for string: %r' % revision)
938
raise BzrError('No namespace registered for string: %r' %
941
raise TypeError('Unhandled revision type %s' % revision)
895
if revno is None or revno <= 0 or revno > len(revs):
896
raise BzrError("no such revision %s" % revision)
897
return revno, revs[revno-1]
945
raise bzrlib.errors.NoSuchRevision(self, revision)
899
948
def _namespace_revno(self, revs, revision):
900
949
"""Lookup a revision by revision number"""
901
950
assert revision.startswith('revno:')
903
return int(revision[6:])
952
return (int(revision[6:]),)
904
953
except ValueError:
906
955
REVISION_NAMESPACES['revno:'] = _namespace_revno
908
957
def _namespace_revid(self, revs, revision):
909
958
assert revision.startswith('revid:')
959
rev_id = revision[len('revid:'):]
911
return revs.index(revision[6:]) + 1
961
return revs.index(rev_id) + 1, rev_id
912
962
except ValueError:
914
964
REVISION_NAMESPACES['revid:'] = _namespace_revid
916
966
def _namespace_last(self, revs, revision):
1298
def get_parent(self):
1299
"""Return the parent location of the branch.
1301
This is the default location for push/pull/missing. The usual
1302
pattern is that the user can override it by specifying a
1306
_locs = ['parent', 'pull', 'x-pull']
1309
return self.controlfile(l, 'r').read().strip('\n')
1311
if e.errno != errno.ENOENT:
1316
def set_parent(self, url):
1317
# TODO: Maybe delete old location files?
1318
from bzrlib.atomicfile import AtomicFile
1321
f = AtomicFile(self.controlfilename('parent'))
1330
def check_revno(self, revno):
1332
Check whether a revno corresponds to any revision.
1333
Zero (the NULL revision) is considered valid.
1336
self.check_real_revno(revno)
1338
def check_real_revno(self, revno):
1340
Check whether a revno corresponds to a real revision.
1341
Zero (the NULL revision) is considered invalid
1343
if revno < 1 or revno > self.revno():
1344
raise InvalidRevisionNumber(revno)
1253
1349
class ScratchBranch(Branch):
1254
1350
"""Special test class: a branch that cleans up after itself.
1371
1469
"""Return a new tree-root file id."""
1372
1470
return gen_file_id('TREE_ROOT')
1473
def pull_loc(branch):
1474
# TODO: Should perhaps just make attribute be 'base' in
1475
# RemoteBranch and Branch?
1476
if hasattr(branch, "baseurl"):
1477
return branch.baseurl
1482
def copy_branch(branch_from, to_location, revision=None):
1483
"""Copy branch_from into the existing directory to_location.
1486
If not None, only revisions up to this point will be copied.
1487
The head of the new branch will be that revision. Can be a
1491
The name of a local directory that exists but is empty.
1493
# TODO: This could be done *much* more efficiently by just copying
1494
# all the whole weaves and revisions, rather than getting one
1495
# revision at a time.
1496
from bzrlib.merge import merge
1497
from bzrlib.branch import Branch
1499
assert isinstance(branch_from, Branch)
1500
assert isinstance(to_location, basestring)
1502
br_to = Branch(to_location, init=True)
1503
br_to.set_root_id(branch_from.get_root_id())
1504
if revision is None:
1507
revno, rev_id = branch_from.get_revision_info(revision)
1508
br_to.update_revisions(branch_from, stop_revno=revno)
1509
merge((to_location, -1), (to_location, 0), this_dir=to_location,
1510
check_clean=False, ignore_zero=True)
1512
from_location = pull_loc(branch_from)
1513
br_to.set_parent(pull_loc(branch_from))