/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
7413.8.1 by Jelmer Vernooij
Add Mercurial support.
1
# Copyright (C) 2019 Jelmer Vernooij <jelmer@jelmer.uk>
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; version 3 of the License or
6
# (at your option) a 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
17
"""Mercurial foreign branch support.
18
19
Currently only tells the user that Mercurial is not supported.
20
"""
21
22
from ... import (
23
    controldir,
24
    errors,
25
    )
26
7413.8.12 by Jelmer Vernooij
Fix hg plugin.
27
from ... import version_info  # noqa: F401
7413.8.11 by Jelmer Vernooij
Don't lazy-import errors.
28
7413.8.1 by Jelmer Vernooij
Add Mercurial support.
29
30
class MercurialUnsupportedError(errors.UnsupportedFormatError):
31
32
    _fmt = ('Mercurial branches are not yet supported. '
7413.8.2 by Jelmer Vernooij
Fix typos.
33
            'To convert Mercurial branches to Bazaar branches or vice versa, '
7413.8.1 by Jelmer Vernooij
Add Mercurial support.
34
            'use the fastimport format. ')
35
36
7413.8.7 by Jelmer Vernooij
Test fixes.
37
class LocalHgDirFormat(controldir.ControlDirFormat):
7413.8.1 by Jelmer Vernooij
Add Mercurial support.
38
    """Mercurial directory format."""
39
40
    def get_converter(self):
41
        raise NotImplementedError(self.get_converter)
42
43
    def get_format_description(self):
7413.8.7 by Jelmer Vernooij
Test fixes.
44
        return "Local Mercurial control directory"
7413.8.1 by Jelmer Vernooij
Add Mercurial support.
45
46
    def initialize_on_transport(self, transport):
47
        raise errors.UninitializableFormat(self)
48
49
    def is_supported(self):
50
        return False
51
52
    def supports_transport(self, transport):
53
        return False
54
55
    def check_support_status(self, allow_unsupported, recommend_upgrade=True,
56
                             basedir=None):
7413.8.2 by Jelmer Vernooij
Fix typos.
57
        raise MercurialUnsupportedError()
7413.8.1 by Jelmer Vernooij
Add Mercurial support.
58
59
    def open(self, transport):
60
        # Raise NotBranchError if there is nothing there
61
        LocalHgProber().probe_transport(transport)
62
        raise NotImplementedError(self.open)
63
64
65
class LocalHgProber(controldir.Prober):
66
7413.8.5 by Jelmer Vernooij
Set priorities.
67
    @classmethod
68
    def priority(klass, transport):
69
        return 100
70
7413.8.4 by Jelmer Vernooij
Split dumb / smart probing.
71
    @staticmethod
72
    def _has_hg_dumb_repository(transport):
73
        try:
74
            return transport.has_any([".hg/requires", ".hg/00changelog.i"])
75
        except (errors.NoSuchFile, errors.PermissionDenied,
76
                errors.InvalidHttpResponse):
77
            return False
78
7413.8.1 by Jelmer Vernooij
Add Mercurial support.
79
    @classmethod
80
    def probe_transport(klass, transport):
81
        """Our format is present if the transport has a '.hg/' subdir."""
7413.8.4 by Jelmer Vernooij
Split dumb / smart probing.
82
        if klass._has_hg_dumb_repository(transport):
7413.8.2 by Jelmer Vernooij
Fix typos.
83
            return HgDirFormat()
7413.8.1 by Jelmer Vernooij
Add Mercurial support.
84
        raise errors.NotBranchError(path=transport.base)
85
86
    @classmethod
87
    def known_formats(cls):
7413.8.7 by Jelmer Vernooij
Test fixes.
88
        return [LocalHgDirFormat()]
89
90
91
class SmartHgDirFormat(controldir.ControlDirFormat):
92
    """Mercurial directory format."""
93
94
    def get_converter(self):
95
        raise NotImplementedError(self.get_converter)
96
97
    def get_format_description(self):
98
        return "Smart Mercurial control directory"
99
100
    def initialize_on_transport(self, transport):
101
        raise errors.UninitializableFormat(self)
102
103
    def is_supported(self):
104
        return False
105
106
    def supports_transport(self, transport):
