Commit a7fdd3ed authored by Dan Villiom Podlaski Christiansen's avatar Dan Villiom Podlaski Christiansen
Browse files

http: use Mercurial's infrastructure with latest Dulwich

parent b963d11cc1c8
Pipeline #55927 passed with stages
in 20 minutes and 36 seconds
from __future__ import generator_stop
import functools
import urllib.parse
from mercurial.i18n import _
from mercurial import (
error,
node,
......@@ -4,7 +5,8 @@
from mercurial.i18n import _
from mercurial import (
error,
node,
httppeer,
pycompat,
ui,
......@@ -9,5 +11,6 @@
pycompat,
ui,
url as urlmod,
util as hgutil,
wireprotov1peer,
)
......@@ -127,3 +130,71 @@
"""
for other in others:
check_at_most_one_arg(opts, first, other)
try:
# added in dulwich 0.20.24
from dulwich.client import AbstractHttpGitClient
class HgHttpGitClient(AbstractHttpGitClient):
def __init__(self, ui, base_url, **kwargs):
url = urlutil.url(base_url)
if url.query or url.fragment:
raise error.Abort(
_(b'unsupported URL component: "%s"')
% (url.query or url.fragment)
)
# urllib cannot handle URLs with embedded user or passwd.
url, authinfo = url.authinfo()
ui.debug(b'using %s\n' % url)
self.__ui = ui
self.__opener = urlmod.opener(ui, authinfo)
super().__init__(pycompat.strurl(url), **kwargs)
def _get_url(self, path):
u = urllib.parse.urljoin(self._base_url, pycompat.strurl(path))
return u.rstrip("/") + "/"
def _http_request(
self,
str_url,
headers=None,
data=None,
allow_compression=False,
):
self.__ui.debug(
b"requesting %s\n"
% urlutil.hidepassword(pycompat.bytesurl(str_url))
)
if headers is None:
headers = {}
if allow_compression:
headers["Accept-Encoding"] = "gzip"
else:
headers["Accept-Encoding"] = "identity"
# TODO: recent versions of dulwich use generators for
# passing data on lazily; it would be nice to somehow
# retain that memory optimisation
if data is not None and not isinstance(data, bytes):
data = b''.join(data)
req = hgutil.urlreq.request(str_url, data, headers)
resp = httppeer.sendrequest(self.__ui, self.__opener, req)
resp_url = resp.geturl()
# used by dulwich
resp.redirect_location = resp_url if resp_url != url else ""
resp.content_type = resp.getheader("Content-Type")
return resp, resp.read
except ImportError:
HgHttpGitClient = None
......@@ -2197,6 +2197,7 @@
>>> from dulwich.client import HttpGitClient, SSHGitClient
>>> from mercurial import ui
>>> from .compat import HgHttpGitClient
>>> class SubHandler(GitHandler):
... def __init__(self):
... self.ui = ui.ui()
......@@ -2206,7 +2207,7 @@
... )
>>> tp = SubHandler()._get_transport_and_path
>>> client, url = tp(b'http://fqdn.com/test.git')
>>> print(isinstance(client, HttpGitClient))
>>> print(isinstance(client, HgHttpGitClient or HttpGitClient))
True
>>> print(url.decode())
http://fqdn.com/test.git
......@@ -2253,6 +2254,9 @@
uri = uri[4:]
if uri.startswith(b'http://') or uri.startswith(b'https://'):
if compat.HgHttpGitClient is not None:
return compat.HgHttpGitClient(self.ui, uri), uri
ua = b'git/20x6 (hg-git ; uses dulwich and hg ; like git-core)'
config = dul_config.ConfigDict()
config.set(b'http', b'useragent', ua)
......
......@@ -424,6 +424,7 @@
(0, 20, 0),
(0, 20, 3),
(0, 20, 4),
(0, 20, 24),
(0, 20, 37),
(0, 20, 44),
)
......
......@@ -31,6 +31,6 @@
updating to branch default
0 files updated, 0 files merged, 0 files removed, 0 files unresolved
..and HTTP:
..and HTTP, which requires authentication:
$ hg clone http://git-server/repo.git repo-http
......@@ -35,4 +35,5 @@
$ hg clone http://git-server/repo.git repo-http
abort: http authorization required for http://git-server/repo.git
abort: http authorization required for http://git-server/repo.git (no-dulwich02024 !)
abort: http authorization required for http://git-server/repo.git/info/refs (dulwich02024 !)
[255]
......@@ -38,3 +39,6 @@
[255]
So pass it in the configuration:
$ hg clone --config ui.interactive=yes \
> --config ui.interactive=yes \
......@@ -39,5 +43,5 @@
$ hg clone --config ui.interactive=yes \
> --config ui.interactive=yes \
> --config auth.git.prefix=http://git-server \
> --config auth.git.prefix=http://git-server/ \
> --config auth.git.username=git \
> http://git-server/repo.git repo-http
......@@ -42,6 +46,6 @@
> --config auth.git.username=git \
> http://git-server/repo.git repo-http
http authorization required for http://git-server/repo.git
http authorization required for http://git-server/repo.git* (glob)
realm: Git (no-dulwich0203 !)
realm: Git Access (dulwich0203 !)
user: git
......@@ -52,7 +56,7 @@
> http://git-server/repo.git repo-http <<EOF
> git
> EOF
http authorization required for http://git-server/repo.git
http authorization required for http://git-server/repo.git* (glob)
realm: Git (no-dulwich0203 !)
realm: Git Access (dulwich0203 !)
user: git
......@@ -63,7 +67,7 @@
..and finally SSH:
$ hg clone git@git-server:/srv/repo.git repo-ssh
Warning: Permanently added * (glob)
Warning: Permanently added * (glob) (?)
updating to branch default
0 files updated, 0 files merged, 0 files removed, 0 files unresolved
......@@ -98,9 +102,9 @@
$ hg -R repo-http pull -u
pulling from http://git-server/repo.git
abort: http authorization required for http://git-server/repo.git
abort: http authorization required for http://git-server/repo.git* (glob)
[255]
But we can specify authentication in the configuration:
$ hg -R repo-http \
......@@ -102,9 +106,9 @@
[255]
But we can specify authentication in the configuration:
$ hg -R repo-http \
> --config auth.git.prefix=http://git-server \
> --config auth.git.prefix=http://git-server/ \
> --config auth.git.username=git \
> --config auth.git.password=git \
> pull -u
......@@ -115,7 +119,7 @@
new changesets fa22339f4ab8 (1 drafts)
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
#if dulwich0200
#if dulwich0200 no-dulwich02024
Try using git credentials, only supported on Dulwich 0.20+
NB: the use of printf is deliberate; otherwise the test fails due to
......
......@@ -54,10 +54,6 @@
HG:c4d188f6e13d GIT:b23744d34f97
#if dulwich02037 no-dulwich02044
Broken due to bug #977 in Dulwich
#else
$ cd copy
$ hg up master
\r (no-eol) (esc)
......@@ -86,7 +82,6 @@
updating reference refs/heads/master
$ hg log -T 'HG:{node|short} GIT:{gitnode|short}\n' -r .
HG:daf1ae153bf8 GIT:ab88565d0614
#endif
Prevent the test from hanging:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment