/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 bzrlib/revisionspec.py

  • Committer: Jelmer Vernooij
  • Date: 2011-02-24 12:08:37 UTC
  • mto: (5676.1.5 per_interrepo-extra)
  • mto: This revision was merged to the branch mainline in revision 5686.
  • Revision ID: jelmer@samba.org-20110224120837-73x5dp7h8w6fusfz
Refactor, move to bzrlib.controldir.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
lazy_import(globals(), """
22
22
import bisect
23
23
import datetime
 
24
 
 
25
from bzrlib import (
 
26
    workingtree,
 
27
    )
24
28
""")
25
29
 
26
30
from bzrlib import (
 
31
    branch as _mod_branch,
27
32
    errors,
28
33
    osutils,
29
34
    registry,
30
35
    revision,
31
36
    symbol_versioning,
32
37
    trace,
 
38
    workingtree,
33
39
    )
34
40
 
35
41
 
166
172
                         spectype.__name__, spec)
167
173
            return spectype(spec, _internal=True)
168
174
        else:
169
 
            for spectype in SPEC_TYPES:
170
 
                if spec.startswith(spectype.prefix):
171
 
                    trace.mutter('Returning RevisionSpec %s for %s',
172
 
                                 spectype.__name__, spec)
173
 
                    return spectype(spec, _internal=True)
174
175
            # Otherwise treat it as a DWIM, build the RevisionSpec object and
175
176
            # wait for _match_on to be called.
176
177
            return RevisionSpec_dwim(spec, _internal=True)
444
445
 
445
446
 
446
447
 
447
 
class RevisionSpec_revid(RevisionSpec):
 
448
class RevisionIDSpec(RevisionSpec):
 
449
 
 
450
    def _match_on(self, branch, revs):
 
451
        revision_id = self.as_revision_id(branch)
 
452
        return RevisionInfo.from_revision_id(branch, revision_id, revs)
 
453
 
 
454
 
 
455
class RevisionSpec_revid(RevisionIDSpec):
448
456
    """Selects a revision using the revision id."""
449
457
 
450
458
    help_txt = """Selects a revision using the revision id.
459
467
 
460
468
    prefix = 'revid:'
461
469
 
462
 
    def _match_on(self, branch, revs):
 
470
    def _as_revision_id(self, context_branch):
463
471
        # self.spec comes straight from parsing the command line arguments,
464
472
        # so we expect it to be a Unicode string. Switch it to the internal
465
473
        # representation.
466
 
        revision_id = osutils.safe_revision_id(self.spec, warn=False)
467
 
        return RevisionInfo.from_revision_id(branch, revision_id, revs)
468
 
 
469
 
    def _as_revision_id(self, context_branch):
470
474
        return osutils.safe_revision_id(self.spec, warn=False)
471
475
 
472
476
 
813
817
        revision_b = other_branch.last_revision()
814
818
        if revision_b in (None, revision.NULL_REVISION):
815
819
            raise errors.NoCommits(other_branch)
816
 
        # pull in the remote revisions so we can diff
817
 
        branch.fetch(other_branch, revision_b)
 
820
        if branch is None:
 
821
            branch = other_branch
 
822
        else:
 
823
            try:
 
824
                # pull in the remote revisions so we can diff
 
825
                branch.fetch(other_branch, revision_b)
 
826
            except errors.ReadOnlyError:
 
827
                branch = other_branch
818
828
        try:
819
829
            revno = branch.revision_id_to_revno(revision_b)
820
830
        except errors.NoSuchRevision:
840
850
            raise errors.NoCommits(other_branch)
841
851
        return other_branch.repository.revision_tree(last_revision)
842
852
 
 
853
    def needs_branch(self):
 
854
        return False
 
855
 
 
856
    def get_branch(self):
 
857
        return self.spec
 
858
 
843
859
 
844
860
 
845
861
class RevisionSpec_submit(RevisionSpec_ancestor):
884
900
            self._get_submit_location(context_branch))
885
901
 
886
902
 
 
903
class RevisionSpec_annotate(RevisionIDSpec):
 
904
 
 
905
    prefix = 'annotate:'
 
906
 
 
907
    help_txt = """Select the revision that last modified the specified line.
 
908
 
 
909
    Select the revision that last modified the specified line.  Line is
 
910
    specified as path:number.  Path is a relative path to the file.  Numbers
 
911
    start at 1, and are relative to the current version, not the last-
 
912
    committed version of the file.
 
913
    """
 
914
 
 
915
    def _raise_invalid(self, numstring, context_branch):
 
916
        raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
 
917
            'No such line: %s' % numstring)
 
918
 
 
919
    def _as_revision_id(self, context_branch):
 
920
        path, numstring = self.spec.rsplit(':', 1)
 
921
        try:
 
922
            index = int(numstring) - 1
 
923
        except ValueError:
 
924
            self._raise_invalid(numstring, context_branch)
 
925
        tree, file_path = workingtree.WorkingTree.open_containing(path)
 
926
        tree.lock_read()
 
927
        try:
 
928
            file_id = tree.path2id(file_path)
 
929
            if file_id is None:
 
930
                raise errors.InvalidRevisionSpec(self.user_spec,
 
931
                    context_branch, "File '%s' is not versioned." %
 
932
                    file_path)
 
933
            revision_ids = [r for (r, l) in tree.annotate_iter(file_id)]
 
934
        finally:
 
935
            tree.unlock()
 
936
        try:
 
937
            revision_id = revision_ids[index]
 
938
        except IndexError:
 
939
            self._raise_invalid(numstring, context_branch)
 
940
        if revision_id == revision.CURRENT_REVISION:
 
941
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch,
 
942
                'Line %s has not been committed.' % numstring)
 
943
        return revision_id
 
944
 
 
945
 
 
946
class RevisionSpec_mainline(RevisionIDSpec):
 
947
 
 
948
    help_txt = """Select mainline revision that merged the specified revision.
 
949
 
 
950
    Select the revision that merged the specified revision into mainline.
 
951
    """
 
952
 
 
953
    prefix = 'mainline:'
 
954
 
 
955
    def _as_revision_id(self, context_branch):
 
956
        revspec = RevisionSpec.from_string(self.spec)
 
957
        if revspec.get_branch() is None:
 
958
            spec_branch = context_branch
 
959
        else:
 
960
            spec_branch = _mod_branch.Branch.open(revspec.get_branch())
 
961
        revision_id = revspec.as_revision_id(spec_branch)
 
962
        graph = context_branch.repository.get_graph()
 
963
        result = graph.find_lefthand_merger(revision_id,
 
964
                                            context_branch.last_revision())
 
965
        if result is None:
 
966
            raise errors.InvalidRevisionSpec(self.user_spec, context_branch)
 
967
        return result
 
968
 
 
969
 
887
970
# The order in which we want to DWIM a revision spec without any prefix.
888
971
# revno is always tried first and isn't listed here, this is used by
889
972
# RevisionSpec_dwim._match_on
908
991
_register_revspec(RevisionSpec_ancestor)
909
992
_register_revspec(RevisionSpec_branch)
910
993
_register_revspec(RevisionSpec_submit)
911
 
 
912
 
# classes in this list should have a "prefix" attribute, against which
913
 
# string specs are matched
914
 
SPEC_TYPES = symbol_versioning.deprecated_list(
915
 
    symbol_versioning.deprecated_in((1, 12, 0)), "SPEC_TYPES", [])
 
994
_register_revspec(RevisionSpec_annotate)
 
995
_register_revspec(RevisionSpec_mainline)