20
from cStringIO import StringIO
22
23
from bzrlib.trace import mutter, note
23
24
from bzrlib.osutils import isdir, quotefn, compact_date, rand_bytes, \
25
26
sha_file, appendpath, file_kind
26
from bzrlib.errors import BzrError, InvalidRevisionNumber, InvalidRevisionId
28
from bzrlib.errors import (BzrError, InvalidRevisionNumber, InvalidRevisionId,
28
30
from bzrlib.textui import show_status
29
31
from bzrlib.revision import Revision
30
from bzrlib.xml import unpack_xml
31
32
from bzrlib.delta import compare_trees
32
33
from bzrlib.tree import EmptyTree, RevisionTree
34
BZR_BRANCH_FORMAT = "Bazaar-NG branch, format 0.0.4\n"
34
from bzrlib.inventory import Inventory
35
from bzrlib.weavestore import WeaveStore
36
from bzrlib.store import ImmutableStore
41
INVENTORY_FILEID = '__inventory'
42
ANCESTRY_FILEID = '__ancestry'
45
BZR_BRANCH_FORMAT_4 = "Bazaar-NG branch, format 0.0.4\n"
46
BZR_BRANCH_FORMAT_5 = "Bazaar-NG branch, format 5\n"
35
47
## TODO: Maybe include checks for common corruption of newlines, etc?
38
50
# TODO: Some operations like log might retrieve the same revisions
39
51
# repeatedly to calculate deltas. We could perhaps have a weakref
40
# cache in memory to make this faster.
52
# cache in memory to make this faster. In general anything can be
53
# cached in memory between lock and unlock operations.
55
# TODO: please move the revision-string syntax stuff out of the branch
56
# object; it's clutter
43
59
def find_branch(f, **args):
297
309
raise BzrError("invalid controlfile mode %r" % mode)
301
311
def _make_control(self):
302
from bzrlib.inventory import Inventory
303
from bzrlib.xml import pack_xml
305
312
os.mkdir(self.controlfilename([]))
306
313
self.controlfile('README', 'w').write(
307
314
"This is a Bazaar-NG control directory.\n"
308
315
"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'):
316
self.controlfile('branch-format', 'w').write(BZR_BRANCH_FORMAT_5)
317
for d in ('text-store', 'revision-store',
311
319
os.mkdir(self.controlfilename(d))
312
320
for f in ('revision-history', 'merged-patches',
313
321
'pending-merged-patches', 'branch-name',
316
324
self.controlfile(f, 'w').write('')
317
325
mutter('created control directory in ' + self.base)
319
pack_xml(Inventory(gen_root_id()), self.controlfile('inventory','w'))
327
# if we want per-tree root ids then this is the place to set
328
# them; they're not needed for now and so ommitted for
330
f = self.controlfile('inventory','w')
331
bzrlib.xml5.serializer_v5.write_inventory(Inventory(), f)
322
335
def _check_format(self):
323
336
"""Check this branch format is supported.
325
The current tool only supports the current unstable format.
338
The format level is stored, as an integer, in
339
self._branch_format for code that needs to check it later.
327
341
In the future, we might need different in-memory Branch
328
342
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
344
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'])
345
if fmt == BZR_BRANCH_FORMAT_5:
346
self._branch_format = 5
348
raise BzrError('sorry, branch format "%s" not supported; '
349
'use a different bzr version, '
350
'or run "bzr upgrade", '
351
'or remove the .bzr directory and "bzr init" again'
352
% fmt.rstrip('\n\r'))
340
354
def get_root_id(self):
341
355
"""Return the id of this branches root"""
357
371
def read_working_inventory(self):
358
372
"""Read the working inventory."""
359
from bzrlib.inventory import Inventory
360
from bzrlib.xml import unpack_xml
361
from time import time
365
375
# ElementTree does its own conversion from UTF-8, so open in
367
inv = unpack_xml(Inventory,
368
self.controlfile('inventory', 'rb'))
369
mutter("loaded inventory of %d items in %f"
370
% (len(inv), time() - before))
377
f = self.controlfile('inventory', 'rb')
378
return bzrlib.xml5.serializer_v5.read_inventory(f)
595
594
return self.revision_store[revision_id]
596
595
except IndexError:
597
raise bzrlib.errors.NoSuchRevision(revision_id)
596
raise bzrlib.errors.NoSuchRevision(self, revision_id)
601
def get_revision_xml(self, revision_id):
602
return self.get_revision_xml_file(revision_id).read()
602
605
def get_revision(self, revision_id):
603
606
"""Return the Revision object for a named revision"""
604
xml_file = self.get_revision_xml(revision_id)
607
xml_file = self.get_revision_xml_file(revision_id)
607
r = unpack_xml(Revision, xml_file)
610
r = bzrlib.xml5.serializer_v5.read_revision(xml_file)
608
611
except SyntaxError, e:
609
612
raise bzrlib.errors.BzrError('failed to unpack revision_xml',
640
643
def get_revision_sha1(self, revision_id):
641
644
"""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):
645
return bzrlib.osutils.sha_file(self.get_revision_xml_file(revision_id))
648
def get_ancestry(self, revision_id):
649
"""Return a list of revision-ids integrated by a revision.
651
w = self.weave_store.get_weave(ANCESTRY_FILEID)
653
return [l[:-1] for l in w.get_iter(w.lookup(revision_id))]
656
def get_inventory_weave(self):
657
return self.weave_store.get_weave(INVENTORY_FILEID)
660
def get_inventory(self, revision_id):
661
"""Get Inventory object by hash."""
662
# FIXME: The text gets passed around a lot coming from the weave.
663
f = StringIO(self.get_inventory_xml(revision_id))
664
return bzrlib.xml5.serializer_v5.read_inventory(f)
667
def get_inventory_xml(self, revision_id):
668
"""Get inventory XML as a file object."""
670
assert isinstance(revision_id, basestring), type(revision_id)
671
iw = self.get_inventory_weave()
672
return iw.get_text(iw.lookup(revision_id))
674
raise bzrlib.errors.HistoryMissing(self, 'inventory', revision_id)
677
def get_inventory_sha1(self, revision_id):
664
678
"""Return the sha1 hash of the inventory entry
666
return sha_file(self.inventory_store[inventory_id])
680
return self.get_revision(revision_id).inventory_sha1
669
683
def get_revision_inventory(self, revision_id):
670
684
"""Return inventory of a past revision."""
671
# bzr 0.0.6 imposes the constraint that the inventory_id
685
# bzr 0.0.6 and later imposes the constraint that the inventory_id
672
686
# must be the same as its revision, so this is trivial.
673
687
if revision_id == None:
674
from bzrlib.inventory import Inventory
675
688
return Inventory(self.get_root_id())
677
690
return self.get_inventory(revision_id)
697
710
>>> sb = ScratchBranch(files=['foo', 'foo~'])
698
711
>>> sb.common_ancestor(sb) == (None, None)
700
>>> commit.commit(sb, "Committing first revision", verbose=False)
713
>>> commit.commit(sb, "Committing first revision")
701
714
>>> sb.common_ancestor(sb)[0]
703
716
>>> clone = sb.clone()
704
>>> commit.commit(sb, "Committing second revision", verbose=False)
717
>>> commit.commit(sb, "Committing second revision")
705
718
>>> sb.common_ancestor(sb)[0]
707
720
>>> sb.common_ancestor(clone)[0]
709
>>> commit.commit(clone, "Committing divergent second revision",
722
>>> commit.commit(clone, "Committing divergent second revision")
711
723
>>> sb.common_ancestor(clone)[0]
713
725
>>> sb.common_ancestor(clone) == clone.common_ancestor(sb)
794
810
if stop_revision is None:
795
811
stop_revision = other_len
796
812
elif stop_revision > other_len:
797
raise NoSuchRevision(self, stop_revision)
813
raise bzrlib.errors.NoSuchRevision(self, stop_revision)
799
815
return other_history[self_len:stop_revision]
802
818
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()
819
"""Pull in new perfect-fit revisions.
825
from bzrlib.progress import ProgressBar
829
pb.update('comparing histories')
821
from bzrlib.fetch import greedy_fetch
823
greedy_fetch(to_branch=self, from_branch=other,
824
revision_limit=stop_revision)
830
826
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
828
if len(revision_ids) > 0:
829
count = greedy_fetch(self, other, revision_ids[-1], pb)[0]
832
self.append_revision(*revision_ids)
870
835
def commit(self, *args, **kw):
871
from bzrlib.commit import commit
872
commit(self, *args, **kw)
836
from bzrlib.commit import Commit
837
Commit().commit(self, *args, **kw)
875
840
def lookup_revision(self, revision):
876
841
"""Return the revision identifier for a given revision information."""
877
revno, info = self.get_revision_info(revision)
842
revno, info = self._get_revision_info(revision)
846
def revision_id_to_revno(self, revision_id):
847
"""Given a revision id, return its revno"""
848
history = self.revision_history()
850
return history.index(revision_id) + 1
852
raise bzrlib.errors.NoSuchRevision(self, revision_id)
880
855
def get_revision_info(self, revision):
881
856
"""Return (revno, revision id) for revision identifier.
885
860
revision can also be a string, in which case it is parsed for something like
886
861
'date:' or 'revid:' etc.
863
revno, rev_id = self._get_revision_info(revision)
865
raise bzrlib.errors.NoSuchRevision(self, revision)
868
def get_rev_id(self, revno, history=None):
869
"""Find the revision id of the specified revno."""
873
history = self.revision_history()
874
elif revno <= 0 or revno > len(history):
875
raise bzrlib.errors.NoSuchRevision(self, revno)
876
return history[revno - 1]
878
def _get_revision_info(self, revision):
879
"""Return (revno, revision id) for revision specifier.
881
revision can be an integer, in which case it is assumed to be revno
882
(though this will translate negative values into positive ones)
883
revision can also be a string, in which case it is parsed for something
884
like 'date:' or 'revid:' etc.
886
A revid is always returned. If it is None, the specifier referred to
887
the null revision. If the revid does not occur in the revision
888
history, revno will be None.
888
891
if revision is None:
895
898
revs = self.revision_history()
896
899
if isinstance(revision, int):
899
# Mabye we should do this first, but we don't need it if revision == 0
901
901
revno = len(revs) + revision + 1
904
rev_id = self.get_rev_id(revno, revs)
904
905
elif isinstance(revision, basestring):
905
906
for prefix, func in Branch.REVISION_NAMESPACES.iteritems():
906
907
if revision.startswith(prefix):
907
revno = func(self, revs, revision)
908
result = func(self, revs, revision)
910
revno, rev_id = result
913
rev_id = self.get_rev_id(revno, revs)
910
raise BzrError('No namespace registered for string: %r' % revision)
916
raise BzrError('No namespace registered for string: %r' %
919
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]
923
raise bzrlib.errors.NoSuchRevision(self, revision)
916
926
def _namespace_revno(self, revs, revision):
917
927
"""Lookup a revision by revision number"""
918
928
assert revision.startswith('revno:')
920
return int(revision[6:])
930
return (int(revision[6:]),)
921
931
except ValueError:
923
933
REVISION_NAMESPACES['revno:'] = _namespace_revno
925
935
def _namespace_revid(self, revs, revision):
926
936
assert revision.startswith('revid:')
937
rev_id = revision[len('revid:'):]
928
return revs.index(revision[6:]) + 1
939
return revs.index(rev_id) + 1, rev_id
929
940
except ValueError:
931
942
REVISION_NAMESPACES['revid:'] = _namespace_revid
933
944
def _namespace_last(self, revs, revision):
1277
def get_parent(self):
1278
"""Return the parent location of the branch.
1280
This is the default location for push/pull/missing. The usual
1281
pattern is that the user can override it by specifying a
1285
_locs = ['parent', 'pull', 'x-pull']
1288
return self.controlfile(l, 'r').read().strip('\n')
1290
if e.errno != errno.ENOENT:
1295
def set_parent(self, url):
1296
# TODO: Maybe delete old location files?
1297
from bzrlib.atomicfile import AtomicFile
1300
f = AtomicFile(self.controlfilename('parent'))
1309
def check_revno(self, revno):
1311
Check whether a revno corresponds to any revision.
1312
Zero (the NULL revision) is considered valid.
1315
self.check_real_revno(revno)
1317
def check_real_revno(self, revno):
1319
Check whether a revno corresponds to a real revision.
1320
Zero (the NULL revision) is considered invalid
1322
if revno < 1 or revno > self.revno():
1323
raise InvalidRevisionNumber(revno)
1268
1328
class ScratchBranch(Branch):
1269
1329
"""Special test class: a branch that cleans up after itself.
1386
1448
"""Return a new tree-root file id."""
1387
1449
return gen_file_id('TREE_ROOT')
1452
def pull_loc(branch):
1453
# TODO: Should perhaps just make attribute be 'base' in
1454
# RemoteBranch and Branch?
1455
if hasattr(branch, "baseurl"):
1456
return branch.baseurl
1461
def copy_branch(branch_from, to_location, revision=None):
1462
"""Copy branch_from into the existing directory to_location.
1465
If not None, only revisions up to this point will be copied.
1466
The head of the new branch will be that revision.
1469
The name of a local directory that exists but is empty.
1471
from bzrlib.merge import merge
1472
from bzrlib.branch import Branch
1474
assert isinstance(branch_from, Branch)
1475
assert isinstance(to_location, basestring)
1477
br_to = Branch(to_location, init=True)
1478
br_to.set_root_id(branch_from.get_root_id())
1479
if revision is None:
1480
revno = branch_from.revno()
1482
revno, rev_id = branch_from.get_revision_info(revision)
1483
br_to.update_revisions(branch_from, stop_revision=revno)
1484
merge((to_location, -1), (to_location, 0), this_dir=to_location,
1485
check_clean=False, ignore_zero=True)
1487
from_location = pull_loc(branch_from)
1488
br_to.set_parent(pull_loc(branch_from))