~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/revisionspec.py

  • Committer: Vincent Ladeuil
  • Date: 2017-01-30 14:30:10 UTC
  • mfrom: (6615.3.7 merges)
  • mto: This revision was merged to the branch mainline in revision 6621.
  • Revision ID: v.ladeuil+lp@free.fr-20170130143010-p31t1ranfeqbaeki
Merge  2.7 into trunk including fix for bug #1657238

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2005-2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
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
17
18
 
18
 
import re
19
19
 
20
20
from bzrlib.lazy_import import lazy_import
21
21
lazy_import(globals(), """
22
22
import bisect
23
23
import datetime
 
24
 
 
25
from bzrlib import (
 
26
    branch as _mod_branch,
 
27
    osutils,
 
28
    revision,
 
29
    symbol_versioning,
 
30
    workingtree,
 
31
    )
 
32
from bzrlib.i18n import gettext
24
33
""")
25
34
 
26
35
from bzrlib 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
            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
67
78
 
68
79
    def __nonzero__(self):
69
 
        # first the easy ones...
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
99
107
            self.revno, self.rev_id, self.branch)
100
108
 
101
109
    @staticmethod
102
 
    def from_revision_id(branch, revision_id, revs):
 
110
    def from_revision_id(branch, revision_id, revs=symbol_versioning.DEPRECATED_PARAMETER):
103
111
        """Construct a RevisionInfo given just the id.
104
112
 
105
113
        Use this if you don't know or care what the revno is.
106
114
        """
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
 
# classes in this list should have a "prefix" attribute, against which
117
 
# string specs are matched
118
 
_revno_regex = None
 
115
        if symbol_versioning.deprecated_passed(revs):
 
116
            symbol_versioning.warn(
 
117
                'RevisionInfo.from_revision_id(revs) was deprecated in 2.5.',
 
118
                DeprecationWarning,
 
119
                stacklevel=2)
 
120
        return RevisionInfo(branch, revno=None, rev_id=revision_id)
119
121
 
120
122
 
121
123
class RevisionSpec(object):
123
125
 
124
126
    help_txt = """A parsed revision specification.
125
127
 
126
 
    A revision specification can be an integer, in which case it is
127
 
    assumed to be a revno (though this will translate negative values
128
 
    into positive ones); or it can be a string, in which case it is
129
 
    parsed for something like 'date:' or 'revid:' etc.
 
128
    A revision specification is a string, which may be unambiguous about
 
129
    what it represents by giving a prefix like 'date:' or 'revid:' etc,
 
130
    or it may have no prefix, in which case it's tried against several
 
131
    specifier types in sequence to determine what the user meant.
130
132
 
131
133
    Revision specs are an UI element, and they have been moved out
132
134
    of the branch class to leave "back-end" classes unaware of such
138
140
    """
139
141
 
140
142
    prefix = None
141
 
    wants_revision_history = True
 
143
    # wants_revision_history has been deprecated in 2.5.
 
144
    wants_revision_history = False
 
145
    dwim_catchable_exceptions = (errors.InvalidRevisionSpec,)
 
146
    """Exceptions that RevisionSpec_dwim._match_on will catch.
 
147
 
 
148
    If the revspec is part of ``dwim_revspecs``, it may be tried with an
 
149
    invalid revspec and raises some exception. The exceptions mentioned here
 
150
    will not be reported to the user but simply ignored without stopping the
 
151
    dwim processing.
 
152
    """
142
153
 
143
154
    @staticmethod
144
155
    def from_string(spec):
160
171
                         spectype.__name__, spec)
161
172
            return spectype(spec, _internal=True)
162
173
        else:
163
 
            for spectype in SPEC_TYPES:
164
 
                trace.mutter('Returning RevisionSpec %s for %s',
165
 
                             spectype.__name__, spec)
166
 
                if spec.startswith(spectype.prefix):
167
 
                    return spectype(spec, _internal=True)
168
 
            # RevisionSpec_revno is special cased, because it is the only
169
 
            # one that directly handles plain integers
170
 
            # TODO: This should not be special cased rather it should be
171
 
            # a method invocation on spectype.canparse()
172
 
            global _revno_regex
173
 
            if _revno_regex is None:
174
 
                _revno_regex = re.compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
175
 
            if _revno_regex.match(spec) is not None:
176
 
                return RevisionSpec_revno(spec, _internal=True)
177
 
 
178
 
            raise errors.NoSuchRevisionSpec(spec)
 
174
            # Otherwise treat it as a DWIM, build the RevisionSpec object and
 
175
            # wait for _match_on to be called.
 
176
            return RevisionSpec_dwim(spec, _internal=True)
179
177
 
180
178
    def __init__(self, spec, _internal=False):
181
179
        """Create a RevisionSpec referring to the Null revision.
185
183
            called directly. Only from RevisionSpec.from_string()
186
184
        """
187
185
        if not _internal:
188
 
            # XXX: Update this after 0.10 is released
189
186
            symbol_versioning.warn('Creating a RevisionSpec directly has'
190
187
                                   ' been deprecated in version 0.11. Use'
191
188
                                   ' RevisionSpec.from_string()'
215
212
    def in_history(self, branch):
216
213
        if branch:
217
214
            if self.wants_revision_history:
218
 
                revs = branch.revision_history()
 
215
                symbol_versioning.warn(
 
216
                    "RevisionSpec.wants_revision_history was "
 
217
                    "deprecated in 2.5 (%s)." % self.__class__.__name__,
 
218
                    DeprecationWarning)
 
219
                branch.lock_read()
 
220
                try:
 
221
                    graph = branch.repository.get_graph()
 
222
                    revs = list(graph.iter_lefthand_ancestry(
 
223
                        branch.last_revision(), [revision.NULL_REVISION]))
 
224
                finally:
 
225
                    branch.unlock()
 
226
                revs.reverse()
219
227
            else:
220
228
                revs = None
221
229
        else:
291
299
 
292
300
# private API
293
301
 
 
302
class RevisionSpec_dwim(RevisionSpec):
 
303
    """Provides a DWIMish revision specifier lookup.
 
304
 
 
305
    Note that this does not go in the revspec_registry because by definition
 
306
    there is no prefix to identify it.  It's solely called from
 
307
    RevisionSpec.from_string() because the DWIMification happen when _match_on
 
308
    is called so the string describing the revision is kept here until needed.
 
309
    """
 
310
 
 
311
    help_txt = None
 
312
 
 
313
    _revno_regex = lazy_regex.lazy_compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
 
314
 
 
315
    # The revspecs to try
 
316
    _possible_revspecs = []
 
317
 
 
318
    def _try_spectype(self, rstype, branch):
 
319
        rs = rstype(self.spec, _internal=True)
 
320
        # Hit in_history to find out if it exists, or we need to try the
 
321
        # next type.
 
322
        return rs.in_history(branch)
 
323
 
 
324
    def _match_on(self, branch, revs):
 
325
        """Run the lookup and see what we can get."""
 
326
 
 
327
        # First, see if it's a revno
 
328
        if self._revno_regex.match(self.spec) is not None:
 
329
            try:
 
330
                return self._try_spectype(RevisionSpec_revno, branch)
 
331
            except RevisionSpec_revno.dwim_catchable_exceptions:
 
332
                pass
 
333
 
 
334
        # Next see what has been registered
 
335
        for objgetter in self._possible_revspecs:
 
336
            rs_class = objgetter.get_obj()
 
337
            try:
 
338
                return self._try_spectype(rs_class, branch)
 
339
            except rs_class.dwim_catchable_exceptions:
 
340
                pass
 
341
 
 
342
        # Try the old (deprecated) dwim list:
 
343
        for rs_class in dwim_revspecs:
 
344
            try:
 
345
                return self._try_spectype(rs_class, branch)
 
346
            except rs_class.dwim_catchable_exceptions:
 
347
                pass
 
348
 
 
349
        # Well, I dunno what it is. Note that we don't try to keep track of the
 
350
        # first of last exception raised during the DWIM tries as none seems
 
351
        # really relevant.
 
352
        raise errors.InvalidRevisionSpec(self.spec, branch)
 
353
 
 
354
    @classmethod
 
355
    def append_possible_revspec(cls, revspec):
 
356
        """Append a possible DWIM revspec.
 
357
 
 
358
        :param revspec: Revision spec to try.
 
359
        """
 
360
        cls._possible_revspecs.append(registry._ObjectGetter(revspec))
 
361
 
 
362
    @classmethod
 
363
    def append_possible_lazy_revspec(cls, module_name, member_name):
 
364
        """Append a possible lazily loaded DWIM revspec.
 
365
 
 
366
        :param module_name: Name of the module with the revspec
 
367
        :param member_name: Name of the revspec within the module
 
368
        """
 
369
        cls._possible_revspecs.append(
 
370
            registry._LazyObjectGetter(module_name, member_name))
 
371
 
 
372
 
294
373
class RevisionSpec_revno(RevisionSpec):
295
374
    """Selects a revision using a number."""
296
375
 
297
376
    help_txt = """Selects a revision using a number.
298
377
 
299
378
    Use an integer to specify a revision in the history of the branch.
300
 
    Optionally a branch can be specified. The 'revno:' prefix is optional.
301
 
    A negative number will count from the end of the branch (-1 is the
302
 
    last revision, -2 the previous one). If the negative number is larger
303
 
    than the branch's history, the first revision is returned.
 
379
    Optionally a branch can be specified.  A negative number will count
 
380
    from the end of the branch (-1 is the last revision, -2 the previous
 
381
    one). If the negative number is larger than the branch's history, the
 
382
    first revision is returned.
304
383
    Examples::
305
384
 
306
385
      revno:1                   -> return the first revision of this branch
313
392
                                   your history is very long.
314
393
    """
315
394
    prefix = 'revno:'
316
 
    wants_revision_history = False
317
395
 
318
396
    def _match_on(self, branch, revs):
319
397
        """Lookup a revision by revision number"""
320
 
        branch, revno, revision_id = self._lookup(branch, revs)
 
398
        branch, revno, revision_id = self._lookup(branch)
321
399
        return RevisionInfo(branch, revno, revision_id)
322
400
 
323
 
    def _lookup(self, branch, revs_or_none):
 
401
    def _lookup(self, branch):
324
402
        loc = self.spec.find(':')
325
403
        if loc == -1:
326
404
            revno_spec = self.spec
350
428
                dotted = True
351
429
 
352
430
        if branch_spec:
353
 
            # the user has override the branch to look in.
354
 
            # we need to refresh the revision_history map and
355
 
            # the branch object.
356
 
            from bzrlib.branch import Branch
357
 
            branch = Branch.open(branch_spec)
358
 
            revs_or_none = None
 
431
            # the user has overriden the branch to look in.
 
432
            branch = _mod_branch.Branch.open(branch_spec)
359
433
 
360
434
        if dotted:
361
435
            try:
365
439
                raise errors.InvalidRevisionSpec(self.user_spec, branch)
366
440
            else:
367
441
                # there is no traditional 'revno' for dotted-decimal revnos.
368
 
                # so for  API compatability we return None.
 
442
                # so for API compatibility we return None.
369
443
                return branch, None, revision_id
370
444
        else:
371
445
            last_revno, last_revision_id = branch.last_revision_info()
377
451
                else:
378
452
                    revno = last_revno + revno + 1
379
453
            try:
380
 
                revision_id = branch.get_rev_id(revno, revs_or_none)
 
454
                revision_id = branch.get_rev_id(revno)
381
455
            except errors.NoSuchRevision:
382
456
                raise errors.InvalidRevisionSpec(self.user_spec, branch)
383
457
        return branch, revno, revision_id
384
458
 
385
459
    def _as_revision_id(self, context_branch):
386
460
        # We would have the revno here, but we don't really care
387
 
        branch, revno, revision_id = self._lookup(context_branch, None)
 
461
        branch, revno, revision_id = self._lookup(context_branch)
388
462
        return revision_id
389
463
 
390
464
    def needs_branch(self):
400
474
RevisionSpec_int = RevisionSpec_revno
401
475
 
402
476
 
403
 
 
404
 
class RevisionSpec_revid(RevisionSpec):
 
477
class RevisionIDSpec(RevisionSpec):
 
478
 
 
479
    def _match_on(self, branch, revs):
 
480
        revision_id = self.as_revision_id(branch)
 
481
        return RevisionInfo.from_revision_id(branch, revision_id)
 
482
 
 
483
 
 
484
class RevisionSpec_revid(RevisionIDSpec):
405
485
    """Selects a revision using the revision id."""
406
486
 
407
487
    help_txt = """Selects a revision using the revision id.
416
496
 
417
497
    prefix = 'revid:'
418
498
 
419
 
    def _match_on(self, branch, revs):
 
499
    def _as_revision_id(self, context_branch):
420
500
        # self.spec comes straight from parsing the command line arguments,
421
501
        # so we expect it to be a Unicode string. Switch it to the internal
422
502
        # representation.
423
 
        revision_id = osutils.safe_revision_id(self.spec, warn=False)
424
 
        return RevisionInfo.from_revision_id(branch, revision_id, revs)
425
 
 
426
 
    def _as_revision_id(self, context_branch):
427
503
        return osutils.safe_revision_id(self.spec, warn=False)
428
504
 
429
505
 
444
520
    prefix = 'last:'
445
521
 
446
522
    def _match_on(self, branch, revs):
447
 
        revno, revision_id = self._revno_and_revision_id(branch, revs)
 
523
        revno, revision_id = self._revno_and_revision_id(branch)
448
524
        return RevisionInfo(branch, revno, revision_id)
449
525
 
450
 
    def _revno_and_revision_id(self, context_branch, revs_or_none):
 
526
    def _revno_and_revision_id(self, context_branch):
451
527
        last_revno, last_revision_id = context_branch.last_revision_info()
452
528
 
453
529
        if self.spec == '':
466
542
 
467
543
        revno = last_revno - offset + 1
468
544
        try:
469
 
            revision_id = context_branch.get_rev_id(revno, revs_or_none)
 
545
            revision_id = context_branch.get_rev_id(revno)
470
546
        except errors.NoSuchRevision:
471
547
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch)
472
548
        return revno, revision_id
474
550
    def _as_revision_id(self, context_branch):
475
551
        # We compute the revno as part of the process, but we don't really care
476
552
        # about it.
477
 
        revno, revision_id = self._revno_and_revision_id(context_branch, None)
 
553
        revno, revision_id = self._revno_and_revision_id(context_branch)
478
554
        return revision_id
479
555
 
480
556
 
512
588
            # We need to use the repository history here
513
589
            rev = branch.repository.get_revision(r.rev_id)
514
590
            if not rev.parent_ids:
515
 
                revno = 0
516
591
                revision_id = revision.NULL_REVISION
517
592
            else:
518
593
                revision_id = rev.parent_ids[0]
519
 
                try:
520
 
                    revno = revs.index(revision_id) + 1
521
 
                except ValueError:
522
 
                    revno = None
 
594
            revno = None
523
595
        else:
524
596
            revno = r.revno - 1
525
597
            try:
530
602
        return RevisionInfo(branch, revno, revision_id)
531
603
 
532
604
    def _as_revision_id(self, context_branch):
533
 
        base_revspec = RevisionSpec.from_string(self.spec)
534
 
        base_revision_id = base_revspec.as_revision_id(context_branch)
 
605
        base_revision_id = RevisionSpec.from_string(self.spec)._as_revision_id(context_branch)
535
606
        if base_revision_id == revision.NULL_REVISION:
536
607
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
537
608
                                         'cannot go before the null: revision')
562
633
    """
563
634
 
564
635
    prefix = 'tag:'
 
636
    dwim_catchable_exceptions = (errors.NoSuchTag, errors.TagsNotSupported)
565
637
 
566
638
    def _match_on(self, branch, revs):
567
639
        # Can raise tags not supported, NoSuchTag, etc
568
640
        return RevisionInfo.from_revision_id(branch,
569
 
            branch.tags.lookup_tag(self.spec),
570
 
            revs)
 
641
            branch.tags.lookup_tag(self.spec))
