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