/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/revisionspec.py

  • Committer: Jelmer Vernooij
  • Date: 2020-03-22 20:02:36 UTC
  • mto: (7490.7.7 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200322200236-fsbl91ktcn6fcbdd
Fix tests.

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 .lazy_import import lazy_import
19
21
lazy_import(globals(), """
35
37
    registry,
36
38
    trace,
37
39
    )
38
 
 
39
 
 
40
 
class InvalidRevisionSpec(errors.BzrError):
41
 
 
42
 
    _fmt = ("Requested revision: '%(spec)s' does not exist in branch:"
43
 
            " %(branch_url)s%(extra)s")
44
 
 
45
 
    def __init__(self, spec, branch, extra=None):
46
 
        errors.BzrError.__init__(self, branch=branch, spec=spec)
47
 
        self.branch_url = getattr(branch, 'user_url', str(branch))
48
 
        if extra:
49
 
            self.extra = '\n' + str(extra)
50
 
        else:
51
 
            self.extra = ''
52
 
 
53
 
 
54
 
class InvalidRevisionSpec(errors.BzrError):
55
 
 
56
 
    _fmt = ("Requested revision: '%(spec)s' does not exist in branch:"
57
 
            " %(branch_url)s%(extra)s")
58
 
 
59
 
    def __init__(self, spec, branch, extra=None):
60
 
        errors.BzrError.__init__(self, branch=branch, spec=spec)
61
 
        self.branch_url = getattr(branch, 'user_url', str(branch))
62
 
        if extra:
63
 
            self.extra = '\n' + str(extra)
64
 
        else:
65
 
            self.extra = ''
 
40
from .sixish import (
 
41
    text_type,
 
42
    )
66
43
 
67
44
 
68
45
class RevisionInfo(object):
164
141
    """
165
142
 
166
143
    prefix = None
167
 
    dwim_catchable_exceptions = (InvalidRevisionSpec,)
 
144
    dwim_catchable_exceptions = (errors.InvalidRevisionSpec,)
168
145
    """Exceptions that RevisionSpec_dwim._match_on will catch.
169
146
 
170
147
    If the revspec is part of ``dwim_revspecs``, it may be tried with an
183
160
        """
184
161
        if spec is None:
185
162
            return RevisionSpec(None, _internal=True)
186
 
        if not isinstance(spec, str):
 
163
        if not isinstance(spec, (str, text_type)):
187
164
            raise TypeError("revision spec needs to be text")
188
165
        match = revspec_registry.get_prefix(spec)
189
166
        if match is not None:
224
201
            # special case - nothing supplied
225
202
            return info
226
203
        elif self.prefix:
227
 
            raise InvalidRevisionSpec(self.user_spec, branch)
 
204
            raise errors.InvalidRevisionSpec(self.user_spec, branch)
228
205
        else:
229
 
            raise InvalidRevisionSpec(self.spec, branch)
 
206
            raise errors.InvalidRevisionSpec(self.spec, branch)
230
207
 
231
208
    def in_history(self, branch):
232
209
        return self._match_on_and_check(branch, revs=None)
341
318
        # Well, I dunno what it is. Note that we don't try to keep track of the
342
319
        # first of last exception raised during the DWIM tries as none seems
343
320
        # really relevant.
344
 
        raise InvalidRevisionSpec(self.spec, branch)
 
321
        raise errors.InvalidRevisionSpec(self.spec, branch)
345
322
 
346
323
    @classmethod
347
324
    def append_possible_revspec(cls, revspec):
401
378
 
402
379
        if revno_spec == '':
403
380
            if not branch_spec:
404
 
                raise InvalidRevisionSpec(
405
 
                    self.user_spec, branch,
406
 
                    'cannot have an empty revno and no branch')
 
381
                raise errors.InvalidRevisionSpec(self.user_spec,
 
382
                                                 branch, 'cannot have an empty revno and no branch')
407
383
            revno = None
408
384
        else:
409
385
            try:
417
393
                    match_revno = tuple((int(number)
418
394
                                         for number in revno_spec.split('.')))
419
395
                except ValueError as e:
420
 
                    raise InvalidRevisionSpec(self.user_spec, branch, e)
 
396
                    raise errors.InvalidRevisionSpec(self.user_spec, branch, e)
421
397
 
422
398
                dotted = True
423
399
 
430
406
                revision_id = branch.dotted_revno_to_revision_id(match_revno,
431
407
                                                                 _cache_reverse=True)
432
408
            except (errors.NoSuchRevision, errors.RevnoOutOfBounds):
433
 
                raise InvalidRevisionSpec(self.user_spec, branch)
 
409
                raise errors.InvalidRevisionSpec(self.user_spec, branch)
434
410
            else:
435
411
                # there is no traditional 'revno' for dotted-decimal revnos.
436
412
                # so for API compatibility we return None.
447
423
            try:
448
424
                revision_id = branch.get_rev_id(revno)
449
425
            except (errors.NoSuchRevision, errors.RevnoOutOfBounds):
450
 
                raise InvalidRevisionSpec(self.user_spec, branch)
 
426
                raise errors.InvalidRevisionSpec(self.user_spec, branch)
451
427
        return branch, revno, revision_id
452
428
 
453
429
    def _as_revision_id(self, context_branch):
495
471
        # self.spec comes straight from parsing the command line arguments,
496
472
        # so we expect it to be a Unicode string. Switch it to the internal
497
473
        # representation.
498
 
        if isinstance(self.spec, str):
 
474
        if isinstance(self.spec, text_type):
499
475
            return cache_utf8.encode(self.spec)
500
476
        return self.spec
501
477
 
530
506
        try:
531
507
            offset = int(self.spec)
532
508
        except ValueError as e:
533
 
            raise InvalidRevisionSpec(self.user_spec, context_branch, e)
 
509
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch, e)
534
510
 
535
511
        if offset <= 0:
536
 
            raise InvalidRevisionSpec(
537
 
                self.user_spec, context_branch,
538
 
                'you must supply a positive value')
 
512
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
 
513
                                             'you must supply a positive value')
