Mercurial > hg-git-serve
annotate src/git_serve.py @ 1:a39dd69b8972
Create a more-or-less real package and make it work (?)
| author | Paul Fisher <paul@pfish.zone> |
|---|---|
| date | Sat, 14 Feb 2026 18:41:52 -0500 |
| parents | gitserve.py@c1dc9d21fa57 |
| 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 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
3 import binascii |
|
0
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
4 import email.parser |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
5 import email.policy |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
6 import os |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
7 import pathlib |
|
0
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
8 import re |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
9 import shutil |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
10 import subprocess |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
11 import threading |
|
0
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
12 import typing as t |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
13 |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
14 import dulwich.refs |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
15 import mercurial.error as hgerr |
|
0
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
16 from mercurial import extensions |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
17 from mercurial import registrar |
|
0
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
18 from mercurial import wireprotoserver |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
19 |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
20 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
|
21 import hggit.git_handler |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
22 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
|
23 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
|
24 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
|
25 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
|
26 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
27 class GittyRepo(hgrepo.IRepo, t.Protocol): |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
28 githandler: hggit.git_handler.GitHandler |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
29 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
30 PermissionCheck = t.Callable[ |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
31 [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
|
32 None, |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
33 ] |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
34 GitPrelude = t.Sequence[bytes | str | os.PathLike] |
|
0
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
35 |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
36 |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
37 _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
|
38 """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
|
39 |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
40 |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
41 def _build_git_environ( |
|
0
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
42 req_ctx: web_inner.requestcontext, |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
43 request: hgreq.parsedrequest, |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
44 ) -> dict[bytes, bytes]: |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
45 """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
|
46 fixed = { |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
47 k: v |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
48 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
|
49 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
|
50 } |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
51 fixed[b'GIT_HTTP_EXPORT_ALL'] = b'yes' |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
52 fixed[b'GIT_PROJECT_ROOT'] = req_ctx.repo.path |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
53 fixed[b'PATH_INFO'] = b'/git/' + request.dispatchpath |
|
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]]: |
|
0
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
60 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
|
61 while line := output.readline(): |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
62 if not line.rstrip(b'\r\n'): |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
63 # 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
|
64 # 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
|
65 break |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
66 parser.feed(line) |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
67 msg = parser.close() |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
68 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
|
69 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
|
70 byte_headers = { |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
71 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
|
72 } |
|
0
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
73 return status, byte_headers, output |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
74 |
|
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 _feeds = 0 |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
77 _feeder_lock = threading.Lock() |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
78 |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
79 |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
80 def feed_count() -> int: |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
81 global _feeds |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
82 with _feeder_lock: |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
83 _feeds += 1 |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
84 return _feeds |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
85 |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
86 |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
87 def git_binary(ui: hgui.ui) -> bytes: |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
88 return ui.config(b'git-serve', b'git', default=b'git') |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
89 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
90 |
|
0
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
91 def handle_git_protocol( |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
92 original: t.Callable[..., bool], |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
93 req_ctx: web_inner.requestcontext, |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
94 request: hgreq.parsedrequest, |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
95 response: hgreq.wsgiresponse, |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
96 check_permission: PermissionCheck, |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
97 ) -> bool: |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
98 repo = req_ctx.repo |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
99 if not is_gitty(repo) or b'git-protocol' not in request.headers: |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
100 # We only handle Git requests; everything else is normal. |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
101 return original(req_ctx, request, response, check_permission) |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
102 check_permission(req_ctx, request, b'pull') |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
103 # If a request is git, we assume we should be the one handling it. |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
104 cgi_env = _build_git_environ(req_ctx, request) |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
105 http_backend = req_ctx.repo.ui.configlist( |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
106 b'git-serve', b'http-backend', default=(b'git', b'http-backend') |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
107 ) |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
108 is_post = request.method == b'POST' |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
109 call = subprocess.Popen( |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
110 http_backend, |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
111 close_fds=True, |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
112 stdin=subprocess.PIPE if is_post else None, |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
113 stdout=subprocess.PIPE, |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
114 stderr=subprocess.DEVNULL, |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
115 env=cgi_env, |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
116 text=False, |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
117 ) |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
118 assert call.stdout |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
119 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
120 def feed(): |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
121 try: |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
122 with call.stdin as stdin: |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
123 shutil.copyfileobj(request.bodyfh, stdin) |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
124 except (OSError, BrokenPipeError): |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
125 pass # Expected; this just means it's closed. |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
126 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
127 if is_post: |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
128 threading.Thread(target=feed, name=f'git-feeder-{feed_count()}').start() |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
129 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
|
130 response.status = status |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
131 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
|
132 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
|
133 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
134 def write_the_rest(): |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
135 with call, rest: |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
136 while more := rest.read(64 * 1024): |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
137 yield more |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
138 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
139 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
|
140 response.sendresponse() |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
141 return True |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
142 |
|
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 def clean_all_refs(refs: dulwich.refs.RefsContainer) -> None: |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
145 for ref in refs.allkeys(): |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
146 refs.remove_if_equals(ref, None) |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
147 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
148 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
149 def set_head(repo: GittyRepo) -> None: |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
150 """Creates a HEAD reference in Git referring to the current HEAD.""" |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
151 # By default, we use '@', since that's what will be auto checked out. |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
152 current = b'@' |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
153 if current not in repo._bookmarks: |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
154 current = repo._bookmarks.active or current |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
155 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
156 # We'll be moving this (possibly fake) bookmark into Git. |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
157 git_current = current |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
158 if current == b'@': |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
159 # @ is a special keyword in Git, so we can't use it as a bookmark. |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
160 git_current = b'__default__' |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
161 git_branch = dulwich.refs.LOCAL_BRANCH_PREFIX + git_current |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
162 if not dulwich.refs.check_ref_format(git_branch): |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
163 # We can't export this ref to Git. Give up. |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
164 return |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
165 refs = repo.githandler.git.refs |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
166 if git_branch not in refs: |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
167 # This means our bookmark isn't actually in Git (usually because |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
168 # there's no real bookmark called '@'). We need to fake it. |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
169 try: |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
170 # Maybe this is a real bookmark? |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
171 hgsha = repo._bookmarks[current] |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
172 except KeyError: |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
173 # Not a real bookmark. Assume we want the tip of the current branch. |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
174 branch = repo.dirstate.branch() |
|
0
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
175 try: |
|
1
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
176 tip = repo.branchtip(branch) |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
177 except hgerr.RepoLookupError: |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
178 # This branch somehow doesn't exist??? |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
179 return |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
180 hgsha = binascii.hexlify(tip) |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
181 gitsha = repo.githandler.map_git_get(hgsha) |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
182 if not gitsha: |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
183 # No Git SHA to match this Hg sha. Give up. |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
184 return |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
185 refs.add_packed_refs({git_branch: gitsha}) |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
186 refs.set_symbolic_ref(b'HEAD', git_branch) |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
187 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
188 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
189 def export_hook(ui: hgui.ui, repo: GittyRepo, **__: object) -> None: |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
190 never_export = ui.configbool(b'git-serve', b'never-export') |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
191 if never_export: |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
192 return |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
193 git_repo = pathlib.Path(repo.githandler.gitdir) |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
194 always_export = ui.configbool(b'git-serve', b'always-export', False) |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
195 if always_export or git_repo.exists(): |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
196 export_repo(repo) |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
197 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
198 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
199 def export_repo(repo: GittyRepo) -> None: |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
200 clean_all_refs(repo.githandler.git.refs) |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
201 repo.githandler.export_commits() |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
202 set_head(repo) |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
203 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
204 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
205 def is_gitty(repo: hgrepo.IRepo) -> t.TypeGuard[GittyRepo]: |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
206 return hasattr(repo, 'githandler') |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
207 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
208 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
209 # Interfacing with Mercurial |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
210 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
211 __version__ = '0.1' |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
212 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
213 cmdtable: dict[bytes, object] = {} |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
214 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
215 command = registrar.command(cmdtable) |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
216 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
217 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
218 @command(b'git-serve-export') |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
219 def git_serve_export(_: hgui.ui, repo: hgrepo.IRepo, **__: object) -> None: |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
220 if not is_gitty(repo): |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
221 raise hgerr.Abort(b'this extension depends on the `hggit` extension') |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
222 export_repo(repo) |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
223 |
|
0
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
224 |
|
c1dc9d21fa57
First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
225 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
|
226 extensions.wrapfunction( |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
227 wireprotoserver, 'handlewsgirequest', handle_git_protocol |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
228 ) |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
229 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
230 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
231 def reposetup(ui: hgui.ui, _: hgrepo.IRepo) -> None: |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
232 ui.setconfig(b'hooks', b'txnclose.__gitserve_internal__', export_hook) |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
233 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
234 |
|
a39dd69b8972
Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents:
0
diff
changeset
|
235 __all__ = ('__version__', 'cmdtable', 'command', 'uisetup', 'reposetup') |
