~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/revisionspec.py

  • Committer: Jelmer Vernooij
  • Date: 2011-11-27 17:42:25 UTC
  • mto: This revision was merged to the branch mainline in revision 6311.
  • Revision ID: jelmer@samba.org-20111127174225-tspfeewl0gwxxumt
Add possible_transports in a couple more places.

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
17
 
20
18
from bzrlib.lazy_import import lazy_import
21
19
lazy_import(globals(), """
40
38
    )
41
39
 
42
40
 
 
41
_marker = []
 
42
 
 
43
 
43
44
class RevisionInfo(object):
44
45
    """The results of applying a revision specification to a branch."""
45
46
 
57
58
    or treat the result as a tuple.
58
59
    """
59
60
 
60
 
    def __init__(self, branch, revno=None, rev_id=None):
 
61
    def __init__(self, branch, revno, rev_id=_marker):
61
62
        self.branch = branch
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:
 
63
        self.revno = revno
 
64
        if rev_id is _marker:
66
65
            # allow caller to be lazy
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
 
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
78
72
 
79
73
    def __nonzero__(self):
 
74
        # first the easy ones...
80
75
        if self.rev_id is None:
81
76
            return False
 
77
        if self.revno is not None:
 
78
            return True
82
79
        # TODO: otherwise, it should depend on how I was built -
83
80
        # if it's in_history(branch), then check revision_history(),
84
81
        # if it's in_store(branch), do the check below
107
104
            self.revno, self.rev_id, self.branch)
108
105
 
109
106
    @staticmethod
110
 
    def from_revision_id(branch, revision_id, revs=symbol_versioning.DEPRECATED_PARAMETER):
 
107
    def from_revision_id(branch, revision_id, revs):
111
108
        """Construct a RevisionInfo given just the id.
112
109
 
113
110
        Use this if you don't know or care what the revno is.
114
111
        """
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)
 
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)
121
119
 
122
120
 
123
121
class RevisionSpec(object):
140
138
    """
141
139
 
142
140
    prefix = None
143
 
    # wants_revision_history has been deprecated in 2.5.
144
 
    wants_revision_history = False
 
141
    wants_revision_history = True
145
142
    dwim_catchable_exceptions = (errors.InvalidRevisionSpec,)
146
143
    """Exceptions that RevisionSpec_dwim._match_on will catch.
147
144
 
212
209
    def in_history(self, branch):
213
210
        if branch:
214
211
            if self.wants_revision_history:
215
 
                symbol_versioning.warn(
216
 
                    "RevisionSpec.wants_revision_history was "
217
 
                    "deprecated in 2.5 (%s)." % self.__class__.__name__,
218
 
                    DeprecationWarning)
 
212
                # TODO: avoid looking at all of history
219
213
                branch.lock_read()
220
214
                try:
221
215
                    graph = branch.repository.get_graph()
309
303
    """
310
304
 
311
305
    help_txt = None
 
306
    # We don't need to build the revision history ourself, that's delegated to
 
307
    # each revspec we try.
 
308
    wants_revision_history = False
312
309
 
313
310
    _revno_regex = lazy_regex.lazy_compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
314
311
 
392
389
                                   your history is very long.
393
390
    """
394
391
    prefix = 'revno:'
 
392
    wants_revision_history = False
395
393
 
396
394
    def _match_on(self, branch, revs):
397
395
        """Lookup a revision by revision number"""
398
 
        branch, revno, revision_id = self._lookup(branch)
 
396
        branch, revno, revision_id = self._lookup(branch, revs)
399
397
        return RevisionInfo(branch, revno, revision_id)
400
398
 
401
 
    def _lookup(self, branch):
 
399
    def _lookup(self, branch, revs_or_none):
402
400
        loc = self.spec.find(':')
403
401
        if loc == -1:
404
402
            revno_spec = self.spec
428
426
                dotted = True
429
427
 
430
428
        if branch_spec:
