/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: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2020-03-25 22:47:16 UTC
  • mfrom: (7499.1.2 revspec-errors)
  • Revision ID: breezy.the.bot@gmail.com-20200325224716-wxn1o0ga8c81p34z
Move InvalidRevisionSpec to breezy.revisionspec.

Merged from https://code.launchpad.net/~jelmer/brz/revspec-errors/+merge/381196

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
    )
38
38
 
39
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
 
40
54
class RevisionInfo(object):
41
55
    """The results of applying a revision specification to a branch."""
42
56
 
136
150
    """
137
151
 
138
152
    prefix = None
139
 
    dwim_catchable_exceptions = (errors.InvalidRevisionSpec,)
 
153
    dwim_catchable_exceptions = (InvalidRevisionSpec,)
140
154
    """Exceptions that RevisionSpec_dwim._match_on will catch.
141
155
 
142
156
    If the revspec is part of ``dwim_revspecs``, it may be tried with an
196
210
            # special case - nothing supplied
197
211
            return info
198
212
        elif self.prefix:
199
 
            raise errors.InvalidRevisionSpec(self.user_spec, branch)
 
213
            raise InvalidRevisionSpec(self.user_spec, branch)
200
214
        else:
201
 
            raise errors.InvalidRevisionSpec(self.spec, branch)
 
215
            raise InvalidRevisionSpec(self.spec, branch)
202
216
 
203
217
    def in_history(self, branch):
204
218
        return self._match_on_and_check(branch, revs=None)
313
327
        # Well, I dunno what it is. Note that we don't try to keep track of the
314
328
        # first of last exception raised during the DWIM tries as none seems
315
329
        # really relevant.
316
 
        raise errors.InvalidRevisionSpec(self.spec, branch)
 
330
        raise InvalidRevisionSpec(self.spec, branch)
317
331
 
318
332
    @classmethod
319
333
    def append_possible_revspec(cls, revspec):
373
387
 
374
388
        if revno_spec == '':
375
389
            if not branch_spec:
376
 
                raise errors.InvalidRevisionSpec(self.user_spec,
377
 
                                                 branch, 'cannot have an empty revno and no branch')
 
390
                raise InvalidRevisionSpec(
 
391
                    self.user_spec, branch,
 
392
                    'cannot have an empty revno and no branch')
378
393
            revno = None
379
394
        else:
380
395
            try:
388
403
                    match_revno = tuple((int(number)
389
404
                                         for number in revno_spec.split('.')))
390
405
                except ValueError as e:
391
 
                    raise errors.InvalidRevisionSpec(self.user_spec, branch, e)
 
406
                    raise InvalidRevisionSpec(self.user_spec, branch, e)
392
407
 
393
408
                dotted = True
394
409
 
401
416
                revision_id = branch.dotted_revno_to_revision_id(match_revno,
402
417
                                                                 _cache_reverse=True)
403
418
            except (errors.NoSuchRevision, errors.RevnoOutOfBounds):
404
 
                raise errors.InvalidRevisionSpec(self.user_spec, branch)
 
419
                raise InvalidRevisionSpec(self.user_spec, branch)
405
420
            else:
406
421
                # there is no traditional 'revno' for dotted-decimal revnos.
407
422
                # so for API compatibility we return None.
418
433
            try:
419
434
                revision_id = branch.get_rev_id(revno)
420
435
            except (errors.NoSuchRevision, errors.RevnoOutOfBounds):
421
 
                raise errors.InvalidRevisionSpec(self.user_spec, branch)
 
436
                raise InvalidRevisionSpec(self.user_spec, branch)
422
437
        return branch, revno, revision_id
423
438
 
424
439
    def _as_revision_id(self, context_branch):
501
516
        try:
502
517
            offset = int(self.spec)
503
518
        except ValueError as e:
504
 
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch, e)
 
519
            raise InvalidRevisionSpec(self.user_spec, context_branch, e)
505
520
 
506
521
        if offset <= 0:
507
 
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
508
 
                                             'you must supply a positive value')
 
522
            raise InvalidRevisionSpec(
 
523
                self.user_spec, context_branch,
 
524
                'you must supply a positive value')
509
525
 
510
526
        revno = last_revno - offset + 1
511
527
        try:
512
528
            revision_id = context_branch.get_rev_id(revno)
513
529
        except (errors.NoSuchRevision, errors.RevnoOutOfBounds):
514
 
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch)
 
530
            raise InvalidRevisionSpec(self.user_spec, context_branch)
515
531
        return revno, revision_id
516
532
 
517
533
    def _as_revision_id(self, context_branch):
548
564
    def _match_on(self, branch, revs):
549
565
        r = RevisionSpec.from_string(self.spec)._match_on(branch, revs)
550
566
        if r.revno == 0:
551
 
            raise errors.InvalidRevisionSpec(self.user_spec, branch,
552
 
                                             'cannot go before the null: revision')
 
567
            raise InvalidRevisionSpec(
 
568
                self.user_spec, branch,
 
569
                'cannot go before the null: revision')