539
514
 
540
515
        revno = last_revno - offset + 1
541
516
        try:
542
517
            revision_id = context_branch.get_rev_id(revno)
543
518
        except (errors.NoSuchRevision, errors.RevnoOutOfBounds):
544
 
            raise InvalidRevisionSpec(self.user_spec, context_branch)
 
519
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch)
545
520
        return revno, revision_id
546
521
 
547
522
    def _as_revision_id(self, context_branch):
578
553
    def _match_on(self, branch, revs):
579
554
        r = RevisionSpec.from_string(self.spec)._match_on(branch, revs)
580
555
        if r.revno == 0:
581
 
            raise InvalidRevisionSpec(
582
 
                self.user_spec, branch,
583
 
                'cannot go before the null: revision')
 
556
            raise errors.InvalidRevisionSpec(self.user_spec, branch,
 
557
                                             'cannot go before the null: revision')
584
558
        if r.revno is None:
585
559
            # We need to use the repository history here
586
560
            rev = branch.repository.get_revision(r.rev_id)
594
568
            try:
595
569
                revision_id = branch.get_rev_id(revno, revs)
596
570
            except (errors.NoSuchRevision, errors.RevnoOutOfBounds):
597
 
                raise InvalidRevisionSpec(self.user_spec, branch)
 
571
                raise errors.InvalidRevisionSpec(self.user_spec,
 
572
                                                 branch)
598
573
        return RevisionInfo(branch, revno, revision_id)
599
574
 
600
575
    def _as_revision_id(self, context_branch):
601
576
        base_revision_id = RevisionSpec.from_string(
602
577
            self.spec)._as_revision_id(context_branch)
603
578
        if base_revision_id == revision.NULL_REVISION:
604
 
            raise InvalidRevisionSpec(
605
 
                self.user_spec, context_branch,
606
 
                'cannot go before the null: revision')
 
579
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
 
580
                                             'cannot go before the null: revision')
