/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/revisionspec.py

  • Committer: Martin
  • Date: 2017-06-14 23:29:06 UTC
  • mto: This revision was merged to the branch mainline in revision 6698.
  • Revision ID: gzlist@googlemail.com-20170614232906-rcxh4ror0f0uwiof
Remove remaining uses of basestring from the codebase

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
16
16
 
17
 
 
18
 
import re
19
 
 
20
 
from bzrlib.lazy_import import lazy_import
 
17
from __future__ import absolute_import
 
18
 
 
19
 
 
20
from .lazy_import import lazy_import
21
21
lazy_import(globals(), """
22
22
import bisect
23
23
import datetime
 
24
 
 
25
from breezy import (
 
26
    branch as _mod_branch,
 
27
    cache_utf8,
 
28
    osutils,
 
29
    revision,
 
30
    workingtree,
 
31
    )
 
32
from breezy.i18n import gettext
24
33
""")
25
34
 
26
 
from bzrlib import (
 
35
from . import (
27
36
    errors,
28
 
    osutils,
 
37
    lazy_regex,
29
38
    registry,
30
 
    revision,
31
 
    symbol_versioning,
32
39
    trace,
33
40
    )
34
41
 
35
42
 
36
 
_marker = []
37
 
 
38
 
 
39
43
class RevisionInfo(object):
40
44
    """The results of applying a revision specification to a branch."""
41
45
 
53
57
    or treat the result as a tuple.
54
58
    """
55
59
 
56
 
    def __init__(self, branch, revno, rev_id=_marker):
 
60
    def __init__(self, branch, revno=None, rev_id=None):
57
61
        self.branch = branch
58
 
        self.revno = revno
59
 
        if rev_id is _marker:
 
62
        self._has_revno = (revno is not None)
 
63
        self._revno = revno
 
64
        self.rev_id = rev_id
 
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:
62
 
                self.rev_id = None
63
 
            else:
64
 
                self.rev_id = branch.get_rev_id(self.revno)
65
 
        else:
66
 
            self.rev_id = rev_id
67
 
 
68
 
    def __nonzero__(self):
69
 
        # first the easy ones...
 
67
            self.rev_id = branch.get_rev_id(self._revno)
 
68
 
 
69
    @property
 
70
    def revno(self):
 
71
        if not self._has_revno and self.rev_id is not None:
 
72
            try:
 
73
                self._revno = self.branch.revision_id_to_revno(self.rev_id)
 
74
            except errors.NoSuchRevision:
 
75
                self._revno = None
 
76
            self._has_revno = True
 
77
        return self._revno
 
78
 
 
79
    def __bool__(self):
70
80
        if self.rev_id is None:
71
81
            return False
72
 
        if self.revno is not None:
73
 
            return True
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)
78
86
 
 
87
    __nonzero__ = __bool__
 
88
 
79
89
    def __len__(self):
80
90
        return 2
81
91
 
90
100
    def __eq__(self, other):
91
101
        if type(other) not in (tuple, list, type(self)):
92
102
            return False
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:
94
104
            return False
95
105
        return tuple(self) == tuple(other)
96
106
 
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)
100
110
 
101
111
    @staticmethod
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.
104
114
 
105
115
        Use this if you don't know or care what the revno is.
106
116
        """
107
 
        if revision_id == revision.NULL_REVISION:
108
 
            return RevisionInfo(branch, 0, revision_id)
109
 
        try:
110
 
            revno = revs.index(revision_id) + 1
111
 
        except ValueError:
112
 
            revno = None
113
 
        return RevisionInfo(branch, revno, revision_id)
114
 
 
115
 
 
116
 
_revno_regex = None
 
117
        return RevisionInfo(branch, revno=None, rev_id=revision_id)
117
118
 
118
119
 
119
120
class RevisionSpec(object):
136
137
    """
137
138
 
138
139
    prefix = None
139
 
    wants_revision_history = True
140
140
    dwim_catchable_exceptions = (errors.InvalidRevisionSpec,)
141
141
    """Exceptions that RevisionSpec_dwim._match_on will catch.
142
142
 
154
154
        :return: A RevisionSpec object that understands how to parse the
155
155
            supplied notation.
156
156
        """
157
 
        if not isinstance(spec, (type(None), basestring)):
158
 
            raise TypeError('error')
159
 
 
160
157
        if spec is None:
161
158
            return RevisionSpec(None, _internal=True)
162
159
        match = revspec_registry.get_prefix(spec)
166
163
                         spectype.__name__, spec)
167
164
            return spectype(spec, _internal=True)
168
165
        else:
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
166
            # Otherwise treat it as a DWIM, build the RevisionSpec object and
175
167
            # wait for _match_on to be called.
176
168
            return RevisionSpec_dwim(spec, _internal=True)
183
175
            called directly. Only from RevisionSpec.from_string()
184
176
        """
185
177
        if not _internal:
186
 
            symbol_versioning.warn('Creating a RevisionSpec directly has'
187
 
                                   ' been deprecated in version 0.11. Use'
188
 
                                   ' RevisionSpec.from_string()'
189
 
                                   ' instead.',
190
 
                                   DeprecationWarning, stacklevel=2)
 
178
            raise AssertionError(
 
179
                'Creating a RevisionSpec directly is not supported. '
 
180
                'Use RevisionSpec.from_string() instead.')
191
181
        self.user_spec = spec
192
182
        if self.prefix and spec.startswith(self.prefix):
193
183
            spec = spec[len(self.prefix):]
210
200
            raise errors.InvalidRevisionSpec(self.spec, branch)
211
201
 
212
202
    def in_history(self, branch):
213
 
        if branch:
214
 
            if self.wants_revision_history:
215
 
                revs = branch.revision_history()
216
 
            else:
217
 
                revs = None
218
 
        else:
219
 
            # this should never trigger.
220
 
            # TODO: make it a deprecated code path. RBC 20060928
221
 
            revs = None
222
 
        return self._match_on_and_check(branch, revs)
 
203
        return self._match_on_and_check(branch, revs=None)
223
204
 
224
205
        # FIXME: in_history is somewhat broken,
225
206
        # it will return non-history revisions in many
298
279
    """
299
280
 
300
281
    help_txt = None
301
 
    # We don't need to build the revision history ourself, that's delegated to
302
 
    # each revspec we try.
303
 
    wants_revision_history = False
 
282
 
 
283
    _revno_regex = lazy_regex.lazy_compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
 
284
 
 
285
    # The revspecs to try
 
286
    _possible_revspecs = []
304
287
 
305
288
    def _try_spectype(self, rstype, branch):
306
289
        rs = rstype(self.spec, _internal=True)
312
295
        """Run the lookup and see what we can get."""
313
296
 
314
297
        # First, see if it's a revno
315
 
        global _revno_regex
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:
 
298
        if self._revno_regex.match(self.spec) is not None:
319
299
            try:
320
300
                return self._try_spectype(RevisionSpec_revno, branch)
321
301
            except RevisionSpec_revno.dwim_catchable_exceptions:
322
302
                pass
323
303
 
324
304
        # Next see what has been registered
325
 
        for rs_class in dwim_revspecs:
 
305
        for objgetter in self._possible_revspecs:
 
306
            rs_class = objgetter.get_obj()
326
307
            try:
327
308
                return self._try_spectype(rs_class, branch)
328
309
            except rs_class.dwim_catchable_exceptions:
333
314
        # really relevant.
334
315
        raise errors.InvalidRevisionSpec(self.spec, branch)
335
316
 
 
317
    @classmethod
 
318
    def append_possible_revspec(cls, revspec):
 
319
        """Append a possible DWIM revspec.
 
320
 
 
321
        :param revspec: Revision spec to try.
 
322
        """
 
323
        cls._possible_revspecs.append(registry._ObjectGetter(revspec))
 
324
 
 
325
    @classmethod
 
326
    def append_possible_lazy_revspec(cls, module_name, member_name):
 
327
        """Append a possible lazily loaded DWIM revspec.
 
328
 
 
329
        :param module_name: Name of the module with the revspec
 
330
        :param member_name: Name of the revspec within the module
 
331
        """
 
332
        cls._possible_revspecs.append(
 
333
            registry._LazyObjectGetter(module_name, member_name))
 
334
 
336
335
 
337
336
class RevisionSpec_revno(RevisionSpec):
338
337
    """Selects a revision using a number."""
356
355
                                   your history is very long.
357
356
    """
358
357
    prefix = 'revno:'
359
 
    wants_revision_history = False
360
358
 
361
359
    def _match_on(self, branch, revs):
362
360
        """Lookup a revision by revision number"""
363
 
        branch, revno, revision_id = self._lookup(branch, revs)
 
361
        branch, revno, revision_id = self._lookup(branch)
364
362
        return RevisionInfo(branch, revno, revision_id)
365
363
 
366
 
    def _lookup(self, branch, revs_or_none):
 
364
    def _lookup(self, branch):
367
365
        loc = self.spec.find(':')
368
366
        if loc == -1:
369
367
            revno_spec = self.spec
387
385
                # right now - RBC 20060928
388
386
                try:
389
387
                    match_revno = tuple((int(number) for number in revno_spec.split('.')))
390
 
                except ValueError, e:
 
388
                except ValueError as e:
391
389
                    raise errors.InvalidRevisionSpec(self.user_spec, branch, e)
392
390
 
393
391
                dotted = True
394
392
 
395
393
        if branch_spec:
396
 
            # the user has override the branch to look in.
397
 
            # we need to refresh the revision_history map and
398
 
            # the branch object.
399
 
            from bzrlib.branch import Branch
400
 
            branch = Branch.open(branch_spec)
401
 
            revs_or_none = None
 
394
            # the user has overriden the branch to look in.
 
395
            branch = _mod_branch.Branch.open(branch_spec)
402
396
 
403
397
        if dotted:
404
398
            try:
408
402
                raise errors.InvalidRevisionSpec(self.user_spec, branch)
409
403
            else:
410
404
                # there is no traditional 'revno' for dotted-decimal revnos.
411
 
                # so for  API compatability we return None.
 
405
                # so for API compatibility we return None.
412
406
                return branch, None, revision_id
413
407
        else:
414
408
            last_revno, last_revision_id = branch.last_revision_info()
420
414
                else:
421
415
                    revno = last_revno + revno + 1
422
416
            try:
423
 
                revision_id = branch.get_rev_id(revno, revs_or_none)
 
417
                revision_id = branch.get_rev_id(revno)
424
418
            except errors.NoSuchRevision:
425
419
                raise errors.InvalidRevisionSpec(self.user_spec, branch)
426
420
        return branch, revno, revision_id
427
421
 
428
422
    def _as_revision_id(self, context_branch):
429
423
        # We would have the revno here, but we don't really care
430
 
        branch, revno, revision_id = self._lookup(context_branch, None)
 
424
        branch, revno, revision_id = self._lookup(context_branch)
431
425
        return revision_id
432
426
 
433
427
    def needs_branch(self):
443
437
RevisionSpec_int = RevisionSpec_revno
444
438
 
445
439
 
446
 
 
447
 
class RevisionSpec_revid(RevisionSpec):
 
440
class RevisionIDSpec(RevisionSpec):
 
441
 
 
442
    def _match_on(self, branch, revs):
 
443
        revision_id = self.as_revision_id(branch)
 
444
        return RevisionInfo.from_revision_id(branch, revision_id)
 
445
 
 
446
 
 
447
class RevisionSpec_revid(RevisionIDSpec):
448
448
    """Selects a revision using the revision id."""
449
449
 
450
450
    help_txt = """Selects a revision using the revision id.
459
459
 
460
460
    prefix = 'revid:'
461
461
 
462
 
    def _match_on(self, branch, revs):
 
462
    def _as_revision_id(self, context_branch):
463
463
        # self.spec comes straight from parsing the command line arguments,
464
464
        # so we expect it to be a Unicode string. Switch it to the internal
465
465
        # representation.
466
 
        revision_id = osutils.safe_revision_id(self.spec, warn=False)
467
 
        return RevisionInfo.from_revision_id(branch, revision_id, revs)
468
 
 
469
 
    def _as_revision_id(self, context_branch):
470
 
        return osutils.safe_revision_id(self.spec, warn=False)
 
466
        if isinstance(self.spec, unicode):
 
467
            return cache_utf8.encode(self.spec)
 
468
        return self.spec
471
469
 
472
470
 
473
471
 
487
485
    prefix = 'last:'
488
486
 
489
487
    def _match_on(self, branch, revs):
490
 
        revno, revision_id = self._revno_and_revision_id(branch, revs)
 
488
        revno, revision_id = self._revno_and_revision_id(branch)
491
489
        return RevisionInfo(branch, revno, revision_id)
492
490
 
493
 
    def _revno_and_revision_id(self, context_branch, revs_or_none):
 
491
    def _revno_and_revision_id(self, context_branch):
494
492
        last_revno, last_revision_id = context_branch.last_revision_info()
495
493
 
496
494
        if self.spec == '':
500
498
 
501
499
        try:
502
500
            offset = int(self.spec)
503
 
        except ValueError, e:
 
501
        except ValueError as e:
504
502
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch, e)
505
503
 
506
504
        if offset <= 0:
509
507
 
510
508
        revno = last_revno - offset + 1
511
509
        try:
512
 
            revision_id = context_branch.get_rev_id(revno, revs_or_none)
 
510
            revision_id = context_branch.get_rev_id(revno)
513
511
        except errors.NoSuchRevision:
514
512
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch)
515
513
        return revno, revision_id
517
515
    def _as_revision_id(self, context_branch):
518
516
        # We compute the revno as part of the process, but we don't really care
519
517
        # about it.
520
 
        revno, revision_id = self._revno_and_revision_id(context_branch, None)
 
518
        revno, revision_id = self._revno_and_revision_id(context_branch)
521
519
        return revision_id
522
520
 
523
521
 
555
553
            # We need to use the repository history here
556
554
            rev = branch.repository.get_revision(r.rev_id)
557
555
            if not rev.parent_ids:
558
 
                revno = 0
559
556
                revision_id = revision.NULL_REVISION
560
557
            else:
561
558
                revision_id = rev.parent_ids[0]
562
 
                try:
563
 
                    revno = revs.index(revision_id) + 1
564
 
                except ValueError:
565
 
                    revno = None
 
559
            revno = None
566
560
        else:
567
561
            revno = r.revno - 1
568
562
            try:
573
567
        return RevisionInfo(branch, revno, revision_id)
574
568
 
575
569
    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)
 
570
        base_revision_id = RevisionSpec.from_string(self.spec)._as_revision_id(context_branch)
578
571
        if base_revision_id == revision.NULL_REVISION:
579
572
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
580
573
                                         'cannot go before the null: revision')
610
603
    def _match_on(self, branch, revs):
611
604
        # Can raise tags not supported, NoSuchTag, etc
612
605
        return RevisionInfo.from_revision_id(branch,
613
 
            branch.tags.lookup_tag(self.spec),
614
 
            revs)
 
606
            branch.tags.lookup_tag(self.spec))
615
607
 
616
608
    def _as_revision_id(self, context_branch):
617
609
        return context_branch.tags.lookup_tag(self.spec)
621
613
class _RevListToTimestamps(object):
622
614
    """This takes a list of revisions, and allows you to bisect by date"""
623
615
 
624
 
    __slots__ = ['revs', 'branch']
 
616
    __slots__ = ['branch']
625
617
 
626
 
    def __init__(self, revs, branch):
627
 
        self.revs = revs
 
618
    def __init__(self, branch):
628
619
        self.branch = branch
629
620
 
630
621
    def __getitem__(self, index):
631
622
        """Get the date of the index'd item"""
632
 
        r = self.branch.repository.get_revision(self.revs[index])
 
623
        r = self.branch.repository.get_revision(self.branch.get_rev_id(index))
633
624
        # TODO: Handle timezone.
634
625
        return datetime.datetime.fromtimestamp(r.timestamp)
635
626
 
636
627
    def __len__(self):
637
 
        return len(self.revs)
 
628
        return self.branch.revno()
638
629
 
639
630
 
640
631
class RevisionSpec_date(RevisionSpec):
649
640
 
650
641
    One way to display all the changes since yesterday would be::
651
642
 
652
 
        bzr log -r date:yesterday..
 
643
        brz log -r date:yesterday..
653
644
 
654
645
    Examples::
655
646
 
658
649
                                   August 14th, 2006 at 5:10pm.
659
650
    """
660
651
    prefix = 'date:'
661
 
    _date_re = re.compile(
 
652
    _date_regex = lazy_regex.lazy_compile(
662
653
            r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
663
654
            r'(,|T)?\s*'
664
655
            r'(?P<time>(?P<hour>\d\d):(?P<minute>\d\d)(:(?P<second>\d\d))?)?'
682
673
        elif self.spec.lower() == 'tomorrow':
683
674
            dt = today + datetime.timedelta(days=1)
684
675
        else:
685
 
            m = self._date_re.match(self.spec)
 
676
            m = self._date_regex.match(self.spec)
686
677
            if not m or (not m.group('date') and not m.group('time')):
687
678
                raise errors.InvalidRevisionSpec(self.user_spec,
688
679
                                                 branch, 'invalid date')
714
705
                    hour=hour, minute=minute, second=second)
715
706
        branch.lock_read()
716
707
        try:
717
 
            rev = bisect.bisect(_RevListToTimestamps(revs, branch), dt)
 
708
            rev = bisect.bisect(_RevListToTimestamps(branch), dt, 1)
718
709
        finally:
719
710
            branch.unlock()
720
 
        if rev == len(revs):
 
711
        if rev == branch.revno():
721
712
            raise errors.InvalidRevisionSpec(self.user_spec, branch)
722
 
        else:
723
 
            return RevisionInfo(branch, rev + 1)
 
713
        return RevisionInfo(branch, rev)
724
714
 
725
715
 
726
716
 
757
747
    def _find_revision_info(branch, other_location):
758
748
        revision_id = RevisionSpec_ancestor._find_revision_id(branch,
759
749
                                                              other_location)
760
 
        try:
761
 
            revno = branch.revision_id_to_revno(revision_id)
762
 
        except errors.NoSuchRevision:
763
 
            revno = None
764
 
        return RevisionInfo(branch, revno, revision_id)
 
750
        return RevisionInfo(branch, None, revision_id)
765
751
 
766
752
    @staticmethod
767
753
    def _find_revision_id(branch, other_location):
768
 
        from bzrlib.branch import Branch
 
754
        from .branch import Branch
769
755
 
770
756
        branch.lock_read()
771
757
        try:
808
794
    dwim_catchable_exceptions = (errors.NotBranchError,)
809
795
 
810
796
    def _match_on(self, branch, revs):
811
 
        from bzrlib.branch import Branch
 
797
        from .branch import Branch
812
798
        other_branch = Branch.open(self.spec)
813
799
        revision_b = other_branch.last_revision()
814
800
        if revision_b in (None, revision.NULL_REVISION):
815
801
            raise errors.NoCommits(other_branch)
816
 
        # pull in the remote revisions so we can diff
817
 
        branch.fetch(other_branch, revision_b)
818
 
        try:
819
 
            revno = branch.revision_id_to_revno(revision_b)
820
 
        except errors.NoSuchRevision:
821
 
            revno = None
822
 
        return RevisionInfo(branch, revno, revision_b)
 
802
        if branch is None:
 
803
            branch = other_branch
 
804
        else:
 
805
            try:
 
806
                # pull in the remote revisions so we can diff
 
807
                branch.fetch(other_branch, revision_b)
 
808
            except errors.ReadOnlyError:
 
809
                branch = other_branch
 
810
        return RevisionInfo(branch, None, revision_b)
823
811
 
824
812
    def _as_revision_id(self, context_branch):
825
 
        from bzrlib.branch import Branch
 
813
        from .branch import Branch
826
814
        other_branch = Branch.open(self.spec)
827
815
        last_revision = other_branch.last_revision()
828
816
        last_revision = revision.ensure_null(last_revision)
832
820
        return last_revision
833
821
 
834
822
    def _as_tree(self, context_branch):
835
 
        from bzrlib.branch import Branch
 
823
        from .branch import Branch
836
824
        other_branch = Branch.open(self.spec)
837
825
        last_revision = other_branch.last_revision()
838
826
        last_revision = revision.ensure_null(last_revision)
840
828
            raise errors.NoCommits(other_branch)
841
829
        return other_branch.repository.revision_tree(last_revision)
842
830
 
 
831
    def needs_branch(self):
 
832
        return False
 
833
 
 
834
    def get_branch(self):
 
835
        return self.spec
 
836
 
843
837
 
844
838
 
845
839
class RevisionSpec_submit(RevisionSpec_ancestor):
871
865
            location_type = 'parent branch'
872
866
        if submit_location is None:
873
867
            raise errors.NoSubmitBranch(branch)
874
 
        trace.note('Using %s %s', location_type, submit_location)
 
868
        trace.note(gettext('Using {0} {1}').format(location_type,
 
869
                                                        submit_location))
875
870
        return submit_location
876
871
 
877
872
    def _match_on(self, branch, revs):
884
879
            self._get_submit_location(context_branch))
885
880
 
886
881
 
 
882
class RevisionSpec_annotate(RevisionIDSpec):
 
883
 
 
884
    prefix = 'annotate:'
 
885
 
 
886
    help_txt = """Select the revision that last modified the specified line.
 
887
 
 
888
    Select the revision that last modified the specified line.  Line is
 
889
    specified as path:number.  Path is a relative path to the file.  Numbers
 
890
    start at 1, and are relative to the current version, not the last-
 
891
    committed version of the file.
 
892
    """
 
893
 
 
894
    def _raise_invalid(self, numstring, context_branch):
 
895
        raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
 
896
            'No such line: %s' % numstring)
 
897
 
 
898
    def _as_revision_id(self, context_branch):
 
899
        path, numstring = self.spec.rsplit(':', 1)
 
900
        try:
 
901
            index = int(numstring) - 1
 
902
        except ValueError:
 
903
            self._raise_invalid(numstring, context_branch)
 
904
        tree, file_path = workingtree.WorkingTree.open_containing(path)
 
905
        tree.lock_read()
 
906
        try:
 
907
            file_id = tree.path2id(file_path)
 
908
            if file_id is None:
 
909
                raise errors.InvalidRevisionSpec(self.user_spec,
 
910
                    context_branch, "File '%s' is not versioned." %
 
911
                    file_path)
 
912
            revision_ids = [r for (r, l) in tree.annotate_iter(file_id)]
 
913
        finally:
 
914
            tree.unlock()
 
915
        try:
 
916
            revision_id = revision_ids[index]
 
917
        except IndexError:
 
918
            self._raise_invalid(numstring, context_branch)
 
919
        if revision_id == revision.CURRENT_REVISION:
 
920
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
 
921
                'Line %s has not been committed.' % numstring)
 
922
        return revision_id
 
923
 
 
924
 
 
925
class RevisionSpec_mainline(RevisionIDSpec):
 
926
 
 
927
    help_txt = """Select mainline revision that merged the specified revision.
 
928
 
 
929
    Select the revision that merged the specified revision into mainline.
 
930
    """
 
931
 
 
932
    prefix = 'mainline:'
 
933
 
 
934
    def _as_revision_id(self, context_branch):
 
935
        revspec = RevisionSpec.from_string(self.spec)
 
936
        if revspec.get_branch() is None:
 
937
            spec_branch = context_branch
 
938
        else:
 
939
            spec_branch = _mod_branch.Branch.open(revspec.get_branch())
 
940
        revision_id = revspec.as_revision_id(spec_branch)
 
941
        graph = context_branch.repository.get_graph()
 
942
        result = graph.find_lefthand_merger(revision_id,
 
943
                                            context_branch.last_revision())
 
944
        if result is None:
 
945
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch)
 
946
        return result
 
947
 
 
948
 
887
949
# The order in which we want to DWIM a revision spec without any prefix.
888
950
# revno is always tried first and isn't listed here, this is used by
889
951
# RevisionSpec_dwim._match_on
890
 
dwim_revspecs = [
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
895
 
    ]
896
 
 
 
952
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_tag)
 
953
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_revid)
 
954
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_date)
 
955
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_branch)
897
956
 
898
957
revspec_registry = registry.Registry()
899
958
def _register_revspec(revspec):
908
967
_register_revspec(RevisionSpec_ancestor)
909
968
_register_revspec(RevisionSpec_branch)
910
969
_register_revspec(RevisionSpec_submit)
911
 
 
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", [])
 
970
_register_revspec(RevisionSpec_annotate)
 
971
_register_revspec(RevisionSpec_mainline)