/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: 2019-05-29 03:22:34 UTC
  • mfrom: (7303 work)
  • mto: This revision was merged to the branch mainline in revision 7306.
  • Revision ID: jelmer@jelmer.uk-20190529032234-mt3fuws8gq03tapi
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
from breezy import (
26
26
    branch as _mod_branch,
27
27
    cache_utf8,
28
 
    osutils,
29
28
    revision,
30
29
    workingtree,
31
30
    )
93
92
        return 2
94
93
 
95
94
    def __getitem__(self, index):
96
 
        if index == 0: return self.revno
97
 
        if index == 1: return self.rev_id
 
95
        if index == 0:
 
96
            return self.revno
 
97
        if index == 1:
 
98
            return self.rev_id
98
99
        raise IndexError(index)
99
100
 
100
101
    def get(self):
255
256
    def __repr__(self):
256
257
        # this is mostly for helping with testing
257
258
        return '<%s %s>' % (self.__class__.__name__,
258
 
                              self.user_spec)
 
259
                            self.user_spec)
259
260
 
260
261
    def needs_branch(self):
261
262
        """Whether this revision spec needs a branch.
373
374
            branch_spec = None
374
375
        else:
375
376
            revno_spec = self.spec[:loc]
376
 
            branch_spec = self.spec[loc+1:]
 
377
            branch_spec = self.spec[loc + 1:]
377
378
 
378
379
        if revno_spec == '':
379
380
            if not branch_spec:
380
381
                raise errors.InvalidRevisionSpec(self.user_spec,
381
 
                        branch, 'cannot have an empty revno and no branch')
 
382
                                                 branch, 'cannot have an empty revno and no branch')
382
383
            revno = None
383
384
        else:
384
385
            try:
389
390
                # but the from_string method is a little primitive
390
391
                # right now - RBC 20060928
391
392
                try:
392
 
                    match_revno = tuple((int(number) for number in revno_spec.split('.')))
 
393
                    match_revno = tuple((int(number)
 
394
                                         for number in revno_spec.split('.')))
393
395
                except ValueError as e:
394
396
                    raise errors.InvalidRevisionSpec(self.user_spec, branch, e)
395
397
 
402
404
        if dotted:
403
405
            try:
404
406
                revision_id = branch.dotted_revno_to_revision_id(match_revno,
405
 
                    _cache_reverse=True)
 
407
                                                                 _cache_reverse=True)
406
408
            except errors.NoSuchRevision:
407
409
                raise errors.InvalidRevisionSpec(self.user_spec, branch)
408
410
            else:
436
438
        if self.spec.find(':') == -1:
437
439
            return None
438
440
        else:
439
 
            return self.spec[self.spec.find(':')+1:]
 
441
            return self.spec[self.spec.find(':') + 1:]
 
442
 
440
443
 
441
444
# Old compatibility
442
445
RevisionSpec_int = RevisionSpec_revno
473
476
        return self.spec
474
477
 
475
478
 
476
 
 
477
479
class RevisionSpec_last(RevisionSpec):
478
480
    """Selects the nth revision from the end."""
479
481
 
524
526
        return revision_id
525
527
 
526
528
 
527
 
 
528
529
class RevisionSpec_before(RevisionSpec):
529
530
    """Selects the parent of the revision specified."""
530
531
 
553
554
        r = RevisionSpec.from_string(self.spec)._match_on(branch, revs)
554
555
        if r.revno == 0:
555
556
            raise errors.InvalidRevisionSpec(self.user_spec, branch,
556
 
                                         'cannot go before the null: revision')
 
557
                                             'cannot go before the null: revision')
557
558
        if r.revno is None:
558
559
            # We need to use the repository history here
559
560
            rev = branch.repository.get_revision(r.rev_id)
572
573
        return RevisionInfo(branch, revno, revision_id)
573
574
 
574
575
    def _as_revision_id(self, context_branch):
575
 
        base_revision_id = RevisionSpec.from_string(self.spec)._as_revision_id(context_branch)
 
576
        base_revision_id = RevisionSpec.from_string(
 
577
            self.spec)._as_revision_id(context_branch)
576
578
        if base_revision_id == revision.NULL_REVISION:
577
579
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
578
 
                                         'cannot go before the null: revision')
 
580
                                             'cannot go before the null: revision')
579
581
        context_repo = context_branch.repository
580
582
        context_repo.lock_read()
581
583
        try:
585
587
        if base_revision_id not in parent_map:
586
588
            # Ghost, or unknown revision id
587
589
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
588
 
                'cannot find the matching revision')
 
590
                                             'cannot find the matching revision')
589
591
        parents = parent_map[base_revision_id]
590
592
        if len(parents) < 1:
591
593
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
592
 
                'No parents for revision.')
 
594
                                             'No parents for revision.')
593
595
        return parents[0]
594
596
 
595
597
 
596
 
 
597
598
class RevisionSpec_tag(RevisionSpec):
598
599
    """Select a revision identified by tag name"""
599
600
 
608
609
    def _match_on(self, branch, revs):
609
610
        # Can raise tags not supported, NoSuchTag, etc
610
611
        return RevisionInfo.from_revision_id(branch,
611
 
            branch.tags.lookup_tag(self.spec))
 
612
                                             branch.tags.lookup_tag(self.spec))
612
613
 
613
614
    def _as_revision_id(self, context_branch):
614
615
        return context_branch.tags.lookup_tag(self.spec)
615
616
 
616
617
 
617
 
 
618
618
class _RevListToTimestamps(object):
619
619
    """This takes a list of revisions, and allows you to bisect by date"""
620
620
 
655
655
    """
