25
25
sha_file, appendpath, file_kind
27
from bzrlib.errors import BzrError, InvalidRevisionNumber, InvalidRevisionId, \
28
DivergedBranches, NotBranchError
27
from bzrlib.errors import (BzrError, InvalidRevisionNumber, InvalidRevisionId,
29
29
from bzrlib.textui import show_status
30
30
from bzrlib.revision import Revision
31
31
from bzrlib.delta import compare_trees
32
32
from bzrlib.tree import EmptyTree, RevisionTree
33
from bzrlib.inventory import Inventory
34
from bzrlib.weavestore import WeaveStore
38
BZR_BRANCH_FORMAT = "Bazaar-NG branch, format 0.0.4\n"
40
BZR_BRANCH_FORMAT_4 = "Bazaar-NG branch, format 0.0.4\n"
41
BZR_BRANCH_FORMAT_5 = "Bazaar-NG branch, format 5\n"
39
42
## TODO: Maybe include checks for common corruption of newlines, etc?
43
46
# repeatedly to calculate deltas. We could perhaps have a weakref
44
47
# cache in memory to make this faster.
46
def find_branch(*ignored, **ignored_too):
47
# XXX: leave this here for about one release, then remove it
48
raise NotImplementedError('find_branch() is not supported anymore, '
49
'please use one of the new branch constructors')
49
# TODO: please move the revision-string syntax stuff out of the branch
50
# object; it's clutter
53
def find_branch(f, **args):
54
if f and (f.startswith('http://') or f.startswith('https://')):
56
return remotebranch.RemoteBranch(f, **args)
58
return Branch(f, **args)
61
def find_cached_branch(f, cache_root, **args):
62
from remotebranch import RemoteBranch
63
br = find_branch(f, **args)
64
def cacheify(br, store_name):
65
from meta_store import CachedStore
66
cache_path = os.path.join(cache_root, store_name)
68
new_store = CachedStore(getattr(br, store_name), cache_path)
69
setattr(br, store_name, new_store)
71
if isinstance(br, RemoteBranch):
72
cacheify(br, 'inventory_store')
73
cacheify(br, 'text_store')
74
cacheify(br, 'revision_store')
51
78
def _relpath(base, path):
52
79
"""Return path relative to base, or raise exception.
116
151
"""Branch holding a history of revisions.
119
Base directory/url of the branch.
123
def __init__(self, *ignored, **ignored_too):
124
raise NotImplementedError('The Branch class is abstract')
128
"""Open an existing branch, rooted at 'base' (url)"""
129
if base and (base.startswith('http://') or base.startswith('https://')):
130
from bzrlib.remotebranch import RemoteBranch
131
return RemoteBranch(base, find_root=False)
133
return LocalBranch(base, find_root=False)
136
def open_containing(url):
137
"""Open an existing branch, containing url (search upwards for the root)
139
if url and (url.startswith('http://') or url.startswith('https://')):
140
from bzrlib.remotebranch import RemoteBranch
141
return RemoteBranch(url)
143
return LocalBranch(url)
146
def initialize(base):
147
"""Create a new branch, rooted at 'base' (url)"""
148
if base and (base.startswith('http://') or base.startswith('https://')):
149
from bzrlib.remotebranch import RemoteBranch
150
return RemoteBranch(base, init=True)
152
return LocalBranch(base, init=True)
154
def setup_caching(self, cache_root):
155
"""Subclasses that care about caching should override this, and set
156
up cached stores located under cache_root.
160
class LocalBranch(Branch):
161
"""A branch stored in the actual filesystem.
163
Note that it's "local" in the context of the filesystem; it doesn't
164
really matter if it's on an nfs/smb/afs/coda/... share, as long as
165
it's writable, and can be accessed via the normal filesystem API.
154
Base directory of the branch.
168
157
None, or 'r' or 'w'
333
323
# them; they're not needed for now and so ommitted for
335
325
f = self.controlfile('inventory','w')
336
bzrlib.xml.serializer_v4.write_inventory(Inventory(), f)
326
bzrlib.xml5.serializer_v5.write_inventory(Inventory(), f)
339
329
def _check_format(self):
340
330
"""Check this branch format is supported.
342
The current tool only supports the current unstable format.
332
The format level is stored, as an integer, in
333
self._branch_format for code that needs to check it later.
344
335
In the future, we might need different in-memory Branch
345
336
classes to support downlevel branches. But not yet.
347
# This ignores newlines so that we can open branches created
348
# on Windows from Linux and so on. I think it might be better
349
# to always make all internal files in unix format.
350
338
fmt = self.controlfile('branch-format', 'r').read()
351
fmt = fmt.replace('\r\n', '\n')
352
if fmt != BZR_BRANCH_FORMAT:
353
raise BzrError('sorry, branch format %r not supported' % fmt,
354
['use a different bzr version',
355
'or remove the .bzr directory and "bzr init" again'])
339
if fmt == BZR_BRANCH_FORMAT_5:
340
self._branch_format = 5
342
raise BzrError('sorry, branch format "%s" not supported; '
343
'use a different bzr version, '
344
'or run "bzr upgrade", '
345
'or remove the .bzr directory and "bzr init" again'
346
% fmt.rstrip('\n\r'))
357
348
def get_root_id(self):
358
349
"""Return the id of this branches root"""
655
645
return bzrlib.osutils.sha_file(self.get_revision_xml(revision_id))
658
def get_inventory(self, inventory_id):
648
def get_inventory(self, revision_id):
659
649
"""Get Inventory object by hash.
661
651
TODO: Perhaps for this and similar methods, take a revision
662
652
parameter which can be either an integer revno or a
664
from bzrlib.inventory import Inventory
666
f = self.get_inventory_xml_file(inventory_id)
667
return bzrlib.xml.serializer_v4.read_inventory(f)
670
def get_inventory_xml(self, inventory_id):
654
f = self.get_inventory_xml_file(revision_id)
655
return bzrlib.xml5.serializer_v5.read_inventory(f)
658
def get_inventory_xml(self, revision_id):
671
659
"""Get inventory XML as a file object."""
672
return self.inventory_store[inventory_id]
661
assert isinstance(revision_id, basestring), type(revision_id)
662
return self.inventory_store[revision_id]
664
raise bzrlib.errors.HistoryMissing(self, 'inventory', revision_id)
674
666
get_inventory_xml_file = get_inventory_xml
677
def get_inventory_sha1(self, inventory_id):
669
def get_inventory_sha1(self, revision_id):
678
670
"""Return the sha1 hash of the inventory entry
680
return sha_file(self.get_inventory_xml(inventory_id))
672
return sha_file(self.get_inventory_xml_file(revision_id))
683
675
def get_revision_inventory(self, revision_id):
708
699
def common_ancestor(self, other, self_revno=None, other_revno=None):
710
>>> from bzrlib.commit import commit
711
702
>>> sb = ScratchBranch(files=['foo', 'foo~'])
712
703
>>> sb.common_ancestor(sb) == (None, None)
714
>>> commit(sb, "Committing first revision", verbose=False)
705
>>> commit.commit(sb, "Committing first revision", verbose=False)
715
706
>>> sb.common_ancestor(sb)[0]
717
708
>>> clone = sb.clone()
718
>>> commit(sb, "Committing second revision", verbose=False)
709
>>> commit.commit(sb, "Committing second revision", verbose=False)
719
710
>>> sb.common_ancestor(sb)[0]
721
712
>>> sb.common_ancestor(clone)[0]
723
>>> commit(clone, "Committing divergent second revision",
714
>>> commit.commit(clone, "Committing divergent second revision",
724
715
... verbose=False)
725
716
>>> sb.common_ancestor(clone)[0]
817
808
"""Pull in all new revisions from other branch.
819
810
from bzrlib.fetch import greedy_fetch
820
from bzrlib.revision import get_intervening_revisions
822
812
pb = bzrlib.ui.ui_factory.progress_bar()
823
813
pb.update('comparing histories')
824
if stop_revision is None:
825
other_revision = other.last_patch()
815
revision_ids = self.missing_revisions(other, stop_revision)
817
if len(revision_ids) > 0:
818
count = greedy_fetch(self, other, revision_ids[-1], pb)[0]
827
other_revision = other.get_rev_id(stop_revision)
828
count = greedy_fetch(self, other, other_revision, pb)[0]
830
revision_ids = self.missing_revisions(other, stop_revision)
831
except DivergedBranches, e:
833
revision_ids = get_intervening_revisions(self.last_patch(),
834
other_revision, self)
835
assert self.last_patch() not in revision_ids
836
except bzrlib.errors.NotAncestor:
839
821
self.append_revision(*revision_ids)
822
## note("Added %d revisions." % count)
842
825
def install_revisions(self, other, revision_ids, pb):
843
826
if hasattr(other.revision_store, "prefetch"):
844
827
other.revision_store.prefetch(revision_ids)
845
828
if hasattr(other.inventory_store, "prefetch"):
847
for rev_id in revision_ids:
849
revision = other.get_revision(rev_id).inventory_id
850
inventory_ids.append(revision)
851
except bzrlib.errors.NoSuchRevision:
829
inventory_ids = [other.get_revision(r).inventory_id
830
for r in revision_ids]
853
831
other.inventory_store.prefetch(inventory_ids)
916
915
raise bzrlib.errors.NoSuchRevision(self, revno)
917
916
return history[revno - 1]
918
def _get_revision_info(self, revision):
919
"""Return (revno, revision id) for revision specifier.
921
revision can be an integer, in which case it is assumed to be revno
922
(though this will translate negative values into positive ones)
923
revision can also be a string, in which case it is parsed for something
924
like 'date:' or 'revid:' etc.
926
A revid is always returned. If it is None, the specifier referred to
927
the null revision. If the revid does not occur in the revision
928
history, revno will be None.
934
try:# Convert to int if possible
935
revision = int(revision)
938
revs = self.revision_history()
939
if isinstance(revision, int):
941
revno = len(revs) + revision + 1
944
rev_id = self.get_rev_id(revno, revs)
945
elif isinstance(revision, basestring):
946
for prefix, func in Branch.REVISION_NAMESPACES.iteritems():
947
if revision.startswith(prefix):
948
result = func(self, revs, revision)
950
revno, rev_id = result
953
rev_id = self.get_rev_id(revno, revs)
956
raise BzrError('No namespace registered for string: %r' %
959
raise TypeError('Unhandled revision type %s' % revision)
963
raise bzrlib.errors.NoSuchRevision(self, revision)
966
def _namespace_revno(self, revs, revision):
967
"""Lookup a revision by revision number"""
968
assert revision.startswith('revno:')
970
return (int(revision[6:]),)
973
REVISION_NAMESPACES['revno:'] = _namespace_revno
975
def _namespace_revid(self, revs, revision):
976
assert revision.startswith('revid:')
977
rev_id = revision[len('revid:'):]
979
return revs.index(rev_id) + 1, rev_id
982
REVISION_NAMESPACES['revid:'] = _namespace_revid
984
def _namespace_last(self, revs, revision):
985
assert revision.startswith('last:')
987
offset = int(revision[5:])
992
raise BzrError('You must supply a positive value for --revision last:XXX')
993
return (len(revs) - offset + 1,)
994
REVISION_NAMESPACES['last:'] = _namespace_last
996
def _namespace_tag(self, revs, revision):
997
assert revision.startswith('tag:')
998
raise BzrError('tag: namespace registered, but not implemented.')
999
REVISION_NAMESPACES['tag:'] = _namespace_tag
1001
def _namespace_date(self, revs, revision):
1002
assert revision.startswith('date:')
1004
# Spec for date revisions:
1006
# value can be 'yesterday', 'today', 'tomorrow' or a YYYY-MM-DD string.
1007
# it can also start with a '+/-/='. '+' says match the first
1008
# entry after the given date. '-' is match the first entry before the date
1009
# '=' is match the first entry after, but still on the given date.
1011
# +2005-05-12 says find the first matching entry after May 12th, 2005 at 0:00
1012
# -2005-05-12 says find the first matching entry before May 12th, 2005 at 0:00
1013
# =2005-05-12 says find the first match after May 12th, 2005 at 0:00 but before
1014
# May 13th, 2005 at 0:00
1016
# So the proper way of saying 'give me all entries for today' is:
1017
# -r {date:+today}:{date:-tomorrow}
1018
# The default is '=' when not supplied
1021
if val[:1] in ('+', '-', '='):
1022
match_style = val[:1]
1025
today = datetime.datetime.today().replace(hour=0,minute=0,second=0,microsecond=0)
1026
if val.lower() == 'yesterday':
1027
dt = today - datetime.timedelta(days=1)
1028
elif val.lower() == 'today':
1030
elif val.lower() == 'tomorrow':
1031
dt = today + datetime.timedelta(days=1)
1034
# This should be done outside the function to avoid recompiling it.
1035
_date_re = re.compile(
1036
r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
1038
r'(?P<time>(?P<hour>\d\d):(?P<minute>\d\d)(:(?P<second>\d\d))?)?'
1040
m = _date_re.match(val)
1041
if not m or (not m.group('date') and not m.group('time')):
1042
raise BzrError('Invalid revision date %r' % revision)
1045
year, month, day = int(m.group('year')), int(m.group('month')), int(m.group('day'))
1047
year, month, day = today.year, today.month, today.day
1049
hour = int(m.group('hour'))
1050
minute = int(m.group('minute'))
1051
if m.group('second'):
1052
second = int(m.group('second'))
1056
hour, minute, second = 0,0,0
1058
dt = datetime.datetime(year=year, month=month, day=day,
1059
hour=hour, minute=minute, second=second)
1063
if match_style == '-':
1065
elif match_style == '=':
1066
last = dt + datetime.timedelta(days=1)
1069
for i in range(len(revs)-1, -1, -1):
1070
r = self.get_revision(revs[i])
1071
# TODO: Handle timezone.
1072
dt = datetime.datetime.fromtimestamp(r.timestamp)
1073
if first >= dt and (last is None or dt >= last):
1076
for i in range(len(revs)):
1077
r = self.get_revision(revs[i])
1078
# TODO: Handle timezone.
1079
dt = datetime.datetime.fromtimestamp(r.timestamp)
1080
if first <= dt and (last is None or dt <= last):
1082
REVISION_NAMESPACES['date:'] = _namespace_date
920
1084
def revision_tree(self, revision_id):
921
1085
"""Return Tree for a revision on this branch.
1342
1509
The name of a local directory that exists but is empty.
1344
1511
from bzrlib.merge import merge
1345
from bzrlib.revisionspec import RevisionSpec
1512
from bzrlib.branch import Branch
1347
1514
assert isinstance(branch_from, Branch)
1348
1515
assert isinstance(to_location, basestring)
1350
br_to = Branch.initialize(to_location)
1517
br_to = Branch(to_location, init=True)
1351
1518
br_to.set_root_id(branch_from.get_root_id())
1352
1519
if revision is None:
1353
1520
revno = branch_from.revno()
1355
revno, rev_id = RevisionSpec(revision).in_history(branch_from)
1522
revno, rev_id = branch_from.get_revision_info(revision)
1356
1523
br_to.update_revisions(branch_from, stop_revision=revno)
1357
1524
merge((to_location, -1), (to_location, 0), this_dir=to_location,
1358
1525
check_clean=False, ignore_zero=True)
1359
br_to.set_parent(branch_from.base)
1527
from_location = pull_loc(branch_from)
1528
br_to.set_parent(pull_loc(branch_from))