43
43
# repeatedly to calculate deltas. We could perhaps have a weakref
44
44
# 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')
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://')):
52
from bzrlib.remotebranch import RemoteBranch
53
return RemoteBranch(f, **args)
55
return Branch(f, **args)
58
def find_cached_branch(f, cache_root, **args):
59
from bzrlib.remotebranch import RemoteBranch
60
br = find_branch(f, **args)
61
def cacheify(br, store_name):
62
from bzrlib.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')
51
75
def _relpath(base, path):
52
76
"""Return path relative to base, or raise exception.
116
140
"""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.
143
Base directory of the branch.
168
146
None, or 'r' or 'w'
817
796
"""Pull in all new revisions from other branch.
819
798
from bzrlib.fetch import greedy_fetch
820
from bzrlib.revision import get_intervening_revisions
822
800
pb = bzrlib.ui.ui_factory.progress_bar()
823
801
pb.update('comparing histories')
824
if stop_revision is None:
825
other_revision = other.last_patch()
803
revision_ids = self.missing_revisions(other, stop_revision)
805
if len(revision_ids) > 0:
806
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
809
self.append_revision(*revision_ids)
810
## note("Added %d revisions." % count)
842
813
def install_revisions(self, other, revision_ids, pb):
843
814
if hasattr(other.revision_store, "prefetch"):
844
815
other.revision_store.prefetch(revision_ids)
845
816
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:
817
inventory_ids = [other.get_revision(r).inventory_id
818
for r in revision_ids]
853
819
other.inventory_store.prefetch(inventory_ids)
916
903
raise bzrlib.errors.NoSuchRevision(self, revno)
917
904
return history[revno - 1]
906
def _get_revision_info(self, revision):
907
"""Return (revno, revision id) for revision specifier.
909
revision can be an integer, in which case it is assumed to be revno
910
(though this will translate negative values into positive ones)
911
revision can also be a string, in which case it is parsed for something
912
like 'date:' or 'revid:' etc.
914
A revid is always returned. If it is None, the specifier referred to
915
the null revision. If the revid does not occur in the revision
916
history, revno will be None.
922
try:# Convert to int if possible
923
revision = int(revision)
926
revs = self.revision_history()
927
if isinstance(revision, int):
929
revno = len(revs) + revision + 1
932
rev_id = self.get_rev_id(revno, revs)
933
elif isinstance(revision, basestring):
934
for prefix, func in Branch.REVISION_NAMESPACES.iteritems():
935
if revision.startswith(prefix):
936
result = func(self, revs, revision)
938
revno, rev_id = result
941
rev_id = self.get_rev_id(revno, revs)
944
raise BzrError('No namespace registered for string: %r' %
947
raise TypeError('Unhandled revision type %s' % revision)
951
raise bzrlib.errors.NoSuchRevision(self, revision)
954
def _namespace_revno(self, revs, revision):
955
"""Lookup a revision by revision number"""
956
assert revision.startswith('revno:')
958
return (int(revision[6:]),)
961
REVISION_NAMESPACES['revno:'] = _namespace_revno
963
def _namespace_revid(self, revs, revision):
964
assert revision.startswith('revid:')
965
rev_id = revision[len('revid:'):]
967
return revs.index(rev_id) + 1, rev_id
970
REVISION_NAMESPACES['revid:'] = _namespace_revid
972
def _namespace_last(self, revs, revision):
973
assert revision.startswith('last:')
975
offset = int(revision[5:])
980
raise BzrError('You must supply a positive value for --revision last:XXX')
981
return (len(revs) - offset + 1,)
982
REVISION_NAMESPACES['last:'] = _namespace_last
984
def _namespace_tag(self, revs, revision):
985
assert revision.startswith('tag:')
986
raise BzrError('tag: namespace registered, but not implemented.')
987
REVISION_NAMESPACES['tag:'] = _namespace_tag
989
def _namespace_date(self, revs, revision):
990
assert revision.startswith('date:')
992
# Spec for date revisions:
994
# value can be 'yesterday', 'today', 'tomorrow' or a YYYY-MM-DD string.
995
# it can also start with a '+/-/='. '+' says match the first
996
# entry after the given date. '-' is match the first entry before the date
997
# '=' is match the first entry after, but still on the given date.
999
# +2005-05-12 says find the first matching entry after May 12th, 2005 at 0:00
1000
# -2005-05-12 says find the first matching entry before May 12th, 2005 at 0:00
1001
# =2005-05-12 says find the first match after May 12th, 2005 at 0:00 but before
1002
# May 13th, 2005 at 0:00
1004
# So the proper way of saying 'give me all entries for today' is:
1005
# -r {date:+today}:{date:-tomorrow}
1006
# The default is '=' when not supplied
1009
if val[:1] in ('+', '-', '='):
1010
match_style = val[:1]
1013
today = datetime.datetime.today().replace(hour=0,minute=0,second=0,microsecond=0)
1014
if val.lower() == 'yesterday':
1015
dt = today - datetime.timedelta(days=1)
1016
elif val.lower() == 'today':
1018
elif val.lower() == 'tomorrow':
1019
dt = today + datetime.timedelta(days=1)
1022
# This should be done outside the function to avoid recompiling it.
1023
_date_re = re.compile(
1024
r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
1026
r'(?P<time>(?P<hour>\d\d):(?P<minute>\d\d)(:(?P<second>\d\d))?)?'
1028
m = _date_re.match(val)
1029
if not m or (not m.group('date') and not m.group('time')):
1030
raise BzrError('Invalid revision date %r' % revision)
1033
year, month, day = int(m.group('year')), int(m.group('month')), int(m.group('day'))
1035
year, month, day = today.year, today.month, today.day
1037
hour = int(m.group('hour'))
1038
minute = int(m.group('minute'))
1039
if m.group('second'):
1040
second = int(m.group('second'))
1044
hour, minute, second = 0,0,0
1046
dt = datetime.datetime(year=year, month=month, day=day,
1047
hour=hour, minute=minute, second=second)
1051
if match_style == '-':
1053
elif match_style == '=':
1054
last = dt + datetime.timedelta(days=1)
1057
for i in range(len(revs)-1, -1, -1):
1058
r = self.get_revision(revs[i])
1059
# TODO: Handle timezone.
1060
dt = datetime.datetime.fromtimestamp(r.timestamp)
1061
if first >= dt and (last is None or dt >= last):
1064
for i in range(len(revs)):
1065
r = self.get_revision(revs[i])
1066
# TODO: Handle timezone.
1067
dt = datetime.datetime.fromtimestamp(r.timestamp)
1068
if first <= dt and (last is None or dt <= last):
1070
REVISION_NAMESPACES['date:'] = _namespace_date
920
1072
def revision_tree(self, revision_id):
921
1073
"""Return Tree for a revision on this branch.
1342
1493
The name of a local directory that exists but is empty.
1344
1495
from bzrlib.merge import merge
1345
from bzrlib.revisionspec import RevisionSpec
1347
1497
assert isinstance(branch_from, Branch)
1348
1498
assert isinstance(to_location, basestring)
1350
br_to = Branch.initialize(to_location)
1500
br_to = Branch(to_location, init=True)
1351
1501
br_to.set_root_id(branch_from.get_root_id())
1352
1502
if revision is None:
1353
1503
revno = branch_from.revno()
1355
revno, rev_id = RevisionSpec(revision).in_history(branch_from)
1505
revno, rev_id = branch_from.get_revision_info(revision)
1356
1506
br_to.update_revisions(branch_from, stop_revision=revno)
1357
1507
merge((to_location, -1), (to_location, 0), this_dir=to_location,
1358
1508
check_clean=False, ignore_zero=True)
1510
from_location = branch_from.base
1359
1511
br_to.set_parent(branch_from.base)