656
656
    prefix = 'date:'
657
657
    _date_regex = lazy_regex.lazy_compile(
658
 
            r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
659
 
            r'(,|T)?\s*'
660
 
            r'(?P<time>(?P<hour>\d\d):(?P<minute>\d\d)(:(?P<second>\d\d))?)?'
 
658
        r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
 
659
        r'(,|T)?\s*'
 
660
        r'(?P<time>(?P<hour>\d\d):(?P<minute>\d\d)(:(?P<second>\d\d))?)?'
661
661
        )
662
662
 
663
663
    def _match_on(self, branch, revs):
670
670
        #  XXX: This doesn't actually work
671
671
        #  So the proper way of saying 'give me all entries for today' is:
672
672
        #      -r date:yesterday..date:today
673
 
        today = datetime.datetime.fromordinal(datetime.date.today().toordinal())
 
673
        today = datetime.datetime.fromordinal(
 
674
            datetime.date.today().toordinal())
674
675
        if self.spec.lower() == 'yesterday':
675
676
            dt = today - datetime.timedelta(days=1)
676
677
        elif self.spec.lower() == 'today':
707
708
                                                 branch, 'invalid date')
708
709
 
709
710
            dt = datetime.datetime(year=year, month=month, day=day,
710
 
                    hour=hour, minute=minute, second=second)
 
711
                                   hour=hour, minute=minute, second=second)
711
712
        with branch.lock_read():
712
713
            rev = bisect.bisect(_RevListToTimestamps(branch), dt, 1)
713
714
        if rev == branch.revno():
715
716
        return RevisionInfo(branch, rev)
716
717
 
717
718
 
718
 
 
719
719
class RevisionSpec_ancestor(RevisionSpec):
720
720
    """Selects a common ancestor with a second branch."""
721
721
 
829
829
        return self.spec
830
830
 
831
831
 
832
 
 
833
832
class RevisionSpec_submit(RevisionSpec_ancestor):
834
833
    """Selects a common ancestor with a submit branch."""
835
834
 
860
859
        if submit_location is None:
861
860
            raise errors.NoSubmitBranch(branch)
862
861
        trace.note(gettext('Using {0} {1}').format(location_type,
863
 
                                                        submit_location))
 
862
                                                   submit_location))
864
863
        return submit_location
865
864
 
866
865
    def _match_on(self, branch, revs):
867
866
        trace.mutter('matching ancestor: on: %s, %s', self.spec, branch)
868
867
        return self._find_revision_info(branch,
869
 
            self._get_submit_location(branch))
 
868
                                        self._get_submit_location(branch))
870
869
 
871
870
    def _as_revision_id(self, context_branch):
872
871
        return self._find_revision_id(context_branch,
873
 
            self._get_submit_location(context_branch))
 
872
                                      self._get_submit_location(context_branch))
874
873
 
875
874
 
876
875
class RevisionSpec_annotate(RevisionIDSpec):
887
886
 
888
887
    def _raise_invalid(self, numstring, context_branch):
889
888
        raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
890
 
            'No such line: %s' % numstring)
 
889
                                         'No such line: %s' % numstring)
891
890
 
892
891
    def _as_revision_id(self, context_branch):
893
892
        path, numstring = self.spec.rsplit(':', 1)
899
898
        with tree.lock_read():
900
899
            if not tree.has_filename(file_path):
901
900
                raise errors.InvalidRevisionSpec(self.user_spec,
902
 
                    context_branch, "File '%s' is not versioned." %
903
 
                    file_path)
 
901
                                                 context_branch, "File '%s' is not versioned." %
 
902
                                                 file_path)
904
903
            revision_ids = [r for (r, l) in tree.annotate_iter(file_path)]
905
904
        try:
906
905
            revision_id = revision_ids[index]
908
907
            self._raise_invalid(numstring, context_branch)
909
908
        if revision_id == revision.CURRENT_REVISION:
910
909
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
911
 
                'Line %s has not been committed.' % numstring)
 
910
                                             'Line %s has not been committed.' % numstring)
912
911
        return revision_id
913
912
 
914
913
 
945
944
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_branch)
946
945
 
947
946
revspec_registry = registry.Registry()
 
947
 
 
948
 
948
949
def _register_revspec(revspec):
949
950
    revspec_registry.register(revspec.prefix, revspec)
950
951
 
 
952
 
951
953
_register_revspec(RevisionSpec_revno)
952
954
_register_revspec(RevisionSpec_revid)
953
955
_register_revspec(RevisionSpec_last)