~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/revisionspec.py

  • Committer: Patch Queue Manager
  • Date: 2015-12-17 18:39:00 UTC
  • mfrom: (6606.1.2 fix-float)
  • Revision ID: pqm@pqm.ubuntu.com-20151217183900-0719du2uv1kwu3lc
(vila) Inline testtools private method to fix an issue in xenial (the
 private implementation has changed in an backward incompatible way).
 (Jelmer Vernooij)

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
 
17
19
 
18
20
from bzrlib.lazy_import import lazy_import
19
21
lazy_import(globals(), """
38
40
    )
39
41
 
40
42
 
41
 
_marker = []
42
 
 
43
 
 
44
43
class RevisionInfo(object):
45
44
    """The results of applying a revision specification to a branch."""
46
45
 
58
57
    or treat the result as a tuple.
59
58
    """
60
59
 
61
 
    def __init__(self, branch, revno, rev_id=_marker):
 
60
    def __init__(self, branch, revno=None, rev_id=None):
62
61
        self.branch = branch
63
 
        self.revno = revno
64
 
        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:
65
66
            # allow caller to be lazy
66
 
            if self.revno is None:
67
 
                self.rev_id = None
68
 
            else:
69
 
                self.rev_id = branch.get_rev_id(self.revno)
70
 
        else:
71
 
            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
72
78
 
73
79
    def __nonzero__(self):
74
 
        # first the easy ones...
75
80
        if self.rev_id is None:
76
81
            return False
77
 
        if self.revno is not None:
78
 
            return True
79
82
        # TODO: otherwise, it should depend on how I was built -
80
83
        # if it's in_history(branch), then check revision_history(),
81
84
        # if it's in_store(branch), do the check below
104
107
            self.revno, self.rev_id, self.branch)
105
108
 
106
109
    @staticmethod
107
 
    def from_revision_id(branch, revision_id, revs):
 
110
    def from_revision_id(branch, revision_id, revs=symbol_versioning.DEPRECATED_PARAMETER):
108
111
        """Construct a RevisionInfo given just the id.
109
112
 
110
113
        Use this if you don't know or care what the revno is.
111
114
        """
112
 
        if revision_id == revision.NULL_REVISION:
113
 
            return RevisionInfo(branch, 0, revision_id)
114
 
        try:
115
 
            revno = revs.index(revision_id) + 1
116
 
        except ValueError:
117
 
            revno = None
118
 
        return RevisionInfo(branch, revno, revision_id)
 
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):
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
142
145
    dwim_catchable_exceptions = (errors.InvalidRevisionSpec,)
143
146
    """Exceptions that RevisionSpec_dwim._match_on will catch.
144
147
 
209
212
    def in_history(self, branch):
210
213
        if branch:
211
214
            if self.wants_revision_history:
212
 
                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()
213
227
            else:
214
228
                revs = None
215
229
        else:
295
309
    """
296
310
 
297
311
    help_txt = None
298
 
    # We don't need to build the revision history ourself, that's delegated to
299
 
    # each revspec we try.
300
 
    wants_revision_history = False
301
312
 
302
313
    _revno_regex = lazy_regex.lazy_compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
303
314
 
381
392
                                   your history is very long.
