~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/revisionspec.py

(gz) Merge 2.4 for one more test fix with newer zlib (Martin Packman)

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
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
24
 
26
25
from bzrlib import (
27
26
    branch as _mod_branch,
28
 
    errors,
29
27
    osutils,
30
 
    registry,
31
28
    revision,
32
29
    symbol_versioning,
 
30
    workingtree,
 
31
    )
 
32
from bzrlib.i18n import gettext
 
33
""")
 
34
 
 
35
from bzrlib import (
 
36
    errors,
 
37
    lazy_regex,
 
38
    registry,
33
39
    trace,
34
 
    workingtree,
35
40
    )
36
41
 
37
42
 
38
 
_marker = []
39
 
 
40
 
 
41
43
class RevisionInfo(object):
42
44
    """The results of applying a revision specification to a branch."""
43
45
 
55
57
    or treat the result as a tuple.
56
58
    """
57
59
 
58
 
    def __init__(self, branch, revno, rev_id=_marker):
 
60
    def __init__(self, branch, revno=None, rev_id=None):
59
61
        self.branch = branch
60
 
        self.revno = revno
61
 
        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:
62
66
            # allow caller to be lazy
63
 
            if self.revno is None:
64
 
                self.rev_id = None
65
 
            else:
66
 
                self.rev_id = branch.get_rev_id(self.revno)
67
 
        else:
68
 
            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
69
78
 
70
79
    def __nonzero__(self):
71
 
        # first the easy ones...
72
80
        if self.rev_id is None:
73
81
            return False
74
 
        if self.revno is not None:
75
 
            return True
76
82
        # TODO: otherwise, it should depend on how I was built -
77
83
        # if it's in_history(branch), then check revision_history(),
78
84
        # if it's in_store(branch), do the check below
101
107
            self.revno, self.rev_id, self.branch)
102
108
 
103
109
    @staticmethod
104
 
    def from_revision_id(branch, revision_id, revs):
 
110
    def from_revision_id(branch, revision_id, revs=symbol_versioning.DEPRECATED_PARAMETER):
105
111
        """Construct a RevisionInfo given just the id.
106
112
 
107
113
        Use this if you don't know or care what the revno is.
108
114
        """
109
 
        if revision_id == revision.NULL_REVISION:
110
 
            return RevisionInfo(branch, 0, revision_id)
111
 
        try:
112
 
            revno = revs.index(revision_id) + 1
113
 
        except ValueError:
114
 
            revno = None
115
 
        return RevisionInfo(branch, revno, revision_id)
116
 
 
117
 
 
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):
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
 
168
171
                         spectype.__name__, spec)
169
172
            return spectype(spec, _internal=True)
170
173
        else:
171
 
            for spectype in SPEC_TYPES:
172
 
                if spec.startswith(spectype.prefix):
173
 
                    trace.mutter('Returning RevisionSpec %s for %s',
174
 
                                 spectype.__name__, spec)
175
 
                    return spectype(spec, _internal=True)
176
174
            # Otherwise treat it as a DWIM, build the RevisionSpec object and
177
175
            # wait for _match_on to be called.
178
176
            return RevisionSpec_dwim(spec, _internal=True)
214
212
    def in_history(self, branch):
215
213
        if branch:
216
214
            if self.wants_revision_history:
217
 
                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()
218
227
            else:
219
228
                revs = None
220
229
        else:
