140
137
"""Branch holding a history of revisions.
143
Base directory of the branch.
140
Base directory/url of the branch.
144
def __new__(cls, *a, **kw):
145
"""this is temporary, till we get rid of all code that does
148
# XXX: AAARGH! MY EYES! UUUUGLY!!!
151
b = object.__new__(cls)
156
"""Open an existing branch, rooted at 'base' (url)"""
157
if base and (base.startswith('http://') or base.startswith('https://')):
158
from bzrlib.remotebranch import RemoteBranch
159
return RemoteBranch(base, find_root=False)
161
return LocalBranch(base, find_root=False)
164
def open_containing(url):
165
"""Open an existing branch, containing url (search upwards for the root)
167
if url and (url.startswith('http://') or url.startswith('https://')):
168
from bzrlib.remotebranch import RemoteBranch
169
return RemoteBranch(url)
171
return LocalBranch(url)
174
def initialize(base):
175
"""Create a new branch, rooted at 'base' (url)"""
176
if base and (base.startswith('http://') or base.startswith('https://')):
177
from bzrlib.remotebranch import RemoteBranch
178
return RemoteBranch(base, init=True)
180
return LocalBranch(base, init=True)
183
class LocalBranch(Branch):
184
"""A branch stored in the actual filesystem.
186
Note that it's "local" in the context of the filesystem; it doesn't
187
really matter if it's on an nfs/smb/afs/coda/... share, as long as
188
it's writable, and can be accessed via the normal filesystem API.
146
191
None, or 'r' or 'w'
797
839
"""Pull in all new revisions from other branch.
799
841
from bzrlib.fetch import greedy_fetch
800
from bzrlib.revision import get_intervening_revisions
802
843
pb = bzrlib.ui.ui_factory.progress_bar()
803
844
pb.update('comparing histories')
804
if stop_revision is None:
805
other_revision = other.last_patch()
846
revision_ids = self.missing_revisions(other, stop_revision)
848
if len(revision_ids) > 0:
849
count = greedy_fetch(self, other, revision_ids[-1], pb)[0]
807
other_revision = other.lookup_revision(stop_revision)
808
count = greedy_fetch(self, other, other_revision, pb)[0]
810
revision_ids = self.missing_revisions(other, stop_revision)
811
except DivergedBranches, e:
813
revision_ids = get_intervening_revisions(self.last_patch(),
814
other_revision, self)
815
assert self.last_patch() not in revision_ids
816
except bzrlib.errors.NotAncestor:
819
852
self.append_revision(*revision_ids)
853
## note("Added %d revisions." % count)
822
856
def install_revisions(self, other, revision_ids, pb):
823
857
if hasattr(other.revision_store, "prefetch"):
824
858
other.revision_store.prefetch(revision_ids)
825
859
if hasattr(other.inventory_store, "prefetch"):
827
for rev_id in revision_ids:
829
revision = other.get_revision(rev_id).inventory_id
830
inventory_ids.append(revision)
831
except bzrlib.errors.NoSuchRevision:
860
inventory_ids = [other.get_revision(r).inventory_id
861
for r in revision_ids]
833
862
other.inventory_store.prefetch(inventory_ids)
917
927
raise bzrlib.errors.NoSuchRevision(self, revno)
918
928
return history[revno - 1]
920
def _get_revision_info(self, revision):
921
"""Return (revno, revision id) for revision specifier.
923
revision can be an integer, in which case it is assumed to be revno
924
(though this will translate negative values into positive ones)
925
revision can also be a string, in which case it is parsed for something
926
like 'date:' or 'revid:' etc.
928
A revid is always returned. If it is None, the specifier referred to
929
the null revision. If the revid does not occur in the revision
930
history, revno will be None.
936
try:# Convert to int if possible
937
revision = int(revision)
940
revs = self.revision_history()
941
if isinstance(revision, int):
943
revno = len(revs) + revision + 1
946
rev_id = self.get_rev_id(revno, revs)
947
elif isinstance(revision, basestring):
948
for prefix, func in Branch.REVISION_NAMESPACES.iteritems():
949
if revision.startswith(prefix):
950
result = func(self, revs, revision)
952
revno, rev_id = result
955
rev_id = self.get_rev_id(revno, revs)
958
raise BzrError('No namespace registered for string: %r' %
961
raise TypeError('Unhandled revision type %s' % revision)
965
raise bzrlib.errors.NoSuchRevision(self, revision)
968
def _namespace_revno(self, revs, revision):
969
"""Lookup a revision by revision number"""
970
assert revision.startswith('revno:')
972
return (int(revision[6:]),)
975
REVISION_NAMESPACES['revno:'] = _namespace_revno
977
def _namespace_revid(self, revs, revision):
978
assert revision.startswith('revid:')
979
rev_id = revision[len('revid:'):]
981
return revs.index(rev_id) + 1, rev_id
984
REVISION_NAMESPACES['revid:'] = _namespace_revid
986
def _namespace_last(self, revs, revision):
987
assert revision.startswith('last:')
989
offset = int(revision[5:])
994
raise BzrError('You must supply a positive value for --revision last:XXX')
995
return (len(revs) - offset + 1,)
996
REVISION_NAMESPACES['last:'] = _namespace_last
998
def _namespace_tag(self, revs, revision):
999
assert revision.startswith('tag:')
1000
raise BzrError('tag: namespace registered, but not implemented.')
1001
REVISION_NAMESPACES['tag:'] = _namespace_tag
1003
def _namespace_date(self, revs, revision):
1004
assert revision.startswith('date:')
1006
# Spec for date revisions:
1008
# value can be 'yesterday', 'today', 'tomorrow' or a YYYY-MM-DD string.
1009
# it can also start with a '+/-/='. '+' says match the first
1010
# entry after the given date. '-' is match the first entry before the date
1011
# '=' is match the first entry after, but still on the given date.
1013
# +2005-05-12 says find the first matching entry after May 12th, 2005 at 0:00
1014
# -2005-05-12 says find the first matching entry before May 12th, 2005 at 0:00
1015
# =2005-05-12 says find the first match after May 12th, 2005 at 0:00 but before
1016
# May 13th, 2005 at 0:00
1018
# So the proper way of saying 'give me all entries for today' is:
1019
# -r {date:+today}:{date:-tomorrow}
1020
# The default is '=' when not supplied
1023
if val[:1] in ('+', '-', '='):
1024
match_style = val[:1]
1027
today = datetime.datetime.today().replace(hour=0,minute=0,second=0,microsecond=0)
1028
if val.lower() == 'yesterday':
1029
dt = today - datetime.timedelta(days=1)
1030
elif val.lower() == 'today':
1032
elif val.lower() == 'tomorrow':
1033
dt = today + datetime.timedelta(days=1)
1036
# This should be done outside the function to avoid recompiling it.
1037
_date_re = re.compile(
1038
r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
1040
r'(?P<time>(?P<hour>\d\d):(?P<minute>\d\d)(:(?P<second>\d\d))?)?'
1042
m = _date_re.match(val)
1043
if not m or (not m.group('date') and not m.group('time')):
1044
raise BzrError('Invalid revision date %r' % revision)
1047
year, month, day = int(m.group('year')), int(m.group('month')), int(m.group('day'))
1049
year, month, day = today.year, today.month, today.day
1051
hour = int(m.group('hour'))
1052
minute = int(m.group('minute'))
1053
if m.group('second'):
1054
second = int(m.group('second'))
1058
hour, minute, second = 0,0,0
1060
dt = datetime.datetime(year=year, month=month, day=day,
1061
hour=hour, minute=minute, second=second)
1065
if match_style == '-':
1067
elif match_style == '=':
1068
last = dt + datetime.timedelta(days=1)
1071
for i in range(len(revs)-1, -1, -1):
1072
r = self.get_revision(revs[i])
1073
# TODO: Handle timezone.
1074
dt = datetime.datetime.fromtimestamp(r.timestamp)
1075
if first >= dt and (last is None or dt >= last):
1078
for i in range(len(revs)):
1079
r = self.get_revision(revs[i])
1080
# TODO: Handle timezone.
1081
dt = datetime.datetime.fromtimestamp(r.timestamp)
1082
if first <= dt and (last is None or dt <= last):
1084
REVISION_NAMESPACES['date:'] = _namespace_date
1087
def _namespace_ancestor(self, revs, revision):
1088
from revision import common_ancestor, MultipleRevisionSources
1089
other_branch = find_branch(_trim_namespace('ancestor', revision))
1090
revision_a = self.last_patch()
1091
revision_b = other_branch.last_patch()
1092
for r, b in ((revision_a, self), (revision_b, other_branch)):
1094
raise bzrlib.errors.NoCommits(b)
1095
revision_source = MultipleRevisionSources(self, other_branch)
1096
result = common_ancestor(revision_a, revision_b, revision_source)
1098
revno = self.revision_id_to_revno(result)
1099
except bzrlib.errors.NoSuchRevision:
1104
REVISION_NAMESPACES['ancestor:'] = _namespace_ancestor
1106
930
def revision_tree(self, revision_id):
1107
931
"""Return Tree for a revision on this branch.