Mercurial > hg-git-serve
annotate src/hgext3rd/hggit_serve/_http.py @ 17:a70f387ab3cd default tip
Fix formatting in documentation.
| author | Paul Fisher <paul@pfish.zone> |
|---|---|
| date | Fri, 20 Feb 2026 21:12:40 -0500 |
| parents | 00bdfac5416c |
| children |
| 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.ui as hgui |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
20 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
21 PermissionCheck = t.Callable[ |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
22 [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
|
23 None, |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
24 ] |
|
0
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
25 |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
26 |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
27 _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
|
28 """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
|
29 |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
30 |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
31 def _build_git_environ( |
|
0
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
32 req_ctx: web_inner.requestcontext, |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
33 request: hgreq.parsedrequest, |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
34 ) -> dict[bytes, bytes]: |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
35 """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
|
36 fixed = { |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
37 k: v |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
38 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
|
39 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
|
40 } |
|
13
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
12
diff
changeset
|
41 repo = req_ctx.repo |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
12
diff
changeset
|
42 assert xp.is_gitty(repo) |
|
8
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
43 fixed.update( |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
44 { |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
45 b'GIT_HTTP_EXPORT_ALL': b'yes', |
|
13
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
12
diff
changeset
|
46 b'GIT_PROJECT_ROOT': repo.githandler.gitdir, |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
12
diff
changeset
|
47 b'PATH_INFO': b'/' + request.dispatchpath, |
|
8
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
48 # 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
|
49 # we tell Git to always allow push. |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
50 b'GIT_CONFIG_COUNT': b'1', |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
51 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
|
52 b'GIT_CONFIG_VALUE_0': b'true', |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
53 } |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
54 ) |
|
0
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
55 return fixed |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
56 |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
57 |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
58 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
|
59 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
|
60 ) -> 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
|
61 """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
|
62 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
|
63 while line := output.readline(): |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
64 if not line.rstrip(b'\r\n'): |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
65 # 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
|
66 # 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
|
67 break |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
68 parser.feed(line) |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
69 msg = parser.close() |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
70 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
|
71 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
|
72 byte_headers = { |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
73 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
|
74 } |
|
0
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
75 return status, byte_headers, output |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
76 |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
77 |
|
8
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
78 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
|
79 """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
|
80 |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
81 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
|
82 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
|
83 """ |
|
12
f630d9904ea7
Reorganize project into multiple files.
Paul Fisher <paul@pfish.zone>
parents:
11
diff
changeset
|
84 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
|
85 return perm |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
86 if request.dispatchpath != b'info/refs': |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
87 return None |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
88 qs = request.querystring |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
89 service = qs.removeprefix(b'service=') |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
90 if qs == service: |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
91 # Nothing was stripped. |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
92 return None |
|
12
f630d9904ea7
Reorganize project into multiple files.
Paul Fisher <paul@pfish.zone>
parents:
11
diff
changeset
|
93 return xp.SERVICE_PERMISSIONS.get(service) |
|
8
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
94 |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
95 |
|
4
5ad58438318a
Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
96 def _handle_git_protocol( |
|
0
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
97 original: t.Callable[..., bool], |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
98 req_ctx: web_inner.requestcontext, |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
99 request: hgreq.parsedrequest, |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
100 response: hgreq.wsgiresponse, |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
101 check_permission: PermissionCheck, |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
102 ) -> bool: |
|
4
5ad58438318a
Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
103 """Intercepts requests from Git, if needed.""" |
|
8
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
104 perm = _git_service_permission(request) |
|
13
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
12
diff
changeset
|
105 repo = req_ctx.repo |
|
12
f630d9904ea7
Reorganize project into multiple files.
Paul Fisher <paul@pfish.zone>
parents:
11
diff
changeset
|
106 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
|
107 # 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
|
108 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
|
109 |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
110 # Permission workaround: Mercurial requires POSTs for push, |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
111 # 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
|
112 # 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
|
113 check_permission( |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
114 req_ctx, |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
115 ( |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
116 attr.evolve(req_ctx.req, method=b'POST') |
|
12
f630d9904ea7
Reorganize project into multiple files.
Paul Fisher <paul@pfish.zone>
parents:
11
diff
changeset
|
117 if perm == xp.PUSH |
|
8
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
118 else req_ctx.req |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
119 ), |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
120 perm, |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
121 ) |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
122 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
|
123 http_backend = repo.ui.configlist( |
|
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
124 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
|
125 ) |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
126 call = subprocess.Popen( |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
127 http_backend, |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
128 close_fds=True, |
|
4
5ad58438318a
Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
129 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
|
130 stdout=subprocess.PIPE, |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
131 stderr=subprocess.DEVNULL, |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
132 env=cgi_env, |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
133 text=False, |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
134 ) |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
135 assert call.stdout |
|
4
5ad58438318a
Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
136 assert call.stdin |
|
5ad58438318a
Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
137 # 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
|
138 with call.stdin: |
|
9
5000914da3ff
simplify handling of stream copying
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
139 # 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
|
140 # 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
|
141 # it hangs forever. |
|
5000914da3ff
simplify handling of stream copying
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
142 if b'CONTENT_LENGTH' in request.rawenv: |
|
5000914da3ff
simplify handling of stream copying
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
143 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
|
144 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
145 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
|
146 response.status = status |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
147 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
|
148 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
|
149 |
|
8
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
150 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
|
151 with call, rest: |
|
9
5000914da3ff
simplify handling of stream copying
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
152 # if it's good enough for shutil it's good enough for me |
| 10 | 153 # technically not in the docs but everybody it |
| 154 bs = shutil.COPY_BUFSIZE # type: ignore[attr-defined] | |
| 155 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
|
156 yield more |
|
12
f630d9904ea7
Reorganize project into multiple files.
Paul Fisher <paul@pfish.zone>
parents:
11
diff
changeset
|
157 if perm == xp.PUSH: |
|
f630d9904ea7
Reorganize project into multiple files.
Paul Fisher <paul@pfish.zone>
parents:
11
diff
changeset
|
158 # If we pushed, we need to import any new refs back into Mercurial. |
|
13
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
12
diff
changeset
|
159 xp.import_all(repo, b'git-http-push') |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
160 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
161 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
|
162 response.sendresponse() |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
163 return True |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
164 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
165 |
|
8
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
166 # |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
167 # Interfacing with Mercurial |
|
8
fe3c9fae4d4d
Add support for pushes, and improve authentication.
Paul Fisher <paul@pfish.zone>
parents:
7
diff
changeset
|
168 # |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
169 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
170 |
|
0
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
171 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
|
172 extensions.wrapfunction( |
|
4
5ad58438318a
Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
173 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
|
174 ) |
