118
107
return RevisionInfo(branch, revno, revision_id)
110
# classes in this list should have a "prefix" attribute, against which
111
# string specs are matched
121
116
class RevisionSpec(object):
122
117
"""A parsed revision specification."""
124
119
help_txt = """A parsed revision specification.
126
A revision specification is a string, which may be unambiguous about
127
what it represents by giving a prefix like 'date:' or 'revid:' etc,
128
or it may have no prefix, in which case it's tried against several
129
specifier types in sequence to determine what the user meant.
121
A revision specification can be an integer, in which case it is
122
assumed to be a revno (though this will translate negative values
123
into positive ones); or it can be a string, in which case it is
124
parsed for something like 'date:' or 'revid:' etc.
131
126
Revision specs are an UI element, and they have been moved out
132
127
of the branch class to leave "back-end" classes unaware of such
141
wants_revision_history = True
142
dwim_catchable_exceptions = (errors.InvalidRevisionSpec,)
143
"""Exceptions that RevisionSpec_dwim._match_on will catch.
145
If the revspec is part of ``dwim_revspecs``, it may be tried with an
146
invalid revspec and raises some exception. The exceptions mentioned here
147
will not be reported to the user but simply ignored without stopping the
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)
152
149
def from_string(spec):
163
160
return RevisionSpec(None, _internal=True)
164
match = revspec_registry.get_prefix(spec)
165
if match is not None:
166
spectype, specsuffix = match
167
trace.mutter('Returning RevisionSpec %s for %s',
168
spectype.__name__, spec)
169
return spectype(spec, _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)
171
# Otherwise treat it as a DWIM, build the RevisionSpec object and
172
# wait for _match_on to be called.
173
return RevisionSpec_dwim(spec, _internal=True)
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)
175
183
def __init__(self, spec, _internal=False):
176
184
"""Create a RevisionSpec referring to the Null revision.
235
233
# will do what you expect.
236
234
in_store = in_history
237
235
in_branch = in_store
239
def as_revision_id(self, context_branch):
240
"""Return just the revision_id for this revisions spec.
242
Some revision specs require a context_branch to be able to determine
243
their value. Not all specs will make use of it.
245
return self._as_revision_id(context_branch)
247
def _as_revision_id(self, context_branch):
248
"""Implementation of as_revision_id()
250
Classes should override this function to provide appropriate
251
functionality. The default is to just call '.in_history().rev_id'
253
return self.in_history(context_branch).rev_id
255
def as_tree(self, context_branch):
256
"""Return the tree object for this revisions spec.
258
Some revision specs require a context_branch to be able to determine
259
the revision id and access the repository. Not all specs will make
262
return self._as_tree(context_branch)
264
def _as_tree(self, context_branch):
265
"""Implementation of as_tree().
267
Classes should override this function to provide appropriate
268
functionality. The default is to just call '.as_revision_id()'
269
and get the revision tree from context_branch's repository.
271
revision_id = self.as_revision_id(context_branch)
272
return context_branch.repository.revision_tree(revision_id)
274
237
def __repr__(self):
275
238
# this is mostly for helping with testing
276
239
return '<%s %s>' % (self.__class__.__name__,
279
242
def needs_branch(self):
280
243
"""Whether this revision spec needs a branch.
296
class RevisionSpec_dwim(RevisionSpec):
297
"""Provides a DWIMish revision specifier lookup.
299
Note that this does not go in the revspec_registry because by definition
300
there is no prefix to identify it. It's solely called from
301
RevisionSpec.from_string() because the DWIMification happen when _match_on
302
is called so the string describing the revision is kept here until needed.
306
# We don't need to build the revision history ourself, that's delegated to
307
# each revspec we try.
308
wants_revision_history = False
310
_revno_regex = lazy_regex.lazy_compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
312
# The revspecs to try
313
_possible_revspecs = []
315
def _try_spectype(self, rstype, branch):
316
rs = rstype(self.spec, _internal=True)
317
# Hit in_history to find out if it exists, or we need to try the
319
return rs.in_history(branch)
321
def _match_on(self, branch, revs):
322
"""Run the lookup and see what we can get."""
324
# First, see if it's a revno
325
if self._revno_regex.match(self.spec) is not None:
327
return self._try_spectype(RevisionSpec_revno, branch)
328
except RevisionSpec_revno.dwim_catchable_exceptions:
331
# Next see what has been registered
332
for objgetter in self._possible_revspecs:
333
rs_class = objgetter.get_obj()
335
return self._try_spectype(rs_class, branch)
336
except rs_class.dwim_catchable_exceptions:
339
# Try the old (deprecated) dwim list:
340
for rs_class in dwim_revspecs:
342
return self._try_spectype(rs_class, branch)
343
except rs_class.dwim_catchable_exceptions:
346
# Well, I dunno what it is. Note that we don't try to keep track of the
347
# first of last exception raised during the DWIM tries as none seems
349
raise errors.InvalidRevisionSpec(self.spec, branch)
352
def append_possible_revspec(cls, revspec):
353
"""Append a possible DWIM revspec.
355
:param revspec: Revision spec to try.
357
cls._possible_revspecs.append(registry._ObjectGetter(revspec))
360
def append_possible_lazy_revspec(cls, module_name, member_name):
361
"""Append a possible lazily loaded DWIM revspec.
363
:param module_name: Name of the module with the revspec
364
:param member_name: Name of the revspec within the module
366
cls._possible_revspecs.append(
367
registry._LazyObjectGetter(module_name, member_name))
370
259
class RevisionSpec_revno(RevisionSpec):
371
260
"""Selects a revision using a number."""
373
262
help_txt = """Selects a revision using a number.
375
264
Use an integer to specify a revision in the history of the branch.
376
Optionally a branch can be specified. A negative number will count
377
from the end of the branch (-1 is the last revision, -2 the previous
378
one). If the negative number is larger than the branch's history, the
379
first revision is returned.
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.
382
revno:1 -> return the first revision of this branch
271
revno:1 -> return the first revision
383
272
revno:3:/path/to/branch -> return the 3rd revision of
384
273
the branch '/path/to/branch'
385
274
revno:-1 -> The last revision in a branch.
431
315
# the branch object.
432
316
from bzrlib.branch import Branch
433
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()
438
revision_id = branch.dotted_revno_to_revision_id(match_revno,
440
except errors.NoSuchRevision:
441
raise errors.InvalidRevisionSpec(self.user_spec, branch)
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)
443
334
# there is no traditional 'revno' for dotted-decimal revnos.
444
335
# so for API compatability we return None.
445
return branch, None, revision_id
336
return RevisionInfo(branch, None, revisions[0])
447
last_revno, last_revision_id = branch.last_revision_info()
449
339
# if get_rev_id supported negative revnos, there would not be a
450
340
# need for this special case.
451
if (-revno) >= last_revno:
341
if (-revno) >= len(revs):
454
revno = last_revno + revno + 1
344
revno = len(revs) + revno + 1
456
revision_id = branch.get_rev_id(revno, revs_or_none)
346
revision_id = branch.get_rev_id(revno, revs)
457
347
except errors.NoSuchRevision:
458
348
raise errors.InvalidRevisionSpec(self.user_spec, branch)
459
return branch, revno, revision_id
461
def _as_revision_id(self, context_branch):
462
# We would have the revno here, but we don't really care
463
branch, revno, revision_id = self._lookup(context_branch, None)
349
return RevisionInfo(branch, revno, revision_id)
466
351
def needs_branch(self):
467
352
return self.spec.find(':') == -1
473
358
return self.spec[self.spec.find(':')+1:]
476
361
RevisionSpec_int = RevisionSpec_revno
480
class RevisionIDSpec(RevisionSpec):
482
def _match_on(self, branch, revs):
483
revision_id = self.as_revision_id(branch)
484
return RevisionInfo.from_revision_id(branch, revision_id, revs)
487
class RevisionSpec_revid(RevisionIDSpec):
363
SPEC_TYPES.append(RevisionSpec_revno)
366
class RevisionSpec_revid(RevisionSpec):
488
367
"""Selects a revision using the revision id."""
490
369
help_txt = """Selects a revision using the revision id.
492
371
Supply a specific revision id, that can be used to specify any
493
revision id in the ancestry of the branch.
372
revision id in the ancestry of the branch.
494
373
Including merges, and pending merges.
497
376
revid:aaaa@bbbb-123456789 -> Select revision 'aaaa@bbbb-123456789'
500
378
prefix = 'revid:'
502
def _as_revision_id(self, context_branch):
380
def _match_on(self, branch, revs):
503
381
# self.spec comes straight from parsing the command line arguments,
504
382
# so we expect it to be a Unicode string. Switch it to the internal
505
383
# representation.
506
return osutils.safe_revision_id(self.spec, warn=False)
384
revision_id = osutils.safe_revision_id(self.spec, warn=False)
385
return RevisionInfo.from_revision_id(branch, revision_id, revs)
387
SPEC_TYPES.append(RevisionSpec_revid)
510
390
class RevisionSpec_last(RevisionSpec):
519
399
last:1 -> return the last revision
520
400
last:3 -> return the revision 2 before the end.
525
405
def _match_on(self, branch, revs):
526
revno, revision_id = self._revno_and_revision_id(branch, revs)
527
return RevisionInfo(branch, revno, revision_id)
529
def _revno_and_revision_id(self, context_branch, revs_or_none):
530
last_revno, last_revision_id = context_branch.last_revision_info()
532
406
if self.spec == '':
534
raise errors.NoCommits(context_branch)
535
return last_revno, last_revision_id
408
raise errors.NoCommits(branch)
409
return RevisionInfo(branch, len(revs), revs[-1])
538
412
offset = int(self.spec)
539
413
except ValueError, e:
540
raise errors.InvalidRevisionSpec(self.user_spec, context_branch, e)
414
raise errors.InvalidRevisionSpec(self.user_spec, branch, e)
543
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
417
raise errors.InvalidRevisionSpec(self.user_spec, branch,
544
418
'you must supply a positive value')
546
revno = last_revno - offset + 1
419
revno = len(revs) - offset + 1
548
revision_id = context_branch.get_rev_id(revno, revs_or_none)
421
revision_id = branch.get_rev_id(revno, revs)
549
422
except errors.NoSuchRevision:
550
raise errors.InvalidRevisionSpec(self.user_spec, context_branch)
551
return revno, revision_id
553
def _as_revision_id(self, context_branch):
554
# We compute the revno as part of the process, but we don't really care
556
revno, revision_id = self._revno_and_revision_id(context_branch, None)
423
raise errors.InvalidRevisionSpec(self.user_spec, branch)
424
return RevisionInfo(branch, revno, revision_id)
426
SPEC_TYPES.append(RevisionSpec_last)
561
429
class RevisionSpec_before(RevisionSpec):
564
432
help_txt = """Selects the parent of the revision specified.
566
Supply any revision spec to return the parent of that revision. This is
567
mostly useful when inspecting revisions that are not in the revision history
434
Supply any revision spec to return the parent of that revision.
570
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.
574
441
before:1913 -> Return the parent of revno 1913 (revno 1912)
575
442
before:revid:aaaa@bbbb-1234567890 -> return the parent of revision
576
443
aaaa@bbbb-1234567890
577
bzr diff -r before:1913..1913
578
-> Find the changes between revision 1913 and its parent (1912).
579
(What changes did revision 1913 introduce).
580
This is equivalent to: bzr diff -c 1913
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)
583
449
prefix = 'before:'
585
451
def _match_on(self, branch, revs):
586
452
r = RevisionSpec.from_string(self.spec)._match_on(branch, revs)
609
475
return RevisionInfo(branch, revno, revision_id)
611
def _as_revision_id(self, context_branch):
612
base_revspec = RevisionSpec.from_string(self.spec)
613
base_revision_id = base_revspec.as_revision_id(context_branch)
614
if base_revision_id == revision.NULL_REVISION:
615
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
616
'cannot go before the null: revision')
617
context_repo = context_branch.repository
618
context_repo.lock_read()
620
parent_map = context_repo.get_parent_map([base_revision_id])
622
context_repo.unlock()
623
if base_revision_id not in parent_map:
624
# Ghost, or unknown revision id
625
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
626
'cannot find the matching revision')
627
parents = parent_map[base_revision_id]
629
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
630
'No parents for revision.')
477
SPEC_TYPES.append(RevisionSpec_before)
635
480
class RevisionSpec_tag(RevisionSpec):
786
629
trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
787
630
return self._find_revision_info(branch, self.spec)
789
def _as_revision_id(self, context_branch):
790
return self._find_revision_id(context_branch, self.spec)
793
633
def _find_revision_info(branch, other_location):
794
revision_id = RevisionSpec_ancestor._find_revision_id(branch,
797
revno = branch.revision_id_to_revno(revision_id)
798
except errors.NoSuchRevision:
800
return RevisionInfo(branch, revno, revision_id)
803
def _find_revision_id(branch, other_location):
804
634
from bzrlib.branch import Branch
636
other_branch = Branch.open(other_location)
637
revision_a = branch.last_revision()
638
revision_b = other_branch.last_revision()
639
for r, b in ((revision_a, branch), (revision_b, other_branch)):
640
if r in (None, revision.NULL_REVISION):
641
raise errors.NoCommits(b)
806
642
branch.lock_read()
643
other_branch.lock_read()
808
revision_a = revision.ensure_null(branch.last_revision())
809
if revision_a == revision.NULL_REVISION:
810
raise errors.NoCommits(branch)
811
if other_location == '':
812
other_location = branch.get_parent()
813
other_branch = Branch.open(other_location)
814
other_branch.lock_read()
816
revision_b = revision.ensure_null(other_branch.last_revision())
817
if revision_b == revision.NULL_REVISION:
818
raise errors.NoCommits(other_branch)
819
graph = branch.repository.get_graph(other_branch.repository)
645
graph = branch.repository.get_graph(other_branch.repository)
646
revision_a = revision.ensure_null(revision_a)
647
revision_b = revision.ensure_null(revision_b)
648
if revision.NULL_REVISION in (revision_a, revision_b):
649
rev_id = revision.NULL_REVISION
820
651
rev_id = graph.find_unique_lca(revision_a, revision_b)
822
other_branch.unlock()
823
if rev_id == revision.NULL_REVISION:
824
raise errors.NoCommonAncestor(revision_a, revision_b)
652
if rev_id == revision.NULL_REVISION:
653
raise errors.NoCommonAncestor(revision_a, revision_b)
655
revno = branch.revision_id_to_revno(rev_id)
656
except errors.NoSuchRevision:
658
return RevisionInfo(branch, revno, rev_id)
661
other_branch.unlock()
664
SPEC_TYPES.append(RevisionSpec_ancestor)
832
667
class RevisionSpec_branch(RevisionSpec):
849
683
revision_b = other_branch.last_revision()
850
684
if revision_b in (None, revision.NULL_REVISION):
851
685
raise errors.NoCommits(other_branch)
853
branch = other_branch
856
# pull in the remote revisions so we can diff
857
branch.fetch(other_branch, revision_b)
858
except errors.ReadOnlyError:
859
branch = other_branch
686
# pull in the remote revisions so we can diff
687
branch.fetch(other_branch, revision_b)
861
689
revno = branch.revision_id_to_revno(revision_b)
862
690
except errors.NoSuchRevision:
864
692
return RevisionInfo(branch, revno, revision_b)
866
def _as_revision_id(self, context_branch):
867
from bzrlib.branch import Branch
868
other_branch = Branch.open(self.spec)
869
last_revision = other_branch.last_revision()
870
last_revision = revision.ensure_null(last_revision)
871
context_branch.fetch(other_branch, last_revision)
872
if last_revision == revision.NULL_REVISION:
873
raise errors.NoCommits(other_branch)
876
def _as_tree(self, context_branch):
877
from bzrlib.branch import Branch
878
other_branch = Branch.open(self.spec)
879
last_revision = other_branch.last_revision()
880
last_revision = revision.ensure_null(last_revision)
881
if last_revision == revision.NULL_REVISION:
882
raise errors.NoCommits(other_branch)
883
return other_branch.repository.revision_tree(last_revision)
885
def needs_branch(self):
888
def get_branch(self):
694
SPEC_TYPES.append(RevisionSpec_branch)
893
697
class RevisionSpec_submit(RevisionSpec_ancestor):
919
724
location_type = 'parent branch'
920
725
if submit_location is None:
921
726
raise errors.NoSubmitBranch(branch)
922
trace.note(gettext('Using {0} {1}').format(location_type,
924
return submit_location
926
def _match_on(self, branch, revs):
927
trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
928
return self._find_revision_info(branch,
929
self._get_submit_location(branch))
931
def _as_revision_id(self, context_branch):
932
return self._find_revision_id(context_branch,
933
self._get_submit_location(context_branch))
936
class RevisionSpec_annotate(RevisionIDSpec):
940
help_txt = """Select the revision that last modified the specified line.
942
Select the revision that last modified the specified line. Line is
943
specified as path:number. Path is a relative path to the file. Numbers
944
start at 1, and are relative to the current version, not the last-
945
committed version of the file.
948
def _raise_invalid(self, numstring, context_branch):
949
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
950
'No such line: %s' % numstring)
952
def _as_revision_id(self, context_branch):
953
path, numstring = self.spec.rsplit(':', 1)
955
index = int(numstring) - 1
957
self._raise_invalid(numstring, context_branch)
958
tree, file_path = workingtree.WorkingTree.open_containing(path)
961
file_id = tree.path2id(file_path)
963
raise errors.InvalidRevisionSpec(self.user_spec,
964
context_branch, "File '%s' is not versioned." %
966
revision_ids = [r for (r, l) in tree.annotate_iter(file_id)]
970
revision_id = revision_ids[index]
972
self._raise_invalid(numstring, context_branch)
973
if revision_id == revision.CURRENT_REVISION:
974
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
975
'Line %s has not been committed.' % numstring)
979
class RevisionSpec_mainline(RevisionIDSpec):
981
help_txt = """Select mainline revision that merged the specified revision.
983
Select the revision that merged the specified revision into mainline.
988
def _as_revision_id(self, context_branch):
989
revspec = RevisionSpec.from_string(self.spec)
990
if revspec.get_branch() is None:
991
spec_branch = context_branch
993
spec_branch = _mod_branch.Branch.open(revspec.get_branch())
994
revision_id = revspec.as_revision_id(spec_branch)
995
graph = context_branch.repository.get_graph()
996
result = graph.find_lefthand_merger(revision_id,
997
context_branch.last_revision())
999
raise errors.InvalidRevisionSpec(self.user_spec, context_branch)
1003
# The order in which we want to DWIM a revision spec without any prefix.
1004
# revno is always tried first and isn't listed here, this is used by
1005
# RevisionSpec_dwim._match_on
1006
dwim_revspecs = symbol_versioning.deprecated_list(
1007
symbol_versioning.deprecated_in((2, 4, 0)), "dwim_revspecs", [])
1009
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_tag)
1010
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_revid)
1011
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_date)
1012
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_branch)
1014
revspec_registry = registry.Registry()
1015
def _register_revspec(revspec):
1016
revspec_registry.register(revspec.prefix, revspec)
1018
_register_revspec(RevisionSpec_revno)
1019
_register_revspec(RevisionSpec_revid)
1020
_register_revspec(RevisionSpec_last)
1021
_register_revspec(RevisionSpec_before)
1022
_register_revspec(RevisionSpec_tag)
1023
_register_revspec(RevisionSpec_date)
1024
_register_revspec(RevisionSpec_ancestor)
1025
_register_revspec(RevisionSpec_branch)
1026
_register_revspec(RevisionSpec_submit)
1027
_register_revspec(RevisionSpec_annotate)
1028
_register_revspec(RevisionSpec_mainline)
727
trace.note('Using %s %s', location_type, submit_location)
728
return self._find_revision_info(branch, submit_location)
731
SPEC_TYPES.append(RevisionSpec_submit)