553
570
        if r.revno is None:
554
571
            # We need to use the repository history here
555
572
            rev = branch.repository.get_revision(r.rev_id)
563
580
            try:
564
581
                revision_id = branch.get_rev_id(revno, revs)
565
582
            except (errors.NoSuchRevision, errors.RevnoOutOfBounds):
566
 
                raise errors.InvalidRevisionSpec(self.user_spec,
567
 
                                                 branch)
 
583
                raise InvalidRevisionSpec(self.user_spec, branch)
568
584
        return RevisionInfo(branch, revno, revision_id)
569
585
 
570
586
    def _as_revision_id(self, context_branch):
571
587
        base_revision_id = RevisionSpec.from_string(
572
588
            self.spec)._as_revision_id(context_branch)
573
589
        if base_revision_id == revision.NULL_REVISION:
574
 
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
575
 
                                             'cannot go before the null: revision')
 
590
            raise InvalidRevisionSpec(
 
591
                self.user_spec, context_branch,
 
592
                'cannot go before the null: revision')
576
593
        context_repo = context_branch.repository
577
594
        with context_repo.lock_read():
578
595
            parent_map = context_repo.get_parent_map([base_revision_id])
579
596
        if base_revision_id not in parent_map:
580
597
            # Ghost, or unknown revision id
581
 
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
582
 
                                             'cannot find the matching revision')
 
598
            raise InvalidRevisionSpec(
 
599
                self.user_spec, context_branch, 'cannot find the matching revision')
583
600
        parents = parent_map[base_revision_id]
584
601
        if len(parents) < 1:
585
 
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
586
 
                                             'No parents for revision.')
 
602
            raise errors.InvalidRevisionSpec(
 
603
                self.user_spec, context_branch, 'No parents for revision.')
587
604
        return parents[0]
588
605
 
589
606
 
673
690
        else:
674
691
            m = self._date_regex.match(self.spec)
675
692
            if not m or (not m.group('date') and not m.group('time')):
676
 
                raise errors.InvalidRevisionSpec(self.user_spec,
677
 
                                                 branch, 'invalid date')
 
693
                raise InvalidRevisionSpec(
 
694
                    self.user_spec, branch, 'invalid date')
678
695
 
679
696
            try:
680
697
                if m.group('date'):
696
713
                else:
697
714
                    hour, minute, second = 0, 0, 0
698
715
            except ValueError:
699
 
                raise errors.InvalidRevisionSpec(self.user_spec,
700
 
                                                 branch, 'invalid date')
 
716
                raise InvalidRevisionSpec(
 
717
                    self.user_spec, branch, 'invalid date')
701
718
 
702
719
            dt = datetime.datetime(year=year, month=month, day=day,
703
720
                                   hour=hour, minute=minute, second=second)
704
721
        with branch.lock_read():
705
722
            rev = bisect.bisect(_RevListToTimestamps(branch), dt, 1)
706
723
        if rev == branch.revno():
707
 
            raise errors.InvalidRevisionSpec(self.user_spec, branch)
 
724
            raise InvalidRevisionSpec(self.user_spec, branch)
708
725
        return RevisionInfo(branch, rev)
709
726
 
710
727
 
877
894
    """
878
895
 
879
896
    def _raise_invalid(self, numstring, context_branch):
880
 
        raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
881
 
                                         'No such line: %s' % numstring)
 
897
        raise InvalidRevisionSpec(
 
898
            self.user_spec, context_branch,
 
899
            'No such line: %s' % numstring)
882
900
 
883
901
    def _as_revision_id(self, context_branch):
884
902
        path, numstring = self.spec.rsplit(':', 1)
889
907
        tree, file_path = workingtree.WorkingTree.open_containing(path)
890
908
        with tree.lock_read():
891
909
            if not tree.has_filename(file_path):
892
 
                raise errors.InvalidRevisionSpec(self.user_spec,
893
 
                                                 context_branch, "File '%s' is not versioned." %
894
 
                                                 file_path)
 
910
                raise InvalidRevisionSpec(
 
911
                    self.user_spec, context_branch,
 
912
                    "File '%s' is not versioned." % file_path)
895
913
            revision_ids = [r for (r, l) in tree.annotate_iter(file_path)]
896
914
        try:
897
915
            revision_id = revision_ids[index]
898
916
        except IndexError:
899
917
            self._raise_invalid(numstring, context_branch)
900
918
        if revision_id == revision.CURRENT_REVISION:
901
 
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
902
 
                                             'Line %s has not been committed.' % numstring)
 
919
            raise InvalidRevisionSpec(
 
920
                self.user_spec, context_branch,
 
921
                'Line %s has not been committed.' % numstring)
903
922
        return revision_id
904
923
 
905
924
 
923
942
        result = graph.find_lefthand_merger(revision_id,
924
943
                                            context_branch.last_revision())
925
944
        if result is None:
926
 
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch)
 
945
            raise InvalidRevisionSpec(self.user_spec, context_branch)
927
946
        return result
928
947
 
929
948