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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
20
from bzrlib.lazy_import import lazy_import
19
21
lazy_import(globals(), """
23
26
from bzrlib import (
24
branch as _mod_branch,
117
112
return RevisionInfo(branch, revno, revision_id)
115
# classes in this list should have a "prefix" attribute, against which
116
# string specs are matched
120
121
class RevisionSpec(object):
121
122
"""A parsed revision specification."""
123
124
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.
126
A revision specification can be an integer, in which case it is
127
assumed to be a revno (though this will translate negative values
128
into positive ones); or it can be a string, in which case it is
129
parsed for something like 'date:' or 'revid:' etc.
130
131
Revision specs are an UI element, and they have been moved out
131
132
of the branch class to leave "back-end" classes unaware of such
162
155
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)
156
for spectype in SPEC_TYPES:
157
if spec.startswith(spectype.prefix):
158
trace.mutter('Returning RevisionSpec %s for %s',
159
spectype.__name__, spec)
160
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)
162
# RevisionSpec_revno is special cased, because it is the only
163
# one that directly handles plain integers
164
# TODO: This should not be special cased rather it should be
165
# a method invocation on spectype.canparse()
167
if _revno_regex is None:
168
_revno_regex = re.compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
169
if _revno_regex.match(spec) is not None:
170
return RevisionSpec_revno(spec, _internal=True)
172
raise errors.NoSuchRevisionSpec(spec)
174
174
def __init__(self, spec, _internal=False):
175
175
"""Create a RevisionSpec referring to the Null revision.
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
288
class RevisionSpec_revno(RevisionSpec):
362
289
"""Selects a revision using a number."""
364
291
help_txt = """Selects a revision using a number.
366
293
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.
294
Optionally a branch can be specified. The 'revno:' prefix is optional.
295
A negative number will count from the end of the branch (-1 is the
296
last revision, -2 the previous one). If the negative number is larger
297
than the branch's history, the first revision is returned.
373
300
revno:1 -> return the first revision of this branch
425
352
revs_or_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)
357
revision_id_to_revno = branch.get_revision_id_to_revno_map()
358
revisions = [revision_id for revision_id, revno
359
in revision_id_to_revno.iteritems()
360
if revno == match_revno]
363
if len(revisions) != 1:
364
return branch, None, None
434
366
# there is no traditional 'revno' for dotted-decimal revnos.
435
367
# so for API compatability we return None.
436
return branch, None, revision_id
368
return branch, None, revisions[0]
438
370
last_revno, last_revision_id = branch.last_revision_info()
464
396
return self.spec[self.spec.find(':')+1:]
467
399
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):
401
SPEC_TYPES.append(RevisionSpec_revno)
404
class RevisionSpec_revid(RevisionSpec):
479
405
"""Selects a revision using the revision id."""
481
407
help_txt = """Selects a revision using the revision id.
483
409
Supply a specific revision id, that can be used to specify any
484
revision id in the ancestry of the branch.
410
revision id in the ancestry of the branch.
485
411
Including merges, and pending merges.
491
417
prefix = 'revid:'
493
def _as_revision_id(self, context_branch):
419
def _match_on(self, branch, revs):
494
420
# self.spec comes straight from parsing the command line arguments,
495
421
# so we expect it to be a Unicode string. Switch it to the internal
496
422
# representation.
423
revision_id = osutils.safe_revision_id(self.spec, warn=False)
424
return RevisionInfo.from_revision_id(branch, revision_id, revs)
426
def _as_revision_id(self, context_branch):
497
427
return osutils.safe_revision_id(self.spec, warn=False)
429
SPEC_TYPES.append(RevisionSpec_revid)
501
432
class RevisionSpec_last(RevisionSpec):
923
844
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)
847
SPEC_TYPES.append(RevisionSpec_submit)