107
        return False
108
109
    def check_support_status(self, allow_unsupported, recommend_upgrade=True,
110
                             basedir=None):
111
        raise MercurialUnsupportedError()
112
113
    def open(self, transport):
114
        # Raise NotBranchError if there is nothing there
115
        SmartHgProber().probe_transport(transport)
116
        raise NotImplementedError(self.open)
7413.8.1 by Jelmer Vernooij
Add Mercurial support.
117
118
7413.8.6 by Jelmer Vernooij
Properly split dumb and smart probing.
119
class SmartHgProber(controldir.Prober):
7413.8.3 by Jelmer Vernooij
Support remote git repositories.
120
121
    # Perhaps retrieve list from mercurial.hg.schemes ?
7413.8.6 by Jelmer Vernooij
Properly split dumb and smart probing.
122
    _supported_schemes = ["http", "https"]
7413.8.3 by Jelmer Vernooij
Support remote git repositories.
123
7413.8.5 by Jelmer Vernooij
Set priorities.
124
    @classmethod
125
    def priority(klass, transport):
126
        return 90
127
7413.8.3 by Jelmer Vernooij
Support remote git repositories.
128
    @staticmethod
129
    def _has_hg_http_smart_server(transport, external_url):
130
        """Check if there is a Mercurial smart server at the remote location.
131
132
        :param transport: Transport to check
133
        :param externa_url: External URL for transport
134
        :return: Boolean indicating whether transport is backed onto hg
135
        """
7413.8.7 by Jelmer Vernooij
Test fixes.
136
        from breezy.urlutils import urlparse
137
        parsed_url = urlparse.urlparse(external_url)
7413.8.8 by Jelmer Vernooij
Fix redirect handling.
138
        parsed_url = parsed_url._replace(
139
            query='cmd=capabilities', path=parsed_url.path.rstrip('/') + '/hg')
7413.8.7 by Jelmer Vernooij
Test fixes.
140
        url = urlparse.urlunparse(parsed_url)
7413.8.3 by Jelmer Vernooij
Support remote git repositories.
141
        resp = transport.request(
142
            'GET', url, headers={'Accept': 'application/mercurial-0.1'})
143
        if resp.status == 404:
144
            return False
145
        ct = resp.getheader("Content-Type")
146
        if ct is None:
147
            return False
148
        return ct.startswith("application/mercurial")
149
150
    @classmethod
151
    def probe_transport(klass, transport):
152
        try:
153
            external_url = transport.external_url()
154
        except errors.InProcessTransport:
155
            raise errors.NotBranchError(path=transport.base)
156
        scheme = external_url.split(":")[0]
157
        if scheme not in klass._supported_schemes:
158
            raise errors.NotBranchError(path=transport.base)
159
        from breezy import urlutils
7441.1.1 by Jelmer Vernooij
Add strip_segment_parameters function.
160
        external_url = urlutils.strip_segment_parameters(external_url)
7413.8.3 by Jelmer Vernooij
Support remote git repositories.
161
        # Explicitly check for .hg directories here, so we avoid
162
        # loading foreign branches through Mercurial.
163
        if (external_url.startswith("http:") or
164
                external_url.startswith("https:")):
7413.8.6 by Jelmer Vernooij
Properly split dumb and smart probing.
165
            if klass._has_hg_http_smart_server(transport, external_url):
7413.8.7 by Jelmer Vernooij
Test fixes.
166
                return SmartHgDirFormat()
7413.8.6 by Jelmer Vernooij
Properly split dumb and smart probing.
167
        raise errors.NotBranchError(path=transport.base)
7413.8.3 by Jelmer Vernooij
Support remote git repositories.
168
169
    @classmethod
170
    def known_formats(cls):
7413.8.9 by Jelmer Vernooij
Fix return type.
171
        return [SmartHgDirFormat()]
7413.8.3 by Jelmer Vernooij
Support remote git repositories.
172
173
7413.8.1 by Jelmer Vernooij
Add Mercurial support.
174
controldir.ControlDirFormat.register_prober(LocalHgProber)
7413.8.12 by Jelmer Vernooij
Fix hg plugin.
175
controldir.ControlDirFormat.register_prober(SmartHgProber)