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

Raise SettingFileIdUnsupported

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
 
2
 
 
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.
 
7
 
 
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.
 
12
 
 
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."""
 
17
 
 
18
from __future__ import absolute_import
 
19
 
 
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.
 
22
 
 
23
from ... import version_info as breezy_version
 
24
from ...errors import (
 
25
    InvalidRevisionId,
 
26
    InvalidRevisionSpec,
 
27
    )
 
28
from ...revision import (
 
29
    NULL_REVISION,
 
30
)
 
31
from ...revisionspec import (
 
32
    RevisionInfo,
 
33
    RevisionSpec,
 
34
    )
 
35
 
 
36
 
 
37
def valid_git_sha1(hex):
 
38
    """Check if `hex` is a validly formatted Git SHA1.
 
39
 
 
40
    :param hex: Hex string to validate
 
41
    :return: Boolean
 
42
    """
 
43
    import binascii
 
44
    try:
 
45
        binascii.unhexlify(hex)
 
46
    except TypeError:
 
47
        return False
 
48
    else:
 
49
        return True
 
50
 
 
51
 
 
52
class RevisionSpec_git(RevisionSpec):
 
53
    """Selects a revision using a Subversion revision number."""
 
54
 
 
55
    help_txt = """Selects a revision using a Git revision sha1.
 
56
    """
 
57
 
 
58
    prefix = 'git:'
 
59
    wants_revision_history = False
 
60
 
 
61
    def _lookup_git_sha1(self, branch, sha1):
 
62
        from .errors import (
 
63
            GitSmartRemoteNotSupported,
 
64
            )
 
65
        from .mapping import (
 
66
            default_mapping,
 
67
            )
 
68
 
 
69
        bzr_revid = getattr(branch.repository, "lookup_foreign_revision_id",
 
70
                              default_mapping.revision_id_foreign_to_bzr)(sha1)
 
71
        try:
 
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)
 
77
 
 
78
    def __nonzero__(self):
 
79
        # The default implementation uses branch.repository.has_revision()
 
80
        if self.rev_id is None:
 
81
            return False
 
82
        if self.rev_id == NULL_REVISION:
 
83
            return False
 
84
        return True
 
85
 
 
86
    def _find_short_git_sha1(self, branch, sha1):
 
87
        from .mapping import (
 
88
            ForeignGit,
 
89
            mapping_registry,
 
90
            )
 
91
        parse_revid = getattr(branch.repository, "lookup_bzr_revision_id",
 
92
                              mapping_registry.parse_revision_id)
 
93
        branch.repository.lock_read()
 
94
        try:
 
95
            graph = branch.repository.get_graph()
 
96
            for revid, _ in graph.iter_ancestry([branch.last_revision()]):
 
97
                if revid == NULL_REVISION:
 
98
                    continue
 
99
                try:
 
100
                    foreign_revid, mapping = parse_revid(revid)
 
101
                except InvalidRevisionId:
 
102
                    continue
 
103
                if not isinstance(mapping.vcs, ForeignGit):
 
104
                    continue
 
105
                if foreign_revid.startswith(sha1):
 
106
                    return RevisionInfo.from_revision_id(branch, revid)
 
107
            raise InvalidRevisionSpec(self.user_spec, branch)
 
108
        finally:
 
109
            branch.repository.unlock()
 
110
 
 
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)
 
116
        from . import (
 
117
            lazy_check_versions,
 
118
            )
 
119
        lazy_check_versions()
 
120
        if len(git_sha1) == 40:
 
121
            return self._lookup_git_sha1(branch, git_sha1)
 
122
        else:
 
123
            return self._find_short_git_sha1(branch, git_sha1)
 
124
 
 
125
    def needs_branch(self):
 
126
        return True
 
127
 
 
128
    def get_branch(self):
 
129
        return None