14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
from __future__ import absolute_import
20
20
from bzrlib.lazy_import import lazy_import
21
21
lazy_import(globals(), """
25
26
from bzrlib import (
26
branch as _mod_branch,
32
from bzrlib.i18n import gettext
43
39
class RevisionInfo(object):
44
40
"""The results of applying a revision specification to a branch."""
57
53
or treat the result as a tuple.
60
def __init__(self, branch, revno=None, rev_id=None):
56
def __init__(self, branch, revno, rev_id=_marker):
61
57
self.branch = branch
62
self._has_revno = (revno is not None)
65
if self.rev_id is None and self._revno is not None:
66
60
# allow caller to be lazy
67
self.rev_id = branch.get_rev_id(self._revno)
71
if not self._has_revno and self.rev_id is not None:
73
self._revno = self.branch.revision_id_to_revno(self.rev_id)
74
except errors.NoSuchRevision:
76
self._has_revno = True
61
if self.revno is None:
64
self.rev_id = branch.get_rev_id(self.revno)
79
68
def __nonzero__(self):
69
# first the easy ones...
80
70
if self.rev_id is None:
72
if self.revno is not None:
82
74
# TODO: otherwise, it should depend on how I was built -
83
75
# if it's in_history(branch), then check revision_history(),
84
76
# if it's in_store(branch), do the check below
107
99
self.revno, self.rev_id, self.branch)
110
def from_revision_id(branch, revision_id, revs=symbol_versioning.DEPRECATED_PARAMETER):
102
def from_revision_id(branch, revision_id, revs):
111
103
"""Construct a RevisionInfo given just the id.
113
105
Use this if you don't know or care what the revno is.
115
if symbol_versioning.deprecated_passed(revs):
116
symbol_versioning.warn(
117
'RevisionInfo.from_revision_id(revs) was deprecated in 2.5.',
120
return RevisionInfo(branch, revno=None, rev_id=revision_id)
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)
116
# classes in this list should have a "prefix" attribute, against which
117
# string specs are matched
123
121
class RevisionSpec(object):
126
124
help_txt = """A parsed revision specification.
128
A revision specification is a string, which may be unambiguous about
129
what it represents by giving a prefix like 'date:' or 'revid:' etc,
130
or it may have no prefix, in which case it's tried against several
131
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.
133
131
Revision specs are an UI element, and they have been moved out
134
132
of the branch class to leave "back-end" classes unaware of such
143
# wants_revision_history has been deprecated in 2.5.
144
wants_revision_history = False
145
dwim_catchable_exceptions = (errors.InvalidRevisionSpec,)
146
"""Exceptions that RevisionSpec_dwim._match_on will catch.
148
If the revspec is part of ``dwim_revspecs``, it may be tried with an
149
invalid revspec and raises some exception. The exceptions mentioned here
150
will not be reported to the user but simply ignored without stopping the
141
wants_revision_history = True
155
144
def from_string(spec):
171
160
spectype.__name__, spec)
172
161
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)
163
for spectype in SPEC_TYPES:
164
if spec.startswith(spectype.prefix):
165
trace.mutter('Returning RevisionSpec %s for %s',
166
spectype.__name__, spec)
167
return spectype(spec, _internal=True)
168
# RevisionSpec_revno is special cased, because it is the only
169
# one that directly handles plain integers
170
# TODO: This should not be special cased rather it should be
171
# a method invocation on spectype.canparse()
173
if _revno_regex is None:
174
_revno_regex = re.compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
175
if _revno_regex.match(spec) is not None:
176
return RevisionSpec_revno(spec, _internal=True)
178
raise errors.NoSuchRevisionSpec(spec)
178
180
def __init__(self, spec, _internal=False):
179
181
"""Create a RevisionSpec referring to the Null revision.
212
214
def in_history(self, branch):
214
216
if self.wants_revision_history:
215
symbol_versioning.warn(
216
"RevisionSpec.wants_revision_history was "
217
"deprecated in 2.5 (%s)." % self.__class__.__name__,
221
graph = branch.repository.get_graph()
222
revs = list(graph.iter_lefthand_ancestry(
223
branch.last_revision(), [revision.NULL_REVISION]))
217
revs = branch.revision_history()
302
class RevisionSpec_dwim(RevisionSpec):
303
"""Provides a DWIMish revision specifier lookup.
305
Note that this does not go in the revspec_registry because by definition
306
there is no prefix to identify it. It's solely called from
307
RevisionSpec.from_string() because the DWIMification happen when _match_on
308
is called so the string describing the revision is kept here until needed.
313
_revno_regex = lazy_regex.lazy_compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
315
# The revspecs to try
316
_possible_revspecs = []
318
def _try_spectype(self, rstype, branch):
319
rs = rstype(self.spec, _internal=True)
320
# Hit in_history to find out if it exists, or we need to try the
322
return rs.in_history(branch)
324
def _match_on(self, branch, revs):
325
"""Run the lookup and see what we can get."""
327
# First, see if it's a revno
328
if self._revno_regex.match(self.spec) is not None:
330
return self._try_spectype(RevisionSpec_revno, branch)
331
except RevisionSpec_revno.dwim_catchable_exceptions:
334
# Next see what has been registered
335
for objgetter in self._possible_revspecs:
336
rs_class = objgetter.get_obj()
338
return self._try_spectype(rs_class, branch)
339
except rs_class.dwim_catchable_exceptions:
342
# Try the old (deprecated) dwim list:
343
for rs_class in dwim_revspecs:
345
return self._try_spectype(rs_class, branch)
346
except rs_class.dwim_catchable_exceptions:
349
# Well, I dunno what it is. Note that we don't try to keep track of the
350
# first of last exception raised during the DWIM tries as none seems
352
raise errors.InvalidRevisionSpec(self.spec, branch)
355
def append_possible_revspec(cls, revspec):
356
"""Append a possible DWIM revspec.
358
:param revspec: Revision spec to try.
360
cls._possible_revspecs.append(registry._ObjectGetter(revspec))
363
def append_possible_lazy_revspec(cls, module_name, member_name):
364
"""Append a possible lazily loaded DWIM revspec.
366
:param module_name: Name of the module with the revspec
367
:param member_name: Name of the revspec within the module
369
cls._possible_revspecs.append(
370
registry._LazyObjectGetter(module_name, member_name))
373
293
class RevisionSpec_revno(RevisionSpec):
374
294
"""Selects a revision using a number."""
376
296
help_txt = """Selects a revision using a number.
378
298
Use an integer to specify a revision in the history of the branch.
379
Optionally a branch can be specified. A negative number will count
380
from the end of the branch (-1 is the last revision, -2 the previous
381
one). If the negative number is larger than the branch's history, the
382
first revision is returned.
299
Optionally a branch can be specified. The 'revno:' prefix is optional.
300
A negative number will count from the end of the branch (-1 is the
301
last revision, -2 the previous one). If the negative number is larger
302
than the branch's history, the first revision is returned.
385
305
revno:1 -> return the first revision of this branch
392
312
your history is very long.
394
314
prefix = 'revno:'
315
wants_revision_history = False
396
317
def _match_on(self, branch, revs):
397
318
"""Lookup a revision by revision number"""
398
branch, revno, revision_id = self._lookup(branch)
319
branch, revno, revision_id = self._lookup(branch, revs)
399
320
return RevisionInfo(branch, revno, revision_id)
401
def _lookup(self, branch):
322
def _lookup(self, branch, revs_or_none):
402
323
loc = self.spec.find(':')
404
325
revno_spec = self.spec
452
377
revno = last_revno + revno + 1
454
revision_id = branch.get_rev_id(revno)
379
revision_id = branch.get_rev_id(revno, revs_or_none)
455
380
except errors.NoSuchRevision:
456
381
raise errors.InvalidRevisionSpec(self.user_spec, branch)
457
382
return branch, revno, revision_id
459
384
def _as_revision_id(self, context_branch):
460
385
# We would have the revno here, but we don't really care
461
branch, revno, revision_id = self._lookup(context_branch)
386
branch, revno, revision_id = self._lookup(context_branch, None)
462
387
return revision_id
464
389
def needs_branch(self):
474
399
RevisionSpec_int = RevisionSpec_revno
477
class RevisionIDSpec(RevisionSpec):
479
def _match_on(self, branch, revs):
480
revision_id = self.as_revision_id(branch)
481
return RevisionInfo.from_revision_id(branch, revision_id)
484
class RevisionSpec_revid(RevisionIDSpec):
403
class RevisionSpec_revid(RevisionSpec):
485
404
"""Selects a revision using the revision id."""
487
406
help_txt = """Selects a revision using the revision id.
497
416
prefix = 'revid:'
499
def _as_revision_id(self, context_branch):
418
def _match_on(self, branch, revs):
500
419
# self.spec comes straight from parsing the command line arguments,
501
420
# so we expect it to be a Unicode string. Switch it to the internal
502
421
# representation.
422
revision_id = osutils.safe_revision_id(self.spec, warn=False)
423
return RevisionInfo.from_revision_id(branch, revision_id, revs)
425
def _as_revision_id(self, context_branch):
503
426
return osutils.safe_revision_id(self.spec, warn=False)
522
445
def _match_on(self, branch, revs):
523
revno, revision_id = self._revno_and_revision_id(branch)
446
revno, revision_id = self._revno_and_revision_id(branch, revs)
524
447
return RevisionInfo(branch, revno, revision_id)
526
def _revno_and_revision_id(self, context_branch):
449
def _revno_and_revision_id(self, context_branch, revs_or_none):
527
450
last_revno, last_revision_id = context_branch.last_revision_info()
529
452
if self.spec == '':
602
529
return RevisionInfo(branch, revno, revision_id)
604
531
def _as_revision_id(self, context_branch):
605
base_revision_id = RevisionSpec.from_string(self.spec)._as_revision_id(context_branch)
532
base_revspec = RevisionSpec.from_string(self.spec)
533
base_revision_id = base_revspec.as_revision_id(context_branch)
606
534
if base_revision_id == revision.NULL_REVISION:
607
535
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
608
536
'cannot go before the null: revision')
636
dwim_catchable_exceptions = (errors.NoSuchTag, errors.TagsNotSupported)
638
565
def _match_on(self, branch, revs):
639
566
# Can raise tags not supported, NoSuchTag, etc
640
567
return RevisionInfo.from_revision_id(branch,
641
branch.tags.lookup_tag(self.spec))
568
branch.tags.lookup_tag(self.spec),
643
571
def _as_revision_id(self, context_branch):
644
572
return context_branch.tags.lookup_tag(self.spec)
648
576
class _RevListToTimestamps(object):
649
577
"""This takes a list of revisions, and allows you to bisect by date"""
651
__slots__ = ['branch']
579
__slots__ = ['revs', 'branch']
653
def __init__(self, branch):
581
def __init__(self, revs, branch):
654
583
self.branch = branch
656
585
def __getitem__(self, index):
657
586
"""Get the date of the index'd item"""
658
r = self.branch.repository.get_revision(self.branch.get_rev_id(index))
587
r = self.branch.repository.get_revision(self.revs[index])
659
588
# TODO: Handle timezone.
660
589
return datetime.datetime.fromtimestamp(r.timestamp)
662
591
def __len__(self):
663
return self.branch.revno()
592
return len(self.revs)
666
595
class RevisionSpec_date(RevisionSpec):
740
669
hour=hour, minute=minute, second=second)
741
670
branch.lock_read()
743
rev = bisect.bisect(_RevListToTimestamps(branch), dt, 1)
672
rev = bisect.bisect(_RevListToTimestamps(revs, branch), dt)
746
if rev == branch.revno():
747
676
raise errors.InvalidRevisionSpec(self.user_spec, branch)
748
return RevisionInfo(branch, rev)
678
return RevisionInfo(branch, rev + 1)
782
712
def _find_revision_info(branch, other_location):
783
713
revision_id = RevisionSpec_ancestor._find_revision_id(branch,
785
return RevisionInfo(branch, None, revision_id)
716
revno = branch.revision_id_to_revno(revision_id)
717
except errors.NoSuchRevision:
719
return RevisionInfo(branch, revno, revision_id)
788
722
def _find_revision_id(branch, other_location):
834
767
revision_b = other_branch.last_revision()
835
768
if revision_b in (None, revision.NULL_REVISION):
836
769
raise errors.NoCommits(other_branch)
838
branch = other_branch
841
# pull in the remote revisions so we can diff
842
branch.fetch(other_branch, revision_b)
843
except errors.ReadOnlyError:
844
branch = other_branch
845
return RevisionInfo(branch, None, revision_b)
770
# pull in the remote revisions so we can diff
771
branch.fetch(other_branch, revision_b)
773
revno = branch.revision_id_to_revno(revision_b)
774
except errors.NoSuchRevision:
776
return RevisionInfo(branch, revno, revision_b)
847
778
def _as_revision_id(self, context_branch):
848
779
from bzrlib.branch import Branch
914
838
self._get_submit_location(context_branch))
917
class RevisionSpec_annotate(RevisionIDSpec):
921
help_txt = """Select the revision that last modified the specified line.
923
Select the revision that last modified the specified line. Line is
924
specified as path:number. Path is a relative path to the file. Numbers
925
start at 1, and are relative to the current version, not the last-
926
committed version of the file.
929
def _raise_invalid(self, numstring, context_branch):
930
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
931
'No such line: %s' % numstring)
933
def _as_revision_id(self, context_branch):
934
path, numstring = self.spec.rsplit(':', 1)
936
index = int(numstring) - 1
938
self._raise_invalid(numstring, context_branch)
939
tree, file_path = workingtree.WorkingTree.open_containing(path)
942
file_id = tree.path2id(file_path)
944
raise errors.InvalidRevisionSpec(self.user_spec,
945
context_branch, "File '%s' is not versioned." %
947
revision_ids = [r for (r, l) in tree.annotate_iter(file_id)]
951
revision_id = revision_ids[index]
953
self._raise_invalid(numstring, context_branch)
954
if revision_id == revision.CURRENT_REVISION:
955
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
956
'Line %s has not been committed.' % numstring)
960
class RevisionSpec_mainline(RevisionIDSpec):
962
help_txt = """Select mainline revision that merged the specified revision.
964
Select the revision that merged the specified revision into mainline.
969
def _as_revision_id(self, context_branch):
970
revspec = RevisionSpec.from_string(self.spec)
971
if revspec.get_branch() is None:
972
spec_branch = context_branch
974
spec_branch = _mod_branch.Branch.open(revspec.get_branch())
975
revision_id = revspec.as_revision_id(spec_branch)
976
graph = context_branch.repository.get_graph()
977
result = graph.find_lefthand_merger(revision_id,
978
context_branch.last_revision())
980
raise errors.InvalidRevisionSpec(self.user_spec, context_branch)
984
# The order in which we want to DWIM a revision spec without any prefix.
985
# revno is always tried first and isn't listed here, this is used by
986
# RevisionSpec_dwim._match_on
987
dwim_revspecs = symbol_versioning.deprecated_list(
988
symbol_versioning.deprecated_in((2, 4, 0)), "dwim_revspecs", [])
990
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_tag)
991
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_revid)
992
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_date)
993
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_branch)
995
841
revspec_registry = registry.Registry()
996
842
def _register_revspec(revspec):
997
843
revspec_registry.register(revspec.prefix, revspec)