~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/revisionspec.py

  • Committer: Vincent Ladeuil
  • Date: 2016-01-21 17:48:07 UTC
  • mto: This revision was merged to the branch mainline in revision 6613.
  • Revision ID: v.ladeuil+lp@free.fr-20160121174807-g4ybpaij9ln5wj6a
Make all transport put_bytes() raises TypeError when given unicode strings rather than bytes.

There was a mix of AssertionError or UnicodeEncodeError.

Also deleted test_put_file_unicode() which was bogus, files contain bytes not unicode strings.

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