300
309
    """
301
310
 
302
311
    help_txt = None
303
 
    # We don't need to build the revision history ourself, that's delegated to
304
 
    # each revspec we try.
305
 
    wants_revision_history = False
 
312
 
 
313
    _revno_regex = lazy_regex.lazy_compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
 
314
 
 
315
    # The revspecs to try
 
316
    _possible_revspecs = []
306
317
 
307
318
    def _try_spectype(self, rstype, branch):
308
319
        rs = rstype(self.spec, _internal=True)
314
325
        """Run the lookup and see what we can get."""
315
326
 
316
327
        # First, see if it's a revno
317
 
        global _revno_regex
318
 
        if _revno_regex is None:
319
 
            _revno_regex = re.compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
320
 
        if _revno_regex.match(self.spec) is not None:
 
328
        if self._revno_regex.match(self.spec) is not None:
321
329
            try:
322
330
                return self._try_spectype(RevisionSpec_revno, branch)
323
331
            except RevisionSpec_revno.dwim_catchable_exceptions:
324
332
                pass
325
333
 
326
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:
327
343
        for rs_class in dwim_revspecs:
328
344
            try:
329
345
                return self._try_spectype(rs_class, branch)
335
351
        # really relevant.
336
352
        raise errors.InvalidRevisionSpec(self.spec, branch)
337
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
 
338
372
 
339
373
class RevisionSpec_revno(RevisionSpec):
340
374
    """Selects a revision using a number."""
358
392
                                   your history is very long.
359
393
    """
360
394
    prefix = 'revno:'
361
 
    wants_revision_history = False
362
395
 
363
396
    def _match_on(self, branch, revs):
364
397
        """Lookup a revision by revision number"""
365
 
        branch, revno, revision_id = self._lookup(branch, revs)
 
398
        branch, revno, revision_id = self._lookup(branch)
366
399
        return RevisionInfo(branch, revno, revision_id)
367
400
 
368
 
    def _lookup(self, branch, revs_or_none):
 
401
    def _lookup(self, branch):
369
402
        loc = self.spec.find(':')
370
403
        if loc == -1:
371
404
            revno_spec = self.spec
395
428
                dotted = True
396
429
 
397
430
        if branch_spec:
398
 
            # the user has override the branch to look in.
399
 
            # we need to refresh the revision_history map and
400
 
            # the branch object.
401
 
            from bzrlib.branch import Branch
402
 
            branch = Branch.open(branch_spec)
403
 
            revs_or_none = None
 
431
            # the user has overriden the branch to look in.
 
432
            branch = _mod_branch.Branch.open(branch_spec)
404
433
 
405
434
        if dotted:
406
435
            try:
410
439
                raise errors.InvalidRevisionSpec(self.user_spec, branch)
411
440
            else:
412
441
                # there is no traditional 'revno' for dotted-decimal revnos.
413
 
                # so for  API compatability we return None.
 
442
                # so for API compatibility we return None.
414
443
                return branch, None, revision_id
415
444
        else:
416
445
            last_revno, last_revision_id = branch.last_revision_info()
422
451
                else:
423
452
                    revno = last_revno + revno + 1
424
453
            try:
425
 
                revision_id = branch.get_rev_id(revno, revs_or_none)
 
454
                revision_id = branch.get_rev_id(revno)
426
455
            except errors.NoSuchRevision:
427
456
                raise errors.InvalidRevisionSpec(self.user_spec, branch)
428
457
        return branch, revno, revision_id
429
458
 
430
459
    def _as_revision_id(self, context_branch):
431
460
        # We would have the revno here, but we don't really care
432
 
        branch, revno, revision_id = self._lookup(context_branch, None)
 
461
        branch, revno, revision_id = self._lookup(context_branch)
433
462
        return revision_id
434
463
 
435
464
    def needs_branch(self):
445
474
RevisionSpec_int = RevisionSpec_revno
446
475
 
447
476
 
448
 
 
449
477
class RevisionIDSpec(RevisionSpec):
450
478
 
451
479
    def _match_on(self, branch, revs):
452
480
        revision_id = self.as_revision_id(branch)
453
 
        return RevisionInfo.from_revision_id(branch, revision_id, revs)
 
481
        return RevisionInfo.from_revision_id(branch, revision_id)
454
482
 
455
483
 
456
484
class RevisionSpec_revid(RevisionIDSpec):
492
520
    prefix = 'last:'
493
521
 
494
522
    def _match_on(self, branch, revs):
495
 
        revno, revision_id = self._revno_and_revision_id(branch, revs)
 
523
        revno, revision_id = self._revno_and_revision_id(branch)