571
642
 
572
643
    def _as_revision_id(self, context_branch):
573
644
        return context_branch.tags.lookup_tag(self.spec)
577
648
class _RevListToTimestamps(object):
578
649
    """This takes a list of revisions, and allows you to bisect by date"""
579
650
 
580
 
    __slots__ = ['revs', 'branch']
 
651
    __slots__ = ['branch']
581
652
 
582
 
    def __init__(self, revs, branch):
583
 
        self.revs = revs
 
653
    def __init__(self, branch):
584
654
        self.branch = branch
585
655
 
586
656
    def __getitem__(self, index):
587
657
        """Get the date of the index'd item"""
588
 
        r = self.branch.repository.get_revision(self.revs[index])
 
658
        r = self.branch.repository.get_revision(self.branch.get_rev_id(index))
589
659
        # TODO: Handle timezone.
590
660
        return datetime.datetime.fromtimestamp(r.timestamp)
591
661
 
592
662
    def __len__(self):
593
 
        return len(self.revs)
 
663
        return self.branch.revno()
594
664
 
595
665
 
596
666
class RevisionSpec_date(RevisionSpec):
614
684
                                   August 14th, 2006 at 5:10pm.
615
685
    """
616
686
    prefix = 'date:'
617
 
    _date_re = re.compile(
 
687
    _date_regex = lazy_regex.lazy_compile(
618
688
            r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
619
689
            r'(,|T)?\s*'
620
690
            r'(?P<time>(?P<hour>\d\d):(?P<minute>\d\d)(:(?P<second>\d\d))?)?'
638
708
        elif self.spec.lower() == 'tomorrow':
639
709
            dt = today + datetime.timedelta(days=1)
640
710
        else:
641
 
            m = self._date_re.match(self.spec)
 
711
            m = self._date_regex.match(self.spec)
642
712
            if not m or (not m.group('date') and not m.group('time')):
643
713
                raise errors.InvalidRevisionSpec(self.user_spec,
644
714
                                                 branch, 'invalid date')
670
740
                    hour=hour, minute=minute, second=second)
671
741
        branch.lock_read()
672
742
        try:
673
 
            rev = bisect.bisect(_RevListToTimestamps(revs, branch), dt)
 
743
            rev = bisect.bisect(_RevListToTimestamps(branch), dt, 1)
674
744
        finally:
675
745
            branch.unlock()
676
 
        if rev == len(revs):
 
746
        if rev == branch.revno():
677
747
            raise errors.InvalidRevisionSpec(self.user_spec, branch)
678
 
        else:
679
 
            return RevisionInfo(branch, rev + 1)
 
748
        return RevisionInfo(branch, rev)
680
749
 
681
750
 
682
751
 
713
782
    def _find_revision_info(branch, other_location):
714
783
        revision_id = RevisionSpec_ancestor._find_revision_id(branch,
715
784
                                                              other_location)
716
 
        try:
717
 
            revno = branch.revision_id_to_revno(revision_id)
718
 
        except errors.NoSuchRevision:
719
 
            revno = None
720
 
        return RevisionInfo(branch, revno, revision_id)
 
785
        return RevisionInfo(branch, None, revision_id)
721
786
 
722
787
    @staticmethod
723
788
    def _find_revision_id(branch, other_location):
761
826
      branch:/path/to/branch
762
827
    """
