108
def __new__(cls, spec, foo=_marker):
109
"""Parse a revision specifier.
119
def __new__(cls, spec, _internal=False):
121
return object.__new__(cls, spec, _internal=_internal)
123
symbol_versioning.warn('Creating a RevisionSpec directly has'
124
' been deprecated in version 0.11. Use'
125
' RevisionSpec.from_string()'
127
DeprecationWarning, stacklevel=2)
128
return RevisionSpec.from_string(spec)
131
def from_string(spec):
132
"""Parse a revision spec string into a RevisionSpec object.
134
:param spec: A string specified by the user
135
:return: A RevisionSpec object that understands how to parse the
138
if not isinstance(spec, (type(None), basestring)):
139
raise TypeError('error')
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' %
142
return RevisionSpec(None, _internal=True)
144
assert isinstance(spec, basestring), \
145
"You should only supply strings not %s" % (type(spec),)
147
for spectype in SPEC_TYPES:
148
if spec.startswith(spectype.prefix):
149
trace.mutter('Returning RevisionSpec %s for %s',
150
spectype.__name__, spec)
151
return spectype(spec, _internal=True)
129
raise TypeError('Unhandled revision type %s' % spec)
131
def __init__(self, spec):
153
# RevisionSpec_revno is special cased, because it is the only
154
# one that directly handles plain integers
156
if _revno_regex is None:
157
_revno_regex = re.compile(r'-?\d+(:.*)?$')
158
if _revno_regex.match(spec) is not None:
159
return RevisionSpec_revno(spec, _internal=True)
161
raise errors.NoSuchRevisionSpec(spec)
163
def __init__(self, spec, _internal=False):
164
"""Create a RevisionSpec referring to the Null revision.
166
:param spec: The original spec supplied by the user
167
:param _internal: Used to ensure that RevisionSpec is not being
168
called directly. Only from RevisionSpec.from_string()
171
# XXX: Update this after 0.10 is released
172
symbol_versioning.warn('Creating a RevisionSpec directly has'
173
' been deprecated in version 0.11. Use'
174
' RevisionSpec.from_string()'
176
DeprecationWarning, stacklevel=2)
177
self.user_spec = spec
132
178
if self.prefix and spec.startswith(self.prefix):
133
179
spec = spec[len(self.prefix):]
136
182
def _match_on(self, branch, revs):
183
trace.mutter('Returning RevisionSpec._match_on: None')
137
184
return RevisionInfo(branch, 0, None)
139
186
def _match_on_and_check(self, branch, revs):
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
230
class RevisionSpec_revno(RevisionSpec):
198
231
prefix = 'revno:'
200
233
def _match_on(self, branch, revs):
201
234
"""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)
235
loc = self.spec.find(':')
237
revno_spec = self.spec
240
revno_spec = self.spec[:loc]
241
branch_spec = self.spec[loc+1:]
245
raise errors.InvalidRevisionSpec(self.user_spec,
246
branch, 'cannot have an empty revno and no branch')
250
revno = int(revno_spec)
251
except ValueError, e:
252
raise errors.InvalidRevisionSpec(self.user_spec,
256
from bzrlib.branch import Branch
257
branch = Branch.open(branch_spec)
258
# Need to use a new revision history
259
# because we are using a specific branch
260
revs = branch.revision_history()
263
if (-revno) >= len(revs):
266
revno = len(revs) + revno + 1
268
revision_id = branch.get_rev_id(revno, revs)
269
except errors.NoSuchRevision:
270
raise errors.InvalidRevisionSpec(self.user_spec, branch)
271
return RevisionInfo(branch, revno, revision_id)
218
273
def needs_branch(self):
219
274
return self.spec.find(':') == -1
277
RevisionSpec_int = RevisionSpec_revno
221
279
SPEC_TYPES.append(RevisionSpec_revno)
240
299
def _match_on(self, branch, revs):
302
raise errors.NoCommits(branch)
303
return RevisionInfo(branch, len(revs), revs[-1])
242
306
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)
307
except ValueError, e:
308
raise errors.InvalidRevisionSpec(self.user_spec, branch, e)
311
raise errors.InvalidRevisionSpec(self.user_spec, branch,
312
'you must supply a positive value')
313
revno = len(revs) - offset + 1
315
revision_id = branch.get_rev_id(revno, revs)
316
except errors.NoSuchRevision:
317
raise errors.InvalidRevisionSpec(self.user_spec, branch)
318
return RevisionInfo(branch, revno, revision_id)
250
320
SPEC_TYPES.append(RevisionSpec_last)
255
325
prefix = 'before:'
257
327
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)
328
r = RevisionSpec.from_string(self.spec)._match_on(branch, revs)
330
raise errors.InvalidRevisionSpec(self.user_spec, branch,
331
'cannot go before the null: revision')
333
# We need to use the repository history here
334
rev = branch.repository.get_revision(r.rev_id)
335
if not rev.parent_ids:
339
revision_id = rev.parent_ids[0]
341
revno = revs.index(revision_id) + 1
347
revision_id = branch.get_rev_id(revno, revs)
348
except errors.NoSuchRevision:
349
raise errors.InvalidRevisionSpec(self.user_spec,
351
return RevisionInfo(branch, revno, revision_id)
263
353
SPEC_TYPES.append(RevisionSpec_before)
269
359
def _match_on(self, branch, revs):
270
raise BzrError('tag: namespace registered, but not implemented.')
360
raise errors.InvalidRevisionSpec(self.user_spec, branch,
361
'tag: namespace registered,'
362
' but not implemented')
272
364
SPEC_TYPES.append(RevisionSpec_tag)
275
class RevisionSpec_revs:
367
class _RevListToTimestamps(object):
368
"""This takes a list of revisions, and allows you to bisect by date"""
370
__slots__ = ['revs', 'branch']
276
372
def __init__(self, revs, branch):
278
374
self.branch = branch
279
376
def __getitem__(self, index):
377
"""Get the date of the index'd item"""
280
378
r = self.branch.repository.get_revision(self.revs[index])
281
379
# TODO: Handle timezone.
282
380
return datetime.datetime.fromtimestamp(r.timestamp)
283
382
def __len__(self):
284
383
return len(self.revs)
314
413
m = self._date_re.match(self.spec)
315
414
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
415
raise errors.InvalidRevisionSpec(self.user_spec,
416
branch, 'invalid date')
420
year = int(m.group('year'))
421
month = int(m.group('month'))
422
day = int(m.group('day'))
429
hour = int(m.group('hour'))
430
minute = int(m.group('minute'))
431
if m.group('second'):
432
second = int(m.group('second'))
436
hour, minute, second = 0,0,0
438
raise errors.InvalidRevisionSpec(self.user_spec,
439
branch, 'invalid date')
332
441
dt = datetime.datetime(year=year, month=month, day=day,
333
442
hour=hour, minute=minute, second=second)
334
443
branch.lock_read()
336
rev = bisect.bisect(RevisionSpec_revs(revs, branch), dt)
445
rev = bisect.bisect(_RevListToTimestamps(revs, branch), dt)
339
448
if rev == len(revs):
348
457
prefix = 'ancestor:'
350
459
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]
460
from bzrlib.branch import Branch
462
trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
463
other_branch = Branch.open(self.spec)
354
464
revision_a = branch.last_revision()
355
465
revision_b = other_branch.last_revision()
356
466
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)
467
if r in (None, revision.NULL_REVISION):
468
raise errors.NoCommits(b)
469
revision_source = revision.MultipleRevisionSources(
470
branch.repository, other_branch.repository)
471
rev_id = revision.common_ancestor(revision_a, revision_b,
363
474
revno = branch.revision_id_to_revno(rev_id)
364
except NoSuchRevision:
475
except errors.NoSuchRevision:
366
477
return RevisionInfo(branch, revno, rev_id)
368
479
SPEC_TYPES.append(RevisionSpec_ancestor)
370
482
class RevisionSpec_branch(RevisionSpec):
371
483
"""A branch: revision specifier.
375
487
prefix = 'branch:'
377
489
def _match_on(self, branch, revs):
378
from branch import Branch
379
other_branch = Branch.open_containing(self.spec)[0]
490
from bzrlib.branch import Branch
491
other_branch = Branch.open(self.spec)
380
492
revision_b = other_branch.last_revision()
381
if revision_b is None:
382
raise NoCommits(other_branch)
493
if revision_b in (None, revision.NULL_REVISION):
494
raise errors.NoCommits(other_branch)
383
495
# pull in the remote revisions so we can diff
384
496
branch.fetch(other_branch, revision_b)
386
498
revno = branch.revision_id_to_revno(revision_b)
387
except NoSuchRevision:
499
except errors.NoSuchRevision:
389
501
return RevisionInfo(branch, revno, revision_b)