107
def __new__(cls, spec, foo=_marker):
108
"""Parse a revision specifier.
137
def __new__(cls, spec, _internal=False):
139
return object.__new__(cls, spec, _internal=_internal)
141
symbol_versioning.warn('Creating a RevisionSpec directly has'
142
' been deprecated in version 0.11. Use'
143
' RevisionSpec.from_string()'
145
DeprecationWarning, stacklevel=2)
146
return RevisionSpec.from_string(spec)
149
def from_string(spec):
150
"""Parse a revision spec string into a RevisionSpec object.
152
:param spec: A string specified by the user
153
:return: A RevisionSpec object that understands how to parse the
156
if not isinstance(spec, (type(None), basestring)):
157
raise TypeError('error')
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' %
160
return RevisionSpec(None, _internal=True)
162
assert isinstance(spec, basestring), \
163
"You should only supply strings not %s" % (type(spec),)
165
for spectype in SPEC_TYPES:
166
if spec.startswith(spectype.prefix):
167
trace.mutter('Returning RevisionSpec %s for %s',
168
spectype.__name__, spec)
169
return spectype(spec, _internal=True)
128
raise TypeError('Unhandled revision type %s' % spec)
130
def __init__(self, spec):
171
# RevisionSpec_revno is special cased, because it is the only
172
# one that directly handles plain integers
173
# TODO: This should not be special cased rather it should be
174
# a method invocation on spectype.canparse()
176
if _revno_regex is None:
177
_revno_regex = re.compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
178
if _revno_regex.match(spec) is not None:
179
return RevisionSpec_revno(spec, _internal=True)
181
raise errors.NoSuchRevisionSpec(spec)
183
def __init__(self, spec, _internal=False):
184
"""Create a RevisionSpec referring to the Null revision.
186
:param spec: The original spec supplied by the user
187
:param _internal: Used to ensure that RevisionSpec is not being
188
called directly. Only from RevisionSpec.from_string()
191
# XXX: Update this after 0.10 is released
192
symbol_versioning.warn('Creating a RevisionSpec directly has'
193
' been deprecated in version 0.11. Use'
194
' RevisionSpec.from_string()'
196
DeprecationWarning, stacklevel=2)
197
self.user_spec = spec
131
198
if self.prefix and spec.startswith(self.prefix):
132
199
spec = spec[len(self.prefix):]
135
202
def _match_on(self, branch, revs):
203
trace.mutter('Returning RevisionSpec._match_on: None')
136
204
return RevisionInfo(branch, 0, None)
138
206
def _match_on_and_check(self, branch, revs):
164
237
def __repr__(self):
165
238
# this is mostly for helping with testing
166
return '<%s %s%s>' % (self.__class__.__name__,
239
return '<%s %s>' % (self.__class__.__name__,
242
def needs_branch(self):
243
"""Whether this revision spec needs a branch.
245
Set this to False the branch argument of _match_on is not used.
249
def get_branch(self):
250
"""When the revision specifier contains a branch location, return it.
252
Otherwise, return None.
173
class RevisionSpec_int(RevisionSpec):
174
"""Spec is a number. Special case."""
175
def __init__(self, spec):
176
self.spec = int(spec)
178
def _match_on(self, branch, revs):
180
revno = len(revs) + self.spec + 1
183
rev_id = branch.get_rev_id(revno, revs)
184
return RevisionInfo(branch, revno, rev_id)
187
259
class RevisionSpec_revno(RevisionSpec):
260
"""Selects a revision using a number."""
262
help_txt = """Selects a revision using a number.
264
Use an integer to specify a revision in the history of the branch.
265
Optionally a branch can be specified. The 'revno:' prefix is optional.
266
A negative number will count from the end of the branch (-1 is the
267
last revision, -2 the previous one). If the negative number is larger
268
than the branch's history, the first revision is returned.
271
revno:1 -> return the first revision
272
revno:3:/path/to/branch -> return the 3rd revision of
273
the branch '/path/to/branch'
274
revno:-1 -> The last revision in a branch.
275
-2:http://other/branch -> The second to last revision in the
277
-1000000 -> Most likely the first revision, unless
278
your history is very long.
188
280
prefix = 'revno:'
190
282
def _match_on(self, branch, revs):
191
283
"""Lookup a revision by revision number"""
193
return RevisionInfo(branch, int(self.spec))
195
return RevisionInfo(branch, None)
284
loc = self.spec.find(':')
286
revno_spec = self.spec
289
revno_spec = self.spec[:loc]
290
branch_spec = self.spec[loc+1:]
294
raise errors.InvalidRevisionSpec(self.user_spec,
295
branch, 'cannot have an empty revno and no branch')
299
revno = int(revno_spec)
302
# dotted decimal. This arguably should not be here
303
# but the from_string method is a little primitive
304
# right now - RBC 20060928
306
match_revno = tuple((int(number) for number in revno_spec.split('.')))
307
except ValueError, e:
308
raise errors.InvalidRevisionSpec(self.user_spec, branch, e)
313
# the user has override the branch to look in.
314
# we need to refresh the revision_history map and
316
from bzrlib.branch import Branch
317
branch = Branch.open(branch_spec)
318
# Need to use a new revision history
319
# because we are using a specific branch
320
revs = branch.revision_history()
325
revision_id_to_revno = branch.get_revision_id_to_revno_map()
326
revisions = [revision_id for revision_id, revno
327
in revision_id_to_revno.iteritems()
328
if revno == match_revno]
331
if len(revisions) != 1:
332
return RevisionInfo(branch, None, None)
334
# there is no traditional 'revno' for dotted-decimal revnos.
335
# so for API compatability we return None.
336
return RevisionInfo(branch, None, revisions[0])
339
# if get_rev_id supported negative revnos, there would not be a
340
# need for this special case.
341
if (-revno) >= len(revs):
344
revno = len(revs) + revno + 1
346
revision_id = branch.get_rev_id(revno, revs)
347
except errors.NoSuchRevision:
348
raise errors.InvalidRevisionSpec(self.user_spec, branch)
349
return RevisionInfo(branch, revno, revision_id)
351
def needs_branch(self):
352
return self.spec.find(':') == -1
354
def get_branch(self):
355
if self.spec.find(':') == -1:
358
return self.spec[self.spec.find(':')+1:]
361
RevisionSpec_int = RevisionSpec_revno
197
363
SPEC_TYPES.append(RevisionSpec_revno)
200
366
class RevisionSpec_revid(RevisionSpec):
367
"""Selects a revision using the revision id."""
369
help_txt = """Selects a revision using the revision id.
371
Supply a specific revision id, that can be used to specify any
372
revision id in the ancestry of the branch.
373
Including merges, and pending merges.
376
revid:aaaa@bbbb-123456789 -> Select revision 'aaaa@bbbb-123456789'
201
378
prefix = 'revid:'
203
380
def _match_on(self, branch, revs):
205
return RevisionInfo(branch, revs.index(self.spec) + 1, self.spec)
207
return RevisionInfo(branch, None, self.spec)
381
# self.spec comes straight from parsing the command line arguments,
382
# so we expect it to be a Unicode string. Switch it to the internal
384
revision_id = osutils.safe_revision_id(self.spec, warn=False)
385
return RevisionInfo.from_revision_id(branch, revision_id, revs)
209
387
SPEC_TYPES.append(RevisionSpec_revid)
212
390
class RevisionSpec_last(RevisionSpec):
391
"""Selects the nth revision from the end."""
393
help_txt = """Selects the nth revision from the end.
395
Supply a positive number to get the nth revision from the end.
396
This is the same as supplying negative numbers to the 'revno:' spec.
399
last:1 -> return the last revision
400
last:3 -> return the revision 2 before the end.
216
405
def _match_on(self, branch, revs):
408
raise errors.NoCommits(branch)
409
return RevisionInfo(branch, len(revs), revs[-1])
218
412
offset = int(self.spec)
220
return RevisionInfo(branch, None)
223
raise BzrError('You must supply a positive value for --revision last:XXX')
224
return RevisionInfo(branch, len(revs) - offset + 1)
413
except ValueError, e:
414
raise errors.InvalidRevisionSpec(self.user_spec, branch, e)
417
raise errors.InvalidRevisionSpec(self.user_spec, branch,
418
'you must supply a positive value')
419
revno = len(revs) - offset + 1
421
revision_id = branch.get_rev_id(revno, revs)
422
except errors.NoSuchRevision:
423
raise errors.InvalidRevisionSpec(self.user_spec, branch)
424
return RevisionInfo(branch, revno, revision_id)
226
426
SPEC_TYPES.append(RevisionSpec_last)
229
429
class RevisionSpec_before(RevisionSpec):
430
"""Selects the parent of the revision specified."""
432
help_txt = """Selects the parent of the revision specified.
434
Supply any revision spec to return the parent of that revision.
435
It is an error to request the parent of the null revision (before:0).
436
This is mostly useful when inspecting revisions that are not in the
437
revision history of a branch.
441
before:1913 -> Return the parent of revno 1913 (revno 1912)
442
before:revid:aaaa@bbbb-1234567890 -> return the parent of revision
444
bzr diff -r before:revid:aaaa..revid:aaaa
445
-> Find the changes between revision 'aaaa' and its parent.
446
(what changes did 'aaaa' introduce)
231
449
prefix = 'before:'
233
451
def _match_on(self, branch, revs):
234
r = RevisionSpec(self.spec)._match_on(branch, revs)
235
if (r.revno is None) or (r.revno == 0):
237
return RevisionInfo(branch, r.revno - 1)
452
r = RevisionSpec.from_string(self.spec)._match_on(branch, revs)
454
raise errors.InvalidRevisionSpec(self.user_spec, branch,
455
'cannot go before the null: revision')
457
# We need to use the repository history here
458
rev = branch.repository.get_revision(r.rev_id)
459
if not rev.parent_ids:
461
revision_id = revision.NULL_REVISION
463
revision_id = rev.parent_ids[0]
465
revno = revs.index(revision_id) + 1
471
revision_id = branch.get_rev_id(revno, revs)
472
except errors.NoSuchRevision:
473
raise errors.InvalidRevisionSpec(self.user_spec,
475
return RevisionInfo(branch, revno, revision_id)
239
477
SPEC_TYPES.append(RevisionSpec_before)
242
480
class RevisionSpec_tag(RevisionSpec):
481
"""Select a revision identified by tag name"""
483
help_txt = """Selects a revision identified by a tag name.
485
Tags are stored in the branch and created by the 'tag' command.
245
490
def _match_on(self, branch, revs):
246
raise BzrError('tag: namespace registered, but not implemented.')
491
# Can raise tags not supported, NoSuchTag, etc
492
return RevisionInfo.from_revision_id(branch,
493
branch.tags.lookup_tag(self.spec),
248
496
SPEC_TYPES.append(RevisionSpec_tag)
499
class _RevListToTimestamps(object):
500
"""This takes a list of revisions, and allows you to bisect by date"""
502
__slots__ = ['revs', 'branch']
504
def __init__(self, revs, branch):
508
def __getitem__(self, index):
509
"""Get the date of the index'd item"""
510
r = self.branch.repository.get_revision(self.revs[index])
511
# TODO: Handle timezone.
512
return datetime.datetime.fromtimestamp(r.timestamp)
515
return len(self.revs)
251
518
class RevisionSpec_date(RevisionSpec):
519
"""Selects a revision on the basis of a datestamp."""
521
help_txt = """Selects a revision on the basis of a datestamp.
523
Supply a datestamp to select the first revision that matches the date.
524
Date can be 'yesterday', 'today', 'tomorrow' or a YYYY-MM-DD string.
525
Matches the first entry after a given date (either at midnight or
526
at a specified time).
528
One way to display all the changes since yesterday would be::
530
bzr log -r date:yesterday..-1
534
date:yesterday -> select the first revision since yesterday
535
date:2006-08-14,17:10:14 -> select the first revision after
536
August 14th, 2006 at 5:10pm.
253
539
_date_re = re.compile(
254
540
r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
278
563
m = self._date_re.match(self.spec)
279
564
if not m or (not m.group('date') and not m.group('time')):
280
raise BzrError('Invalid revision date %r' % self.spec)
283
year, month, day = int(m.group('year')), int(m.group('month')), int(m.group('day'))
285
year, month, day = today.year, today.month, today.day
287
hour = int(m.group('hour'))
288
minute = int(m.group('minute'))
289
if m.group('second'):
290
second = int(m.group('second'))
294
hour, minute, second = 0,0,0
565
raise errors.InvalidRevisionSpec(self.user_spec,
566
branch, 'invalid date')
570
year = int(m.group('year'))
571
month = int(m.group('month'))
572
day = int(m.group('day'))
579
hour = int(m.group('hour'))
580
minute = int(m.group('minute'))
581
if m.group('second'):
582
second = int(m.group('second'))
586
hour, minute, second = 0,0,0
588
raise errors.InvalidRevisionSpec(self.user_spec,
589
branch, 'invalid date')
296
591
dt = datetime.datetime(year=year, month=month, day=day,
297
592
hour=hour, minute=minute, second=second)
299
for i in range(len(revs)):
300
r = branch.get_revision(revs[i])
301
# TODO: Handle timezone.
302
dt = datetime.datetime.fromtimestamp(r.timestamp)
304
return RevisionInfo(branch, i+1)
305
return RevisionInfo(branch, None)
595
rev = bisect.bisect(_RevListToTimestamps(revs, branch), dt)
599
return RevisionInfo(branch, None)
601
return RevisionInfo(branch, rev + 1)
307
603
SPEC_TYPES.append(RevisionSpec_date)
310
606
class RevisionSpec_ancestor(RevisionSpec):
607
"""Selects a common ancestor with a second branch."""
609
help_txt = """Selects a common ancestor with a second branch.
611
Supply the path to a branch to select the common ancestor.
613
The common ancestor is the last revision that existed in both
614
branches. Usually this is the branch point, but it could also be
615
a revision that was merged.
617
This is frequently used with 'diff' to return all of the changes
618
that your branch introduces, while excluding the changes that you
619
have not merged from the remote branch.
623
ancestor:/path/to/branch
624
$ bzr diff -r ancestor:../../mainline/branch
311
626
prefix = 'ancestor:'
313
628
def _match_on(self, branch, revs):
314
from branch import Branch
315
from revision import common_ancestor, MultipleRevisionSources
316
other_branch = Branch.open_containing(self.spec)[0]
629
trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
630
return self._find_revision_info(branch, self.spec)
633
def _find_revision_info(branch, other_location):
634
from bzrlib.branch import Branch
636
other_branch = Branch.open(other_location)
317
637
revision_a = branch.last_revision()
318
638
revision_b = other_branch.last_revision()
319
639
for r, b in ((revision_a, branch), (revision_b, other_branch)):
322
revision_source = MultipleRevisionSources(branch, other_branch)
323
rev_id = common_ancestor(revision_a, revision_b, revision_source)
640
if r in (None, revision.NULL_REVISION):
641
raise errors.NoCommits(b)
643
other_branch.lock_read()
325
revno = branch.revision_id_to_revno(rev_id)
326
except NoSuchRevision:
328
return RevisionInfo(branch, revno, rev_id)
645
revision_source = revision.MultipleRevisionSources(
646
branch.repository, other_branch.repository)
647
graph = branch.repository.get_graph(other_branch.repository)
648
revision_a = revision.ensure_null(revision_a)
649
revision_b = revision.ensure_null(revision_b)
650
if revision.NULL_REVISION in (revision_a, revision_b):
651
rev_id = revision.NULL_REVISION
653
rev_id = graph.find_unique_lca(revision_a, revision_b)
654
if rev_id == revision.NULL_REVISION:
655
raise errors.NoCommonAncestor(revision_a, revision_b)
657
revno = branch.revision_id_to_revno(rev_id)
658
except errors.NoSuchRevision:
660
return RevisionInfo(branch, revno, rev_id)
663
other_branch.unlock()
330
666
SPEC_TYPES.append(RevisionSpec_ancestor)
332
669
class RevisionSpec_branch(RevisionSpec):
333
"""A branch: revision specifier.
335
This takes the path to a branch and returns its tip revision id.
670
"""Selects the last revision of a specified branch."""
672
help_txt = """Selects the last revision of a specified branch.
674
Supply the path to a branch to select its last revision.
678
branch:/path/to/branch
337
680
prefix = 'branch:'
339
682
def _match_on(self, branch, revs):
340
from branch import Branch
341
from fetch import greedy_fetch
342
other_branch = Branch.open_containing(self.spec)[0]
683
from bzrlib.branch import Branch
684
other_branch = Branch.open(self.spec)
343
685
revision_b = other_branch.last_revision()
344
if revision_b is None:
345
raise NoCommits(other_branch)
686
if revision_b in (None, revision.NULL_REVISION):
687
raise errors.NoCommits(other_branch)
346
688
# pull in the remote revisions so we can diff
347
greedy_fetch(branch, other_branch, revision=revision_b)
689
branch.fetch(other_branch, revision_b)
349
691
revno = branch.revision_id_to_revno(revision_b)
350
except NoSuchRevision:
692
except errors.NoSuchRevision:
352
694
return RevisionInfo(branch, revno, revision_b)
354
696
SPEC_TYPES.append(RevisionSpec_branch)
699
class RevisionSpec_submit(RevisionSpec_ancestor):
700
"""Selects a common ancestor with a submit branch."""
702
help_txt = """Selects a common ancestor with the submit branch.
704
Diffing against this shows all the changes that were made in this branch,
705
and is a good predictor of what merge will do. The submit branch is
706
used by the bundle and merge directive comands. If no submit branch
707
is specified, the parent branch is used instead.
709
The common ancestor is the last revision that existed in both
710
branches. Usually this is the branch point, but it could also be
711
a revision that was merged.
715
$ bzr diff -r submit:
720
def _match_on(self, branch, revs):
721
trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
722
submit_location = branch.get_submit_branch()
723
location_type = 'submit branch'
724
if submit_location is None:
725
submit_location = branch.get_parent()
726
location_type = 'parent branch'
727
if submit_location is None:
728
raise errors.NoSubmitBranch(branch)
729
trace.note('Using %s %s', location_type, submit_location)
730
return self._find_revision_info(branch, submit_location)
733
SPEC_TYPES.append(RevisionSpec_submit)