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
20
from bzrlib.errors import BzrError, NoSuchRevision, NoCommits
24
class RevisionInfo(object):
25
"""The results of applying a revision specification to a branch.
27
An instance has two useful attributes: revno, and rev_id.
29
They can also be accessed as spec[0] and spec[1] respectively,
30
so that you can write code like:
31
revno, rev_id = RevisionSpec(branch, spec)
32
although this is probably going to be deprecated later.
34
This class exists mostly to be the return value of a RevisionSpec,
35
so that you can access the member you're interested in (number or id)
36
or treat the result as a tuple.
39
def __init__(self, branch, revno, rev_id=_marker):
43
# allow caller to be lazy
44
if self.revno is None:
47
self.rev_id = branch.get_rev_id(self.revno)
51
def __nonzero__(self):
52
# first the easy ones...
53
if self.rev_id is None:
55
if self.revno is not None:
57
# TODO: otherwise, it should depend on how I was built -
58
# if it's in_history(branch), then check revision_history(),
59
# if it's in_store(branch), do the check below
60
return self.rev_id in self.branch.revision_store
65
def __getitem__(self, index):
66
if index == 0: return self.revno
67
if index == 1: return self.rev_id
68
raise IndexError(index)
71
return self.branch.get_revision(self.rev_id)
73
def __eq__(self, other):
74
if type(other) not in (tuple, list, type(self)):
76
if type(other) is type(self) and self.branch is not other.branch:
78
return tuple(self) == tuple(other)
81
return '<bzrlib.revisionspec.RevisionInfo object %s, %s for %r>' % (
82
self.revno, self.rev_id, self.branch)
84
# classes in this list should have a "prefix" attribute, against which
85
# string specs are matched
88
class RevisionSpec(object):
89
"""A parsed revision specification.
91
A revision specification can be an integer, in which case it is
92
assumed to be a revno (though this will translate negative values
93
into positive ones); or it can be a string, in which case it is
94
parsed for something like 'date:' or 'revid:' etc.
96
Revision specs are an UI element, and they have been moved out
97
of the branch class to leave "back-end" classes unaware of such
98
details. Code that gets a revno or rev_id from other code should
99
not be using revision specs - revnos and revision ids are the
100
accepted ways to refer to revisions internally.
102
(Equivalent to the old Branch method get_revision_info())
107
def __new__(cls, spec, foo=_marker):
108
"""Parse a revision specifier.
111
return object.__new__(RevisionSpec, spec)
118
if isinstance(spec, int):
119
return object.__new__(RevisionSpec_int, spec)
120
elif isinstance(spec, basestring):
121
for spectype in SPEC_TYPES:
122
if spec.startswith(spectype.prefix):
123
return object.__new__(spectype, spec)
125
raise BzrError('No namespace registered for string: %r' %
128
raise TypeError('Unhandled revision type %s' % spec)
130
def __init__(self, spec):
131
if self.prefix and spec.startswith(self.prefix):
132
spec = spec[len(self.prefix):]
135
def _match_on(self, branch, revs):
136
return RevisionInfo(branch, 0, None)
138
def _match_on_and_check(self, branch, revs):
139
info = self._match_on(branch, revs)
142
elif info == (0, None):
143
# special case - the empty tree
146
raise NoSuchRevision(branch, self.prefix + str(self.spec))
148
raise NoSuchRevision(branch, str(self.spec))
150
def in_history(self, branch):
151
revs = branch.revision_history()
152
return self._match_on_and_check(branch, revs)
155
# this is mostly for helping with testing
156
return '<%s %s%s>' % (self.__class__.__name__,
163
class RevisionSpec_int(RevisionSpec):
164
"""Spec is a number. Special case."""
165
def __init__(self, spec):
166
self.spec = int(spec)
168
def _match_on(self, branch, revs):
170
revno = len(revs) + self.spec + 1
173
rev_id = branch.get_rev_id(revno, revs)
174
return RevisionInfo(branch, revno, rev_id)
177
class RevisionSpec_revno(RevisionSpec):
180
def _match_on(self, branch, revs):
181
"""Lookup a revision by revision number"""
183
return RevisionInfo(branch, int(self.spec))
185
return RevisionInfo(branch, None)
187
SPEC_TYPES.append(RevisionSpec_revno)
190
class RevisionSpec_revid(RevisionSpec):
193
def _match_on(self, branch, revs):
195
return RevisionInfo(branch, revs.index(self.spec) + 1, self.spec)
197
return RevisionInfo(branch, None)
199
SPEC_TYPES.append(RevisionSpec_revid)
202
class RevisionSpec_last(RevisionSpec):
206
def _match_on(self, branch, revs):
208
offset = int(self.spec)
210
return RevisionInfo(branch, None)
213
raise BzrError('You must supply a positive value for --revision last:XXX')
214
return RevisionInfo(branch, len(revs) - offset + 1)
216
SPEC_TYPES.append(RevisionSpec_last)
219
class RevisionSpec_before(RevisionSpec):
223
def _match_on(self, branch, revs):
224
r = RevisionSpec(self.spec)._match_on(branch, revs)
225
if (r.revno is None) or (r.revno == 0):
227
return RevisionInfo(branch, r.revno - 1)
229
SPEC_TYPES.append(RevisionSpec_before)
232
class RevisionSpec_tag(RevisionSpec):
235
def _match_on(self, branch, revs):
236
raise BzrError('tag: namespace registered, but not implemented.')
238
SPEC_TYPES.append(RevisionSpec_tag)
241
class RevisionSpec_date(RevisionSpec):
243
_date_re = re.compile(
244
r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
246
r'(?P<time>(?P<hour>\d\d):(?P<minute>\d\d)(:(?P<second>\d\d))?)?'
249
def _match_on(self, branch, revs):
251
Spec for date revisions:
253
value can be 'yesterday', 'today', 'tomorrow' or a YYYY-MM-DD string.
254
matches the first entry after a given date (either at midnight or
255
at a specified time).
257
So the proper way of saying 'give me all entries for today' is:
258
-r date:today..date:tomorrow
260
today = datetime.datetime.fromordinal(datetime.date.today().toordinal())
261
if self.spec.lower() == 'yesterday':
262
dt = today - datetime.timedelta(days=1)
263
elif self.spec.lower() == 'today':
265
elif self.spec.lower() == 'tomorrow':
266
dt = today + datetime.timedelta(days=1)
268
m = self._date_re.match(self.spec)
269
if not m or (not m.group('date') and not m.group('time')):
270
raise BzrError('Invalid revision date %r' % self.spec)
273
year, month, day = int(m.group('year')), int(m.group('month')), int(m.group('day'))
275
year, month, day = today.year, today.month, today.day
277
hour = int(m.group('hour'))
278
minute = int(m.group('minute'))
279
if m.group('second'):
280
second = int(m.group('second'))
284
hour, minute, second = 0,0,0
286
dt = datetime.datetime(year=year, month=month, day=day,
287
hour=hour, minute=minute, second=second)
289
for i in range(len(revs)):
290
r = branch.get_revision(revs[i])
291
# TODO: Handle timezone.
292
dt = datetime.datetime.fromtimestamp(r.timestamp)
294
return RevisionInfo(branch, i+1)
295
return RevisionInfo(branch, None)
297
SPEC_TYPES.append(RevisionSpec_date)
300
class RevisionSpec_ancestor(RevisionSpec):
303
def _match_on(self, branch, revs):
304
from branch import Branch
305
from revision import common_ancestor, MultipleRevisionSources
306
other_branch = Branch.open_containing(self.spec)
307
revision_a = branch.last_revision()
308
revision_b = other_branch.last_revision()
309
for r, b in ((revision_a, branch), (revision_b, other_branch)):
312
revision_source = MultipleRevisionSources(branch, other_branch)
313
rev_id = common_ancestor(revision_a, revision_b, revision_source)
315
revno = branch.revision_id_to_revno(rev_id)
316
except NoSuchRevision:
318
return RevisionInfo(branch, revno, rev_id)
320
SPEC_TYPES.append(RevisionSpec_ancestor)