382
393
    """
383
394
    prefix = 'revno:'
384
 
    wants_revision_history = False
385
395
 
386
396
    def _match_on(self, branch, revs):
387
397
        """Lookup a revision by revision number"""
388
 
        branch, revno, revision_id = self._lookup(branch, revs)
 
398
        branch, revno, revision_id = self._lookup(branch)
389
399
        return RevisionInfo(branch, revno, revision_id)
390
400
 
391
 
    def _lookup(self, branch, revs_or_none):
 
401
    def _lookup(self, branch):
392
402
        loc = self.spec.find(':')
393
403
        if loc == -1:
394
404
            revno_spec = self.spec
418
428
                dotted = True
419
429
 
420
430
        if branch_spec:
421
 
            # the user has override the branch to look in.
422
 
            # we need to refresh the revision_history map and
423
 
            # the branch object.
424
 
            from bzrlib.branch import Branch
425
 
            branch = Branch.open(branch_spec)
426
 
            revs_or_none = None
 
431
            # the user has overriden the branch to look in.
 
432
            branch = _mod_branch.Branch.open(branch_spec)
427
433
 
428
434
        if dotted:
429
435
            try:
433
439
                raise errors.InvalidRevisionSpec(self.user_spec, branch)
434
440
            else:
435
441
                # there is no traditional 'revno' for dotted-decimal revnos.
436
 
                # so for  API compatability we return None.
 
442
                # so for API compatibility we return None.
437
443
                return branch, None, revision_id
438
444
        else:
439
445
            last_revno, last_revision_id = branch.last_revision_info()
445
451
                else:
446
452
                    revno = last_revno + revno + 1
447
453
            try:
448
 
                revision_id = branch.get_rev_id(revno, revs_or_none)
 
454
                revision_id = branch.get_rev_id(revno)
449
455
            except errors.NoSuchRevision:
450
456
                raise errors.InvalidRevisionSpec(self.user_spec, branch)
451
457
        return branch, revno, revision_id
452
458
 
453
459
    def _as_revision_id(self, context_branch):
454
460
        # We would have the revno here, but we don't really care
455
 
        branch, revno, revision_id = self._lookup(context_branch, None)
 
461
        branch, revno, revision_id = self._lookup(context_branch)
456
462
        return revision_id
457
463
 
458
464
    def needs_branch(self):
468
474
RevisionSpec_int = RevisionSpec_revno
469
475
 
470
476
 
471
 
 
472
477
class RevisionIDSpec(RevisionSpec):
473
478
 
474
479
    def _match_on(self, branch, revs):
475
480
        revision_id = self.as_revision_id(branch)
476
 
        return RevisionInfo.from_revision_id(branch, revision_id, revs)
 
481
        return RevisionInfo.from_revision_id(branch, revision_id)
477
482
 
478
483
 
479
484
class RevisionSpec_revid(RevisionIDSpec):
515
520
    prefix = 'last:'
516
521
 
517
522
    def _match_on(self, branch, revs):
518
 
        revno, revision_id = self._revno_and_revision_id(branch, revs)
 
523
        revno, revision_id = self._revno_and_revision_id(branch)
519
524
        return RevisionInfo(branch, revno, revision_id)
520
525
 
521
 
    def _revno_and_revision_id(self, context_branch, revs_or_none):
 
526
    def _revno_and_revision_id(self, context_branch):
522
527
        last_revno, last_revision_id = context_branch.last_revision_info()
523
528
 
524
529
        if self.spec == '':
537
542
 
538
543
        revno = last_revno - offset + 1
539
544
        try:
540
 
            revision_id = context_branch.get_rev_id(revno, revs_or_none)
 
545
            revision_id = context_branch.get_rev_id(revno)
541
546
        except errors.NoSuchRevision:
542
547
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch)
543
548
        return revno, revision_id
545
550
    def _as_revision_id(self, context_branch):
546
551
        # We compute the revno as part of the process, but we don't really care
547
552
        # about it.
548
 
        revno, revision_id = self._revno_and_revision_id(context_branch, None)
 
553
        revno, revision_id = self._revno_and_revision_id(context_branch)
549
554
        return revision_id
550
555
 
551
556
 
583
588
            # We need to use the repository history here
584
589
            rev = branch.repository.get_revision(r.rev_id)
585
590
            if not rev.parent_ids:
586
 
                revno = 0
587
591
                revision_id = revision.NULL_REVISION
588
592
            else:
589
593
                revision_id = rev.parent_ids[0]
590
 
                try:
591
 
                    revno = revs.index(revision_id) + 1
592
 
                except ValueError:
593
 
                    revno = None
 
594
            revno = None
594
595
        else:
595
596
            revno = r.revno - 1
596
597
            try:
601
602
        return RevisionInfo(branch, revno, revision_id)
602
603
 
603
604
    def _as_revision_id(self, context_branch):
604
 
        base_revspec = RevisionSpec.from_string(self.spec)
605
 
        base_revision_id = base_revspec.as_revision_id(context_branch)
 
605
        base_revision_id = RevisionSpec.from_string(self.spec)._as_revision_id(context_branch)
606
606
        if base_revision_id == revision.NULL_REVISION:
607
607
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
608
608
                                         'cannot go before the null: revision')
638
638
    def _match_on(self, branch, revs):
639
639
        # Can raise tags not supported, NoSuchTag, etc
640
640
        return RevisionInfo.from_revision_id(branch,
641
 
            branch.tags.lookup_tag(self.spec),
642
 
            revs)
 
641
            branch.tags.lookup_tag(self.spec))
643
642
 
644
643
    def _as_revision_id(self, context_branch):
645
644
        return context_branch.tags.lookup_tag(self.spec)
649
648
class _RevListToTimestamps(object):
650
649
    """This takes a list of revisions, and allows you to bisect by date"""
651
650
 
652
 
    __slots__ = ['revs', 'branch']
 
651
    __slots__ = ['branch']
653
652
 
654
 
    def __init__(self, revs, branch):
655
 
        self.revs = revs
 
653
    def __init__(self, branch):
656
654
        self.branch = branch
657
655
 
658
656
    def __getitem__(self, index):
659
657
        """Get the date of the index'd item"""
