53
59
or treat the result as a tuple.
56
def __init__(self, branch, revno, rev_id=_marker):
62
def __init__(self, branch, revno=None, rev_id=None):
57
63
self.branch = branch
64
self._has_revno = (revno is not None)
67
if self.rev_id is None and self._revno is not None:
60
68
# 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...
69
self.rev_id = branch.get_rev_id(self._revno)
73
if not self._has_revno and self.rev_id is not None:
75
self._revno = self.branch.revision_id_to_revno(self.rev_id)
76
except (errors.NoSuchRevision, errors.RevnoOutOfBounds):
78
self._has_revno = True
70
82
if self.rev_id is None:
72
if self.revno is not None:
74
84
# TODO: otherwise, it should depend on how I was built -
75
85
# if it's in_history(branch), then check revision_history(),
76
86
# if it's in_store(branch), do the check below
77
87
return self.branch.repository.has_revision(self.rev_id)
89
__nonzero__ = __bool__
82
94
def __getitem__(self, index):
83
if index == 0: return self.revno
84
if index == 1: return self.rev_id
85
99
raise IndexError(index)
90
104
def __eq__(self, other):
91
105
if type(other) not in (tuple, list, type(self)):
93
if type(other) is type(self) and self.branch is not other.branch:
107
if isinstance(other, type(self)) and self.branch is not other.branch:
95
109
return tuple(self) == tuple(other)
97
111
def __repr__(self):
98
return '<bzrlib.revisionspec.RevisionInfo object %s, %s for %r>' % (
112
return '<breezy.revisionspec.RevisionInfo object %s, %s for %r>' % (
99
113
self.revno, self.rev_id, self.branch)
102
def from_revision_id(branch, revision_id, revs):
116
def from_revision_id(branch, revision_id):
103
117
"""Construct a RevisionInfo given just the id.
105
119
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)
121
return RevisionInfo(branch, revno=None, rev_id=revision_id)
119
124
class RevisionSpec(object):
312
301
"""Run the lookup and see what we can get."""
314
303
# 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:
304
if self._revno_regex.match(self.spec) is not None:
320
306
return self._try_spectype(RevisionSpec_revno, branch)
321
307
except RevisionSpec_revno.dwim_catchable_exceptions:
324
310
# Next see what has been registered
325
for rs_class in dwim_revspecs:
311
for objgetter in self._possible_revspecs:
312
rs_class = objgetter.get_obj()
327
314
return self._try_spectype(rs_class, branch)
328
315
except rs_class.dwim_catchable_exceptions:
333
320
# really relevant.
334
321
raise errors.InvalidRevisionSpec(self.spec, branch)
324
def append_possible_revspec(cls, revspec):
325
"""Append a possible DWIM revspec.
327
:param revspec: Revision spec to try.
329
cls._possible_revspecs.append(registry._ObjectGetter(revspec))
332
def append_possible_lazy_revspec(cls, module_name, member_name):
333
"""Append a possible lazily loaded DWIM revspec.
335
:param module_name: Name of the module with the revspec
336
:param member_name: Name of the revspec within the module
338
cls._possible_revspecs.append(
339
registry._LazyObjectGetter(module_name, member_name))
337
342
class RevisionSpec_revno(RevisionSpec):
338
343
"""Selects a revision using a number."""
356
361
your history is very long.
358
363
prefix = 'revno:'
359
wants_revision_history = False
361
365
def _match_on(self, branch, revs):
362
366
"""Lookup a revision by revision number"""
363
branch, revno, revision_id = self._lookup(branch, revs)
367
branch, revno, revision_id = self._lookup(branch)
364
368
return RevisionInfo(branch, revno, revision_id)
366
def _lookup(self, branch, revs_or_none):
370
def _lookup(self, branch):
367
371
loc = self.spec.find(':')
369
373
revno_spec = self.spec
370
374
branch_spec = None
372
376
revno_spec = self.spec[:loc]
373
branch_spec = self.spec[loc+1:]
377
branch_spec = self.spec[loc + 1:]
375
379
if revno_spec == '':
376
380
if not branch_spec:
377
381
raise errors.InvalidRevisionSpec(self.user_spec,
378
branch, 'cannot have an empty revno and no branch')
382
branch, 'cannot have an empty revno and no branch')
386
390
# but the from_string method is a little primitive
387
391
# right now - RBC 20060928
389
match_revno = tuple((int(number) for number in revno_spec.split('.')))
390
except ValueError, e:
393
match_revno = tuple((int(number)
394
for number in revno_spec.split('.')))
395
except ValueError as e:
391
396
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)
401
# the user has overriden the branch to look in.
402
branch = _mod_branch.Branch.open(branch_spec)
405
406
revision_id = branch.dotted_revno_to_revision_id(match_revno,
407
except errors.NoSuchRevision:
408
except (errors.NoSuchRevision, errors.RevnoOutOfBounds):
408
409
raise errors.InvalidRevisionSpec(self.user_spec, branch)
410
411
# there is no traditional 'revno' for dotted-decimal revnos.
411
# so for API compatability we return None.
412
# so for API compatibility we return None.
412
413
return branch, None, revision_id
414
415
last_revno, last_revision_id = branch.last_revision_info()
421
422
revno = last_revno + revno + 1
423
revision_id = branch.get_rev_id(revno, revs_or_none)
424
except errors.NoSuchRevision:
424
revision_id = branch.get_rev_id(revno)
425
except (errors.NoSuchRevision, errors.RevnoOutOfBounds):
425
426
raise errors.InvalidRevisionSpec(self.user_spec, branch)
426
427
return branch, revno, revision_id
428
429
def _as_revision_id(self, context_branch):
429
430
# We would have the revno here, but we don't really care
430
branch, revno, revision_id = self._lookup(context_branch, None)
431
branch, revno, revision_id = self._lookup(context_branch)
431
432
return revision_id
433
434
def needs_branch(self):
460
468
prefix = 'revid:'
462
def _match_on(self, branch, revs):
470
def _as_revision_id(self, context_branch):
463
471
# self.spec comes straight from parsing the command line arguments,
464
472
# so we expect it to be a Unicode string. Switch it to the internal
465
473
# 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)
474
if isinstance(self.spec, text_type):
475
return cache_utf8.encode(self.spec)
474
479
class RevisionSpec_last(RevisionSpec):
510
515
revno = last_revno - offset + 1
512
revision_id = context_branch.get_rev_id(revno, revs_or_none)
513
except errors.NoSuchRevision:
517
revision_id = context_branch.get_rev_id(revno)
518
except (errors.NoSuchRevision, errors.RevnoOutOfBounds):
514
519
raise errors.InvalidRevisionSpec(self.user_spec, context_branch)
515
520
return revno, revision_id
517
522
def _as_revision_id(self, context_branch):
518
523
# 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)
525
revno, revision_id = self._revno_and_revision_id(context_branch)
521
526
return revision_id
525
529
class RevisionSpec_before(RevisionSpec):
526
530
"""Selects the parent of the revision specified."""
550
554
r = RevisionSpec.from_string(self.spec)._match_on(branch, revs)
552
556
raise errors.InvalidRevisionSpec(self.user_spec, branch,
553
'cannot go before the null: revision')
557
'cannot go before the null: revision')
554
558
if r.revno is None:
555
559
# We need to use the repository history here
556
560
rev = branch.repository.get_revision(r.rev_id)
557
561
if not rev.parent_ids:
559
562
revision_id = revision.NULL_REVISION
561
564
revision_id = rev.parent_ids[0]
563
revno = revs.index(revision_id) + 1
567
567
revno = r.revno - 1
569
569
revision_id = branch.get_rev_id(revno, revs)
570
except errors.NoSuchRevision:
570
except (errors.NoSuchRevision, errors.RevnoOutOfBounds):
571
571
raise errors.InvalidRevisionSpec(self.user_spec,
573
573
return RevisionInfo(branch, revno, revision_id)
575
575
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)
576
base_revision_id = RevisionSpec.from_string(
577
self.spec)._as_revision_id(context_branch)
578
578
if base_revision_id == revision.NULL_REVISION:
579
579
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
580
'cannot go before the null: revision')
580
'cannot go before the null: revision')
581
581
context_repo = context_branch.repository
582
context_repo.lock_read()
582
with context_repo.lock_read():
584
583
parent_map = context_repo.get_parent_map([base_revision_id])
586
context_repo.unlock()
587
584
if base_revision_id not in parent_map:
588
585
# Ghost, or unknown revision id
589
586
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
590
'cannot find the matching revision')
587
'cannot find the matching revision')
591
588
parents = parent_map[base_revision_id]
592
589
if len(parents) < 1:
593
590
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
594
'No parents for revision.')
591
'No parents for revision.')
595
592
return parents[0]
599
595
class RevisionSpec_tag(RevisionSpec):
600
596
"""Select a revision identified by tag name"""
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
615
class _RevListToTimestamps(object):
622
616
"""This takes a list of revisions, and allows you to bisect by date"""
624
__slots__ = ['revs', 'branch']
618
__slots__ = ['branch']
626
def __init__(self, revs, branch):
620
def __init__(self, branch):
628
621
self.branch = branch
630
623
def __getitem__(self, index):
631
624
"""Get the date of the index'd item"""
632
r = self.branch.repository.get_revision(self.revs[index])
625
r = self.branch.repository.get_revision(self.branch.get_rev_id(index))
633
626
# TODO: Handle timezone.
634
627
return datetime.datetime.fromtimestamp(r.timestamp)
636
629
def __len__(self):
637
return len(self.revs)
630
return self.branch.revno()
640
633
class RevisionSpec_date(RevisionSpec):
708
hour, minute, second = 0,0,0
702
hour, minute, second = 0, 0, 0
709
703
except ValueError:
710
704
raise errors.InvalidRevisionSpec(self.user_spec,
711
705
branch, 'invalid date')
713
707
dt = datetime.datetime(year=year, month=month, day=day,
714
hour=hour, minute=minute, second=second)
717
rev = bisect.bisect(_RevListToTimestamps(revs, branch), dt)
708
hour=hour, minute=minute, second=second)
709
with branch.lock_read():
710
rev = bisect.bisect(_RevListToTimestamps(branch), dt, 1)
711
if rev == branch.revno():
721
712
raise errors.InvalidRevisionSpec(self.user_spec, branch)
723
return RevisionInfo(branch, rev + 1)
713
return RevisionInfo(branch, rev)
727
716
class RevisionSpec_ancestor(RevisionSpec):
757
746
def _find_revision_info(branch, other_location):
758
747
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)
749
return RevisionInfo(branch, None, revision_id)
767
752
def _find_revision_id(branch, other_location):
768
from bzrlib.branch import Branch
753
from .branch import Branch
755
with branch.lock_read():
772
756
revision_a = revision.ensure_null(branch.last_revision())
773
757
if revision_a == revision.NULL_REVISION:
774
758
raise errors.NoCommits(branch)
775
759
if other_location == '':
776
760
other_location = branch.get_parent()
777
761
other_branch = Branch.open(other_location)
778
other_branch.lock_read()
762
with other_branch.lock_read():
780
763
revision_b = revision.ensure_null(other_branch.last_revision())
781
764
if revision_b == revision.NULL_REVISION:
782
765
raise errors.NoCommits(other_branch)
783
766
graph = branch.repository.get_graph(other_branch.repository)
784
767
rev_id = graph.find_unique_lca(revision_a, revision_b)
786
other_branch.unlock()
787
768
if rev_id == revision.NULL_REVISION:
788
769
raise errors.NoCommonAncestor(revision_a, revision_b)
796
773
class RevisionSpec_branch(RevisionSpec):
808
785
dwim_catchable_exceptions = (errors.NotBranchError,)
810
787
def _match_on(self, branch, revs):
811
from bzrlib.branch import Branch
788
from .branch import Branch
812
789
other_branch = Branch.open(self.spec)
813
790
revision_b = other_branch.last_revision()
814
791
if revision_b in (None, revision.NULL_REVISION):
815
792
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)
794
branch = other_branch
797
# pull in the remote revisions so we can diff
798
branch.fetch(other_branch, revision_b)
799
except errors.ReadOnlyError:
800
branch = other_branch
801
return RevisionInfo(branch, None, revision_b)
824
803
def _as_revision_id(self, context_branch):
825
from bzrlib.branch import Branch
804
from .branch import Branch
826
805
other_branch = Branch.open(self.spec)
827
806
last_revision = other_branch.last_revision()
828
807
last_revision = revision.ensure_null(last_revision)
871
855
location_type = 'parent branch'
872
856
if submit_location is None:
873
857
raise errors.NoSubmitBranch(branch)
874
trace.note('Using %s %s', location_type, submit_location)
858
trace.note(gettext('Using {0} {1}').format(location_type,
875
860
return submit_location
877
862
def _match_on(self, branch, revs):
878
863
trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
879
864
return self._find_revision_info(branch,
880
self._get_submit_location(branch))
865
self._get_submit_location(branch))
882
867
def _as_revision_id(self, context_branch):
883
868
return self._find_revision_id(context_branch,
884
self._get_submit_location(context_branch))
869
self._get_submit_location(context_branch))
872
class RevisionSpec_annotate(RevisionIDSpec):
876
help_txt = """Select the revision that last modified the specified line.
878
Select the revision that last modified the specified line. Line is
879
specified as path:number. Path is a relative path to the file. Numbers
880
start at 1, and are relative to the current version, not the last-
881
committed version of the file.
884
def _raise_invalid(self, numstring, context_branch):
885
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
886
'No such line: %s' % numstring)
888
def _as_revision_id(self, context_branch):
889
path, numstring = self.spec.rsplit(':', 1)
891
index = int(numstring) - 1
893
self._raise_invalid(numstring, context_branch)
894
tree, file_path = workingtree.WorkingTree.open_containing(path)
895
with tree.lock_read():
896
if not tree.has_filename(file_path):
897
raise errors.InvalidRevisionSpec(self.user_spec,
898
context_branch, "File '%s' is not versioned." %
900
revision_ids = [r for (r, l) in tree.annotate_iter(file_path)]
902
revision_id = revision_ids[index]
904
self._raise_invalid(numstring, context_branch)
905
if revision_id == revision.CURRENT_REVISION:
906
raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
907
'Line %s has not been committed.' % numstring)
911
class RevisionSpec_mainline(RevisionIDSpec):
913
help_txt = """Select mainline revision that merged the specified revision.
915
Select the revision that merged the specified revision into mainline.
920
def _as_revision_id(self, context_branch):
921
revspec = RevisionSpec.from_string(self.spec)
922
if revspec.get_branch() is None:
923
spec_branch = context_branch
925
spec_branch = _mod_branch.Branch.open(revspec.get_branch())
926
revision_id = revspec.as_revision_id(spec_branch)
927
graph = context_branch.repository.get_graph()
928
result = graph.find_lefthand_merger(revision_id,
929
context_branch.last_revision())
931
raise errors.InvalidRevisionSpec(self.user_spec, context_branch)
887
935
# The order in which we want to DWIM a revision spec without any prefix.
888
936
# revno is always tried first and isn't listed here, this is used by
889
937
# 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
938
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_tag)
939
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_revid)
940
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_date)
941
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_branch)
898
943
revspec_registry = registry.Registry()
899
946
def _register_revspec(revspec):
900
947
revspec_registry.register(revspec.prefix, revspec)
902
950
_register_revspec(RevisionSpec_revno)
903
951
_register_revspec(RevisionSpec_revid)
904
952
_register_revspec(RevisionSpec_last)