53
55
or treat the result as a tuple.
56
def __init__(self, branch, revno, rev_id=_marker):
58
def __init__(self, branch, revno=None, rev_id=None):
57
59
self.branch = branch
60
self._has_revno = (revno is not None)
63
if self.rev_id is None and self._revno is not None:
60
64
# allow caller to be lazy
61
if self.revno is None:
64
self.rev_id = branch.get_rev_id(self.revno)
65
self.rev_id = branch.get_rev_id(self._revno)
69
if not self._has_revno and self.rev_id is not None:
71
self._revno = self.branch.revision_id_to_revno(self.rev_id)
72
except errors.NoSuchRevision:
74
self._has_revno = True
68
77
def __nonzero__(self):
69
78
# first the easy ones...
99
108
self.revno, self.rev_id, self.branch)
102
def from_revision_id(branch, revision_id, revs):
111
def from_revision_id(branch, revision_id, revs=symbol_versioning.DEPRECATED_PARAMETER):
103
112
"""Construct a RevisionInfo given just the id.
105
114
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)
116
if symbol_versioning.deprecated_passed(revs):
117
symbol_versioning.warn(
118
'RevisionInfo.from_revision_id(revs) was deprecated in 2.5.',
121
return RevisionInfo(branch, revno=None, rev_id=revision_id)
119
124
class RevisionSpec(object):
166
172
spectype.__name__, spec)
167
173
return spectype(spec, _internal=True)
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
175
# Otherwise treat it as a DWIM, build the RevisionSpec object and
175
176
# wait for _match_on to be called.
176
177
return RevisionSpec_dwim(spec, _internal=True)
312
326
"""Run the lookup and see what we can get."""
314
328
# 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:
329
if self._revno_regex.match(self.spec) is not None:
320
331
return self._try_spectype(RevisionSpec_revno, branch)
321
332
except RevisionSpec_revno.dwim_catchable_exceptions:
324
335
# Next see what has been registered
336
for objgetter in self._possible_revspecs:
337
rs_class = objgetter.get_obj()
339
return self._try_spectype(rs_class, branch)
340
except rs_class.dwim_catchable_exceptions:
343
# Try the old (deprecated) dwim list:
325
344
for rs_class in dwim_revspecs:
327
346
return self._try_spectype(rs_class, branch)
333
352
# really relevant.
334
353
raise errors.InvalidRevisionSpec(self.spec, branch)
356
def append_possible_revspec(cls, revspec):
357
"""Append a possible DWIM revspec.
359
:param revspec: Revision spec to try.
361
cls._possible_revspecs.append(registry._ObjectGetter(revspec))
364
def append_possible_lazy_revspec(cls, module_name, member_name):
365
"""Append a possible lazily loaded DWIM revspec.
367
:param module_name: Name of the module with the revspec
368
:param member_name: Name of the revspec within the module
370
cls._possible_revspecs.append(
371
registry._LazyObjectGetter(module_name, member_name))
337
374
class RevisionSpec_revno(RevisionSpec):
338
375
"""Selects a revision using a number."""
356
393
your history is very long.
358
395
prefix = 'revno:'
359
wants_revision_history = False
361
397
def _match_on(self, branch, revs):
362
398
"""Lookup a revision by revision number"""
363
branch, revno, revision_id = self._lookup(branch, revs)
399
branch, revno, revision_id = self._lookup(branch)
364
400
return RevisionInfo(branch, revno, revision_id)
366
def _lookup(self, branch, revs_or_none):
402
def _lookup(self, branch):
367
403
loc = self.spec.find(':')
369
405
revno_spec = self.spec
421
453
revno = last_revno + revno + 1
423
revision_id = branch.get_rev_id(revno, revs_or_none)
455
revision_id = branch.get_rev_id(revno)
424
456
except errors.NoSuchRevision:
425
457
raise errors.InvalidRevisionSpec(self.user_spec, branch)
426
458
return branch, revno, revision_id
428
460
def _as_revision_id(self, context_branch):
429
461
# We would have the revno here, but we don't really care
430
branch, revno, revision_id = self._lookup(context_branch, None)
462
branch, revno, revision_id = self._lookup(context_branch)
431
463
return revision_id
433
465
def needs_branch(self):
443
475
RevisionSpec_int = RevisionSpec_revno
447
class RevisionSpec_revid(RevisionSpec):
478
class RevisionIDSpec(RevisionSpec):
480
def _match_on(self, branch, revs):
481
revision_id = self.as_revision_id(branch)
482
return RevisionInfo.from_revision_id(branch, revision_id)
485
class RevisionSpec_revid(RevisionIDSpec):
448
486
"""Selects a revision using the revision id."""
450
488
help_txt = """Selects a revision using the revision id.
460
498
prefix = 'revid:'
462
def _match_on(self, branch, revs):
500
def _as_revision_id(self, context_branch):
463
501
# self.spec comes straight from parsing the command line arguments,
464
502
# so we expect it to be a Unicode string. Switch it to the internal
465
503
# representation.
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
504
return osutils.safe_revision_id(self.spec, warn=False)
489
523
def _match_on(self, branch, revs):
490
revno, revision_id = self._revno_and_revision_id(branch, revs)
524
revno, revision_id = self._revno_and_revision_id(branch)
491
525
return RevisionInfo(branch, revno, revision_id)
493
def _revno_and_revision_id(self, context_branch, revs_or_none):
527
def _revno_and_revision_id(self, context_branch):
494
528
last_revno, last_revision_id = context_branch.last_revision_info()
496
530
if self.spec == '':
573
603
return RevisionInfo(branch, revno, revision_id)
575
605
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)
606
base_revision_id = RevisionSpec.from_string(self.spec)._as_revision_id(context_branch)
578
607
if base_revision_id == revision.NULL_REVISION:
579
608
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
580
609
'cannot go before the null: revision')
621
649
class _RevListToTimestamps(object):
622
650
"""This takes a list of revisions, and allows you to bisect by date"""
624
__slots__ = ['revs', 'branch']
652
__slots__ = ['branch']
626
def __init__(self, revs, branch):
654
def __init__(self, branch):
628
655
self.branch = branch
630
657
def __getitem__(self, index):
631
658
"""Get the date of the index'd item"""
632
r = self.branch.repository.get_revision(self.revs[index])
659
r = self.branch.repository.get_revision(self.branch.get_rev_id(index))
633
660
# TODO: Handle timezone.
634
661
return datetime.datetime.fromtimestamp(r.timestamp)
636
663
def __len__(self):
637
return len(self.revs)
664
return self.branch.revno()
640
667
class RevisionSpec_date(RevisionSpec):
714
741
hour=hour, minute=minute, second=second)
715
742
branch.lock_read()
717
rev = bisect.bisect(_RevListToTimestamps(revs, branch), dt)
744
rev = bisect.bisect(_RevListToTimestamps(branch), dt, 1)
747
if rev == branch.revno():
721
748
raise errors.InvalidRevisionSpec(self.user_spec, branch)
723
return RevisionInfo(branch, rev + 1)
749
return RevisionInfo(branch, rev)
757
783
def _find_revision_info(branch, other_location):
758
784
revision_id = RevisionSpec_ancestor._find_revision_id(branch,
761
revno = branch.revision_id_to_revno(revision_id)
762
except errors.NoSuchRevision:
764
return RevisionInfo(branch, revno, revision_id)
786
return RevisionInfo(branch, None, revision_id)
767
789
def _find_revision_id(branch, other_location):
813
835
revision_b = other_branch.last_revision()
814
836
if revision_b in (None, revision.NULL_REVISION):
815
837
raise errors.NoCommits(other_branch)
816
# pull in the remote revisions so we can diff
817
branch.fetch(other_branch, revision_b)
819
revno = branch.revision_id_to_revno(revision_b)
820
except errors.NoSuchRevision:
822
return RevisionInfo(branch, revno, revision_b)
839
branch = other_branch
842
# pull in the remote revisions so we can diff
843
branch.fetch(other_branch, revision_b)
844
except errors.ReadOnlyError:
845
branch = other_branch
846
return RevisionInfo(branch, None, revision_b)
824
848
def _as_revision_id(self, context_branch):
825
849
from bzrlib.branch import Branch
884
915
self._get_submit_location(context_branch))
918
class RevisionSpec_annotate(RevisionIDSpec):
922
help_txt = """Select the revision that last modified the specified line.
924
Select the revision that last modified the specified line. Line is
925
specified as path:number. Path is a relative path to the file. Numbers
926
start at 1, and are relative to the current version, not the last-
927
committed version of the file.
930
def _raise_invalid(self, numstring, context_branch):
931
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
932
'No such line: %s' % numstring)
934
def _as_revision_id(self, context_branch):
935
path, numstring = self.spec.rsplit(':', 1)
937
index = int(numstring) - 1
939
self._raise_invalid(numstring, context_branch)
940
tree, file_path = workingtree.WorkingTree.open_containing(path)
943
file_id = tree.path2id(file_path)
945
raise errors.InvalidRevisionSpec(self.user_spec,
946
context_branch, "File '%s' is not versioned." %
948
revision_ids = [r for (r, l) in tree.annotate_iter(file_id)]
952
revision_id = revision_ids[index]
954
self._raise_invalid(numstring, context_branch)
955
if revision_id == revision.CURRENT_REVISION:
956
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
957
'Line %s has not been committed.' % numstring)
961
class RevisionSpec_mainline(RevisionIDSpec):
963
help_txt = """Select mainline revision that merged the specified revision.
965
Select the revision that merged the specified revision into mainline.
970
def _as_revision_id(self, context_branch):
971
revspec = RevisionSpec.from_string(self.spec)
972
if revspec.get_branch() is None:
973
spec_branch = context_branch
975
spec_branch = _mod_branch.Branch.open(revspec.get_branch())
976
revision_id = revspec.as_revision_id(spec_branch)
977
graph = context_branch.repository.get_graph()
978
result = graph.find_lefthand_merger(revision_id,
979
context_branch.last_revision())
981
raise errors.InvalidRevisionSpec(self.user_spec, context_branch)
887
985
# The order in which we want to DWIM a revision spec without any prefix.
888
986
# revno is always tried first and isn't listed here, this is used by
889
987
# RevisionSpec_dwim._match_on
891
RevisionSpec_tag, # Let's try for a tag
892
RevisionSpec_revid, # Maybe it's a revid?
893
RevisionSpec_date, # Perhaps a date?
894
RevisionSpec_branch, # OK, last try, maybe it's a branch
988
dwim_revspecs = symbol_versioning.deprecated_list(
989
symbol_versioning.deprecated_in((2, 4, 0)), "dwim_revspecs", [])
991
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_tag)
992
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_revid)
993
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_date)
994
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_branch)
898
996
revspec_registry = registry.Registry()
899
997
def _register_revspec(revspec):
908
1006
_register_revspec(RevisionSpec_ancestor)
909
1007
_register_revspec(RevisionSpec_branch)
910
1008
_register_revspec(RevisionSpec_submit)
912
# classes in this list should have a "prefix" attribute, against which
913
# string specs are matched
914
SPEC_TYPES = symbol_versioning.deprecated_list(
915
symbol_versioning.deprecated_in((1, 12, 0)), "SPEC_TYPES", [])
1009
_register_revspec(RevisionSpec_annotate)
1010
_register_revspec(RevisionSpec_mainline)