122
118
help_txt = """A parsed revision specification.
124
A revision specification is a string, which may be unambiguous about
125
what it represents by giving a prefix like 'date:' or 'revid:' etc,
126
or it may have no prefix, in which case it's tried against several
127
specifier types in sequence to determine what the user meant.
120
A revision specification can be an integer, in which case it is
121
assumed to be a revno (though this will translate negative values
122
into positive ones); or it can be a string, in which case it is
123
parsed for something like 'date:' or 'revid:' etc.
129
125
Revision specs are an UI element, and they have been moved out
130
126
of the branch class to leave "back-end" classes unaware of such
139
wants_revision_history = True
140
dwim_catchable_exceptions = (errors.InvalidRevisionSpec,)
141
"""Exceptions that RevisionSpec_dwim._match_on will catch.
143
If the revspec is part of ``dwim_revspecs``, it may be tried with an
144
invalid revspec and raises some exception. The exceptions mentioned here
145
will not be reported to the user but simply ignored without stopping the
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)
150
148
def from_string(spec):
161
159
return RevisionSpec(None, _internal=True)
162
match = revspec_registry.get_prefix(spec)
163
if match is not None:
164
spectype, specsuffix = match
165
trace.mutter('Returning RevisionSpec %s for %s',
166
spectype.__name__, spec)
167
return spectype(spec, _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)
169
for spectype in SPEC_TYPES:
170
if spec.startswith(spectype.prefix):
171
trace.mutter('Returning RevisionSpec %s for %s',
172
spectype.__name__, spec)
173
return spectype(spec, _internal=True)
174
# Otherwise treat it as a DWIM, build the RevisionSpec object and
175
# wait for _match_on to be called.
176
return RevisionSpec_dwim(spec, _internal=True)
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)
178
182
def __init__(self, spec, _internal=False):
179
183
"""Create a RevisionSpec referring to the Null revision.
196
201
def _match_on(self, branch, revs):
197
202
trace.mutter('Returning RevisionSpec._match_on: None')
198
return RevisionInfo(branch, None, None)
203
return RevisionInfo(branch, 0, None)
200
205
def _match_on_and_check(self, branch, revs):
201
206
info = self._match_on(branch, revs)
204
elif info == (None, None):
205
# special case - nothing supplied
209
elif info == (0, None):
210
# special case - the empty tree
207
212
elif self.prefix:
208
213
raise errors.InvalidRevisionSpec(self.user_spec, branch)
230
232
# will do what you expect.
231
233
in_store = in_history
232
234
in_branch = in_store
234
def as_revision_id(self, context_branch):
235
"""Return just the revision_id for this revisions spec.
237
Some revision specs require a context_branch to be able to determine
238
their value. Not all specs will make use of it.
240
return self._as_revision_id(context_branch)
242
def _as_revision_id(self, context_branch):
243
"""Implementation of as_revision_id()
245
Classes should override this function to provide appropriate
246
functionality. The default is to just call '.in_history().rev_id'
248
return self.in_history(context_branch).rev_id
250
def as_tree(self, context_branch):
251
"""Return the tree object for this revisions spec.
253
Some revision specs require a context_branch to be able to determine
254
the revision id and access the repository. Not all specs will make
257
return self._as_tree(context_branch)
259
def _as_tree(self, context_branch):
260
"""Implementation of as_tree().
262
Classes should override this function to provide appropriate
263
functionality. The default is to just call '.as_revision_id()'
264
and get the revision tree from context_branch's repository.
266
revision_id = self.as_revision_id(context_branch)
267
return context_branch.repository.revision_tree(revision_id)
269
236
def __repr__(self):
270
237
# this is mostly for helping with testing
271
238
return '<%s %s>' % (self.__class__.__name__,
274
241
def needs_branch(self):
275
242
"""Whether this revision spec needs a branch.
291
class RevisionSpec_dwim(RevisionSpec):
292
"""Provides a DWIMish revision specifier lookup.
294
Note that this does not go in the revspec_registry because by definition
295
there is no prefix to identify it. It's solely called from
296
RevisionSpec.from_string() because the DWIMification happen when _match_on
297
is called so the string describing the revision is kept here until needed.
301
# We don't need to build the revision history ourself, that's delegated to
302
# each revspec we try.
303
wants_revision_history = False
305
def _try_spectype(self, rstype, branch):
306
rs = rstype(self.spec, _internal=True)
307
# Hit in_history to find out if it exists, or we need to try the
309
return rs.in_history(branch)
311
def _match_on(self, branch, revs):
312
"""Run the lookup and see what we can get."""
314
# First, see if it's a revno
316
if _revno_regex is None:
317
_revno_regex = re.compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
318
if _revno_regex.match(self.spec) is not None:
320
return self._try_spectype(RevisionSpec_revno, branch)
321
except RevisionSpec_revno.dwim_catchable_exceptions:
324
# Next see what has been registered
325
for rs_class in dwim_revspecs:
327
return self._try_spectype(rs_class, branch)
328
except rs_class.dwim_catchable_exceptions:
331
# Well, I dunno what it is. Note that we don't try to keep track of the
332
# first of last exception raised during the DWIM tries as none seems
334
raise errors.InvalidRevisionSpec(self.spec, branch)
337
258
class RevisionSpec_revno(RevisionSpec):
338
259
"""Selects a revision using a number."""
340
261
help_txt = """Selects a revision using a number.
342
263
Use an integer to specify a revision in the history of the branch.
343
Optionally a branch can be specified. A negative number will count
344
from the end of the branch (-1 is the last revision, -2 the previous
345
one). If the negative number is larger than the branch's history, the
346
first revision is returned.
349
revno:1 -> return the first revision of this 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
350
270
revno:3:/path/to/branch -> return the 3rd revision of
351
271
the branch '/path/to/branch'
352
272
revno:-1 -> The last revision in a branch.
398
313
# the branch object.
399
314
from bzrlib.branch import Branch
400
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()
405
revision_id = branch.dotted_revno_to_revision_id(match_revno,
407
except errors.NoSuchRevision:
408
raise errors.InvalidRevisionSpec(self.user_spec, branch)
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)
410
336
# there is no traditional 'revno' for dotted-decimal revnos.
411
337
# so for API compatability we return None.
412
return branch, None, revision_id
338
return RevisionInfo(branch, None, revisions[0][1])
414
last_revno, last_revision_id = branch.last_revision_info()
416
341
# if get_rev_id supported negative revnos, there would not be a
417
342
# need for this special case.
418
if (-revno) >= last_revno:
343
if (-revno) >= len(revs):
421
revno = last_revno + revno + 1
346
revno = len(revs) + revno + 1
423
revision_id = branch.get_rev_id(revno, revs_or_none)
348
revision_id = branch.get_rev_id(revno, revs)
424
349
except errors.NoSuchRevision:
425
350
raise errors.InvalidRevisionSpec(self.user_spec, branch)
426
return branch, revno, revision_id
428
def _as_revision_id(self, context_branch):
429
# We would have the revno here, but we don't really care
430
branch, revno, revision_id = self._lookup(context_branch, None)
351
return RevisionInfo(branch, revno, revision_id)
433
353
def needs_branch(self):
434
354
return self.spec.find(':') == -1
450
371
help_txt = """Selects a revision using the revision id.
452
373
Supply a specific revision id, that can be used to specify any
453
revision id in the ancestry of the branch.
374
revision id in the ancestry of the branch.
454
375
Including merges, and pending merges.
457
377
revid:aaaa@bbbb-123456789 -> Select revision 'aaaa@bbbb-123456789'
460
379
prefix = 'revid:'
462
381
def _match_on(self, branch, revs):
463
# self.spec comes straight from parsing the command line arguments,
464
# so we expect it to be a Unicode string. Switch it to the internal
466
revision_id = osutils.safe_revision_id(self.spec, warn=False)
467
return RevisionInfo.from_revision_id(branch, revision_id, revs)
469
def _as_revision_id(self, context_branch):
470
return osutils.safe_revision_id(self.spec, warn=False)
382
return RevisionInfo.from_revision_id(branch, self.spec, revs)
384
SPEC_TYPES.append(RevisionSpec_revid)
474
387
class RevisionSpec_last(RevisionSpec):
479
392
Supply a positive number to get the nth revision from the end.
480
393
This is the same as supplying negative numbers to the 'revno:' spec.
483
395
last:1 -> return the last revision
484
396
last:3 -> return the revision 2 before the end.
489
401
def _match_on(self, branch, revs):
490
revno, revision_id = self._revno_and_revision_id(branch, revs)
491
return RevisionInfo(branch, revno, revision_id)
493
def _revno_and_revision_id(self, context_branch, revs_or_none):
494
last_revno, last_revision_id = context_branch.last_revision_info()
496
402
if self.spec == '':
498
raise errors.NoCommits(context_branch)
499
return last_revno, last_revision_id
404
raise errors.NoCommits(branch)
405
return RevisionInfo(branch, len(revs), revs[-1])
502
408
offset = int(self.spec)
503
409
except ValueError, e:
504
raise errors.InvalidRevisionSpec(self.user_spec, context_branch, e)
410
raise errors.InvalidRevisionSpec(self.user_spec, branch, e)
507
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
413
raise errors.InvalidRevisionSpec(self.user_spec, branch,
508
414
'you must supply a positive value')
510
revno = last_revno - offset + 1
415
revno = len(revs) - offset + 1
512
revision_id = context_branch.get_rev_id(revno, revs_or_none)
417
revision_id = branch.get_rev_id(revno, revs)
513
418
except errors.NoSuchRevision:
514
raise errors.InvalidRevisionSpec(self.user_spec, context_branch)
515
return revno, revision_id
517
def _as_revision_id(self, context_branch):
518
# We compute the revno as part of the process, but we don't really care
520
revno, revision_id = self._revno_and_revision_id(context_branch, None)
419
raise errors.InvalidRevisionSpec(self.user_spec, branch)
420
return RevisionInfo(branch, revno, revision_id)
422
SPEC_TYPES.append(RevisionSpec_last)
525
425
class RevisionSpec_before(RevisionSpec):
528
428
help_txt = """Selects the parent of the revision specified.
530
Supply any revision spec to return the parent of that revision. This is
531
mostly useful when inspecting revisions that are not in the revision history
430
Supply any revision spec to return the parent of that revision.
534
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.
538
436
before:1913 -> Return the parent of revno 1913 (revno 1912)
539
437
before:revid:aaaa@bbbb-1234567890 -> return the parent of revision
540
438
aaaa@bbbb-1234567890
541
bzr diff -r before:1913..1913
542
-> Find the changes between revision 1913 and its parent (1912).
543
(What changes did revision 1913 introduce).
544
This is equivalent to: bzr diff -c 1913
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)
547
444
prefix = 'before:'
549
446
def _match_on(self, branch, revs):
550
447
r = RevisionSpec.from_string(self.spec)._match_on(branch, revs)
573
470
return RevisionInfo(branch, revno, revision_id)
575
def _as_revision_id(self, context_branch):
576
base_revspec = RevisionSpec.from_string(self.spec)
577
base_revision_id = base_revspec.as_revision_id(context_branch)
578
if base_revision_id == revision.NULL_REVISION:
579
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
580
'cannot go before the null: revision')
581
context_repo = context_branch.repository
582
context_repo.lock_read()
584
parent_map = context_repo.get_parent_map([base_revision_id])
586
context_repo.unlock()
587
if base_revision_id not in parent_map:
588
# Ghost, or unknown revision id
589
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
590
'cannot find the matching revision')
591
parents = parent_map[base_revision_id]
593
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
594
'No parents for revision.')
472
SPEC_TYPES.append(RevisionSpec_before)
599
475
class RevisionSpec_tag(RevisionSpec):
739
612
that your branch introduces, while excluding the changes that you
740
613
have not merged from the remote branch.
744
616
ancestor:/path/to/branch
745
617
$ bzr diff -r ancestor:../../mainline/branch
747
619
prefix = 'ancestor:'
749
621
def _match_on(self, branch, revs):
622
from bzrlib.branch import Branch
750
624
trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
751
return self._find_revision_info(branch, self.spec)
753
def _as_revision_id(self, context_branch):
754
return self._find_revision_id(context_branch, self.spec)
757
def _find_revision_info(branch, other_location):
758
revision_id = RevisionSpec_ancestor._find_revision_id(branch,
625
other_branch = Branch.open(self.spec)
626
revision_a = branch.last_revision()
627
revision_b = other_branch.last_revision()
628
for r, b in ((revision_a, branch), (revision_b, other_branch)):
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,
761
revno = branch.revision_id_to_revno(revision_id)
636
revno = branch.revision_id_to_revno(rev_id)
762
637
except errors.NoSuchRevision:
764
return RevisionInfo(branch, revno, revision_id)
767
def _find_revision_id(branch, other_location):
768
from bzrlib.branch import Branch
772
revision_a = revision.ensure_null(branch.last_revision())
773
if revision_a == revision.NULL_REVISION:
774
raise errors.NoCommits(branch)
775
if other_location == '':
776
other_location = branch.get_parent()
777
other_branch = Branch.open(other_location)
778
other_branch.lock_read()
780
revision_b = revision.ensure_null(other_branch.last_revision())
781
if revision_b == revision.NULL_REVISION:
782
raise errors.NoCommits(other_branch)
783
graph = branch.repository.get_graph(other_branch.repository)
784
rev_id = graph.find_unique_lca(revision_a, revision_b)
786
other_branch.unlock()
787
if rev_id == revision.NULL_REVISION:
788
raise errors.NoCommonAncestor(revision_a, revision_b)
639
return RevisionInfo(branch, revno, rev_id)
641
SPEC_TYPES.append(RevisionSpec_ancestor)
796
644
class RevisionSpec_branch(RevisionSpec):
820
666
except errors.NoSuchRevision:
822
668
return RevisionInfo(branch, revno, revision_b)
824
def _as_revision_id(self, context_branch):
825
from bzrlib.branch import Branch
826
other_branch = Branch.open(self.spec)
827
last_revision = other_branch.last_revision()
828
last_revision = revision.ensure_null(last_revision)
829
context_branch.fetch(other_branch, last_revision)
830
if last_revision == revision.NULL_REVISION:
831
raise errors.NoCommits(other_branch)
834
def _as_tree(self, context_branch):
835
from bzrlib.branch import Branch
836
other_branch = Branch.open(self.spec)
837
last_revision = other_branch.last_revision()
838
last_revision = revision.ensure_null(last_revision)
839
if last_revision == revision.NULL_REVISION:
840
raise errors.NoCommits(other_branch)
841
return other_branch.repository.revision_tree(last_revision)
845
class RevisionSpec_submit(RevisionSpec_ancestor):
846
"""Selects a common ancestor with a submit branch."""
848
help_txt = """Selects a common ancestor with the submit branch.
850
Diffing against this shows all the changes that were made in this branch,
851
and is a good predictor of what merge will do. The submit branch is
852
used by the bundle and merge directive commands. If no submit branch
853
is specified, the parent branch is used instead.
855
The common ancestor is the last revision that existed in both
856
branches. Usually this is the branch point, but it could also be
857
a revision that was merged.
861
$ bzr diff -r submit:
866
def _get_submit_location(self, branch):
867
submit_location = branch.get_submit_branch()
868
location_type = 'submit branch'
869
if submit_location is None:
870
submit_location = branch.get_parent()
871
location_type = 'parent branch'
872
if submit_location is None:
873
raise errors.NoSubmitBranch(branch)
874
trace.note('Using %s %s', location_type, submit_location)
875
return submit_location
877
def _match_on(self, branch, revs):
878
trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
879
return self._find_revision_info(branch,
880
self._get_submit_location(branch))
882
def _as_revision_id(self, context_branch):
883
return self._find_revision_id(context_branch,
884
self._get_submit_location(context_branch))
887
# The order in which we want to DWIM a revision spec without any prefix.
888
# revno is always tried first and isn't listed here, this is used by
889
# RevisionSpec_dwim._match_on
891
RevisionSpec_tag, # Let's try for a tag
892
RevisionSpec_revid, # Maybe it's a revid?
893
RevisionSpec_date, # Perhaps a date?
894
RevisionSpec_branch, # OK, last try, maybe it's a branch
898
revspec_registry = registry.Registry()
899
def _register_revspec(revspec):
900
revspec_registry.register(revspec.prefix, revspec)
902
_register_revspec(RevisionSpec_revno)
903
_register_revspec(RevisionSpec_revid)
904
_register_revspec(RevisionSpec_last)
905
_register_revspec(RevisionSpec_before)
906
_register_revspec(RevisionSpec_tag)
907
_register_revspec(RevisionSpec_date)
908
_register_revspec(RevisionSpec_ancestor)
909
_register_revspec(RevisionSpec_branch)
910
_register_revspec(RevisionSpec_submit)
912
# classes in this list should have a "prefix" attribute, against which
913
# string specs are matched
914
SPEC_TYPES = symbol_versioning.deprecated_list(
915
symbol_versioning.deprecated_in((1, 12, 0)), "SPEC_TYPES", [])
670
SPEC_TYPES.append(RevisionSpec_branch)