108
def __new__(cls, spec, foo=_marker):
109
"""Parse a revision specifier.
136
def __new__(cls, spec, _internal=False):
138
return object.__new__(cls, spec, _internal=_internal)
140
symbol_versioning.warn('Creating a RevisionSpec directly has'
141
' been deprecated in version 0.11. Use'
142
' RevisionSpec.from_string()'
144
DeprecationWarning, stacklevel=2)
145
return RevisionSpec.from_string(spec)
148
def from_string(spec):
149
"""Parse a revision spec string into a RevisionSpec object.
151
:param spec: A string specified by the user
152
:return: A RevisionSpec object that understands how to parse the
155
if not isinstance(spec, (type(None), basestring)):
156
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' %
159
return RevisionSpec(None, _internal=True)
161
assert isinstance(spec, basestring), \
162
"You should only supply strings not %s" % (type(spec),)
164
for spectype in SPEC_TYPES:
165
if spec.startswith(spectype.prefix):
166
trace.mutter('Returning RevisionSpec %s for %s',
167
spectype.__name__, spec)
168
return spectype(spec, _internal=True)
129
raise TypeError('Unhandled revision type %s' % spec)
131
def __init__(self, spec):
170
# RevisionSpec_revno is special cased, because it is the only
171
# one that directly handles plain integers
172
# TODO: This should not be special cased rather it should be
173
# a method invocation on spectype.canparse()
175
if _revno_regex is None:
176
_revno_regex = re.compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
177
if _revno_regex.match(spec) is not None:
178
return RevisionSpec_revno(spec, _internal=True)
180
raise errors.NoSuchRevisionSpec(spec)
182
def __init__(self, spec, _internal=False):
183
"""Create a RevisionSpec referring to the Null revision.
185
:param spec: The original spec supplied by the user
186
:param _internal: Used to ensure that RevisionSpec is not being
187
called directly. Only from RevisionSpec.from_string()
190
# XXX: Update this after 0.10 is released
191
symbol_versioning.warn('Creating a RevisionSpec directly has'
192
' been deprecated in version 0.11. Use'
193
' RevisionSpec.from_string()'
195
DeprecationWarning, stacklevel=2)
196
self.user_spec = spec
132
197
if self.prefix and spec.startswith(self.prefix):
133
198
spec = spec[len(self.prefix):]
136
201
def _match_on(self, branch, revs):
202
trace.mutter('Returning RevisionSpec._match_on: None')
137
203
return RevisionInfo(branch, 0, None)
139
205
def _match_on_and_check(self, branch, revs):
165
236
def __repr__(self):
166
237
# this is mostly for helping with testing
167
return '<%s %s%s>' % (self.__class__.__name__,
238
return '<%s %s>' % (self.__class__.__name__,
241
def needs_branch(self):
242
"""Whether this revision spec needs a branch.
244
Set this to False the branch argument of _match_on is not used.
248
def get_branch(self):
249
"""When the revision specifier contains a branch location, return it.
251
Otherwise, return None.
174
class RevisionSpec_int(RevisionSpec):
175
"""Spec is a number. Special case."""
176
def __init__(self, spec):
177
self.spec = int(spec)
179
def _match_on(self, branch, revs):
181
revno = len(revs) + self.spec + 1
184
rev_id = branch.get_rev_id(revno, revs)
185
return RevisionInfo(branch, revno, rev_id)
188
258
class RevisionSpec_revno(RevisionSpec):
259
"""Selects a revision using a number."""
261
help_txt = """Selects a revision using a number.
263
Use an integer to specify a revision in the history of the branch.
264
Optionally a branch can be specified. The 'revno:' prefix is optional.
265
A negative number will count from the end of the branch (-1 is the
266
last revision, -2 the previous one). If the negative number is larger
267
than the branch's history, the first revision is returned.
269
revno:1 -> return the first revision
270
revno:3:/path/to/branch -> return the 3rd revision of
271
the branch '/path/to/branch'
272
revno:-1 -> The last revision in a branch.
273
-2:http://other/branch -> The second to last revision in the
275
-1000000 -> Most likely the first revision, unless
276
your history is very long.
189
278
prefix = 'revno:'
191
280
def _match_on(self, branch, revs):
192
281
"""Lookup a revision by revision number"""
194
return RevisionInfo(branch, int(self.spec))
196
return RevisionInfo(branch, None)
282
loc = self.spec.find(':')
284
revno_spec = self.spec
287
revno_spec = self.spec[:loc]
288
branch_spec = self.spec[loc+1:]
292
raise errors.InvalidRevisionSpec(self.user_spec,
293
branch, 'cannot have an empty revno and no branch')
297
revno = int(revno_spec)
300
# dotted decimal. This arguably should not be here
301
# but the from_string method is a little primitive
302
# right now - RBC 20060928
304
match_revno = tuple((int(number) for number in revno_spec.split('.')))
305
except ValueError, e:
306
raise errors.InvalidRevisionSpec(self.user_spec, branch, e)
311
# the user has override the branch to look in.
312
# we need to refresh the revision_history map and
314
from bzrlib.branch import Branch
315
branch = Branch.open(branch_spec)
316
# Need to use a new revision history
317
# because we are using a specific branch
318
revs = branch.revision_history()
323
last_rev = branch.last_revision()
324
merge_sorted_revisions = tsort.merge_sort(
325
branch.repository.get_revision_graph(last_rev),
329
return item[3] == match_revno
330
revisions = filter(match, merge_sorted_revisions)
333
if len(revisions) != 1:
334
return RevisionInfo(branch, None, None)
336
# there is no traditional 'revno' for dotted-decimal revnos.
337
# so for API compatability we return None.
338
return RevisionInfo(branch, None, revisions[0][1])
341
# if get_rev_id supported negative revnos, there would not be a
342
# need for this special case.
343
if (-revno) >= len(revs):
346
revno = len(revs) + revno + 1
348
revision_id = branch.get_rev_id(revno, revs)
349
except errors.NoSuchRevision:
350
raise errors.InvalidRevisionSpec(self.user_spec, branch)
351
return RevisionInfo(branch, revno, revision_id)
353
def needs_branch(self):
354
return self.spec.find(':') == -1
356
def get_branch(self):
357
if self.spec.find(':') == -1:
360
return self.spec[self.spec.find(':')+1:]
363
RevisionSpec_int = RevisionSpec_revno
198
365
SPEC_TYPES.append(RevisionSpec_revno)
201
368
class RevisionSpec_revid(RevisionSpec):
369
"""Selects a revision using the revision id."""
371
help_txt = """Selects a revision using the revision id.
373
Supply a specific revision id, that can be used to specify any
374
revision id in the ancestry of the branch.
375
Including merges, and pending merges.
377
revid:aaaa@bbbb-123456789 -> Select revision 'aaaa@bbbb-123456789'
202
379
prefix = 'revid:'
204
381
def _match_on(self, branch, revs):
206
return RevisionInfo(branch, revs.index(self.spec) + 1, self.spec)
208
return RevisionInfo(branch, None, self.spec)
382
return RevisionInfo.from_revision_id(branch, self.spec, revs)
210
384
SPEC_TYPES.append(RevisionSpec_revid)
213
387
class RevisionSpec_last(RevisionSpec):
388
"""Selects the nth revision from the end."""
390
help_txt = """Selects the nth revision from the end.
392
Supply a positive number to get the nth revision from the end.
393
This is the same as supplying negative numbers to the 'revno:' spec.
395
last:1 -> return the last revision
396
last:3 -> return the revision 2 before the end.
217
401
def _match_on(self, branch, revs):
404
raise errors.NoCommits(branch)
405
return RevisionInfo(branch, len(revs), revs[-1])
219
408
offset = int(self.spec)
221
return RevisionInfo(branch, None)
224
raise BzrError('You must supply a positive value for --revision last:XXX')
225
return RevisionInfo(branch, len(revs) - offset + 1)
409
except ValueError, e:
410
raise errors.InvalidRevisionSpec(self.user_spec, branch, e)
413
raise errors.InvalidRevisionSpec(self.user_spec, branch,
414
'you must supply a positive value')
415
revno = len(revs) - offset + 1
417
revision_id = branch.get_rev_id(revno, revs)
418
except errors.NoSuchRevision:
419
raise errors.InvalidRevisionSpec(self.user_spec, branch)
420
return RevisionInfo(branch, revno, revision_id)
227
422
SPEC_TYPES.append(RevisionSpec_last)
230
425
class RevisionSpec_before(RevisionSpec):
426
"""Selects the parent of the revision specified."""
428
help_txt = """Selects the parent of the revision specified.
430
Supply any revision spec to return the parent of that revision.
431
It is an error to request the parent of the null revision (before:0).
432
This is mostly useful when inspecting revisions that are not in the
433
revision history of a branch.
436
before:1913 -> Return the parent of revno 1913 (revno 1912)
437
before:revid:aaaa@bbbb-1234567890 -> return the parent of revision
439
bzr diff -r before:revid:aaaa..revid:aaaa
440
-> Find the changes between revision 'aaaa' and its parent.
441
(what changes did 'aaaa' introduce)
232
444
prefix = 'before:'
234
446
def _match_on(self, branch, revs):
235
r = RevisionSpec(self.spec)._match_on(branch, revs)
236
if (r.revno is None) or (r.revno == 0):
238
return RevisionInfo(branch, r.revno - 1)
447
r = RevisionSpec.from_string(self.spec)._match_on(branch, revs)
449
raise errors.InvalidRevisionSpec(self.user_spec, branch,
450
'cannot go before the null: revision')
452
# We need to use the repository history here
453
rev = branch.repository.get_revision(r.rev_id)
454
if not rev.parent_ids:
458
revision_id = rev.parent_ids[0]
460
revno = revs.index(revision_id) + 1
466
revision_id = branch.get_rev_id(revno, revs)
467
except errors.NoSuchRevision:
468
raise errors.InvalidRevisionSpec(self.user_spec,
470
return RevisionInfo(branch, revno, revision_id)
240
472
SPEC_TYPES.append(RevisionSpec_before)
243
475
class RevisionSpec_tag(RevisionSpec):
476
"""Select a revision identified by tag name"""
478
help_txt = """Selects a revision identified by a tag name.
480
Tags are stored in the repository and created by the 'tag'
246
486
def _match_on(self, branch, revs):
247
raise BzrError('tag: namespace registered, but not implemented.')
487
# Can raise tags not supported, NoSuchTag, etc
488
return RevisionInfo.from_revision_id(branch,
489
branch.tags.lookup_tag(self.spec),
249
492
SPEC_TYPES.append(RevisionSpec_tag)
252
class RevisionSpec_revs:
253
def __init__(self, revs, branch):
256
def __getitem__(self, index):
257
r = self.branch.repository.get_revision(self.revs[index])
258
# TODO: Handle timezone.
259
return datetime.datetime.fromtimestamp(r.timestamp)
261
return len(self.revs)
264
class RevisionSpec_revs:
265
def __init__(self, revs, branch):
268
def __getitem__(self, index):
269
r = self.branch.repository.get_revision(self.revs[index])
270
# TODO: Handle timezone.
271
return datetime.datetime.fromtimestamp(r.timestamp)
495
class _RevListToTimestamps(object):
496
"""This takes a list of revisions, and allows you to bisect by date"""
498
__slots__ = ['revs', 'branch']
500
def __init__(self, revs, branch):
504
def __getitem__(self, index):
505
"""Get the date of the index'd item"""
506
r = self.branch.repository.get_revision(self.revs[index])
507
# TODO: Handle timezone.
508
return datetime.datetime.fromtimestamp(r.timestamp)
272
510
def __len__(self):
273
511
return len(self.revs)
276
514
class RevisionSpec_date(RevisionSpec):
515
"""Selects a revision on the basis of a datestamp."""
517
help_txt = """Selects a revision on the basis of a datestamp.
519
Supply a datestamp to select the first revision that matches the date.
520
Date can be 'yesterday', 'today', 'tomorrow' or a YYYY-MM-DD string.
521
Matches the first entry after a given date (either at midnight or
522
at a specified time).
524
One way to display all the changes since yesterday would be:
525
bzr log -r date:yesterday..-1
528
date:yesterday -> select the first revision since yesterday
529
date:2006-08-14,17:10:14 -> select the first revision after
530
August 14th, 2006 at 5:10pm.
278
533
_date_re = re.compile(
279
534
r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
336
600
class RevisionSpec_ancestor(RevisionSpec):
601
"""Selects a common ancestor with a second branch."""
603
help_txt = """Selects a common ancestor with a second branch.
605
Supply the path to a branch to select the common ancestor.
607
The common ancestor is the last revision that existed in both
608
branches. Usually this is the branch point, but it could also be
609
a revision that was merged.
611
This is frequently used with 'diff' to return all of the changes
612
that your branch introduces, while excluding the changes that you
613
have not merged from the remote branch.
616
ancestor:/path/to/branch
617
$ bzr diff -r ancestor:../../mainline/branch
337
619
prefix = 'ancestor:'
339
621
def _match_on(self, branch, revs):
340
from branch import Branch
341
from revision import common_ancestor, MultipleRevisionSources
342
other_branch = Branch.open_containing(self.spec)[0]
622
from bzrlib.branch import Branch
624
trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
625
other_branch = Branch.open(self.spec)
343
626
revision_a = branch.last_revision()
344
627
revision_b = other_branch.last_revision()
345
628
for r, b in ((revision_a, branch), (revision_b, other_branch)):
348
revision_source = MultipleRevisionSources(branch.repository,
349
other_branch.repository)
350
rev_id = common_ancestor(revision_a, revision_b, revision_source)
629
if r in (None, revision.NULL_REVISION):
630
raise errors.NoCommits(b)
631
revision_source = revision.MultipleRevisionSources(
632
branch.repository, other_branch.repository)
633
rev_id = revision.common_ancestor(revision_a, revision_b,
352
636
revno = branch.revision_id_to_revno(rev_id)
353
except NoSuchRevision:
637
except errors.NoSuchRevision:
355
639
return RevisionInfo(branch, revno, rev_id)
357
641
SPEC_TYPES.append(RevisionSpec_ancestor)
359
644
class RevisionSpec_branch(RevisionSpec):
360
"""A branch: revision specifier.
362
This takes the path to a branch and returns its tip revision id.
645
"""Selects the last revision of a specified branch."""
647
help_txt = """Selects the last revision of a specified branch.
649
Supply the path to a branch to select its last revision.
652
branch:/path/to/branch
364
654
prefix = 'branch:'
366
656
def _match_on(self, branch, revs):
367
from branch import Branch
368
other_branch = Branch.open_containing(self.spec)[0]
657
from bzrlib.branch import Branch
658
other_branch = Branch.open(self.spec)
369
659
revision_b = other_branch.last_revision()
370
if revision_b is None:
371
raise NoCommits(other_branch)
660
if revision_b in (None, revision.NULL_REVISION):
661
raise errors.NoCommits(other_branch)
372
662
# pull in the remote revisions so we can diff
373
663
branch.fetch(other_branch, revision_b)
375
665
revno = branch.revision_id_to_revno(revision_b)
376
except NoSuchRevision:
666
except errors.NoSuchRevision:
378
668
return RevisionInfo(branch, revno, revision_b)