/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: Jelmer Vernooij
  • Date: 2018-11-16 10:50:21 UTC
  • mfrom: (7164 work)
  • mto: This revision was merged to the branch mainline in revision 7165.
  • Revision ID: jelmer@jelmer.uk-20181116105021-xl419v2rh4aus1au
Merge trunk.

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