13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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(), """
22
23
from bzrlib import (
24
branch as _mod_branch,
109
117
return RevisionInfo(branch, revno, revision_id)
112
# classes in this list should have a "prefix" attribute, against which
113
# string specs are matched
118
120
class RevisionSpec(object):
119
121
"""A parsed revision specification."""
121
123
help_txt = """A parsed revision specification.
123
A revision specification can be an integer, in which case it is
124
assumed to be a revno (though this will translate negative values
125
into positive ones); or it can be a string, in which case it is
126
parsed for something like 'date:' or 'revid:' etc.
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.
128
130
Revision specs are an UI element, and they have been moved out
129
131
of the branch class to leave "back-end" classes unaware of such
138
140
wants_revision_history = True
140
def __new__(cls, spec, _internal=False):
142
return object.__new__(cls, spec, _internal=_internal)
144
symbol_versioning.warn('Creating a RevisionSpec directly has'
145
' been deprecated in version 0.11. Use'
146
' RevisionSpec.from_string()'
148
DeprecationWarning, stacklevel=2)
149
return RevisionSpec.from_string(spec)
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
152
151
def from_string(spec):
163
162
return RevisionSpec(None, _internal=True)
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)
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
# 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)
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)
182
174
def __init__(self, spec, _internal=False):
183
175
"""Create a RevisionSpec referring to the Null revision.
253
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)
255
265
def __repr__(self):
256
266
# this is mostly for helping with testing
257
267
return '<%s %s>' % (self.__class__.__name__,
260
270
def needs_branch(self):
261
271
"""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))
277
361
class RevisionSpec_revno(RevisionSpec):
278
362
"""Selects a revision using a number."""
280
364
help_txt = """Selects a revision using a number.
282
366
Use an integer to specify a revision in the history of the branch.
283
Optionally a branch can be specified. The 'revno:' prefix is optional.
284
A negative number will count from the end of the branch (-1 is the
285
last revision, -2 the previous one). If the negative number is larger
286
than the branch's history, the first revision is returned.
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.
289
revno:1 -> return the first revision
373
revno:1 -> return the first revision of this branch
290
374
revno:3:/path/to/branch -> return the 3rd revision of
291
375
the branch '/path/to/branch'
292
376
revno:-1 -> The last revision in a branch.
341
425
revs_or_none = None
346
revision_id_to_revno = branch.get_revision_id_to_revno_map()
347
revisions = [revision_id for revision_id, revno
348
in revision_id_to_revno.iteritems()
349
if revno == match_revno]
352
if len(revisions) != 1:
353
return branch, None, None
429
revision_id = branch.dotted_revno_to_revision_id(match_revno,
431
except errors.NoSuchRevision:
432
raise errors.InvalidRevisionSpec(self.user_spec, branch)
355
434
# there is no traditional 'revno' for dotted-decimal revnos.
356
435
# so for API compatability we return None.
357
return branch, None, revisions[0]
436
return branch, None, revision_id
359
438
last_revno, last_revision_id = branch.last_revision_info()
385
464
return self.spec[self.spec.find(':')+1:]
388
467
RevisionSpec_int = RevisionSpec_revno
390
SPEC_TYPES.append(RevisionSpec_revno)
393
class RevisionSpec_revid(RevisionSpec):
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):
394
479
"""Selects a revision using the revision id."""
396
481
help_txt = """Selects a revision using the revision id.
398
483
Supply a specific revision id, that can be used to specify any
399
revision id in the ancestry of the branch.
484
revision id in the ancestry of the branch.
400
485
Including merges, and pending merges.
406
491
prefix = 'revid:'
408
def _match_on(self, branch, revs):
493
def _as_revision_id(self, context_branch):
409
494
# self.spec comes straight from parsing the command line arguments,
410
495
# so we expect it to be a Unicode string. Switch it to the internal
411
496
# representation.
412
revision_id = osutils.safe_revision_id(self.spec, warn=False)
413
return RevisionInfo.from_revision_id(branch, revision_id, revs)
415
def _as_revision_id(self, context_branch):
416
497
return osutils.safe_revision_id(self.spec, warn=False)
418
SPEC_TYPES.append(RevisionSpec_revid)
421
501
class RevisionSpec_last(RevisionSpec):
476
555
help_txt = """Selects the parent of the revision specified.
478
Supply any revision spec to return the parent of that revision.
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
479
561
It is an error to request the parent of the null revision (before:0).
480
This is mostly useful when inspecting revisions that are not in the
481
revision history of a branch.
485
565
before:1913 -> Return the parent of revno 1913 (revno 1912)
486
566
before:revid:aaaa@bbbb-1234567890 -> return the parent of revision
487
567
aaaa@bbbb-1234567890
488
bzr diff -r before:revid:aaaa..revid:aaaa
489
-> Find the changes between revision 'aaaa' and its parent.
490
(what changes did 'aaaa' introduce)
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
493
574
prefix = 'before:'
495
576
def _match_on(self, branch, revs):
496
577
r = RevisionSpec.from_string(self.spec)._match_on(branch, revs)
777
864
raise errors.NoCommits(other_branch)
778
865
return last_revision
780
SPEC_TYPES.append(RevisionSpec_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):
783
884
class RevisionSpec_submit(RevisionSpec_ancestor):
822
923
self._get_submit_location(context_branch))
825
SPEC_TYPES.append(RevisionSpec_submit)
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)