1
# Copyright (C) 2009-2018 Jelmer Vernooij <jelmer@jelmer.uk>
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Custom revision specifier for Subversion."""
19
# Please note that imports are delayed as much as possible here since
20
# if DWIM revspecs are supported this module is imported by __init__.py.
22
from ..errors import (
25
from ..revision import (
28
from ..revisionspec import (
35
def valid_git_sha1(hex):
36
"""Check if `hex` is a validly formatted Git SHA1.
38
:param hex: Hex string to validate
49
class RevisionSpec_git(RevisionSpec):
50
"""Selects a revision using a Git commit SHA1."""
52
help_txt = """Selects a revision using a Git commit SHA1.
54
Selects a revision using a Git commit SHA1, short or long.
56
This works for both native Git repositories and Git revisions
57
imported into Bazaar repositories.
61
wants_revision_history = False
63
def _lookup_git_sha1(self, branch, sha1):
65
GitSmartRemoteNotSupported,
67
from .mapping import (
71
bzr_revid = getattr(branch.repository, "lookup_foreign_revision_id",
72
default_mapping.revision_id_foreign_to_bzr)(sha1)
74
if branch.repository.has_revision(bzr_revid):
76
except GitSmartRemoteNotSupported:
78
raise InvalidRevisionSpec(self.user_spec, branch)
80
def __nonzero__(self):
81
# The default implementation uses branch.repository.has_revision()
82
if self.rev_id is None:
84
if self.rev_id == NULL_REVISION:
88
def _find_short_git_sha1(self, branch, sha1):
89
from .mapping import (
93
parse_revid = getattr(branch.repository, "lookup_bzr_revision_id",
94
mapping_registry.parse_revision_id)
95
def matches_revid(revid):
96
if revid == NULL_REVISION:
99
foreign_revid, mapping = parse_revid(revid)
100
except InvalidRevisionId:
102
if not isinstance(mapping.vcs, ForeignGit):
104
return foreign_revid.startswith(sha1)
105
with branch.repository.lock_read():
106
graph = branch.repository.get_graph()
107
last_revid = branch.last_revision()
108
if matches_revid(last_revid):
110
for revid, _ in graph.iter_ancestry([last_revid]):
111
if matches_revid(revid):
113
raise InvalidRevisionSpec(self.user_spec, branch)
115
def _as_revision_id(self, context_branch):
116
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)
124
lazy_check_versions()
125
if len(git_sha1) == 40:
126
return self._lookup_git_sha1(context_branch, git_sha1)
128
return self._find_short_git_sha1(context_branch, git_sha1)
130
def _match_on(self, branch, revs):
131
revid = self._as_revision_id(branch)
132
return RevisionInfo.from_revision_id(branch, revid)
134
def needs_branch(self):
137
def get_branch(self):