1# Copyright (C) 2019 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; 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"""Fossil foreign branch support.
18
19Currently only tells the user that Fossil is not supported.
20"""
21
22from ... import version_info  # noqa: F401
23
24from ... import (
25    controldir,
26    errors,
27    )
28
29
30class FossilUnsupportedError(errors.UnsupportedFormatError):
31
32    _fmt = ('Fossil branches are not yet supported. '
33            'To interoperate with Fossil branches, use fastimport.')
34
35
36class FossilDirFormat(controldir.ControlDirFormat):
37    """Fossil directory format."""
38
39    def get_converter(self):
40        raise NotImplementedError(self.get_converter)
41
42    def get_format_description(self):
43        return "Fossil control directory"
44
45    def initialize_on_transport(self, transport):
46        raise errors.UninitializableFormat(self)
47
48    def is_supported(self):
49        return False
50
51    def supports_transport(self, transport):
52        return False
53
54    def check_support_status(self, allow_unsupported, recommend_upgrade=True,
55                             basedir=None):
56        raise FossilUnsupportedError()
57
58    def open(self, transport):
59        # Raise NotBranchError if there is nothing there
60        RemoteFossilProber().probe_transport(transport)
61        raise NotImplementedError(self.open)
62
63
64class RemoteFossilProber(controldir.Prober):
65
66    @classmethod
67    def priority(klass, transport):
68        return 95
69
70    @classmethod
71    def probe_transport(klass, transport):
72        from breezy.transport.http.urllib import HttpTransport
73        if not isinstance(transport, HttpTransport):
74            raise errors.NotBranchError(path=transport.base)
75        response = transport.request(
76            'POST', transport.base, headers={'Content-Type': 'application/x-fossil'})
77        if response.status == 501:
78            raise errors.NotBranchError(path=transport.base)
79        ct = response.getheader('Content-Type')
80        if ct is None:
81            raise errors.NotBranchError(path=transport.base)
82        if ct.split(';')[0] != 'application/x-fossil':
83            raise errors.NotBranchError(path=transport.base)
84        return FossilDirFormat()
85
86    @classmethod
87    def known_formats(cls):
88        return [FossilDirFormat()]
89
90
91controldir.ControlDirFormat.register_prober(RemoteFossilProber)
92