1
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
"""Custom revision specifier for Subversion."""
18
from __future__ import absolute_import
20
# Please note that imports are delayed as much as possible here since
21
# if DWIM revspecs are supported this module is imported by __init__.py.
23
from ... import version_info as breezy_version
24
from ...errors import (
28
from ...revision import (
31
from ...revisionspec import (
37
def valid_git_sha1(hex):
38
"""Check if `hex` is a validly formatted Git SHA1.
40
:param hex: Hex string to validate
45
binascii.unhexlify(hex)
52
class RevisionSpec_git(RevisionSpec):
53
"""Selects a revision using a Subversion revision number."""
55
help_txt = """Selects a revision using a Git revision sha1.
59
wants_revision_history = False
61
def _lookup_git_sha1(self, branch, sha1):
63
GitSmartRemoteNotSupported,
65
from .mapping import (
69
bzr_revid = getattr(branch.repository, "lookup_foreign_revision_id",
70
default_mapping.revision_id_foreign_to_bzr)(sha1)
72
if branch.repository.has_revision(bzr_revid):
73
return RevisionInfo.from_revision_id(branch, bzr_revid)
74
except GitSmartRemoteNotSupported:
75
return RevisionInfo(branch, None, bzr_revid)
76
raise InvalidRevisionSpec(self.user_spec, branch)
78
def __nonzero__(self):
79
# The default implementation uses branch.repository.has_revision()
80
if self.rev_id is None:
82
if self.rev_id == NULL_REVISION:
86
def _find_short_git_sha1(self, branch, sha1):
87
from .mapping import (
91
parse_revid = getattr(branch.repository, "lookup_bzr_revision_id",
92
mapping_registry.parse_revision_id)
93
branch.repository.lock_read()
95
graph = branch.repository.get_graph()
96
for revid, _ in graph.iter_ancestry([branch.last_revision()]):
97
if revid == NULL_REVISION:
100
foreign_revid, mapping = parse_revid(revid)
101
except InvalidRevisionId:
103
if not isinstance(mapping.vcs, ForeignGit):
105
if foreign_revid.startswith(sha1):
106
return RevisionInfo.from_revision_id(branch, revid)
107
raise InvalidRevisionSpec(self.user_spec, branch)
109
branch.repository.unlock()
111
def _match_on(self, branch, revs):
112
loc = self.spec.find(':')
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)
119
lazy_check_versions()
120
if len(git_sha1) == 40:
121
return self._lookup_git_sha1(branch, git_sha1)
123
return self._find_short_git_sha1(branch, git_sha1)
125
def needs_branch(self):
128
def get_branch(self):