431
 
            # the user has overriden the branch to look in.
432
 
            branch = _mod_branch.Branch.open(branch_spec)
 
429
            # the user has override the branch to look in.
 
430
            # we need to refresh the revision_history map and
 
431
            # the branch object.
 
432
            from bzrlib.branch import Branch
 
433
            branch = Branch.open(branch_spec)
 
434
            revs_or_none = None
433
435
 
434
436
        if dotted:
435
437
            try:
439
441
                raise errors.InvalidRevisionSpec(self.user_spec, branch)
440
442
            else:
441
443
                # there is no traditional 'revno' for dotted-decimal revnos.
442
 
                # so for API compatibility we return None.
 
444
                # so for  API compatability we return None.
443
445
                return branch, None, revision_id
444
446
        else:
445
447
            last_revno, last_revision_id = branch.last_revision_info()
451
453
                else:
452
454
                    revno = last_revno + revno + 1
453
455
            try:
454
 
                revision_id = branch.get_rev_id(revno)
 
456
                revision_id = branch.get_rev_id(revno, revs_or_none)
455
457
            except errors.NoSuchRevision:
456
458
                raise errors.InvalidRevisionSpec(self.user_spec, branch)
457
459
        return branch, revno, revision_id
458
460
 
459
461
    def _as_revision_id(self, context_branch):
460
462
        # We would have the revno here, but we don't really care
461
 
        branch, revno, revision_id = self._lookup(context_branch)
 
463
        branch, revno, revision_id = self._lookup(context_branch, None)
462
464
        return revision_id
463
465
 
464
466
    def needs_branch(self):
474
476
RevisionSpec_int = RevisionSpec_revno
475
477
 
476
478
 
 
479
 
477
480
class RevisionIDSpec(RevisionSpec):
478
481
 
479
482
    def _match_on(self, branch, revs):
480
483
        revision_id = self.as_revision_id(branch)
481
 
        return RevisionInfo.from_revision_id(branch, revision_id)
 
484
        return RevisionInfo.from_revision_id(branch, revision_id, revs)
482
485
 
483
486
 
484
487
class RevisionSpec_revid(RevisionIDSpec):
520
523
    prefix = 'last:'
521
524
 
522
525
    def _match_on(self, branch, revs):
523
 
        revno, revision_id = self._revno_and_revision_id(branch)
 
526
        revno, revision_id = self._revno_and_revision_id(branch, revs)
524
527
        return RevisionInfo(branch, revno, revision_id)
525
528
 
526
 
    def _revno_and_revision_id(self, context_branch):
 
529
    def _revno_and_revision_id(self, context_branch, revs_or_none):
527
530
        last_revno, last_revision_id = context_branch.last_revision_info()
528
531
 
529
532
        if self.spec == '':
542
545
 
543
546
        revno = last_revno - offset + 1
544
547
        try:
545
 
            revision_id = context_branch.get_rev_id(revno)
 
548
            revision_id = context_branch.get_rev_id(revno, revs_or_none)
546
549
        except errors.NoSuchRevision:
547
550
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch)
548
551
        return revno, revision_id
550
553
    def _as_revision_id(self, context_branch):
551
554
        # We compute the revno as part of the process, but we don't really care
552
555
        # about it.
553
 
        revno, revision_id = self._revno_and_revision_id(context_branch)
 
556
        revno, revision_id = self._revno_and_revision_id(context_branch, None)
554
557
        return revision_id
555
558
 
556
559
 
588
591
            # We need to use the repository history here
589
592
            rev = branch.repository.get_revision(r.rev_id)
590
593
            if not rev.parent_ids:
 
594
                revno = 0
591
595
                revision_id = revision.NULL_REVISION
592
596
            else:
593
597
                revision_id = rev.parent_ids[0]
594
 
            revno = None
 
598
                try:
 
599
                    revno = revs.index(revision_id) + 1
 
600
                except ValueError:
 
