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
20
from bzrlib.lazy_import import lazy_import
17
from __future__ import absolute_import
20
from .lazy_import import lazy_import
21
21
lazy_import(globals(), """
26
branch as _mod_branch,
32
from breezy.i18n import gettext
39
43
class RevisionInfo(object):
40
44
"""The results of applying a revision specification to a branch."""
53
57
or treat the result as a tuple.
56
def __init__(self, branch, revno, rev_id=_marker):
60
def __init__(self, branch, revno=None, rev_id=None):
57
61
self.branch = branch
62
self._has_revno = (revno is not None)
65
if self.rev_id is None and self._revno is not None:
60
66
# allow caller to be lazy
61
if self.revno is None:
64
self.rev_id = branch.get_rev_id(self.revno)
68
def __nonzero__(self):
69
# first the easy ones...
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
70
80
if self.rev_id is None:
72
if self.revno is not None:
74
82
# TODO: otherwise, it should depend on how I was built -
75
83
# if it's in_history(branch), then check revision_history(),
76
84
# if it's in_store(branch), do the check below
77
85
return self.branch.repository.has_revision(self.rev_id)
87
__nonzero__ = __bool__
90
100
def __eq__(self, other):
91
101
if type(other) not in (tuple, list, type(self)):
93
if type(other) is type(self) and self.branch is not other.branch:
103
if isinstance(other, type(self)) and self.branch is not other.branch:
95
105
return tuple(self) == tuple(other)
97
107
def __repr__(self):
98
return '<bzrlib.revisionspec.RevisionInfo object %s, %s for %r>' % (
108
return '<breezy.revisionspec.RevisionInfo object %s, %s for %r>' % (
99
109
self.revno, self.rev_id, self.branch)
102
def from_revision_id(branch, revision_id, revs):
112
def from_revision_id(branch, revision_id):
103
113
"""Construct a RevisionInfo given just the id.
105
115
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)
117
return RevisionInfo(branch, revno=None, rev_id=revision_id)
119
120
class RevisionSpec(object):
166
166
spectype.__name__, spec)
167
167
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
169
# Otherwise treat it as a DWIM, build the RevisionSpec object and
175
170
# wait for _match_on to be called.
176
171
return RevisionSpec_dwim(spec, _internal=True)
183
178
called directly. Only from RevisionSpec.from_string()
185
180
if not _internal:
186
symbol_versioning.warn('Creating a RevisionSpec directly has'
187
' been deprecated in version 0.11. Use'
188
' RevisionSpec.from_string()'
190
DeprecationWarning, stacklevel=2)
181
raise AssertionError(
182
'Creating a RevisionSpec directly is not supported. '
183
'Use RevisionSpec.from_string() instead.')
191
184
self.user_spec = spec
192
185
if self.prefix and spec.startswith(self.prefix):
193
186
spec = spec[len(self.prefix):]
210
203
raise errors.InvalidRevisionSpec(self.spec, branch)
212
205
def in_history(self, branch):
214
if self.wants_revision_history:
215
revs = branch.revision_history()
219
# this should never trigger.
220
# TODO: make it a deprecated code path. RBC 20060928
222
return self._match_on_and_check(branch, revs)
206
return self._match_on_and_check(branch, revs=None)
224
208
# FIXME: in_history is somewhat broken,
225
209
# it will return non-history revisions in many
312
298
"""Run the lookup and see what we can get."""
314
300
# 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:
301
if self._revno_regex.match(self.spec) is not None:
320
303
return self._try_spectype(RevisionSpec_revno, branch)
321
304
except RevisionSpec_revno.dwim_catchable_exceptions:
324
307
# Next see what has been registered
325
for rs_class in dwim_revspecs:
308
for objgetter in self._possible_revspecs:
309
rs_class = objgetter.get_obj()
327
311
return self._try_spectype(rs_class, branch)
328
312
except rs_class.dwim_catchable_exceptions:
333
317
# really relevant.
334
318
raise errors.InvalidRevisionSpec(self.spec, branch)
321
def append_possible_revspec(cls, revspec):
322
"""Append a possible DWIM revspec.
324
:param revspec: Revision spec to try.
326
cls._possible_revspecs.append(registry._ObjectGetter(revspec))
329
def append_possible_lazy_revspec(cls, module_name, member_name):
330
"""Append a possible lazily loaded DWIM revspec.
332
:param module_name: Name of the module with the revspec
333
:param member_name: Name of the revspec within the module
335
cls._possible_revspecs.append(
336
registry._LazyObjectGetter(module_name, member_name))
337
339
class RevisionSpec_revno(RevisionSpec):
338
340
"""Selects a revision using a number."""
356
358
your history is very long.
358
360
prefix = 'revno:'
359
wants_revision_history = False
361
362
def _match_on(self, branch, revs):
362
363
"""Lookup a revision by revision number"""
363
branch, revno, revision_id = self._lookup(branch, revs)
364
branch, revno, revision_id = self._lookup(branch)
364
365
return RevisionInfo(branch, revno, revision_id)
366
def _lookup(self, branch, revs_or_none):
367
def _lookup(self, branch):
367
368
loc = self.spec.find(':')
369
370
revno_spec = self.spec
387
388
# right now - RBC 20060928
389
390
match_revno = tuple((int(number) for number in revno_spec.split('.')))
390
except ValueError, e:
391
except ValueError as e:
391
392
raise errors.InvalidRevisionSpec(self.user_spec, branch, e)
396
# the user has override the branch to look in.
397
# we need to refresh the revision_history map and
399
from bzrlib.branch import Branch
400
branch = Branch.open(branch_spec)
397
# the user has overriden the branch to look in.
398
branch = _mod_branch.Branch.open(branch_spec)
421
418
revno = last_revno + revno + 1
423
revision_id = branch.get_rev_id(revno, revs_or_none)
420
revision_id = branch.get_rev_id(revno)
424
421
except errors.NoSuchRevision:
425
422
raise errors.InvalidRevisionSpec(self.user_spec, branch)
426
423
return branch, revno, revision_id
428
425
def _as_revision_id(self, context_branch):
429
426
# We would have the revno here, but we don't really care
430
branch, revno, revision_id = self._lookup(context_branch, None)
427
branch, revno, revision_id = self._lookup(context_branch)
431
428
return revision_id
433
430
def needs_branch(self):
443
440
RevisionSpec_int = RevisionSpec_revno
447
class RevisionSpec_revid(RevisionSpec):
443
class RevisionIDSpec(RevisionSpec):
445
def _match_on(self, branch, revs):
446
revision_id = self.as_revision_id(branch)
447
return RevisionInfo.from_revision_id(branch, revision_id)
450
class RevisionSpec_revid(RevisionIDSpec):
448
451
"""Selects a revision using the revision id."""
450
453
help_txt = """Selects a revision using the revision id.
460
463
prefix = 'revid:'
462
def _match_on(self, branch, revs):
465
def _as_revision_id(self, context_branch):
463
466
# self.spec comes straight from parsing the command line arguments,
464
467
# so we expect it to be a Unicode string. Switch it to the internal
465
468
# 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
return osutils.safe_revision_id(self.spec, warn=False)
469
if isinstance(self.spec, unicode):
470
return cache_utf8.encode(self.spec)
489
490
def _match_on(self, branch, revs):
490
revno, revision_id = self._revno_and_revision_id(branch, revs)
491
revno, revision_id = self._revno_and_revision_id(branch)
491
492
return RevisionInfo(branch, revno, revision_id)
493
def _revno_and_revision_id(self, context_branch, revs_or_none):
494
def _revno_and_revision_id(self, context_branch):
494
495
last_revno, last_revision_id = context_branch.last_revision_info()
496
497
if self.spec == '':
573
570
return RevisionInfo(branch, revno, revision_id)
575
572
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)
573
base_revision_id = RevisionSpec.from_string(self.spec)._as_revision_id(context_branch)
578
574
if base_revision_id == revision.NULL_REVISION:
579
575
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
580
576
'cannot go before the null: revision')
610
606
def _match_on(self, branch, revs):
611
607
# Can raise tags not supported, NoSuchTag, etc
612
608
return RevisionInfo.from_revision_id(branch,
613
branch.tags.lookup_tag(self.spec),
609
branch.tags.lookup_tag(self.spec))
616
611
def _as_revision_id(self, context_branch):
617
612
return context_branch.tags.lookup_tag(self.spec)
621
616
class _RevListToTimestamps(object):
622
617
"""This takes a list of revisions, and allows you to bisect by date"""
624
__slots__ = ['revs', 'branch']
619
__slots__ = ['branch']
626
def __init__(self, revs, branch):
621
def __init__(self, branch):
628
622
self.branch = branch
630
624
def __getitem__(self, index):
631
625
"""Get the date of the index'd item"""
632
r = self.branch.repository.get_revision(self.revs[index])
626
r = self.branch.repository.get_revision(self.branch.get_rev_id(index))
633
627
# TODO: Handle timezone.
634
628
return datetime.datetime.fromtimestamp(r.timestamp)
636
630
def __len__(self):
637
return len(self.revs)
631
return self.branch.revno()
640
634
class RevisionSpec_date(RevisionSpec):
714
708
hour=hour, minute=minute, second=second)
715
709
branch.lock_read()
717
rev = bisect.bisect(_RevListToTimestamps(revs, branch), dt)
711
rev = bisect.bisect(_RevListToTimestamps(branch), dt, 1)
714
if rev == branch.revno():
721
715
raise errors.InvalidRevisionSpec(self.user_spec, branch)
723
return RevisionInfo(branch, rev + 1)
716
return RevisionInfo(branch, rev)
757
750
def _find_revision_info(branch, other_location):
758
751
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)
753
return RevisionInfo(branch, None, revision_id)
767
756
def _find_revision_id(branch, other_location):
768
from bzrlib.branch import Branch
757
from .branch import Branch
770
759
branch.lock_read()
808
797
dwim_catchable_exceptions = (errors.NotBranchError,)
810
799
def _match_on(self, branch, revs):
811
from bzrlib.branch import Branch
800
from .branch import Branch
812
801
other_branch = Branch.open(self.spec)
813
802
revision_b = other_branch.last_revision()
814
803
if revision_b in (None, revision.NULL_REVISION):
815
804
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)
806
branch = other_branch
809
# pull in the remote revisions so we can diff
810
branch.fetch(other_branch, revision_b)
811
except errors.ReadOnlyError:
812
branch = other_branch
813
return RevisionInfo(branch, None, revision_b)
824
815
def _as_revision_id(self, context_branch):
825
from bzrlib.branch import Branch
816
from .branch import Branch
826
817
other_branch = Branch.open(self.spec)
827
818
last_revision = other_branch.last_revision()
828
819
last_revision = revision.ensure_null(last_revision)
884
882
self._get_submit_location(context_branch))
885
class RevisionSpec_annotate(RevisionIDSpec):
889
help_txt = """Select the revision that last modified the specified line.
891
Select the revision that last modified the specified line. Line is
892
specified as path:number. Path is a relative path to the file. Numbers
893
start at 1, and are relative to the current version, not the last-
894
committed version of the file.
897
def _raise_invalid(self, numstring, context_branch):
898
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
899
'No such line: %s' % numstring)
901
def _as_revision_id(self, context_branch):
902
path, numstring = self.spec.rsplit(':', 1)
904
index = int(numstring) - 1
906
self._raise_invalid(numstring, context_branch)
907
tree, file_path = workingtree.WorkingTree.open_containing(path)
910
file_id = tree.path2id(file_path)
912
raise errors.InvalidRevisionSpec(self.user_spec,
913
context_branch, "File '%s' is not versioned." %
915
revision_ids = [r for (r, l) in tree.annotate_iter(file_id)]
919
revision_id = revision_ids[index]
921
self._raise_invalid(numstring, context_branch)
922
if revision_id == revision.CURRENT_REVISION:
923
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
924
'Line %s has not been committed.' % numstring)
928
class RevisionSpec_mainline(RevisionIDSpec):
930
help_txt = """Select mainline revision that merged the specified revision.
932
Select the revision that merged the specified revision into mainline.
937
def _as_revision_id(self, context_branch):
938
revspec = RevisionSpec.from_string(self.spec)
939
if revspec.get_branch() is None:
940
spec_branch = context_branch
942
spec_branch = _mod_branch.Branch.open(revspec.get_branch())
943
revision_id = revspec.as_revision_id(spec_branch)
944
graph = context_branch.repository.get_graph()
945
result = graph.find_lefthand_merger(revision_id,
946
context_branch.last_revision())
948
raise errors.InvalidRevisionSpec(self.user_spec, context_branch)
887
952
# The order in which we want to DWIM a revision spec without any prefix.
888
953
# revno is always tried first and isn't listed here, this is used by
889
954
# 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
955
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_tag)
956
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_revid)
957
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_date)
958
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_branch)
898
960
revspec_registry = registry.Registry()
899
961
def _register_revspec(revspec):
908
970
_register_revspec(RevisionSpec_ancestor)
909
971
_register_revspec(RevisionSpec_branch)
910
972
_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", [])
973
_register_revspec(RevisionSpec_annotate)
974
_register_revspec(RevisionSpec_mainline)