496
524
        return RevisionInfo(branch, revno, revision_id)
497
525
 
498
 
    def _revno_and_revision_id(self, context_branch, revs_or_none):
 
526
    def _revno_and_revision_id(self, context_branch):
499
527
        last_revno, last_revision_id = context_branch.last_revision_info()
500
528
 
501
529
        if self.spec == '':
514
542
 
515
543
        revno = last_revno - offset + 1
516
544
        try:
517
 
            revision_id = context_branch.get_rev_id(revno, revs_or_none)
 
545
            revision_id = context_branch.get_rev_id(revno)
518
546
        except errors.NoSuchRevision:
519
547
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch)
520
548
        return revno, revision_id
522
550
    def _as_revision_id(self, context_branch):
523
551
        # We compute the revno as part of the process, but we don't really care
524
552
        # about it.
525
 
        revno, revision_id = self._revno_and_revision_id(context_branch, None)
 
553
        revno, revision_id = self._revno_and_revision_id(context_branch)
526
554
        return revision_id
527
555
 
528
556
 
560
588
            # We need to use the repository history here
561
589
            rev = branch.repository.get_revision(r.rev_id)
562
590
            if not rev.parent_ids:
563
 
                revno = 0
564
591
                revision_id = revision.NULL_REVISION
565
592
            else:
566
593
                revision_id = rev.parent_ids[0]
567
 
                try:
568
 
                    revno = revs.index(revision_id) + 1
569
 
                except ValueError:
570
 
                    revno = None
 
594
            revno = None
571
595
        else:
572
596
            revno = r.revno - 1
573
597
            try:
578
602
        return RevisionInfo(branch, revno, revision_id)
579
603
 
580
604
    def _as_revision_id(self, context_branch):
581
 
        base_revspec = RevisionSpec.from_string(self.spec)
582
 
        base_revision_id = base_revspec.as_revision_id(context_branch)
 
605
        base_revision_id = RevisionSpec.from_string(self.spec)._as_revision_id(context_branch)
583
606
        if base_revision_id == revision.NULL_REVISION:
584
607
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
585
608
                                         'cannot go before the null: revision')
615
638
    def _match_on(self, branch, revs):
616
639
        # Can raise tags not supported, NoSuchTag, etc
617
640
        return RevisionInfo.from_revision_id(branch,
618
 
            branch.tags.lookup_tag(self.spec),
619
 
            revs)
 
641
            branch.tags.lookup_tag(self.spec))
620
642
 
621
643
    def _as_revision_id(self, context_branch):
622
644
        return context_branch.tags.lookup_tag(self.spec)
626
648
class _RevListToTimestamps(object):
627
649
    """This takes a list of revisions, and allows you to bisect by date"""
628
650
 
629
 
    __slots__ = ['revs', 'branch']
 
651
    __slots__ = ['branch']
630
652
 
631
 
    def __init__(self, revs, branch):
632
 
        self.revs = revs
 
653
    def __init__(self, branch):
633
654
        self.branch = branch
634
655
 
635
656
    def __getitem__(self, index):
636
657
        """Get the date of the index'd item"""
637
 
        r = self.branch.repository.get_revision(self.revs[index])
 
658
        r = self.branch.repository.get_revision(self.branch.get_rev_id(index))
638
659
        # TODO: Handle timezone.
639
660
        return datetime.datetime.fromtimestamp(r.timestamp)
640
661
 
641
662
    def __len__(self):
642
 
        return len(self.revs)
 
663
        return self.branch.revno()
643
664
 
644
665
 
645
666
class RevisionSpec_date(RevisionSpec):
663
684
                                   August 14th, 2006 at 5:10pm.
