/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/git/revspec.py

  • Committer: Jelmer Vernooij
  • Date: 2018-06-14 17:59:16 UTC
  • mto: This revision was merged to the branch mainline in revision 7065.
  • Revision ID: jelmer@jelmer.uk-20180614175916-a2e2xh5k533guq1x
Move breezy.plugins.git to breezy.git.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Custom revision specifier for Subversion."""
18
18
 
 
19
from __future__ import absolute_import
 
20
 
19
21
# Please note that imports are delayed as much as possible here since
20
22
# if DWIM revspecs are supported this module is imported by __init__.py.
21
23
 
 
24
from .. import version_info as breezy_version
22
25
from ..errors import (
23
26
    InvalidRevisionId,
 
27
    InvalidRevisionSpec,
24
28
    )
25
29
from ..revision import (
26
30
    NULL_REVISION,
27
31
)
28
32
from ..revisionspec import (
29
 
    InvalidRevisionSpec,
30
33
    RevisionInfo,
31
34
    RevisionSpec,
32
35
    )
38
41
    :param hex: Hex string to validate
39
42
    :return: Boolean
40
43
    """
 
44
    import binascii
41
45
    try:
42
 
        int(hex, 16)
43
 
    except ValueError:
 
46
        binascii.unhexlify(hex)
 
47
    except TypeError:
 
48
        return False
 
49
    except binascii.Error:
44
50
        return False
45
51
    else:
46
52
        return True
47
53
 
48
54
 
49
55
class RevisionSpec_git(RevisionSpec):
50
 
    """Selects a revision using a Git commit SHA1."""
51
 
 
52
 
    help_txt = """Selects a revision using a Git commit SHA1.
53
 
 
54
 
    Selects a revision using a Git commit SHA1, short or long.
55
 
 
56
 
    This works for both native Git repositories and Git revisions
57
 
    imported into Bazaar repositories.
 
56
    """Selects a revision using a Subversion revision number."""
 
57
 
 
58
    help_txt = """Selects a revision using a Git revision sha1.
58
59
    """
59
60
 
60
61
    prefix = 'git:'
69
70
            )
70
71
 
71
72
        bzr_revid = getattr(branch.repository, "lookup_foreign_revision_id",
72
 
                            default_mapping.revision_id_foreign_to_bzr)(sha1)
 
73
                              default_mapping.revision_id_foreign_to_bzr)(sha1)
73
74
        try:
74
75
            if branch.repository.has_revision(bzr_revid):
75
 
                return bzr_revid
 
76
                return RevisionInfo.from_revision_id(branch, bzr_revid)
76
77
        except GitSmartRemoteNotSupported:
77
 
            return bzr_revid
 
78
            return RevisionInfo(branch, None, bzr_revid)
78
79
        raise InvalidRevisionSpec(self.user_spec, branch)
79
80
 
80
81
    def __nonzero__(self):
92
93
            )
93
94
        parse_revid = getattr(branch.repository, "lookup_bzr_revision_id",
94
95
                              mapping_registry.parse_revision_id)
95
 
        def matches_revid(revid):
96
 
            if revid == NULL_REVISION:
97
 
                return False
98
 
            try:
99
 
                foreign_revid, mapping = parse_revid(revid)
100
 
            except InvalidRevisionId:
101
 
                return False
102
 
            if not isinstance(mapping.vcs, ForeignGit):
103
 
                return False
104
 
            return foreign_revid.startswith(sha1)
105
96
        with branch.repository.lock_read():
106
97
            graph = branch.repository.get_graph()
107
 
            last_revid = branch.last_revision()
108
 
            if matches_revid(last_revid):
109
 
                return last_revid
110
 
            for revid, _ in graph.iter_ancestry([last_revid]):
111
 
                if matches_revid(revid):
112
 
                    return revid
 
98
            for revid, _ in graph.iter_ancestry([branch.last_revision()]):
 
99
                if revid == NULL_REVISION:
 
100
                    continue
 
101
                try:
 
102
                    foreign_revid, mapping = parse_revid(revid)
 
103
                except InvalidRevisionId:
 
104
                    continue
 
105
                if not isinstance(mapping.vcs, ForeignGit):
 
106
                    continue
 
107
                if foreign_revid.startswith(sha1):
 
108
                    return RevisionInfo.from_revision_id(branch, revid)
113
109
            raise InvalidRevisionSpec(self.user_spec, branch)
114
110
 
115
 
    def _as_revision_id(self, context_branch):
 
111
    def _match_on(self, branch, revs):
116
112
        loc = self.spec.find(':')
117
 
        git_sha1 = self.spec[loc + 1:].encode("utf-8")
118
 
        if (len(git_sha1) > 40 or len(git_sha1) < 4 or
119
 
                not valid_git_sha1(git_sha1)):
120
 
            raise InvalidRevisionSpec(self.user_spec, context_branch)
 
113
        git_sha1 = self.spec[loc+1:].encode("utf-8")
 
114
        if len(git_sha1) > 40 or not valid_git_sha1(git_sha1):
 
115
            raise InvalidRevisionSpec(self.user_spec, branch)
121
116
        from . import (
122
117
            lazy_check_versions,
123
118
            )
124
119
        lazy_check_versions()
125
120
        if len(git_sha1) == 40:
126
 
            return self._lookup_git_sha1(context_branch, git_sha1)
 
121
            return self._lookup_git_sha1(branch, git_sha1)
127
122
        else:
128
 
            return self._find_short_git_sha1(context_branch, git_sha1)
129
 
 
130
 
    def _match_on(self, branch, revs):
131
 
        revid = self._as_revision_id(branch)
132
 
        return RevisionInfo.from_revision_id(branch, revid)
 
123
            return self._find_short_git_sha1(branch, git_sha1)
133
124
 
134
125
    def needs_branch(self):
135
126
        return True