660
 
        r = self.branch.repository.get_revision(self.revs[index])
 
658
        r = self.branch.repository.get_revision(self.branch.get_rev_id(index))
661
659
        # TODO: Handle timezone.
662
660
        return datetime.datetime.fromtimestamp(r.timestamp)
663
661
 
664
662
    def __len__(self):
665
 
        return len(self.revs)
 
663
        return self.branch.revno()
666
664
 
667
665
 
668
666
class RevisionSpec_date(RevisionSpec):
742
740
                    hour=hour, minute=minute, second=second)
743
741
        branch.lock_read()
744
742
        try:
745
 
            rev = bisect.bisect(_RevListToTimestamps(revs, branch), dt)
 
743
            rev = bisect.bisect(_RevListToTimestamps(branch), dt, 1)
746
744
        finally:
747
745
            branch.unlock()
748
 
        if rev == len(revs):
 
746
        if rev == branch.revno():
749
747
            raise errors.InvalidRevisionSpec(self.user_spec, branch)
750
 
        else:
751
 
            return RevisionInfo(branch, rev + 1)
 
748
        return RevisionInfo(branch, rev)
752
749
 
753
750
 
754
751
 
785
782
    def _find_revision_info(branch, other_location):
786
783
        revision_id = RevisionSpec_ancestor._find_revision_id(branch,
787
784
                                                              other_location)
788
 
        try:
789
 
            revno = branch.revision_id_to_revno(revision_id)
790
 
        except errors.NoSuchRevision:
791
 
            revno = None
792
 
        return RevisionInfo(branch, revno, revision_id)
 
785
        return RevisionInfo(branch, None, revision_id)
793
786
 
794
787
    @staticmethod
795
788
    def _find_revision_id(branch, other_location):
849
842
                branch.fetch(other_branch, revision_b)
850
843
            except errors.ReadOnlyError:
851
844
                branch = other_branch
852
 
        try:
853
 
            revno = branch.revision_id_to_revno(revision_b)
854
 
        except errors.NoSuchRevision:
855
 
            revno = None
856
 
        return RevisionInfo(branch, revno, revision_b)
 
845
        return RevisionInfo(branch, None, revision_b)
857
846
 
858
847
    def _as_revision_id(self, context_branch):
859
848
        from bzrlib.branch import Branch