763
828
    prefix = 'branch:'
 
829
    dwim_catchable_exceptions = (errors.NotBranchError,)
764
830
 
765
831
    def _match_on(self, branch, revs):
766
832
        from bzrlib.branch import Branch
768
834
        revision_b = other_branch.last_revision()
769
835
        if revision_b in (None, revision.NULL_REVISION):
770
836
            raise errors.NoCommits(other_branch)
771
 
        # pull in the remote revisions so we can diff
772
 
        branch.fetch(other_branch, revision_b)
773
 
        try:
774
 
            revno = branch.revision_id_to_revno(revision_b)
775
 
        except errors.NoSuchRevision:
776
 
            revno = None
777
 
        return RevisionInfo(branch, revno, revision_b)
 
837
        if branch is None:
 
838
            branch = other_branch
 
839
        else:
 
840
            try:
 
841
                # pull in the remote revisions so we can diff
 
842
                branch.fetch(other_branch, revision_b)
 
843
            except errors.ReadOnlyError:
 
844
                branch = other_branch
 
845
        return RevisionInfo(branch, None, revision_b)
778
846
 
779
847
    def _as_revision_id(self, context_branch):
780
848
        from bzrlib.branch import Branch
795
863
            raise errors.NoCommits(other_branch)
796
864
        return other_branch.repository.revision_tree(last_revision)
