Mercurial > hg-git-serve
annotate src/hgext3rd/hggit_serve/_http.py @ 12:f630d9904ea7
Reorganize project into multiple files.
| author | Paul Fisher <paul@pfish.zone> |
|---|---|
| date | Wed, 18 Feb 2026 16:17:05 -0500 |
| parents | src/hgext3rd/hggit_serve.py@ce204bcc4e04 |
| children | 00bdfac5416c |
| rev | line source |
|---|---|
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
1 from __future__ import annotations |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
2 |
|
0
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
3 import email.parser |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
4 import email.policy |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
5 import re |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
6 import shutil |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
7 import subprocess |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
8 import typing as t |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
9 |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
10 from mercurial import extensions |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
11 from mercurial import wireprotoserver |
|
8
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
12 from mercurial.thirdparty import attr |
|
0
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
13 |
|
12
f630d9904ea7
Reorganize project into multiple files.
Paul Fisher <paul@pfish.zone>
parents:
11
diff
changeset
|
14 from . import _export as xp |
|
f630d9904ea7
Reorganize project into multiple files.
Paul Fisher <paul@pfish.zone>
parents:
11
diff
changeset
|
15 |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
16 if t.TYPE_CHECKING: |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
17 import mercurial.hgweb.hgweb_mod_inner as web_inner |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
18 import mercurial.hgweb.request as hgreq |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
19 import mercurial.interfaces.repository as hgrepo |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
20 import mercurial.ui as hgui |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
21 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
22 PermissionCheck = t.Callable[ |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
23 [web_inner.requestcontext, hgreq.parsedrequest, bytes], |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
24 None, |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
25 ] |
|
0
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
26 |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
27 |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
28 _CGI_VAR = re.compile(rb'[A-Z0-9_]+$') |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
29 """Environment variables that we need to pass to git-as-cgi.""" |
|
0
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
30 |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
31 |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
32 def _build_git_environ( |
|
0
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
33 req_ctx: web_inner.requestcontext, |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
34 request: hgreq.parsedrequest, |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
35 ) -> dict[bytes, bytes]: |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
36 """Builds the environment to be sent to Git to serve HTTP.""" |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
37 fixed = { |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
38 k: v |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
39 for (k, v) in request.rawenv.items() |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
40 if isinstance(v, bytes) and _CGI_VAR.match(k) |
|
0
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
41 } |
|
8
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
42 fixed.update( |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
43 { |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
44 b'GIT_HTTP_EXPORT_ALL': b'yes', |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
45 b'GIT_PROJECT_ROOT': req_ctx.repo.path, |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
46 b'PATH_INFO': b'/git/' + request.dispatchpath, |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
47 # Since Mercurial is taking care of authorization checking, |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
48 # we tell Git to always allow push. |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
49 b'GIT_CONFIG_COUNT': b'1', |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
50 b'GIT_CONFIG_KEY_0': b'http.receivepack', |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
51 b'GIT_CONFIG_VALUE_0': b'true', |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
52 } |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
53 ) |
|
0
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
54 return fixed |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
55 |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
56 |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
57 def _parse_cgi_response( |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
58 output: t.IO[bytes], |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
59 ) -> tuple[bytes, dict[bytes, bytes], t.IO[bytes]]: |
|
4
5ad58438318a
Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
60 """Parses a CGI response into a status, headers, and everyhting else.""" |
|
0
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
61 parser = email.parser.BytesFeedParser(policy=email.policy.HTTP) |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
62 while line := output.readline(): |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
63 if not line.rstrip(b'\r\n'): |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
64 # We've reached the end of the headers. |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
65 # Leave the rest in the output for later. |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
66 break |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
67 parser.feed(line) |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
68 msg = parser.close() |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
69 status = msg.get('Status', '200 OK I guess').encode('utf-8') |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
70 del msg['Status'] # this won't raise an exception |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
71 byte_headers = { |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
72 k.encode('utf-8'): v.encode('utf-8') for (k, v) in msg.items() |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
73 } |
|
0
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
74 return status, byte_headers, output |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
75 |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
76 |
|
8
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
77 def _git_service_permission(request: hgreq.parsedrequest) -> bytes | None: |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
78 """Figures out what Mercurial permission corresponds to a request from Git. |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
79 |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
80 If the request is a supported Git action, returns the permission it needs. |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
81 If the request is not a Git action, returns None. |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
82 """ |
|
12
f630d9904ea7
Reorganize project into multiple files.
Paul Fisher <paul@pfish.zone>
parents:
11
diff
changeset
|
83 if perm := xp.SERVICE_PERMISSIONS.get(request.dispatchpath): |
|
8
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
84 return perm |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
85 if request.dispatchpath != b'info/refs': |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
86 return None |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
87 qs = request.querystring |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
88 service = qs.removeprefix(b'service=') |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
89 if qs == service: |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
90 # Nothing was stripped. |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
91 return None |
|
12
f630d9904ea7
Reorganize project into multiple files.
Paul Fisher <paul@pfish.zone>
parents:
11
diff
changeset
|
92 return xp.SERVICE_PERMISSIONS.get(service) |
|
8
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
93 |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
94 |
|
4
5ad58438318a
Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
95 def _handle_git_protocol( |
|
0
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
96 original: t.Callable[..., bool], |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
97 req_ctx: web_inner.requestcontext, |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
98 request: hgreq.parsedrequest, |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
99 response: hgreq.wsgiresponse, |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
100 check_permission: PermissionCheck, |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
101 ) -> bool: |
|
4
5ad58438318a
Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
102 """Intercepts requests from Git, if needed.""" |
|
8
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
103 perm = _git_service_permission(request) |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
104 repo: hgrepo.IRepo = req_ctx.repo |
|
12
f630d9904ea7
Reorganize project into multiple files.
Paul Fisher <paul@pfish.zone>
parents:
11
diff
changeset
|
105 if not perm or not xp.is_gitty(repo): |
|
8
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
106 # We only handle Git requests to Gitty repos. |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
107 return original(req_ctx, request, response, check_permission) |
|
8
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
108 |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
109 # Permission workaround: Mercurial requires POSTs for push, |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
110 # but the advertisement request from Git will be a GET. |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
111 # We just lie to Mercurial about what we're doing. |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
112 check_permission( |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
113 req_ctx, |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
114 ( |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
115 attr.evolve(req_ctx.req, method=b'POST') |
|
12
f630d9904ea7
Reorganize project into multiple files.
Paul Fisher <paul@pfish.zone>
parents:
11
diff
changeset
|
116 if perm == xp.PUSH |
|
8
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
117 else req_ctx.req |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
118 ), |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
119 perm, |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
120 ) |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
121 cgi_env = _build_git_environ(req_ctx, request) |
|
8
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
122 http_backend = repo.ui.configlist( |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
123 b'hggit-serve', b'http-backend', default=(b'git', b'http-backend') |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
124 ) |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
125 call = subprocess.Popen( |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
126 http_backend, |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
127 close_fds=True, |
|
4
5ad58438318a
Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
128 stdin=subprocess.PIPE, |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
129 stdout=subprocess.PIPE, |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
130 stderr=subprocess.DEVNULL, |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
131 env=cgi_env, |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
132 text=False, |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
133 ) |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
134 assert call.stdout |
|
4
5ad58438318a
Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
135 assert call.stdin |
|
5ad58438318a
Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
136 # Git will not start writing output until stdin is fully closed. |
|
5ad58438318a
Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
137 with call.stdin: |
|
9
5000914da3ff
simplify handling of stream copying
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
138 # This is how we know if there's anything to read from bodyfh. |
|
5000914da3ff
simplify handling of stream copying
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
139 # If we try to read from bodyfh on a request with no content, |
|
5000914da3ff
simplify handling of stream copying
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
140 # it hangs forever. |
|
5000914da3ff
simplify handling of stream copying
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
141 if b'CONTENT_LENGTH' in request.rawenv: |
|
5000914da3ff
simplify handling of stream copying
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
142 shutil.copyfileobj(request.bodyfh, call.stdin) |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
143 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
144 status, headers, rest = _parse_cgi_response(call.stdout) |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
145 response.status = status |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
146 for k, v in headers.items(): |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
147 response.headers[k] = v |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
148 |
|
8
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
149 def write_the_rest() -> t.Iterator[bytes]: |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
150 with call, rest: |
|
9
5000914da3ff
simplify handling of stream copying
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
151 # if it's good enough for shutil it's good enough for me |
| 10 | 152 # technically not in the docs but everybody it |
| 153 bs = shutil.COPY_BUFSIZE # type: ignore[attr-defined] | |
| 154 while more := rest.read(bs): | |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
155 yield more |
|
12
f630d9904ea7
Reorganize project into multiple files.
Paul Fisher <paul@pfish.zone>
parents:
11
diff
changeset
|
156 if perm == xp.PUSH: |
|
f630d9904ea7
Reorganize project into multiple files.
Paul Fisher <paul@pfish.zone>
parents:
11
diff
changeset
|
157 # If we pushed, we need to import any new refs back into Mercurial. |
|
f630d9904ea7
Reorganize project into multiple files.
Paul Fisher <paul@pfish.zone>
parents:
11
diff
changeset
|
158 xp.importing_enter(repo) |
|
8
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
159 try: |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
160 gh = repo.githandler |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
161 gh.import_git_objects( |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
162 b'git-push', remote_names=(), refs=gh.git.refs.as_dict() |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
163 ) |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
164 finally: |
|
12
f630d9904ea7
Reorganize project into multiple files.
Paul Fisher <paul@pfish.zone>
parents:
11
diff
changeset
|
165 xp.importing_exit(repo) |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
166 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
167 response.setbodygen(write_the_rest()) |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
168 response.sendresponse() |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
169 return True |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
170 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
171 |
|
8
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
172 # |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
173 # Interfacing with Mercurial |
|
8
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
174 # |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
175 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
176 |
|
0
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
177 def uisetup(_: hgui.ui) -> None: |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
178 extensions.wrapfunction( |
|
4
5ad58438318a
Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
179 wireprotoserver, 'handlewsgirequest', _handle_git_protocol |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
180 ) |
