/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 bzrlib/revisionspec.py

  • Committer: Robert Collins
  • Date: 2010-05-06 11:08:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506110810-h3j07fh5gmw54s25
Cleaner matcher matching revised unlocking protocol.

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