117
107
return RevisionInfo(branch, revno, revision_id)
110
# classes in this list should have a "prefix" attribute, against which
111
# string specs are matched
120
116
class RevisionSpec(object):
121
117
"""A parsed revision specification."""
123
119
help_txt = """A parsed revision specification.
125
A revision specification is a string, which may be unambiguous about
126
what it represents by giving a prefix like 'date:' or 'revid:' etc,
127
or it may have no prefix, in which case it's tried against several
128
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.
130
126
Revision specs are an UI element, and they have been moved out
131
127
of the branch class to leave "back-end" classes unaware of such
140
wants_revision_history = True
141
dwim_catchable_exceptions = (errors.InvalidRevisionSpec,)
142
"""Exceptions that RevisionSpec_dwim._match_on will catch.
144
If the revspec is part of ``dwim_revspecs``, it may be tried with an
145
invalid revspec and raises some exception. The exceptions mentioned here
146
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)
151
149
def from_string(spec):
162
160
return RevisionSpec(None, _internal=True)
163
match = revspec_registry.get_prefix(spec)
164
if match is not None:
165
spectype, specsuffix = match
166
trace.mutter('Returning RevisionSpec %s for %s',
167
spectype.__name__, spec)
168
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)
170
# Otherwise treat it as a DWIM, build the RevisionSpec object and
171
# wait for _match_on to be called.
172
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)
174
183
def __init__(self, spec, _internal=False):
175
184
"""Create a RevisionSpec referring to the Null revision.
192
202
def _match_on(self, branch, revs):
193
203
trace.mutter('Returning RevisionSpec._match_on: None')
194
return RevisionInfo(branch, None, None)
204
return RevisionInfo(branch, 0, None)
196
206
def _match_on_and_check(self, branch, revs):
197
207
info = self._match_on(branch, revs)
200
elif info == (None, None):
201
# special case - nothing supplied
210
elif info == (0, None):
211
# special case - the empty tree
203
213
elif self.prefix:
204
214
raise errors.InvalidRevisionSpec(self.user_spec, branch)
226
233
# will do what you expect.
227
234
in_store = in_history
228
235
in_branch = in_store
230
def as_revision_id(self, context_branch):
231
"""Return just the revision_id for this revisions spec.
233
Some revision specs require a context_branch to be able to determine
234
their value. Not all specs will make use of it.
236
return self._as_revision_id(context_branch)
238
def _as_revision_id(self, context_branch):
239
"""Implementation of as_revision_id()
241
Classes should override this function to provide appropriate
242
functionality. The default is to just call '.in_history().rev_id'
244
return self.in_history(context_branch).rev_id
246
def as_tree(self, context_branch):
247
"""Return the tree object for this revisions spec.
249
Some revision specs require a context_branch to be able to determine
250
the revision id and access the repository. Not all specs will make
253
return self._as_tree(context_branch)
255
def _as_tree(self, context_branch):
256
"""Implementation of as_tree().
258
Classes should override this function to provide appropriate
259
functionality. The default is to just call '.as_revision_id()'
260
and get the revision tree from context_branch's repository.
262
revision_id = self.as_revision_id(context_branch)
263
return context_branch.repository.revision_tree(revision_id)
265
237
def __repr__(self):
266
238
# this is mostly for helping with testing
267
239
return '<%s %s>' % (self.__class__.__name__,
270
242
def needs_branch(self):
271
243
"""Whether this revision spec needs a branch.
287
class RevisionSpec_dwim(RevisionSpec):
288
"""Provides a DWIMish revision specifier lookup.
290
Note that this does not go in the revspec_registry because by definition
291
there is no prefix to identify it. It's solely called from
292
RevisionSpec.from_string() because the DWIMification happen when _match_on
293
is called so the string describing the revision is kept here until needed.
297
# We don't need to build the revision history ourself, that's delegated to
298
# each revspec we try.
299
wants_revision_history = False
301
_revno_regex = lazy_regex.lazy_compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
303
# The revspecs to try
304
_possible_revspecs = []
306
def _try_spectype(self, rstype, branch):
307
rs = rstype(self.spec, _internal=True)
308
# Hit in_history to find out if it exists, or we need to try the
310
return rs.in_history(branch)
312
def _match_on(self, branch, revs):
313
"""Run the lookup and see what we can get."""
315
# First, see if it's a revno
316
if self._revno_regex.match(self.spec) is not None:
318
return self._try_spectype(RevisionSpec_revno, branch)
319
except RevisionSpec_revno.dwim_catchable_exceptions:
322
# Next see what has been registered
323
for objgetter in self._possible_revspecs:
324
rs_class = objgetter.get_obj()
326
return self._try_spectype(rs_class, branch)
327
except rs_class.dwim_catchable_exceptions:
330
# Try the old (deprecated) dwim list:
331
for rs_class in dwim_revspecs:
333
return self._try_spectype(rs_class, branch)
334
except rs_class.dwim_catchable_exceptions:
337
# Well, I dunno what it is. Note that we don't try to keep track of the
338
# first of last exception raised during the DWIM tries as none seems
340
raise errors.InvalidRevisionSpec(self.spec, branch)
343
def append_possible_revspec(cls, revspec):
344
"""Append a possible DWIM revspec.
346
:param revspec: Revision spec to try.
348
cls._possible_revspecs.append(registry._ObjectGetter(revspec))
351
def append_possible_lazy_revspec(cls, module_name, member_name):
352
"""Append a possible lazily loaded DWIM revspec.
354
:param module_name: Name of the module with the revspec
355
:param member_name: Name of the revspec within the module
357
cls._possible_revspecs.append(
358
registry._LazyObjectGetter(module_name, member_name))
361
259
class RevisionSpec_revno(RevisionSpec):
362
260
"""Selects a revision using a number."""
364
262
help_txt = """Selects a revision using a number.
366
264
Use an integer to specify a revision in the history of the branch.
367
Optionally a branch can be specified. A negative number will count
368
from the end of the branch (-1 is the last revision, -2 the previous
369
one). If the negative number is larger than the branch's history, the
370
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.
373
revno:1 -> return the first revision of this branch
271
revno:1 -> return the first revision
374
272
revno:3:/path/to/branch -> return the 3rd revision of
375
273
the branch '/path/to/branch'
376
274
revno:-1 -> The last revision in a branch.
422
315
# the branch object.
423
316
from bzrlib.branch import Branch
424
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()
429
revision_id = branch.dotted_revno_to_revision_id(match_revno,
431
except errors.NoSuchRevision:
432
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)
434
334
# there is no traditional 'revno' for dotted-decimal revnos.
435
335
# so for API compatability we return None.
436
return branch, None, revision_id
336
return RevisionInfo(branch, None, revisions[0])
438
last_revno, last_revision_id = branch.last_revision_info()
440
339
# if get_rev_id supported negative revnos, there would not be a
441
340
# need for this special case.
442
if (-revno) >= last_revno:
341
if (-revno) >= len(revs):
445
revno = last_revno + revno + 1
344
revno = len(revs) + revno + 1
447
revision_id = branch.get_rev_id(revno, revs_or_none)
346
revision_id = branch.get_rev_id(revno, revs)
448
347
except errors.NoSuchRevision:
449
348
raise errors.InvalidRevisionSpec(self.user_spec, branch)
450
return branch, revno, revision_id
452
def _as_revision_id(self, context_branch):
453
# We would have the revno here, but we don't really care
454
branch, revno, revision_id = self._lookup(context_branch, None)
349
return RevisionInfo(branch, revno, revision_id)
457
351
def needs_branch(self):
458
352
return self.spec.find(':') == -1
464
358
return self.spec[self.spec.find(':')+1:]
467
361
RevisionSpec_int = RevisionSpec_revno
471
class RevisionIDSpec(RevisionSpec):
473
def _match_on(self, branch, revs):
474
revision_id = self.as_revision_id(branch)
475
return RevisionInfo.from_revision_id(branch, revision_id, revs)
478
class RevisionSpec_revid(RevisionIDSpec):
363
SPEC_TYPES.append(RevisionSpec_revno)
366
class RevisionSpec_revid(RevisionSpec):
479
367
"""Selects a revision using the revision id."""
481
369
help_txt = """Selects a revision using the revision id.
483
371
Supply a specific revision id, that can be used to specify any
484
revision id in the ancestry of the branch.
372
revision id in the ancestry of the branch.
485
373
Including merges, and pending merges.
488
376
revid:aaaa@bbbb-123456789 -> Select revision 'aaaa@bbbb-123456789'
491
378
prefix = 'revid:'
493
def _as_revision_id(self, context_branch):
380
def _match_on(self, branch, revs):
494
381
# self.spec comes straight from parsing the command line arguments,
495
382
# so we expect it to be a Unicode string. Switch it to the internal
496
383
# representation.
497
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)
501
390
class RevisionSpec_last(RevisionSpec):
510
399
last:1 -> return the last revision
511
400
last:3 -> return the revision 2 before the end.
516
405
def _match_on(self, branch, revs):
517
revno, revision_id = self._revno_and_revision_id(branch, revs)
518
return RevisionInfo(branch, revno, revision_id)
520
def _revno_and_revision_id(self, context_branch, revs_or_none):
521
last_revno, last_revision_id = context_branch.last_revision_info()
523
406
if self.spec == '':
525
raise errors.NoCommits(context_branch)
526
return last_revno, last_revision_id
408
raise errors.NoCommits(branch)
409
return RevisionInfo(branch, len(revs), revs[-1])
529
412
offset = int(self.spec)
530
413
except ValueError, e:
531
raise errors.InvalidRevisionSpec(self.user_spec, context_branch, e)
414
raise errors.InvalidRevisionSpec(self.user_spec, branch, e)
534
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
417
raise errors.InvalidRevisionSpec(self.user_spec, branch,
535
418
'you must supply a positive value')
537
revno = last_revno - offset + 1
419
revno = len(revs) - offset + 1
539
revision_id = context_branch.get_rev_id(revno, revs_or_none)
421
revision_id = branch.get_rev_id(revno, revs)
540
422
except errors.NoSuchRevision:
541
raise errors.InvalidRevisionSpec(self.user_spec, context_branch)
542
return revno, revision_id
544
def _as_revision_id(self, context_branch):
545
# We compute the revno as part of the process, but we don't really care
547
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)
552
429
class RevisionSpec_before(RevisionSpec):
555
432
help_txt = """Selects the parent of the revision specified.
557
Supply any revision spec to return the parent of that revision. This is
558
mostly useful when inspecting revisions that are not in the revision history
434
Supply any revision spec to return the parent of that revision.
561
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.
565
441
before:1913 -> Return the parent of revno 1913 (revno 1912)
566
442
before:revid:aaaa@bbbb-1234567890 -> return the parent of revision
567
443
aaaa@bbbb-1234567890
568
bzr diff -r before:1913..1913
569
-> Find the changes between revision 1913 and its parent (1912).
570
(What changes did revision 1913 introduce).
571
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)
574
449
prefix = 'before:'
576
451
def _match_on(self, branch, revs):
577
452
r = RevisionSpec.from_string(self.spec)._match_on(branch, revs)
600
475
return RevisionInfo(branch, revno, revision_id)
602
def _as_revision_id(self, context_branch):
603
base_revspec = RevisionSpec.from_string(self.spec)
604
base_revision_id = base_revspec.as_revision_id(context_branch)
605
if base_revision_id == revision.NULL_REVISION:
606
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
607
'cannot go before the null: revision')
608
context_repo = context_branch.repository
609
context_repo.lock_read()
611
parent_map = context_repo.get_parent_map([base_revision_id])
613
context_repo.unlock()
614
if base_revision_id not in parent_map:
615
# Ghost, or unknown revision id
616
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
617
'cannot find the matching revision')
618
parents = parent_map[base_revision_id]
620
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
621
'No parents for revision.')
477
SPEC_TYPES.append(RevisionSpec_before)
626
480
class RevisionSpec_tag(RevisionSpec):
777
629
trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
778
630
return self._find_revision_info(branch, self.spec)
780
def _as_revision_id(self, context_branch):
781
return self._find_revision_id(context_branch, self.spec)
784
633
def _find_revision_info(branch, other_location):
785
revision_id = RevisionSpec_ancestor._find_revision_id(branch,
788
revno = branch.revision_id_to_revno(revision_id)
789
except errors.NoSuchRevision:
791
return RevisionInfo(branch, revno, revision_id)
794
def _find_revision_id(branch, other_location):
795
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)
797
642
branch.lock_read()
643
other_branch.lock_read()
799
revision_a = revision.ensure_null(branch.last_revision())
800
if revision_a == revision.NULL_REVISION:
801
raise errors.NoCommits(branch)
802
if other_location == '':
803
other_location = branch.get_parent()
804
other_branch = Branch.open(other_location)
805
other_branch.lock_read()
807
revision_b = revision.ensure_null(other_branch.last_revision())
808
if revision_b == revision.NULL_REVISION:
809
raise errors.NoCommits(other_branch)
810
graph = branch.repository.get_graph(other_branch.repository)
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
811
653
rev_id = graph.find_unique_lca(revision_a, revision_b)
813
other_branch.unlock()
814
if rev_id == revision.NULL_REVISION:
815
raise errors.NoCommonAncestor(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()
666
SPEC_TYPES.append(RevisionSpec_ancestor)
823
669
class RevisionSpec_branch(RevisionSpec):
840
685
revision_b = other_branch.last_revision()
841
686
if revision_b in (None, revision.NULL_REVISION):
842
687
raise errors.NoCommits(other_branch)
844
branch = other_branch
847
# pull in the remote revisions so we can diff
848
branch.fetch(other_branch, revision_b)
849
except errors.ReadOnlyError:
850
branch = other_branch
688
# pull in the remote revisions so we can diff
689
branch.fetch(other_branch, revision_b)
852
691
revno = branch.revision_id_to_revno(revision_b)
853
692
except errors.NoSuchRevision:
855
694
return RevisionInfo(branch, revno, revision_b)
857
def _as_revision_id(self, context_branch):
858
from bzrlib.branch import Branch
859
other_branch = Branch.open(self.spec)
860
last_revision = other_branch.last_revision()
861
last_revision = revision.ensure_null(last_revision)
862
context_branch.fetch(other_branch, last_revision)
863
if last_revision == revision.NULL_REVISION:
864
raise errors.NoCommits(other_branch)
867
def _as_tree(self, context_branch):
868
from bzrlib.branch import Branch
869
other_branch = Branch.open(self.spec)
870
last_revision = other_branch.last_revision()
871
last_revision = revision.ensure_null(last_revision)
872
if last_revision == revision.NULL_REVISION:
873
raise errors.NoCommits(other_branch)
874
return other_branch.repository.revision_tree(last_revision)
876
def needs_branch(self):
879
def get_branch(self):
696
SPEC_TYPES.append(RevisionSpec_branch)
884
699
class RevisionSpec_submit(RevisionSpec_ancestor):
911
727
if submit_location is None:
912
728
raise errors.NoSubmitBranch(branch)
913
729
trace.note('Using %s %s', location_type, submit_location)
914
return submit_location
916
def _match_on(self, branch, revs):
917
trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
918
return self._find_revision_info(branch,
919
self._get_submit_location(branch))
921
def _as_revision_id(self, context_branch):
922
return self._find_revision_id(context_branch,
923
self._get_submit_location(context_branch))
926
class RevisionSpec_annotate(RevisionIDSpec):
930
help_txt = """Select the revision that last modified the specified line.
932
Select the revision that last modified the specified line. Line is
933
specified as path:number. Path is a relative path to the file. Numbers
934
start at 1, and are relative to the current version, not the last-
935
committed version of the file.
938
def _raise_invalid(self, numstring, context_branch):
939
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
940
'No such line: %s' % numstring)
942
def _as_revision_id(self, context_branch):
943
path, numstring = self.spec.rsplit(':', 1)
945
index = int(numstring) - 1
947
self._raise_invalid(numstring, context_branch)
948
tree, file_path = workingtree.WorkingTree.open_containing(path)
951
file_id = tree.path2id(file_path)
953
raise errors.InvalidRevisionSpec(self.user_spec,
954
context_branch, "File '%s' is not versioned." %
956
revision_ids = [r for (r, l) in tree.annotate_iter(file_id)]
960
revision_id = revision_ids[index]
962
self._raise_invalid(numstring, context_branch)
963
if revision_id == revision.CURRENT_REVISION:
964
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
965
'Line %s has not been committed.' % numstring)
969
class RevisionSpec_mainline(RevisionIDSpec):
971
help_txt = """Select mainline revision that merged the specified revision.
973
Select the revision that merged the specified revision into mainline.
978
def _as_revision_id(self, context_branch):
979
revspec = RevisionSpec.from_string(self.spec)
980
if revspec.get_branch() is None:
981
spec_branch = context_branch
983
spec_branch = _mod_branch.Branch.open(revspec.get_branch())
984
revision_id = revspec.as_revision_id(spec_branch)
985
graph = context_branch.repository.get_graph()
986
result = graph.find_lefthand_merger(revision_id,
987
context_branch.last_revision())
989
raise errors.InvalidRevisionSpec(self.user_spec, context_branch)
993
# The order in which we want to DWIM a revision spec without any prefix.
994
# revno is always tried first and isn't listed here, this is used by
995
# RevisionSpec_dwim._match_on
996
dwim_revspecs = symbol_versioning.deprecated_list(
997
symbol_versioning.deprecated_in((2, 4, 0)), "dwim_revspecs", [])
999
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_tag)
1000
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_revid)
1001
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_date)
1002
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_branch)
1004
revspec_registry = registry.Registry()
1005
def _register_revspec(revspec):
1006
revspec_registry.register(revspec.prefix, revspec)
1008
_register_revspec(RevisionSpec_revno)
1009
_register_revspec(RevisionSpec_revid)
1010
_register_revspec(RevisionSpec_last)
1011
_register_revspec(RevisionSpec_before)
1012
_register_revspec(RevisionSpec_tag)
1013
_register_revspec(RevisionSpec_date)
1014
_register_revspec(RevisionSpec_ancestor)
1015
_register_revspec(RevisionSpec_branch)
1016
_register_revspec(RevisionSpec_submit)
1017
_register_revspec(RevisionSpec_annotate)
1018
_register_revspec(RevisionSpec_mainline)
730
return self._find_revision_info(branch, submit_location)
733
SPEC_TYPES.append(RevisionSpec_submit)