93
98
return '<bzrlib.revisionspec.RevisionInfo object %s, %s for %r>' % (
94
99
self.revno, self.rev_id, self.branch)
97
# classes in this list should have a "prefix" attribute, against which
98
# string specs are matched
102
def from_revision_id(branch, revision_id, revs):
103
"""Construct a RevisionInfo given just the id.
105
Use this if you don't know or care what the revno is.
107
if revision_id == revision.NULL_REVISION:
108
return RevisionInfo(branch, 0, revision_id)
110
revno = revs.index(revision_id) + 1
113
return RevisionInfo(branch, revno, revision_id)
100
116
_revno_regex = None
124
def __new__(cls, spec, _internal=False):
126
return object.__new__(cls, spec, _internal=_internal)
128
symbol_versioning.warn('Creating a RevisionSpec directly has'
129
' been deprecated in version 0.11. Use'
130
' RevisionSpec.from_string()'
132
DeprecationWarning, stacklevel=2)
133
return RevisionSpec.from_string(spec)
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
150
def from_string(spec):
147
161
return RevisionSpec(None, _internal=True)
149
assert isinstance(spec, basestring), \
150
"You should only supply strings not %s" % (type(spec),)
152
for spectype in SPEC_TYPES:
153
if spec.startswith(spectype.prefix):
154
trace.mutter('Returning RevisionSpec %s for %s',
155
spectype.__name__, spec)
156
return spectype(spec, _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)
158
# RevisionSpec_revno is special cased, because it is the only
159
# one that directly handles plain integers
160
# TODO: This should not be special cased rather it should be
161
# a method invocation on spectype.canparse()
163
if _revno_regex is None:
164
_revno_regex = re.compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
165
if _revno_regex.match(spec) is not None:
166
return RevisionSpec_revno(spec, _internal=True)
168
raise errors.NoSuchRevisionSpec(spec)
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
178
def __init__(self, spec, _internal=False):
171
179
"""Create a RevisionSpec referring to the Null revision.
220
230
# will do what you expect.
221
231
in_store = in_history
222
232
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)
224
269
def __repr__(self):
225
270
# this is mostly for helping with testing
226
271
return '<%s %s>' % (self.__class__.__name__,
229
274
def needs_branch(self):
230
275
"""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)
246
337
class RevisionSpec_revno(RevisionSpec):
247
338
"""Selects a revision using a number."""
249
340
help_txt = """Selects a revision using a number.
251
342
Use an integer to specify a revision in the history of the branch.
252
Optionally a branch can be specified. The 'revno:' prefix is optional.
253
A negative number will count from the end of the branch (-1 is the
254
last revision, -2 the previous one). If the negative number is larger
255
than the branch's history, the first revision is returned.
257
revno:1 -> return the first revision
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
258
350
revno:3:/path/to/branch -> return the 3rd revision of
259
351
the branch '/path/to/branch'
260
352
revno:-1 -> The last revision in a branch.
301
398
# the branch object.
302
399
from bzrlib.branch import Branch
303
400
branch = Branch.open(branch_spec)
304
# Need to use a new revision history
305
# because we are using a specific branch
306
revs = branch.revision_history()
311
last_rev = branch.last_revision()
312
merge_sorted_revisions = tsort.merge_sort(
313
branch.repository.get_revision_graph(last_rev),
317
return item[3] == match_revno
318
revisions = filter(match, merge_sorted_revisions)
321
if len(revisions) != 1:
322
return RevisionInfo(branch, None, None)
405
revision_id = branch.dotted_revno_to_revision_id(match_revno,
407
except errors.NoSuchRevision:
408
raise errors.InvalidRevisionSpec(self.user_spec, branch)
324
410
# there is no traditional 'revno' for dotted-decimal revnos.
325
411
# so for API compatability we return None.
326
return RevisionInfo(branch, None, revisions[0][1])
412
return branch, None, revision_id
414
last_revno, last_revision_id = branch.last_revision_info()
329
416
# if get_rev_id supported negative revnos, there would not be a
330
417
# need for this special case.
331
if (-revno) >= len(revs):
418
if (-revno) >= last_revno:
334
revno = len(revs) + revno + 1
421
revno = last_revno + revno + 1
336
revision_id = branch.get_rev_id(revno, revs)
423
revision_id = branch.get_rev_id(revno, revs_or_none)
337
424
except errors.NoSuchRevision:
338
425
raise errors.InvalidRevisionSpec(self.user_spec, branch)
339
return RevisionInfo(branch, revno, revision_id)
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)
341
433
def needs_branch(self):
342
434
return self.spec.find(':') == -1
359
450
help_txt = """Selects a revision using the revision id.
361
452
Supply a specific revision id, that can be used to specify any
362
revision id in the ancestry of the branch.
453
revision id in the ancestry of the branch.
363
454
Including merges, and pending merges.
365
457
revid:aaaa@bbbb-123456789 -> Select revision 'aaaa@bbbb-123456789'
367
460
prefix = 'revid:'
369
462
def _match_on(self, branch, revs):
371
revno = revs.index(self.spec) + 1
374
return RevisionInfo(branch, revno, self.spec)
376
SPEC_TYPES.append(RevisionSpec_revid)
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)
379
474
class RevisionSpec_last(RevisionSpec):
384
479
Supply a positive number to get the nth revision from the end.
385
480
This is the same as supplying negative numbers to the 'revno:' spec.
387
483
last:1 -> return the last revision
388
484
last:3 -> return the revision 2 before the end.
393
489
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()
394
496
if self.spec == '':
396
raise errors.NoCommits(branch)
397
return RevisionInfo(branch, len(revs), revs[-1])
498
raise errors.NoCommits(context_branch)
499
return last_revno, last_revision_id
400
502
offset = int(self.spec)
401
503
except ValueError, e:
402
raise errors.InvalidRevisionSpec(self.user_spec, branch, e)
504
raise errors.InvalidRevisionSpec(self.user_spec, context_branch, e)
405
raise errors.InvalidRevisionSpec(self.user_spec, branch,
507
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
406
508
'you must supply a positive value')
407
revno = len(revs) - offset + 1
510
revno = last_revno - offset + 1
409
revision_id = branch.get_rev_id(revno, revs)
512
revision_id = context_branch.get_rev_id(revno, revs_or_none)
410
513
except errors.NoSuchRevision:
411
raise errors.InvalidRevisionSpec(self.user_spec, branch)
412
return RevisionInfo(branch, revno, revision_id)
414
SPEC_TYPES.append(RevisionSpec_last)
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)
417
525
class RevisionSpec_before(RevisionSpec):
420
528
help_txt = """Selects the parent of the revision specified.
422
Supply any revision spec to return the parent of that revision.
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
423
534
It is an error to request the parent of the null revision (before:0).
424
This is mostly useful when inspecting revisions that are not in the
425
revision history of a branch.
428
538
before:1913 -> Return the parent of revno 1913 (revno 1912)
429
539
before:revid:aaaa@bbbb-1234567890 -> return the parent of revision
430
540
aaaa@bbbb-1234567890
431
bzr diff -r before:revid:aaaa..revid:aaaa
432
-> Find the changes between revision 'aaaa' and its parent.
433
(what changes did 'aaaa' introduce)
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
436
547
prefix = 'before:'
438
549
def _match_on(self, branch, revs):
439
550
r = RevisionSpec.from_string(self.spec)._match_on(branch, revs)
462
573
return RevisionInfo(branch, revno, revision_id)
464
SPEC_TYPES.append(RevisionSpec_before)
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.')
467
599
class RevisionSpec_tag(RevisionSpec):
468
"""To be implemented."""
470
help_txt = """To be implemented."""
600
"""Select a revision identified by tag name"""
602
help_txt = """Selects a revision identified by a tag name.
604
Tags are stored in the branch and created by the 'tag' command.
608
dwim_catchable_exceptions = (errors.NoSuchTag, errors.TagsNotSupported)
474
610
def _match_on(self, branch, revs):
475
raise errors.InvalidRevisionSpec(self.user_spec, branch,
476
'tag: namespace registered,'
477
' but not implemented')
479
SPEC_TYPES.append(RevisionSpec_tag)
611
# Can raise tags not supported, NoSuchTag, etc
612
return RevisionInfo.from_revision_id(branch,
613
branch.tags.lookup_tag(self.spec),
616
def _as_revision_id(self, context_branch):
617
return context_branch.tags.lookup_tag(self.spec)
482
621
class _RevListToTimestamps(object):
599
739
that your branch introduces, while excluding the changes that you
600
740
have not merged from the remote branch.
603
744
ancestor:/path/to/branch
604
745
$ bzr diff -r ancestor:../../mainline/branch
606
747
prefix = 'ancestor:'
608
749
def _match_on(self, branch, revs):
609
from bzrlib.branch import Branch
611
750
trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
612
other_branch = Branch.open(self.spec)
613
revision_a = branch.last_revision()
614
revision_b = other_branch.last_revision()
615
for r, b in ((revision_a, branch), (revision_b, other_branch)):
616
if r in (None, revision.NULL_REVISION):
617
raise errors.NoCommits(b)
618
revision_source = revision.MultipleRevisionSources(
619
branch.repository, other_branch.repository)
620
rev_id = revision.common_ancestor(revision_a, revision_b,
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,
623
revno = branch.revision_id_to_revno(rev_id)
761
revno = branch.revision_id_to_revno(revision_id)
624
762
except errors.NoSuchRevision:
626
return RevisionInfo(branch, revno, rev_id)
628
SPEC_TYPES.append(RevisionSpec_ancestor)
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)
631
796
class RevisionSpec_branch(RevisionSpec):
646
813
revision_b = other_branch.last_revision()
647
814
if revision_b in (None, revision.NULL_REVISION):
648
815
raise errors.NoCommits(other_branch)
649
# pull in the remote revisions so we can diff
650
branch.fetch(other_branch, revision_b)
817
branch = other_branch
820
# pull in the remote revisions so we can diff
821
branch.fetch(other_branch, revision_b)
822
except errors.ReadOnlyError:
823
branch = other_branch
652
825
revno = branch.revision_id_to_revno(revision_b)
653
826
except errors.NoSuchRevision:
655
828
return RevisionInfo(branch, revno, revision_b)
657
SPEC_TYPES.append(RevisionSpec_branch)
830
def _as_revision_id(self, context_branch):
831
from bzrlib.branch import Branch
832
other_branch = Branch.open(self.spec)
833
last_revision = other_branch.last_revision()
834
last_revision = revision.ensure_null(last_revision)
835
context_branch.fetch(other_branch, last_revision)
836
if last_revision == revision.NULL_REVISION:
837
raise errors.NoCommits(other_branch)
840
def _as_tree(self, context_branch):
841
from bzrlib.branch import Branch
842
other_branch = Branch.open(self.spec)
843
last_revision = other_branch.last_revision()
844
last_revision = revision.ensure_null(last_revision)
845
if last_revision == revision.NULL_REVISION:
846
raise errors.NoCommits(other_branch)
847
return other_branch.repository.revision_tree(last_revision)
849
def needs_branch(self):
852
def get_branch(self):
857
class RevisionSpec_submit(RevisionSpec_ancestor):
858
"""Selects a common ancestor with a submit branch."""
860
help_txt = """Selects a common ancestor with the submit branch.
862
Diffing against this shows all the changes that were made in this branch,
863
and is a good predictor of what merge will do. The submit branch is
864
used by the bundle and merge directive commands. If no submit branch
865
is specified, the parent branch is used instead.
867
The common ancestor is the last revision that existed in both
868
branches. Usually this is the branch point, but it could also be
869
a revision that was merged.
873
$ bzr diff -r submit:
878
def _get_submit_location(self, branch):
879
submit_location = branch.get_submit_branch()
880
location_type = 'submit branch'
881
if submit_location is None:
882
submit_location = branch.get_parent()
883
location_type = 'parent branch'
884
if submit_location is None:
885
raise errors.NoSubmitBranch(branch)
886
trace.note('Using %s %s', location_type, submit_location)
887
return submit_location
889
def _match_on(self, branch, revs):
890
trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
891
return self._find_revision_info(branch,
892
self._get_submit_location(branch))
894
def _as_revision_id(self, context_branch):
895
return self._find_revision_id(context_branch,
896
self._get_submit_location(context_branch))
899
# The order in which we want to DWIM a revision spec without any prefix.
900
# revno is always tried first and isn't listed here, this is used by
901
# RevisionSpec_dwim._match_on
903
RevisionSpec_tag, # Let's try for a tag
904
RevisionSpec_revid, # Maybe it's a revid?
905
RevisionSpec_date, # Perhaps a date?
906
RevisionSpec_branch, # OK, last try, maybe it's a branch
910
revspec_registry = registry.Registry()
911
def _register_revspec(revspec):
912
revspec_registry.register(revspec.prefix, revspec)
914
_register_revspec(RevisionSpec_revno)
915
_register_revspec(RevisionSpec_revid)
916
_register_revspec(RevisionSpec_last)
917
_register_revspec(RevisionSpec_before)
918
_register_revspec(RevisionSpec_tag)
919
_register_revspec(RevisionSpec_date)
920
_register_revspec(RevisionSpec_ancestor)
921
_register_revspec(RevisionSpec_branch)
922
_register_revspec(RevisionSpec_submit)
924
# classes in this list should have a "prefix" attribute, against which
925
# string specs are matched
926
SPEC_TYPES = symbol_versioning.deprecated_list(
927
symbol_versioning.deprecated_in((1, 12, 0)), "SPEC_TYPES", [])