797
865
 
 
866
    def needs_branch(self):
 
867
        return False
 
868
 
 
869
    def get_branch(self):
 
870
        return self.spec
 
871
 
798
872
 
799
873
 
800
874
class RevisionSpec_submit(RevisionSpec_ancestor):
826
900
            location_type = 'parent branch'
827
901
        if submit_location is None:
828
902
            raise errors.NoSubmitBranch(branch)
829
 
        trace.note('Using %s %s', location_type, submit_location)
 
903
        trace.note(gettext('Using {0} {1}').format(location_type,
 
904
                                                        submit_location))
830
905
        return submit_location
831
906
 
832
907
    def _match_on(self, branch, revs):
839
914
            self._get_submit_location(context_branch))
840
915
 
841
916
 
 
917
class RevisionSpec_annotate(RevisionIDSpec):
 
918
 
 
919
    prefix = 'annotate:'
 
920
 
 
921
    help_txt = """Select the revision that last modified the specified line.
 
922
 
 
923
    Select the revision that last modified the specified line.  Line is
 
924
    specified as path:number.  Path is a relative path to the file.  Numbers
 
925
    start at 1, and are relative to the current version, not the last-
 
926
    committed version of the file.
 
927
    """
 
928
 
 
929
    def _raise_invalid(self, numstring, context_branch):
 
