1
# Copyright (C) 2005-2010 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
from bzrlib.lazy_import import lazy_import
19
lazy_import(globals(), """
24
branch as _mod_branch,
43
class RevisionInfo(object):
44
"""The results of applying a revision specification to a branch."""
46
help_txt = """The results of applying a revision specification to a branch.
48
An instance has two useful attributes: revno, and rev_id.
50
They can also be accessed as spec[0] and spec[1] respectively,
51
so that you can write code like:
52
revno, rev_id = RevisionSpec(branch, spec)
53
although this is probably going to be deprecated later.
55
This class exists mostly to be the return value of a RevisionSpec,
56
so that you can access the member you're interested in (number or id)
57
or treat the result as a tuple.
60
def __init__(self, branch, revno, rev_id=_marker):
64
# allow caller to be lazy
65
if self.revno is None:
68
self.rev_id = branch.get_rev_id(self.revno)
72
def __nonzero__(self):
73
# first the easy ones...
74
if self.rev_id is None:
76
if self.revno is not None:
78
# TODO: otherwise, it should depend on how I was built -
79
# if it's in_history(branch), then check revision_history(),
80
# if it's in_store(branch), do the check below
81
return self.branch.repository.has_revision(self.rev_id)
86
def __getitem__(self, index):
87
if index == 0: return self.revno
88
if index == 1: return self.rev_id
89
raise IndexError(index)
92
return self.branch.repository.get_revision(self.rev_id)
94
def __eq__(self, other):
95
if type(other) not in (tuple, list, type(self)):
97
if type(other) is type(self) and self.branch is not other.branch:
99
return tuple(self) == tuple(other)
102
return '<bzrlib.revisionspec.RevisionInfo object %s, %s for %r>' % (
103
self.revno, self.rev_id, self.branch)
106
def from_revision_id(branch, revision_id, revs):
107
"""Construct a RevisionInfo given just the id.
109
Use this if you don't know or care what the revno is.
111
if revision_id == revision.NULL_REVISION:
112
return RevisionInfo(branch, 0, revision_id)
114
revno = revs.index(revision_id) + 1
117
return RevisionInfo(branch, revno, revision_id)
120
class RevisionSpec(object):
121
"""A parsed revision specification."""
123
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.
130
Revision specs are an UI element, and they have been moved out
131
of the branch class to leave "back-end" classes unaware of such
132
details. Code that gets a revno or rev_id from other code should
133
not be using revision specs - revnos and revision ids are the
134
accepted ways to refer to revisions internally.
136
(Equivalent to the old Branch method get_revision_info())
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
151
def from_string(spec):
152
"""Parse a revision spec string into a RevisionSpec object.
154
:param spec: A string specified by the user
155
:return: A RevisionSpec object that understands how to parse the
158
if not isinstance(spec, (type(None), basestring)):
159
raise TypeError('error')
162
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)
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)
174
def __init__(self, spec, _internal=False):
175
"""Create a RevisionSpec referring to the Null revision.
177
:param spec: The original spec supplied by the user
178
:param _internal: Used to ensure that RevisionSpec is not being
179
called directly. Only from RevisionSpec.from_string()
182
symbol_versioning.warn('Creating a RevisionSpec directly has'
183
' been deprecated in version 0.11. Use'
184
' RevisionSpec.from_string()'
186
DeprecationWarning, stacklevel=2)
187
self.user_spec = spec
188
if self.prefix and spec.startswith(self.prefix):
189
spec = spec[len(self.prefix):]
192
def _match_on(self, branch, revs):
193
trace.mutter('Returning RevisionSpec._match_on: None')
194
return RevisionInfo(branch, None, None)
196
def _match_on_and_check(self, branch, revs):
197
info = self._match_on(branch, revs)
200
elif info == (None, None):
201
# special case - nothing supplied
204
raise errors.InvalidRevisionSpec(self.user_spec, branch)
206
raise errors.InvalidRevisionSpec(self.spec, branch)
208
def in_history(self, branch):
210
if self.wants_revision_history:
211
revs = branch.revision_history()
215
# this should never trigger.
216
# TODO: make it a deprecated code path. RBC 20060928
218
return self._match_on_and_check(branch, revs)
220
# FIXME: in_history is somewhat broken,
221
# it will return non-history revisions in many
222
# circumstances. The expected facility is that
223
# in_history only returns revision-history revs,
224
# in_store returns any rev. RBC 20051010
225
# aliases for now, when we fix the core logic, then they
226
# will do what you expect.
227
in_store = in_history
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)
266
# this is mostly for helping with testing
267
return '<%s %s>' % (self.__class__.__name__,
270
def needs_branch(self):
271
"""Whether this revision spec needs a branch.
273
Set this to False the branch argument of _match_on is not used.
277
def get_branch(self):
278
"""When the revision specifier contains a branch location, return it.
280
Otherwise, return None.
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
class RevisionSpec_revno(RevisionSpec):
362
"""Selects a revision using a number."""
364
help_txt = """Selects a revision using a number.
366
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.
373
revno:1 -> return the first revision of this branch
374
revno:3:/path/to/branch -> return the 3rd revision of
375
the branch '/path/to/branch'
376
revno:-1 -> The last revision in a branch.
377
-2:http://other/branch -> The second to last revision in the
379
-1000000 -> Most likely the first revision, unless
380
your history is very long.
383
wants_revision_history = False
385
def _match_on(self, branch, revs):
386
"""Lookup a revision by revision number"""
387
branch, revno, revision_id = self._lookup(branch, revs)
388
return RevisionInfo(branch, revno, revision_id)
390
def _lookup(self, branch, revs_or_none):
391
loc = self.spec.find(':')
393
revno_spec = self.spec
396
revno_spec = self.spec[:loc]
397
branch_spec = self.spec[loc+1:]
401
raise errors.InvalidRevisionSpec(self.user_spec,
402
branch, 'cannot have an empty revno and no branch')
406
revno = int(revno_spec)
409
# dotted decimal. This arguably should not be here
410
# but the from_string method is a little primitive
411
# right now - RBC 20060928
413
match_revno = tuple((int(number) for number in revno_spec.split('.')))
414
except ValueError, e:
415
raise errors.InvalidRevisionSpec(self.user_spec, branch, e)
420
# the user has override the branch to look in.
421
# we need to refresh the revision_history map and
423
from bzrlib.branch import Branch
424
branch = Branch.open(branch_spec)
429
revision_id = branch.dotted_revno_to_revision_id(match_revno,
431
except errors.NoSuchRevision:
432
raise errors.InvalidRevisionSpec(self.user_spec, branch)
434
# there is no traditional 'revno' for dotted-decimal revnos.
435
# so for API compatability we return None.
436
return branch, None, revision_id
438
last_revno, last_revision_id = branch.last_revision_info()
440
# if get_rev_id supported negative revnos, there would not be a
441
# need for this special case.
442
if (-revno) >= last_revno:
445
revno = last_revno + revno + 1
447
revision_id = branch.get_rev_id(revno, revs_or_none)
448
except errors.NoSuchRevision:
449
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)
457
def needs_branch(self):
458
return self.spec.find(':') == -1
460
def get_branch(self):
461
if self.spec.find(':') == -1:
464
return self.spec[self.spec.find(':')+1:]
467
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):
479
"""Selects a revision using the revision id."""
481
help_txt = """Selects a revision using the revision id.
483
Supply a specific revision id, that can be used to specify any
484
revision id in the ancestry of the branch.
485
Including merges, and pending merges.
488
revid:aaaa@bbbb-123456789 -> Select revision 'aaaa@bbbb-123456789'
493
def _as_revision_id(self, context_branch):
494
# self.spec comes straight from parsing the command line arguments,
495
# so we expect it to be a Unicode string. Switch it to the internal
497
return osutils.safe_revision_id(self.spec, warn=False)
501
class RevisionSpec_last(RevisionSpec):
502
"""Selects the nth revision from the end."""
504
help_txt = """Selects the nth revision from the end.
506
Supply a positive number to get the nth revision from the end.
507
This is the same as supplying negative numbers to the 'revno:' spec.
510
last:1 -> return the last revision
511
last:3 -> return the revision 2 before the end.
516
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()
525
raise errors.NoCommits(context_branch)
526
return last_revno, last_revision_id
529
offset = int(self.spec)
530
except ValueError, e:
531
raise errors.InvalidRevisionSpec(self.user_spec, context_branch, e)
534
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
535
'you must supply a positive value')
537
revno = last_revno - offset + 1
539
revision_id = context_branch.get_rev_id(revno, revs_or_none)
540
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)
552
class RevisionSpec_before(RevisionSpec):
553
"""Selects the parent of the revision specified."""
555
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
561
It is an error to request the parent of the null revision (before:0).
565
before:1913 -> Return the parent of revno 1913 (revno 1912)
566
before:revid:aaaa@bbbb-1234567890 -> return the parent of revision
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
576
def _match_on(self, branch, revs):
577
r = RevisionSpec.from_string(self.spec)._match_on(branch, revs)
579
raise errors.InvalidRevisionSpec(self.user_spec, branch,
580
'cannot go before the null: revision')
582
# We need to use the repository history here
583
rev = branch.repository.get_revision(r.rev_id)
584
if not rev.parent_ids:
586
revision_id = revision.NULL_REVISION
588
revision_id = rev.parent_ids[0]
590
revno = revs.index(revision_id) + 1
596
revision_id = branch.get_rev_id(revno, revs)
597
except errors.NoSuchRevision:
598
raise errors.InvalidRevisionSpec(self.user_spec,
600
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.')
626
class RevisionSpec_tag(RevisionSpec):
627
"""Select a revision identified by tag name"""
629
help_txt = """Selects a revision identified by a tag name.
631
Tags are stored in the branch and created by the 'tag' command.
635
dwim_catchable_exceptions = (errors.NoSuchTag, errors.TagsNotSupported)
637
def _match_on(self, branch, revs):
638
# Can raise tags not supported, NoSuchTag, etc
639
return RevisionInfo.from_revision_id(branch,
640
branch.tags.lookup_tag(self.spec),
643
def _as_revision_id(self, context_branch):
644
return context_branch.tags.lookup_tag(self.spec)
648
class _RevListToTimestamps(object):
649
"""This takes a list of revisions, and allows you to bisect by date"""
651
__slots__ = ['revs', 'branch']
653
def __init__(self, revs, branch):
657
def __getitem__(self, index):
658
"""Get the date of the index'd item"""
659
r = self.branch.repository.get_revision(self.revs[index])
660
# TODO: Handle timezone.
661
return datetime.datetime.fromtimestamp(r.timestamp)
664
return len(self.revs)
667
class RevisionSpec_date(RevisionSpec):
668
"""Selects a revision on the basis of a datestamp."""
670
help_txt = """Selects a revision on the basis of a datestamp.
672
Supply a datestamp to select the first revision that matches the date.
673
Date can be 'yesterday', 'today', 'tomorrow' or a YYYY-MM-DD string.
674
Matches the first entry after a given date (either at midnight or
675
at a specified time).
677
One way to display all the changes since yesterday would be::
679
bzr log -r date:yesterday..
683
date:yesterday -> select the first revision since yesterday
684
date:2006-08-14,17:10:14 -> select the first revision after
685
August 14th, 2006 at 5:10pm.
688
_date_regex = lazy_regex.lazy_compile(
689
r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
691
r'(?P<time>(?P<hour>\d\d):(?P<minute>\d\d)(:(?P<second>\d\d))?)?'
694
def _match_on(self, branch, revs):
695
"""Spec for date revisions:
697
value can be 'yesterday', 'today', 'tomorrow' or a YYYY-MM-DD string.
698
matches the first entry after a given date (either at midnight or
699
at a specified time).
701
# XXX: This doesn't actually work
702
# So the proper way of saying 'give me all entries for today' is:
703
# -r date:yesterday..date:today
704
today = datetime.datetime.fromordinal(datetime.date.today().toordinal())
705
if self.spec.lower() == 'yesterday':
706
dt = today - datetime.timedelta(days=1)
707
elif self.spec.lower() == 'today':
709
elif self.spec.lower() == 'tomorrow':
710
dt = today + datetime.timedelta(days=1)
712
m = self._date_regex.match(self.spec)
713
if not m or (not m.group('date') and not m.group('time')):
714
raise errors.InvalidRevisionSpec(self.user_spec,
715
branch, 'invalid date')
719
year = int(m.group('year'))
720
month = int(m.group('month'))
721
day = int(m.group('day'))
728
hour = int(m.group('hour'))
729
minute = int(m.group('minute'))
730
if m.group('second'):
731
second = int(m.group('second'))
735
hour, minute, second = 0,0,0
737
raise errors.InvalidRevisionSpec(self.user_spec,
738
branch, 'invalid date')
740
dt = datetime.datetime(year=year, month=month, day=day,
741
hour=hour, minute=minute, second=second)
744
rev = bisect.bisect(_RevListToTimestamps(revs, branch), dt)
748
raise errors.InvalidRevisionSpec(self.user_spec, branch)
750
return RevisionInfo(branch, rev + 1)
754
class RevisionSpec_ancestor(RevisionSpec):
755
"""Selects a common ancestor with a second branch."""
757
help_txt = """Selects a common ancestor with a second branch.
759
Supply the path to a branch to select the common ancestor.
761
The common ancestor is the last revision that existed in both
762
branches. Usually this is the branch point, but it could also be
763
a revision that was merged.
765
This is frequently used with 'diff' to return all of the changes
766
that your branch introduces, while excluding the changes that you
767
have not merged from the remote branch.
771
ancestor:/path/to/branch
772
$ bzr diff -r ancestor:../../mainline/branch
776
def _match_on(self, branch, revs):
777
trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
778
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
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
from bzrlib.branch import Branch
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)
811
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)
823
class RevisionSpec_branch(RevisionSpec):
824
"""Selects the last revision of a specified branch."""
826
help_txt = """Selects the last revision of a specified branch.
828
Supply the path to a branch to select its last revision.
832
branch:/path/to/branch
835
dwim_catchable_exceptions = (errors.NotBranchError,)
837
def _match_on(self, branch, revs):
838
from bzrlib.branch import Branch
839
other_branch = Branch.open(self.spec)
840
revision_b = other_branch.last_revision()
841
if revision_b in (None, revision.NULL_REVISION):
842
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
852
revno = branch.revision_id_to_revno(revision_b)
853
except errors.NoSuchRevision:
855
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):
884
class RevisionSpec_submit(RevisionSpec_ancestor):
885
"""Selects a common ancestor with a submit branch."""
887
help_txt = """Selects a common ancestor with the submit branch.
889
Diffing against this shows all the changes that were made in this branch,
890
and is a good predictor of what merge will do. The submit branch is
891
used by the bundle and merge directive commands. If no submit branch
892
is specified, the parent branch is used instead.
894
The common ancestor is the last revision that existed in both
895
branches. Usually this is the branch point, but it could also be
896
a revision that was merged.
900
$ bzr diff -r submit:
905
def _get_submit_location(self, branch):
906
submit_location = branch.get_submit_branch()
907
location_type = 'submit branch'
908
if submit_location is None:
909
submit_location = branch.get_parent()
910
location_type = 'parent branch'
911
if submit_location is None:
912
raise errors.NoSubmitBranch(branch)
913
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)