43
43
# repeatedly to calculate deltas. We could perhaps have a weakref
44
44
# cache in memory to make this faster.
46
# TODO: please move the revision-string syntax stuff out of the branch
47
# object; it's clutter
50
def find_branch(f, **args):
51
if f and (f.startswith('http://') or f.startswith('https://')):
53
return remotebranch.RemoteBranch(f, **args)
55
return Branch(f, **args)
58
def find_cached_branch(f, cache_root, **args):
59
from remotebranch import RemoteBranch
60
br = find_branch(f, **args)
61
def cacheify(br, store_name):
62
from meta_store import CachedStore
63
cache_path = os.path.join(cache_root, store_name)
65
new_store = CachedStore(getattr(br, store_name), cache_path)
66
setattr(br, store_name, new_store)
68
if isinstance(br, RemoteBranch):
69
cacheify(br, 'inventory_store')
70
cacheify(br, 'text_store')
71
cacheify(br, 'revision_store')
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')
75
51
def _relpath(base, path):
76
52
"""Return path relative to base, or raise exception.
148
116
"""Branch holding a history of revisions.
151
Base directory of the branch.
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
168
None, or 'r' or 'w'
703
708
def common_ancestor(self, other, self_revno=None, other_revno=None):
710
>>> from bzrlib.commit import commit
706
711
>>> sb = ScratchBranch(files=['foo', 'foo~'])
707
712
>>> sb.common_ancestor(sb) == (None, None)
709
>>> commit.commit(sb, "Committing first revision", verbose=False)
714
>>> commit(sb, "Committing first revision", verbose=False)
710
715
>>> sb.common_ancestor(sb)[0]
712
717
>>> clone = sb.clone()
713
>>> commit.commit(sb, "Committing second revision", verbose=False)
718
>>> commit(sb, "Committing second revision", verbose=False)
714
719
>>> sb.common_ancestor(sb)[0]
716
721
>>> sb.common_ancestor(clone)[0]
718
>>> commit.commit(clone, "Committing divergent second revision",
723
>>> commit(clone, "Committing divergent second revision",
719
724
... verbose=False)
720
725
>>> sb.common_ancestor(clone)[0]
812
817
"""Pull in all new revisions from other branch.
814
819
from bzrlib.fetch import greedy_fetch
820
from bzrlib.revision import get_intervening_revisions
816
822
pb = bzrlib.ui.ui_factory.progress_bar()
817
823
pb.update('comparing histories')
819
revision_ids = self.missing_revisions(other, stop_revision)
821
if len(revision_ids) > 0:
822
count = greedy_fetch(self, other, revision_ids[-1], pb)[0]
824
if stop_revision is None:
825
other_revision = other.last_patch()
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:
825
839
self.append_revision(*revision_ids)
826
## note("Added %d revisions." % count)
829
842
def install_revisions(self, other, revision_ids, pb):
830
843
if hasattr(other.revision_store, "prefetch"):
831
844
other.revision_store.prefetch(revision_ids)
832
845
if hasattr(other.inventory_store, "prefetch"):
833
inventory_ids = [other.get_revision(r).inventory_id
834
for r in revision_ids]
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:
835
853
other.inventory_store.prefetch(inventory_ids)
919
919
raise bzrlib.errors.NoSuchRevision(self, revno)
920
920
return history[revno - 1]
922
def _get_revision_info(self, revision):
923
"""Return (revno, revision id) for revision specifier.
925
revision can be an integer, in which case it is assumed to be revno
926
(though this will translate negative values into positive ones)
927
revision can also be a string, in which case it is parsed for something
928
like 'date:' or 'revid:' etc.
930
A revid is always returned. If it is None, the specifier referred to
931
the null revision. If the revid does not occur in the revision
932
history, revno will be None.
938
try:# Convert to int if possible
939
revision = int(revision)
942
revs = self.revision_history()
943
if isinstance(revision, int):
945
revno = len(revs) + revision + 1
948
rev_id = self.get_rev_id(revno, revs)
949
elif isinstance(revision, basestring):
950
for prefix, func in Branch.REVISION_NAMESPACES.iteritems():
951
if revision.startswith(prefix):
952
result = func(self, revs, revision)
954
revno, rev_id = result
957
rev_id = self.get_rev_id(revno, revs)
960
raise BzrError('No namespace registered for string: %r' %
963
raise TypeError('Unhandled revision type %s' % revision)
967
raise bzrlib.errors.NoSuchRevision(self, revision)
970
def _namespace_revno(self, revs, revision):
971
"""Lookup a revision by revision number"""
972
assert revision.startswith('revno:')
974
return (int(revision[6:]),)
977
REVISION_NAMESPACES['revno:'] = _namespace_revno
979
def _namespace_revid(self, revs, revision):
980
assert revision.startswith('revid:')
981
rev_id = revision[len('revid:'):]
983
return revs.index(rev_id) + 1, rev_id
986
REVISION_NAMESPACES['revid:'] = _namespace_revid
988
def _namespace_last(self, revs, revision):
989
assert revision.startswith('last:')
991
offset = int(revision[5:])
996
raise BzrError('You must supply a positive value for --revision last:XXX')
997
return (len(revs) - offset + 1,)
998
REVISION_NAMESPACES['last:'] = _namespace_last
1000
def _namespace_tag(self, revs, revision):
1001
assert revision.startswith('tag:')
1002
raise BzrError('tag: namespace registered, but not implemented.')
1003
REVISION_NAMESPACES['tag:'] = _namespace_tag
1005
def _namespace_date(self, revs, revision):
1006
assert revision.startswith('date:')
1008
# Spec for date revisions:
1010
# value can be 'yesterday', 'today', 'tomorrow' or a YYYY-MM-DD string.
1011
# it can also start with a '+/-/='. '+' says match the first
1012
# entry after the given date. '-' is match the first entry before the date
1013
# '=' is match the first entry after, but still on the given date.
1015
# +2005-05-12 says find the first matching entry after May 12th, 2005 at 0:00
1016
# -2005-05-12 says find the first matching entry before May 12th, 2005 at 0:00
1017
# =2005-05-12 says find the first match after May 12th, 2005 at 0:00 but before
1018
# May 13th, 2005 at 0:00
1020
# So the proper way of saying 'give me all entries for today' is:
1021
# -r {date:+today}:{date:-tomorrow}
1022
# The default is '=' when not supplied
1025
if val[:1] in ('+', '-', '='):
1026
match_style = val[:1]
1029
today = datetime.datetime.today().replace(hour=0,minute=0,second=0,microsecond=0)
1030
if val.lower() == 'yesterday':
1031
dt = today - datetime.timedelta(days=1)
1032
elif val.lower() == 'today':
1034
elif val.lower() == 'tomorrow':
1035
dt = today + datetime.timedelta(days=1)
1038
# This should be done outside the function to avoid recompiling it.
1039
_date_re = re.compile(
1040
r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
1042
r'(?P<time>(?P<hour>\d\d):(?P<minute>\d\d)(:(?P<second>\d\d))?)?'
1044
m = _date_re.match(val)
1045
if not m or (not m.group('date') and not m.group('time')):
1046
raise BzrError('Invalid revision date %r' % revision)
1049
year, month, day = int(m.group('year')), int(m.group('month')), int(m.group('day'))
1051
year, month, day = today.year, today.month, today.day
1053
hour = int(m.group('hour'))
1054
minute = int(m.group('minute'))
1055
if m.group('second'):
1056
second = int(m.group('second'))
1060
hour, minute, second = 0,0,0
1062
dt = datetime.datetime(year=year, month=month, day=day,
1063
hour=hour, minute=minute, second=second)
1067
if match_style == '-':
1069
elif match_style == '=':
1070
last = dt + datetime.timedelta(days=1)
1073
for i in range(len(revs)-1, -1, -1):
1074
r = self.get_revision(revs[i])
1075
# TODO: Handle timezone.
1076
dt = datetime.datetime.fromtimestamp(r.timestamp)
1077
if first >= dt and (last is None or dt >= last):
1080
for i in range(len(revs)):
1081
r = self.get_revision(revs[i])
1082
# TODO: Handle timezone.
1083
dt = datetime.datetime.fromtimestamp(r.timestamp)
1084
if first <= dt and (last is None or dt <= last):
1086
REVISION_NAMESPACES['date:'] = _namespace_date
1088
923
def revision_tree(self, revision_id):
1089
924
"""Return Tree for a revision on this branch.
1323
class ScratchBranch(Branch):
1161
def get_parent(self):
1162
"""Return the parent location of the branch.
1164
This is the default location for push/pull/missing. The usual
1165
pattern is that the user can override it by specifying a
1169
_locs = ['parent', 'pull', 'x-pull']
1172
return self.controlfile(l, 'r').read().strip('\n')
1174
if e.errno != errno.ENOENT:
1179
def set_parent(self, url):
1180
# TODO: Maybe delete old location files?
1181
from bzrlib.atomicfile import AtomicFile
1184
f = AtomicFile(self.controlfilename('parent'))
1193
def check_revno(self, revno):
1195
Check whether a revno corresponds to any revision.
1196
Zero (the NULL revision) is considered valid.
1199
self.check_real_revno(revno)
1201
def check_real_revno(self, revno):
1203
Check whether a revno corresponds to a real revision.
1204
Zero (the NULL revision) is considered invalid
1206
if revno < 1 or revno > self.revno():
1207
raise InvalidRevisionNumber(revno)
1213
class ScratchBranch(LocalBranch):
1324
1214
"""Special test class: a branch that cleans up after itself.
1326
1216
>>> b = ScratchBranch()
1442
1334
return gen_file_id('TREE_ROOT')
1445
def pull_loc(branch):
1446
# TODO: Should perhaps just make attribute be 'base' in
1447
# RemoteBranch and Branch?
1448
if hasattr(branch, "baseurl"):
1449
return branch.baseurl
1454
1337
def copy_branch(branch_from, to_location, revision=None):
1455
1338
"""Copy branch_from into the existing directory to_location.
1457
If revision is not None, the head of the new branch will be revision.
1341
If not None, only revisions up to this point will be copied.
1342
The head of the new branch will be that revision.
1345
The name of a local directory that exists but is empty.
1459
1347
from bzrlib.merge import merge
1460
from bzrlib.branch import Branch
1461
br_to = Branch(to_location, init=True)
1348
from bzrlib.revisionspec import RevisionSpec
1350
assert isinstance(branch_from, Branch)
1351
assert isinstance(to_location, basestring)
1353
br_to = Branch.initialize(to_location)
1462
1354
br_to.set_root_id(branch_from.get_root_id())
1463
1355
if revision is None:
1464
1356
revno = branch_from.revno()
1466
revno, rev_id = branch_from.get_revision_info(revision)
1358
revno, rev_id = RevisionSpec(revision).in_history(branch_from)
1467
1359
br_to.update_revisions(branch_from, stop_revision=revno)
1468
1360
merge((to_location, -1), (to_location, 0), this_dir=to_location,
1469
1361
check_clean=False, ignore_zero=True)
1470
from_location = pull_loc(branch_from)
1471
br_to.controlfile("x-pull", "wb").write(from_location + "\n")
1362
br_to.set_parent(branch_from.base)