930
        raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
 
931
            'No such line: %s' % numstring)
 
932
 
 
933
    def _as_revision_id(self, context_branch):
 
934
        path, numstring = self.spec.rsplit(':', 1)
 
935
        try:
 
936
            index = int(numstring) - 1
 
937
        except ValueError:
 
938
            self._raise_invalid(numstring, context_branch)
 
939
        tree, file_path = workingtree.WorkingTree.open_containing(path)
 
940
        tree.lock_read()
 
941
        try:
 
942
            file_id = tree.path2id(file_path)
 
943
            if file_id is None:
 
944
                raise errors.InvalidRevisionSpec(self.user_spec,
 
945
                    context_branch, "File '%s' is not versioned." %
 
946
                    file_path)
 
947
            revision_ids = [r for (r, l) in tree.annotate_iter(file_id)]
 
948
        finally:
 
949
            tree.unlock()
 
950
        try:
 
951
            revision_id = revision_ids[index]
 
952
        except IndexError:
 
953
            self._raise_invalid(numstring, context_branch)
 
954
        if revision_id == revision.CURRENT_REVISION:
 
955
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
 
956
                'Line %s has not been committed.' % numstring)
 
957
        return revision_id
 
958
 
 
959
 
 
960
class RevisionSpec_mainline(RevisionIDSpec):
 
961
 
 
962
    help_txt = """Select mainline revision that merged the specified revision.
 
963
 
 
964
    Select the revision that merged the specified revision into mainline.
 
965
    """
 
966
 
 
967
    prefix = 'mainline:'
 
968
 
 
969
    def _as_revision_id(self, context_branch):
 
970
        revspec = RevisionSpec.from_string(self.spec)
 
971
        if revspec.get_branch() is None:
 
972
            spec_branch = context_branch
 
973
        else:
 
974
            spec_branch = _mod_branch.Branch.open(revspec.get_branch())
 
975
        revision_id = revspec.as_revision_id(spec_branch)
 
976
        graph = context_branch.repository.get_graph()
 
977
        result = graph.find_lefthand_merger(revision_id,
 
978
                                            context_branch.last_revision())
 
979
        if result is None:
 
980
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch)
 
981
        return result
 
982
 
 
983
 
 
984
# The order in which we want to DWIM a revision spec without any prefix.
 
985
# revno is always tried first and isn't listed here, this is used by
 
986
# RevisionSpec_dwim._match_on
 
987
dwim_revspecs = symbol_versioning.deprecated_list(
 
988
    symbol_versioning.deprecated_in((2, 4, 0)), "dwim_revspecs", [])
 
989
 
 
990
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_tag)
 
991
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_revid)
 
992
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_date)
 
993
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_branch)
 
994
 
842
995
revspec_registry = registry.Registry()
843
996
def _register_revspec(revspec):
844
997
    revspec_registry.register(revspec.prefix, revspec)
852
1005
_register_revspec(RevisionSpec_ancestor)
853
1006
_register_revspec(RevisionSpec_branch)
854
1007
_register_revspec(RevisionSpec_submit)
855
 
 
856
 
SPEC_TYPES = symbol_versioning.deprecated_list(
857
 
    symbol_versioning.deprecated_in((1, 12, 0)), "SPEC_TYPES", [])
 
1008
_register_revspec(RevisionSpec_annotate)
 
1009
_register_revspec(RevisionSpec_mainline)