664
685
    """
665
686
    prefix = 'date:'
666
 
    _date_re = re.compile(
 
687
    _date_regex = lazy_regex.lazy_compile(
667
688
            r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
668
689
            r'(,|T)?\s*'
669
690
            r'(?P<time>(?P<hour>\d\d):(?P<minute>\d\d)(:(?P<second>\d\d))?)?'
687
708
        elif self.spec.lower() == 'tomorrow':
688
709
            dt = today + datetime.timedelta(days=1)
689
710
        else:
690
 
            m = self._date_re.match(self.spec)
 
711
            m = self._date_regex.match(self.spec)
691
712
            if not m or (not m.group('date') and not m.group('time')):
692
713
                raise errors.InvalidRevisionSpec(self.user_spec,
693
714
                                                 branch, 'invalid date')
719
740
                    hour=hour, minute=minute, second=second)
720
741
        branch.lock_read()
721
742
        try:
722
 
            rev = bisect.bisect(_RevListToTimestamps(revs, branch), dt)
 
743
            rev = bisect.bisect(_RevListToTimestamps(branch), dt, 1)
723
744
        finally:
724
745
            branch.unlock()
725
 
        if rev == len(revs):
 
746
        if rev == branch.revno():
726
747
            raise errors.InvalidRevisionSpec(self.user_spec, branch)
727
 
        else:
728
 
            return RevisionInfo(branch, rev + 1)
 
748
        return RevisionInfo(branch, rev)
729
749
 
730
750
 
731
751
 
762
782
    def _find_revision_info(branch, other_location):
763
783
        revision_id = RevisionSpec_ancestor._find_revision_id(branch,
764
784
                                                              other_location)
765
 
        try:
766
 
            revno = branch.revision_id_to_revno(revision_id)
767
 
        except errors.NoSuchRevision:
768
 
            revno = None
769
 
        return RevisionInfo(branch, revno, revision_id)
 
785
        return RevisionInfo(branch, None, revision_id)
770
786
 
771
787
    @staticmethod
772
788
    def _find_revision_id(branch, other_location):
826
842
                branch.fetch(other_branch, revision_b)
827
843
            except errors.ReadOnlyError:
828
844
                branch = other_branch
829
 
        try:
830
 
            revno = branch.revision_id_to_revno(revision_b)
831
 
        except errors.NoSuchRevision:
832
 
            revno = None
833
 
        return RevisionInfo(branch, revno, revision_b)
 
845
        return RevisionInfo(branch, None, revision_b)
834
846
 
835
847
    def _as_revision_id(self, context_branch):
836
848
        from bzrlib.branch import Branch
888
900
            location_type = 'parent branch'
889
901
        if submit_location is None:
890
902
            raise errors.NoSubmitBranch(branch)
891
 
        trace.note('Using %s %s', location_type, submit_location)
 
903
        trace.note(gettext('Using {0} {1}').format(location_type,
 
904
                                                        submit_location))
892
905
        return submit_location
893
906
 
894
907
    def _match_on(self, branch, revs):
971
984
# The order in which we want to DWIM a revision spec without any prefix.
972
985
# revno is always tried first and isn't listed here, this is used by
973
986
# RevisionSpec_dwim._match_on
974
 
dwim_revspecs = [
975
 
    RevisionSpec_tag, # Let's try for a tag
976
 
    RevisionSpec_revid, # Maybe it's a revid?
977
 
    RevisionSpec_date, # Perhaps a date?
978
 
    RevisionSpec_branch, # OK, last try, maybe it's a branch
979
 
    ]
 
987
dwim_revspecs = symbol_versioning.deprecated_list(
 
988
    symbol_versioning.deprecated_in((2, 4, 0)), "dwim_revspecs", [])
980
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)
981
994
 
982
995
revspec_registry = registry.Registry()
983
996
def _register_revspec(revspec):
994
1007
_register_revspec(RevisionSpec_submit)
995
1008
_register_revspec(RevisionSpec_annotate)
996
1009
_register_revspec(RevisionSpec_mainline)
997
 
 
998
 
# classes in this list should have a "prefix" attribute, against which
999
 
# string specs are matched
1000
 
SPEC_TYPES = symbol_versioning.deprecated_list(
1001
 
    symbol_versioning.deprecated_in((1, 12, 0)), "SPEC_TYPES", [])