53
54
or treat the result as a tuple.
56
def __init__(self, branch, revno, rev_id=_marker):
57
def __init__(self, branch, revno=None, rev_id=None):
57
58
self.branch = branch
59
self._has_revno = (revno is not None)
62
if self.rev_id is None and self._revno is not None:
60
63
# 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...
64
self.rev_id = branch.get_rev_id(self._revno)
68
if not self._has_revno and self.rev_id is not None:
70
self._revno = self.branch.revision_id_to_revno(self.rev_id)
71
except (errors.NoSuchRevision, errors.RevnoOutOfBounds):
73
self._has_revno = True
70
77
if self.rev_id is None:
72
if self.revno is not None:
74
79
# TODO: otherwise, it should depend on how I was built -
75
80
# if it's in_history(branch), then check revision_history(),
76
81
# if it's in_store(branch), do the check below
77
82
return self.branch.repository.has_revision(self.rev_id)
84
__nonzero__ = __bool__
82
89
def __getitem__(self, index):
83
if index == 0: return self.revno
84
if index == 1: return self.rev_id
85
94
raise IndexError(index)
90
99
def __eq__(self, other):
91
100
if type(other) not in (tuple, list, type(self)):
93
if type(other) is type(self) and self.branch is not other.branch:
102
if isinstance(other, type(self)) and self.branch is not other.branch:
95
104
return tuple(self) == tuple(other)
97
106
def __repr__(self):
98
return '<bzrlib.revisionspec.RevisionInfo object %s, %s for %r>' % (
107
return '<breezy.revisionspec.RevisionInfo object %s, %s for %r>' % (
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):
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
return RevisionInfo(branch, revno=None, rev_id=revision_id)
119
119
class RevisionSpec(object):
312
296
"""Run the lookup and see what we can get."""
314
298
# 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:
299
if self._revno_regex.match(self.spec) is not None:
320
301
return self._try_spectype(RevisionSpec_revno, branch)
321
302
except RevisionSpec_revno.dwim_catchable_exceptions:
324
305
# Next see what has been registered
325
for rs_class in dwim_revspecs:
306
for objgetter in self._possible_revspecs:
307
rs_class = objgetter.get_obj()
327
309
return self._try_spectype(rs_class, branch)
328
310
except rs_class.dwim_catchable_exceptions:
333
315
# really relevant.
334
316
raise errors.InvalidRevisionSpec(self.spec, branch)
319
def append_possible_revspec(cls, revspec):
320
"""Append a possible DWIM revspec.
322
:param revspec: Revision spec to try.
324
cls._possible_revspecs.append(registry._ObjectGetter(revspec))
327
def append_possible_lazy_revspec(cls, module_name, member_name):
328
"""Append a possible lazily loaded DWIM revspec.
330
:param module_name: Name of the module with the revspec
331
:param member_name: Name of the revspec within the module
333
cls._possible_revspecs.append(
334
registry._LazyObjectGetter(module_name, member_name))
337
337
class RevisionSpec_revno(RevisionSpec):
338
338
"""Selects a revision using a number."""
356
356
your history is very long.
358
358
prefix = 'revno:'
359
wants_revision_history = False
361
360
def _match_on(self, branch, revs):
362
361
"""Lookup a revision by revision number"""
363
branch, revno, revision_id = self._lookup(branch, revs)
362
branch, revno, revision_id = self._lookup(branch)
364
363
return RevisionInfo(branch, revno, revision_id)
366
def _lookup(self, branch, revs_or_none):
365
def _lookup(self, branch):
367
366
loc = self.spec.find(':')
369
368
revno_spec = self.spec
370
369
branch_spec = None
372
371
revno_spec = self.spec[:loc]
373
branch_spec = self.spec[loc+1:]
372
branch_spec = self.spec[loc + 1:]
375
374
if revno_spec == '':
376
375
if not branch_spec:
377
376
raise errors.InvalidRevisionSpec(self.user_spec,
378
branch, 'cannot have an empty revno and no branch')
377
branch, 'cannot have an empty revno and no branch')
386
385
# but the from_string method is a little primitive
387
386
# right now - RBC 20060928
389
match_revno = tuple((int(number) for number in revno_spec.split('.')))
390
except ValueError, e:
388
match_revno = tuple((int(number)
389
for number in revno_spec.split('.')))
390
except ValueError as e:
391
391
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)
396
# the user has overriden the branch to look in.
397
branch = _mod_branch.Branch.open(branch_spec)
405
401
revision_id = branch.dotted_revno_to_revision_id(match_revno,
407
except errors.NoSuchRevision:
403
except (errors.NoSuchRevision, errors.RevnoOutOfBounds):
408
404
raise errors.InvalidRevisionSpec(self.user_spec, branch)
410
406
# there is no traditional 'revno' for dotted-decimal revnos.
411
# so for API compatability we return None.
407
# so for API compatibility we return None.
412
408
return branch, None, revision_id
414
410
last_revno, last_revision_id = branch.last_revision_info()
421
417
revno = last_revno + revno + 1
423
revision_id = branch.get_rev_id(revno, revs_or_none)
424
except errors.NoSuchRevision:
419
revision_id = branch.get_rev_id(revno)
420
except (errors.NoSuchRevision, errors.RevnoOutOfBounds):
425
421
raise errors.InvalidRevisionSpec(self.user_spec, branch)
426
422
return branch, revno, revision_id
428
424
def _as_revision_id(self, context_branch):
429
425
# We would have the revno here, but we don't really care
430
branch, revno, revision_id = self._lookup(context_branch, None)
426
branch, revno, revision_id = self._lookup(context_branch)
431
427
return revision_id
433
429
def needs_branch(self):
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, str):
470
return cache_utf8.encode(self.spec)
474
474
class RevisionSpec_last(RevisionSpec):
510
510
revno = last_revno - offset + 1
512
revision_id = context_branch.get_rev_id(revno, revs_or_none)
513
except errors.NoSuchRevision:
512
revision_id = context_branch.get_rev_id(revno)
513
except (errors.NoSuchRevision, errors.RevnoOutOfBounds):
514
514
raise errors.InvalidRevisionSpec(self.user_spec, context_branch)
515
515
return revno, revision_id
517
517
def _as_revision_id(self, context_branch):
518
518
# We compute the revno as part of the process, but we don't really care
520
revno, revision_id = self._revno_and_revision_id(context_branch, None)
520
revno, revision_id = self._revno_and_revision_id(context_branch)
521
521
return revision_id
525
524
class RevisionSpec_before(RevisionSpec):
526
525
"""Selects the parent of the revision specified."""
550
549
r = RevisionSpec.from_string(self.spec)._match_on(branch, revs)
552
551
raise errors.InvalidRevisionSpec(self.user_spec, branch,
553
'cannot go before the null: revision')
552
'cannot go before the null: revision')
554
553
if r.revno is None:
555
554
# We need to use the repository history here
556
555
rev = branch.repository.get_revision(r.rev_id)
557
556
if not rev.parent_ids:
559
557
revision_id = revision.NULL_REVISION
561
559
revision_id = rev.parent_ids[0]
563
revno = revs.index(revision_id) + 1
567
562
revno = r.revno - 1
569
564
revision_id = branch.get_rev_id(revno, revs)
570
except errors.NoSuchRevision:
565
except (errors.NoSuchRevision, errors.RevnoOutOfBounds):
571
566
raise errors.InvalidRevisionSpec(self.user_spec,
573
568
return RevisionInfo(branch, revno, revision_id)
575
570
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)
571
base_revision_id = RevisionSpec.from_string(
572
self.spec)._as_revision_id(context_branch)
578
573
if base_revision_id == revision.NULL_REVISION:
579
574
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
580
'cannot go before the null: revision')
575
'cannot go before the null: revision')
581
576
context_repo = context_branch.repository
582
context_repo.lock_read()
577
with context_repo.lock_read():
584
578
parent_map = context_repo.get_parent_map([base_revision_id])
586
context_repo.unlock()
587
579
if base_revision_id not in parent_map:
588
580
# Ghost, or unknown revision id
589
581
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
590
'cannot find the matching revision')
582
'cannot find the matching revision')
591
583
parents = parent_map[base_revision_id]
592
584
if len(parents) < 1:
593
585
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
594
'No parents for revision.')
586
'No parents for revision.')
595
587
return parents[0]
599
590
class RevisionSpec_tag(RevisionSpec):
600
591
"""Select a revision identified by tag name"""
610
601
def _match_on(self, branch, revs):
611
602
# Can raise tags not supported, NoSuchTag, etc
612
603
return RevisionInfo.from_revision_id(branch,
613
branch.tags.lookup_tag(self.spec),
604
branch.tags.lookup_tag(self.spec))
616
606
def _as_revision_id(self, context_branch):
617
607
return context_branch.tags.lookup_tag(self.spec)
621
610
class _RevListToTimestamps(object):
622
611
"""This takes a list of revisions, and allows you to bisect by date"""
624
__slots__ = ['revs', 'branch']
613
__slots__ = ['branch']
626
def __init__(self, revs, branch):
615
def __init__(self, branch):
628
616
self.branch = branch
630
618
def __getitem__(self, index):
631
619
"""Get the date of the index'd item"""
632
r = self.branch.repository.get_revision(self.revs[index])
620
r = self.branch.repository.get_revision(self.branch.get_rev_id(index))
633
621
# TODO: Handle timezone.
634
622
return datetime.datetime.fromtimestamp(r.timestamp)
636
624
def __len__(self):
637
return len(self.revs)
625
return self.branch.revno()
640
628
class RevisionSpec_date(RevisionSpec):
708
hour, minute, second = 0,0,0
697
hour, minute, second = 0, 0, 0
709
698
except ValueError:
710
699
raise errors.InvalidRevisionSpec(self.user_spec,
711
700
branch, 'invalid date')
713
702
dt = datetime.datetime(year=year, month=month, day=day,
714
hour=hour, minute=minute, second=second)
717
rev = bisect.bisect(_RevListToTimestamps(revs, branch), dt)
703
hour=hour, minute=minute, second=second)
704
with branch.lock_read():
705
rev = bisect.bisect(_RevListToTimestamps(branch), dt, 1)
706
if rev == branch.revno():
721
707
raise errors.InvalidRevisionSpec(self.user_spec, branch)
723
return RevisionInfo(branch, rev + 1)
708
return RevisionInfo(branch, rev)
727
711
class RevisionSpec_ancestor(RevisionSpec):
757
741
def _find_revision_info(branch, other_location):
758
742
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)
744
return RevisionInfo(branch, None, revision_id)
767
747
def _find_revision_id(branch, other_location):
768
from bzrlib.branch import Branch
748
from .branch import Branch
750
with branch.lock_read():
772
751
revision_a = revision.ensure_null(branch.last_revision())
773
752
if revision_a == revision.NULL_REVISION:
774
753
raise errors.NoCommits(branch)
775
754
if other_location == '':
776
755
other_location = branch.get_parent()
777
756
other_branch = Branch.open(other_location)
778
other_branch.lock_read()
757
with other_branch.lock_read():
780
758
revision_b = revision.ensure_null(other_branch.last_revision())
781
759
if revision_b == revision.NULL_REVISION:
782
760
raise errors.NoCommits(other_branch)
783
761
graph = branch.repository.get_graph(other_branch.repository)
784
762
rev_id = graph.find_unique_lca(revision_a, revision_b)
786
other_branch.unlock()
787
763
if rev_id == revision.NULL_REVISION:
788
764
raise errors.NoCommonAncestor(revision_a, revision_b)
796
768
class RevisionSpec_branch(RevisionSpec):
808
780
dwim_catchable_exceptions = (errors.NotBranchError,)
810
782
def _match_on(self, branch, revs):
811
from bzrlib.branch import Branch
783
from .branch import Branch
812
784
other_branch = Branch.open(self.spec)
813
785
revision_b = other_branch.last_revision()
814
786
if revision_b in (None, revision.NULL_REVISION):
815
787
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)
789
branch = other_branch
792
# pull in the remote revisions so we can diff
793
branch.fetch(other_branch, revision_b)
794
except errors.ReadOnlyError:
795
branch = other_branch
796
return RevisionInfo(branch, None, revision_b)
824
798
def _as_revision_id(self, context_branch):
825
from bzrlib.branch import Branch
799
from .branch import Branch
826
800
other_branch = Branch.open(self.spec)
827
801
last_revision = other_branch.last_revision()
828
802
last_revision = revision.ensure_null(last_revision)
871
850
location_type = 'parent branch'
872
851
if submit_location is None:
873
852
raise errors.NoSubmitBranch(branch)
874
trace.note('Using %s %s', location_type, submit_location)
853
trace.note(gettext('Using {0} {1}').format(location_type,
875
855
return submit_location
877
857
def _match_on(self, branch, revs):
878
858
trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
879
859
return self._find_revision_info(branch,
880
self._get_submit_location(branch))
860
self._get_submit_location(branch))
882
862
def _as_revision_id(self, context_branch):
883
863
return self._find_revision_id(context_branch,
884
self._get_submit_location(context_branch))
864
self._get_submit_location(context_branch))
867
class RevisionSpec_annotate(RevisionIDSpec):
871
help_txt = """Select the revision that last modified the specified line.
873
Select the revision that last modified the specified line. Line is
874
specified as path:number. Path is a relative path to the file. Numbers
875
start at 1, and are relative to the current version, not the last-
876
committed version of the file.
879
def _raise_invalid(self, numstring, context_branch):
880
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
881
'No such line: %s' % numstring)
883
def _as_revision_id(self, context_branch):
884
path, numstring = self.spec.rsplit(':', 1)
886
index = int(numstring) - 1
888
self._raise_invalid(numstring, context_branch)
889
tree, file_path = workingtree.WorkingTree.open_containing(path)
890
with tree.lock_read():
891
if not tree.has_filename(file_path):
892
raise errors.InvalidRevisionSpec(self.user_spec,
893
context_branch, "File '%s' is not versioned." %
895
revision_ids = [r for (r, l) in tree.annotate_iter(file_path)]
897
revision_id = revision_ids[index]
899
self._raise_invalid(numstring, context_branch)
900
if revision_id == revision.CURRENT_REVISION:
901
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
902
'Line %s has not been committed.' % numstring)
906
class RevisionSpec_mainline(RevisionIDSpec):
908
help_txt = """Select mainline revision that merged the specified revision.
910
Select the revision that merged the specified revision into mainline.
915
def _as_revision_id(self, context_branch):
916
revspec = RevisionSpec.from_string(self.spec)
917
if revspec.get_branch() is None:
918
spec_branch = context_branch
920
spec_branch = _mod_branch.Branch.open(revspec.get_branch())
921
revision_id = revspec.as_revision_id(spec_branch)
922
graph = context_branch.repository.get_graph()
923
result = graph.find_lefthand_merger(revision_id,
924
context_branch.last_revision())
926
raise errors.InvalidRevisionSpec(self.user_spec, context_branch)
887
930
# The order in which we want to DWIM a revision spec without any prefix.
888
931
# revno is always tried first and isn't listed here, this is used by
889
932
# 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
933
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_tag)
934
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_revid)
935
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_date)
936
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_branch)
898
938
revspec_registry = registry.Registry()
899
941
def _register_revspec(revspec):
900
942
revspec_registry.register(revspec.prefix, revspec)
902
945
_register_revspec(RevisionSpec_revno)
903
946
_register_revspec(RevisionSpec_revid)
904
947
_register_revspec(RevisionSpec_last)