/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-09-30 00:33:53 UTC
  • mto: This revision was merged to the branch mainline in revision 7134.
  • Revision ID: jelmer@jelmer.uk-20180930003353-2z5sugalbxfxfiru
When opening working trees with .git files, open the right control transport.

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