607
581
        context_repo = context_branch.repository
608
582
        with context_repo.lock_read():
609
583
            parent_map = context_repo.get_parent_map([base_revision_id])
610
584
        if base_revision_id not in parent_map:
611
585
            # Ghost, or unknown revision id
612
 
            raise InvalidRevisionSpec(
613
 
                self.user_spec, context_branch, 'cannot find the matching revision')
 
586
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
 
587
                                             'cannot find the matching revision')
614
588
        parents = parent_map[base_revision_id]
615
589
        if len(parents) < 1:
616
 
            raise InvalidRevisionSpec(
617
 
                self.user_spec, context_branch, 'No parents for revision.')
 
590
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
 
591
                                             'No parents for revision.')
618
592
        return parents[0]
619
593
 
620
594
 
704
678
        else:
705
679
            m = self._date_regex.match(self.spec)
706
680
            if not m or (not m.group('date') and not m.group('time')):
707
 
                raise InvalidRevisionSpec(
708
 
                    self.user_spec, branch, 'invalid date')
 
681
                raise errors.InvalidRevisionSpec(self.user_spec,
 
682
                                                 branch, 'invalid date')
709
683
 
710
684
            try:
711
685
                if m.group('date'):
727
701
                else:
728
702
                    hour, minute, second = 0, 0, 0
729
703
            except ValueError:
730
 
                raise InvalidRevisionSpec(
731
 
                    self.user_spec, branch, 'invalid date')
 
704
                raise errors.InvalidRevisionSpec(self.user_spec,
 
705
                                                 branch, 'invalid date')
732
706
 
733
707
            dt = datetime.datetime(year=year, month=month, day=day,
734
708
                                   hour=hour, minute=minute, second=second)
735
709
        with branch.lock_read():
736
710
            rev = bisect.bisect(_RevListToTimestamps(branch), dt, 1)
737
711
        if rev == branch.revno():
738
 
            raise InvalidRevisionSpec(self.user_spec, branch)
 
712
            raise errors.InvalidRevisionSpec(self.user_spec, branch)
739
713
        return RevisionInfo(branch, rev)
740
714
 
741
715
 
908
882
    """
909
883
 
910
884
    def _raise_invalid(self, numstring, context_branch):
911
 
        raise InvalidRevisionSpec(
912
 
            self.user_spec, context_branch,
913
 
            'No such line: %s' % numstring)
 
885
        raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
 
886
                                         'No such line: %s' % numstring)
914
887
 
915
888
    def _as_revision_id(self, context_branch):
916
889
        path, numstring = self.spec.rsplit(':', 1)
921
894
        tree, file_path = workingtree.WorkingTree.open_containing(path)
922
895
        with tree.lock_read():
923
896
            if not tree.has_filename(file_path):
924
 
                raise InvalidRevisionSpec(
925
 
                    self.user_spec, context_branch,
926
 
                    "File '%s' is not versioned." % file_path)
 
897
                raise errors.InvalidRevisionSpec(self.user_spec,
 
898
                                                 context_branch, "File '%s' is not versioned." %
 
899
                                                 file_path)
927
900
            revision_ids = [r for (r, l) in tree.annotate_iter(file_path)]
928
901
        try:
929
902
            revision_id = revision_ids[index]
930
903
        except IndexError:
931
904
            self._raise_invalid(numstring, context_branch)
932
905
        if revision_id == revision.CURRENT_REVISION:
933
 
            raise InvalidRevisionSpec(
934
 
                self.user_spec, context_branch,
935
 
                'Line %s has not been committed.' % numstring)
 
906
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
 
907
                                             'Line %s has not been committed.' % numstring)
936
908
        return revision_id
937
909
 
938
910
 
956
928
        result = graph.find_lefthand_merger(revision_id,
957
929
                                            context_branch.last_revision())
958
930
        if result is None:
959
 
            raise InvalidRevisionSpec(self.user_spec, context_branch)
 
931
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch)
960
932
        return result
961
933
 
962
934