601
                    revno = None
595
602
        else:
596
603
            revno = r.revno - 1
597
604
            try:
602
609
        return RevisionInfo(branch, revno, revision_id)
603
610
 
604
611
    def _as_revision_id(self, context_branch):
605
 
        base_revision_id = RevisionSpec.from_string(self.spec)._as_revision_id(context_branch)
 
612
        base_revspec = RevisionSpec.from_string(self.spec)
 
613
        base_revision_id = base_revspec.as_revision_id(context_branch)
606
614
        if base_revision_id == revision.NULL_REVISION:
607
615
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
608
616
                                         'cannot go before the null: revision')
638
646
    def _match_on(self, branch, revs):
639
647
        # Can raise tags not supported, NoSuchTag, etc
640
648
        return RevisionInfo.from_revision_id(branch,
641
 
            branch.tags.lookup_tag(self.spec))
 
649
            branch.tags.lookup_tag(self.spec),
 
650
            revs)
642
651
 
643
652
    def _as_revision_id(self, context_branch):
644
653
        return context_branch.tags.lookup_tag(self.spec)
648
657
class _RevListToTimestamps(object):
649
658
    """This takes a list of revisions, and allows you to bisect by date"""
650
659
 
651
 
    __slots__ = ['branch']
 
660
    __slots__ = ['revs', 'branch']
652
661
 
653
 
    def __init__(self, branch):
 
662
    def __init__(self, revs, branch):
 
663
        self.revs = revs
654
664
        self.branch = branch
655
665
 
656
666
    def __getitem__(self, index):
657
667
        """Get the date of the index'd item"""
658
 
        r = self.branch.repository.get_revision(self.branch.get_rev_id(index))
 
668
        r = self.branch.repository.get_revision(self.revs[index])
659
669
        # TODO: Handle timezone.
660
670
        return datetime.datetime.fromtimestamp(r.timestamp)
661
671
 
662
672
    def __len__(self):
663
 
        return self.branch.revno()
 
673
        return len(self.revs)
664
674
 
665
675
 
666
676
class RevisionSpec_date(RevisionSpec):
740
750
                    hour=hour, minute=minute, second=second)
741
751
        branch.lock_read()
742
752
        try:
743
 
            rev = bisect.bisect(_RevListToTimestamps(branch), dt, 1)
 
753
            rev = bisect.bisect(_RevListToTimestamps(revs, branch), dt)
744
754
        finally:
745
755
            branch.unlock()
746
 
        if rev == branch.revno():
 
756
        if rev == len(revs):
747
757
            raise errors.InvalidRevisionSpec(self.user_spec, branch)
748
 
        return RevisionInfo(branch, rev)
 
758
        else:
 
759
            return RevisionInfo(branch, rev + 1)
749
760
 
750
761
 
751
762
 
782
793
    def _find_revision_info(branch, other_location):
783
794
        revision_id = RevisionSpec_ancestor._find_revision_id(branch,
784
795
                                                              other_location)
785
 
        return RevisionInfo(branch, None, revision_id)
 
796
        try:
 
797
            revno = branch.revision_id_to_revno(revision_id)
 
798
        except errors.NoSuchRevision:
 
799
            revno = None
 
800
        return RevisionInfo(branch, revno, revision_id)
786
801
 
787
802
    @staticmethod
788
803
    def _find_revision_id(branch, other_location):
842
857
                branch.fetch(other_branch, revision_b)
843
858
            except errors.ReadOnlyError:
844
859
                branch = other_branch
845
 
        return RevisionInfo(branch, None, revision_b)
 
860
        try:
 
861
            revno = branch.revision_id_to_revno(revision_b)
 
862
        except errors.NoSuchRevision:
 
863
            revno = None
 
864
        return RevisionInfo(branch, revno, revision_b)
846
865
 
847
866
    def _as_revision_id(self, context_branch):
848
867
        from bzrlib.branch import Branch