1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
# Copyright (C) 2005 Canonical Ltd
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import datetime
import re
from bzrlib.errors import BzrError, NoSuchRevision, NoCommits
_marker = []
class RevisionInfo(object):
"""The results of applying a revision specification to a branch.
An instance has two useful attributes: revno, and rev_id.
They can also be accessed as spec[0] and spec[1] respectively,
so that you can write code like:
revno, rev_id = RevisionSpec(branch, spec)
although this is probably going to be deprecated later.
This class exists mostly to be the return value of a RevisionSpec,
so that you can access the member you're interested in (number or id)
or treat the result as a tuple.
"""
def __init__(self, branch, revno, rev_id=_marker):
self.branch = branch
self.revno = revno
if rev_id is _marker:
# allow caller to be lazy
if self.revno is None:
self.rev_id = None
else:
self.rev_id = branch.get_rev_id(self.revno)
else:
self.rev_id = rev_id
def __nonzero__(self):
# first the easy ones...
if self.rev_id is None:
return False
if self.revno is not None:
return True
# TODO: otherwise, it should depend on how I was built -
# if it's in_history(branch), then check revision_history(),
# if it's in_store(branch), do the check below
return self.branch.revision_store.has_id(self.rev_id)
def __len__(self):
return 2
def __getitem__(self, index):
if index == 0: return self.revno
if index == 1: return self.rev_id
raise IndexError(index)
def get(self):
return self.branch.get_revision(self.rev_id)
def __eq__(self, other):
if type(other) not in (tuple, list, type(self)):
return False
if type(other) is type(self) and self.branch is not other.branch:
return False
return tuple(self) == tuple(other)
def __repr__(self):
return '<bzrlib.revisionspec.RevisionInfo object %s, %s for %r>' % (
self.revno, self.rev_id, self.branch)
# classes in this list should have a "prefix" attribute, against which
# string specs are matched
SPEC_TYPES = []
class RevisionSpec(object):
"""A parsed revision specification.
A revision specification can be an integer, in which case it is
assumed to be a revno (though this will translate negative values
into positive ones); or it can be a string, in which case it is
parsed for something like 'date:' or 'revid:' etc.
Revision specs are an UI element, and they have been moved out
of the branch class to leave "back-end" classes unaware of such
details. Code that gets a revno or rev_id from other code should
not be using revision specs - revnos and revision ids are the
accepted ways to refer to revisions internally.
(Equivalent to the old Branch method get_revision_info())
"""
prefix = None
def __new__(cls, spec, foo=_marker):
"""Parse a revision specifier.
"""
if spec is None:
return object.__new__(RevisionSpec, spec)
try:
spec = int(spec)
except ValueError:
pass
if isinstance(spec, int):
return object.__new__(RevisionSpec_int, spec)
elif isinstance(spec, basestring):
for spectype in SPEC_TYPES:
if spec.startswith(spectype.prefix):
return object.__new__(spectype, spec)
else:
raise BzrError('No namespace registered for string: %r' %
spec)
else:
raise TypeError('Unhandled revision type %s' % spec)
def __init__(self, spec):
if self.prefix and spec.startswith(self.prefix):
spec = spec[len(self.prefix):]
self.spec = spec
def _match_on(self, branch, revs):
return RevisionInfo(branch, 0, None)
def _match_on_and_check(self, branch, revs):
info = self._match_on(branch, revs)
if info:
return info
elif info == (0, None):
# special case - the empty tree
return info
elif self.prefix:
raise NoSuchRevision(branch, self.prefix + str(self.spec))
else:
raise NoSuchRevision(branch, str(self.spec))
def in_history(self, branch):
revs = branch.revision_history()
return self._match_on_and_check(branch, revs)
# FIXME: in_history is somewhat broken,
# it will return non-history revisions in many
# circumstances. The expected facility is that
# in_history only returns revision-history revs,
# in_store returns any rev. RBC 20051010
# aliases for now, when we fix the core logic, then they
# will do what you expect.
in_store = in_history
in_branch = in_store
def __repr__(self):
# this is mostly for helping with testing
return '<%s %s%s>' % (self.__class__.__name__,
self.prefix or '',
self.spec)
# private API
class RevisionSpec_int(RevisionSpec):
"""Spec is a number. Special case."""
def __init__(self, spec):
self.spec = int(spec)
def _match_on(self, branch, revs):
if self.spec < 0:
revno = len(revs) + self.spec + 1
else:
revno = self.spec
rev_id = branch.get_rev_id(revno, revs)
return RevisionInfo(branch, revno, rev_id)
class RevisionSpec_revno(RevisionSpec):
prefix = 'revno:'
def _match_on(self, branch, revs):
"""Lookup a revision by revision number"""
try:
return RevisionInfo(branch, int(self.spec))
except ValueError:
return RevisionInfo(branch, None)
SPEC_TYPES.append(RevisionSpec_revno)
class RevisionSpec_revid(RevisionSpec):
prefix = 'revid:'
def _match_on(self, branch, revs):
try:
return RevisionInfo(branch, revs.index(self.spec) + 1, self.spec)
except ValueError:
return RevisionInfo(branch, None)
SPEC_TYPES.append(RevisionSpec_revid)
class RevisionSpec_last(RevisionSpec):
prefix = 'last:'
def _match_on(self, branch, revs):
try:
offset = int(self.spec)
except ValueError:
return RevisionInfo(branch, None)
else:
if offset <= 0:
raise BzrError('You must supply a positive value for --revision last:XXX')
return RevisionInfo(branch, len(revs) - offset + 1)
SPEC_TYPES.append(RevisionSpec_last)
class RevisionSpec_before(RevisionSpec):
prefix = 'before:'
def _match_on(self, branch, revs):
r = RevisionSpec(self.spec)._match_on(branch, revs)
if (r.revno is None) or (r.revno == 0):
return r
return RevisionInfo(branch, r.revno - 1)
SPEC_TYPES.append(RevisionSpec_before)
class RevisionSpec_tag(RevisionSpec):
prefix = 'tag:'
def _match_on(self, branch, revs):
raise BzrError('tag: namespace registered, but not implemented.')
SPEC_TYPES.append(RevisionSpec_tag)
class RevisionSpec_date(RevisionSpec):
prefix = 'date:'
_date_re = re.compile(
r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
r'(,|T)?\s*'
r'(?P<time>(?P<hour>\d\d):(?P<minute>\d\d)(:(?P<second>\d\d))?)?'
)
def _match_on(self, branch, revs):
"""
Spec for date revisions:
date:value
value can be 'yesterday', 'today', 'tomorrow' or a YYYY-MM-DD string.
matches the first entry after a given date (either at midnight or
at a specified time).
So the proper way of saying 'give me all entries for today' is:
-r date:today..date:tomorrow
"""
today = datetime.datetime.fromordinal(datetime.date.today().toordinal())
if self.spec.lower() == 'yesterday':
dt = today - datetime.timedelta(days=1)
elif self.spec.lower() == 'today':
dt = today
elif self.spec.lower() == 'tomorrow':
dt = today + datetime.timedelta(days=1)
else:
m = self._date_re.match(self.spec)
if not m or (not m.group('date') and not m.group('time')):
raise BzrError('Invalid revision date %r' % self.spec)
if m.group('date'):
year, month, day = int(m.group('year')), int(m.group('month')), int(m.group('day'))
else:
year, month, day = today.year, today.month, today.day
if m.group('time'):
hour = int(m.group('hour'))
minute = int(m.group('minute'))
if m.group('second'):
second = int(m.group('second'))
else:
second = 0
else:
hour, minute, second = 0,0,0
dt = datetime.datetime(year=year, month=month, day=day,
hour=hour, minute=minute, second=second)
first = dt
for i in range(len(revs)):
r = branch.get_revision(revs[i])
# TODO: Handle timezone.
dt = datetime.datetime.fromtimestamp(r.timestamp)
if first <= dt:
return RevisionInfo(branch, i+1)
return RevisionInfo(branch, None)
SPEC_TYPES.append(RevisionSpec_date)
class RevisionSpec_ancestor(RevisionSpec):
prefix = 'ancestor:'
def _match_on(self, branch, revs):
from branch import Branch
from revision import common_ancestor, MultipleRevisionSources
other_branch = Branch.open_containing(self.spec)
revision_a = branch.last_revision()
revision_b = other_branch.last_revision()
for r, b in ((revision_a, branch), (revision_b, other_branch)):
if r is None:
raise NoCommits(b)
revision_source = MultipleRevisionSources(branch, other_branch)
rev_id = common_ancestor(revision_a, revision_b, revision_source)
try:
revno = branch.revision_id_to_revno(rev_id)
except NoSuchRevision:
revno = None
return RevisionInfo(branch, revno, rev_id)
SPEC_TYPES.append(RevisionSpec_ancestor)
class RevisionSpec_branch(RevisionSpec):
"""A branch: revision specifier.
This takes the path to a branch and returns its tip revision id.
"""
prefix = 'branch:'
def _match_on(self, branch, revs):
from branch import Branch
from fetch import greedy_fetch
other_branch = Branch.open_containing(self.spec)
revision_b = other_branch.last_revision()
if revision_b is None:
raise NoCommits(other_branch)
# pull in the remote revisions so we can diff
greedy_fetch(branch, other_branch, revision=revision_b)
try:
revno = branch.revision_id_to_revno(revision_b)
except NoSuchRevision:
revno = None
return RevisionInfo(branch, revno, revision_b)
SPEC_TYPES.append(RevisionSpec_branch)
|