1
# Copyright (C) 2005 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
from bzrlib.errors import BzrError, NoSuchRevision, NoCommits
25
class RevisionInfo(object):
26
"""The results of applying a revision specification to a branch.
28
An instance has two useful attributes: revno, and rev_id.
30
They can also be accessed as spec[0] and spec[1] respectively,
31
so that you can write code like:
32
revno, rev_id = RevisionSpec(branch, spec)
33
although this is probably going to be deprecated later.
35
This class exists mostly to be the return value of a RevisionSpec,
36
so that you can access the member you're interested in (number or id)
37
or treat the result as a tuple.
40
def __init__(self, branch, revno, rev_id=_marker):
44
# allow caller to be lazy
45
if self.revno is None:
48
self.rev_id = branch.get_rev_id(self.revno)
52
def __nonzero__(self):
53
# first the easy ones...
54
if self.rev_id is None:
56
if self.revno is not None:
58
# TODO: otherwise, it should depend on how I was built -
59
# if it's in_history(branch), then check revision_history(),
60
# if it's in_store(branch), do the check below
61
return self.branch.repository.has_revision(self.rev_id)
66
def __getitem__(self, index):
67
if index == 0: return self.revno
68
if index == 1: return self.rev_id
69
raise IndexError(index)
72
return self.branch.repository.get_revision(self.rev_id)
74
def __eq__(self, other):
75
if type(other) not in (tuple, list, type(self)):
77
if type(other) is type(self) and self.branch is not other.branch:
79
return tuple(self) == tuple(other)
82
return '<bzrlib.revisionspec.RevisionInfo object %s, %s for %r>' % (
83
self.revno, self.rev_id, self.branch)
85
# classes in this list should have a "prefix" attribute, against which
86
# string specs are matched
89
class RevisionSpec(object):
90
"""A parsed revision specification.
92
A revision specification can be an integer, in which case it is
93
assumed to be a revno (though this will translate negative values
94
into positive ones); or it can be a string, in which case it is
95
parsed for something like 'date:' or 'revid:' etc.
97
Revision specs are an UI element, and they have been moved out
98
of the branch class to leave "back-end" classes unaware of such
99
details. Code that gets a revno or rev_id from other code should
100
not be using revision specs - revnos and revision ids are the
101
accepted ways to refer to revisions internally.
103
(Equivalent to the old Branch method get_revision_info())
108
def __new__(cls, spec, foo=_marker):
109
"""Parse a revision specifier.
112
return object.__new__(RevisionSpec, spec)
119
if isinstance(spec, int):
120
return object.__new__(RevisionSpec_int, spec)
121
elif isinstance(spec, basestring):
122
for spectype in SPEC_TYPES:
123
if spec.startswith(spectype.prefix):
124
return object.__new__(spectype, spec)
126
raise BzrError('No namespace registered for string: %r' %
129
raise TypeError('Unhandled revision type %s' % spec)
131
def __init__(self, spec):
132
if self.prefix and spec.startswith(self.prefix):
133
spec = spec[len(self.prefix):]
136
def _match_on(self, branch, revs):
137
return RevisionInfo(branch, 0, None)
139
def _match_on_and_check(self, branch, revs):
140
info = self._match_on(branch, revs)
143
elif info == (0, None):
144
# special case - the empty tree
147
raise NoSuchRevision(branch, self.prefix + str(self.spec))
149
raise NoSuchRevision(branch, str(self.spec))
151
def in_history(self, branch):
153
revs = branch.revision_history()
156
return self._match_on_and_check(branch, revs)
158
# FIXME: in_history is somewhat broken,
159
# it will return non-history revisions in many
160
# circumstances. The expected facility is that
161
# in_history only returns revision-history revs,
162
# in_store returns any rev. RBC 20051010
163
# aliases for now, when we fix the core logic, then they
164
# will do what you expect.
165
in_store = in_history
169
# this is mostly for helping with testing
170
return '<%s %s%s>' % (self.__class__.__name__,
174
def needs_branch(self):
175
"""Whether this revision spec needs a branch.
177
Set this to False the branch argument of _match_on is not used.
183
class RevisionSpec_int(RevisionSpec):
184
"""Spec is a number. Special case."""
185
def __init__(self, spec):
186
self.spec = int(spec)
188
def _match_on(self, branch, revs):
190
revno = len(revs) + self.spec + 1
193
rev_id = branch.get_rev_id(revno, revs)
194
return RevisionInfo(branch, revno, rev_id)
197
class RevisionSpec_revno(RevisionSpec):
200
def _match_on(self, branch, revs):
201
"""Lookup a revision by revision number"""
202
if self.spec.find(':') == -1:
204
return RevisionInfo(branch, int(self.spec))
206
return RevisionInfo(branch, None)
208
from branch import Branch
209
revname = self.spec[self.spec.find(':')+1:]
210
other_branch = Branch.open_containing(revname)[0]
212
revno = int(self.spec[:self.spec.find(':')])
214
return RevisionInfo(other_branch, None)
215
revid = other_branch.get_rev_id(revno)
216
return RevisionInfo(other_branch, revno)
218
def needs_branch(self):
219
return self.spec.find(':') == -1
221
SPEC_TYPES.append(RevisionSpec_revno)
224
class RevisionSpec_revid(RevisionSpec):
227
def _match_on(self, branch, revs):
229
return RevisionInfo(branch, revs.index(self.spec) + 1, self.spec)
231
return RevisionInfo(branch, None, self.spec)
233
SPEC_TYPES.append(RevisionSpec_revid)
236
class RevisionSpec_last(RevisionSpec):
240
def _match_on(self, branch, revs):
242
offset = int(self.spec)
244
return RevisionInfo(branch, None)
247
raise BzrError('You must supply a positive value for --revision last:XXX')
248
return RevisionInfo(branch, len(revs) - offset + 1)
250
SPEC_TYPES.append(RevisionSpec_last)
253
class RevisionSpec_before(RevisionSpec):
257
def _match_on(self, branch, revs):
258
r = RevisionSpec(self.spec)._match_on(branch, revs)
259
if (r.revno is None) or (r.revno == 0):
261
return RevisionInfo(branch, r.revno - 1)
263
SPEC_TYPES.append(RevisionSpec_before)
266
class RevisionSpec_tag(RevisionSpec):
269
def _match_on(self, branch, revs):
270
raise BzrError('tag: namespace registered, but not implemented.')
272
SPEC_TYPES.append(RevisionSpec_tag)
275
class RevisionSpec_revs:
276
def __init__(self, revs, branch):
279
def __getitem__(self, index):
280
r = self.branch.repository.get_revision(self.revs[index])
281
# TODO: Handle timezone.
282
return datetime.datetime.fromtimestamp(r.timestamp)
284
return len(self.revs)
287
class RevisionSpec_date(RevisionSpec):
289
_date_re = re.compile(
290
r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
292
r'(?P<time>(?P<hour>\d\d):(?P<minute>\d\d)(:(?P<second>\d\d))?)?'
295
def _match_on(self, branch, revs):
297
Spec for date revisions:
299
value can be 'yesterday', 'today', 'tomorrow' or a YYYY-MM-DD string.
300
matches the first entry after a given date (either at midnight or
301
at a specified time).
303
So the proper way of saying 'give me all entries for today' is:
304
-r date:yesterday..date:today
306
today = datetime.datetime.fromordinal(datetime.date.today().toordinal())
307
if self.spec.lower() == 'yesterday':
308
dt = today - datetime.timedelta(days=1)
309
elif self.spec.lower() == 'today':
311
elif self.spec.lower() == 'tomorrow':
312
dt = today + datetime.timedelta(days=1)
314
m = self._date_re.match(self.spec)
315
if not m or (not m.group('date') and not m.group('time')):
316
raise BzrError('Invalid revision date %r' % self.spec)
319
year, month, day = int(m.group('year')), int(m.group('month')), int(m.group('day'))
321
year, month, day = today.year, today.month, today.day
323
hour = int(m.group('hour'))
324
minute = int(m.group('minute'))
325
if m.group('second'):
326
second = int(m.group('second'))
330
hour, minute, second = 0,0,0
332
dt = datetime.datetime(year=year, month=month, day=day,
333
hour=hour, minute=minute, second=second)
336
rev = bisect.bisect(RevisionSpec_revs(revs, branch), dt)
340
return RevisionInfo(branch, None)
342
return RevisionInfo(branch, rev + 1)
344
SPEC_TYPES.append(RevisionSpec_date)
347
class RevisionSpec_ancestor(RevisionSpec):
350
def _match_on(self, branch, revs):
351
from branch import Branch
352
from revision import common_ancestor, MultipleRevisionSources
353
other_branch = Branch.open_containing(self.spec)[0]
354
revision_a = branch.last_revision()
355
revision_b = other_branch.last_revision()
356
for r, b in ((revision_a, branch), (revision_b, other_branch)):
359
revision_source = MultipleRevisionSources(branch.repository,
360
other_branch.repository)
361
rev_id = common_ancestor(revision_a, revision_b, revision_source)
363
revno = branch.revision_id_to_revno(rev_id)
364
except NoSuchRevision:
366
return RevisionInfo(branch, revno, rev_id)
368
SPEC_TYPES.append(RevisionSpec_ancestor)
370
class RevisionSpec_branch(RevisionSpec):
371
"""A branch: revision specifier.
373
This takes the path to a branch and returns its tip revision id.
377
def _match_on(self, branch, revs):
378
from branch import Branch
379
other_branch = Branch.open_containing(self.spec)[0]
380
revision_b = other_branch.last_revision()
381
if revision_b is None:
382
raise NoCommits(other_branch)
383
# pull in the remote revisions so we can diff
384
branch.fetch(other_branch, revision_b)
386
revno = branch.revision_id_to_revno(revision_b)
387
except NoSuchRevision:
389
return RevisionInfo(branch, revno, revision_b)
391
SPEC_TYPES.append(RevisionSpec_branch)