annotate src/git_serve/__init__.py @ 5:c43ce246240b

Really fix content-length for real for serious.
author Paul Fisher <paul@pfish.zone>
date Sat, 14 Feb 2026 21:39:54 -0500
parents 5ad58438318a
children 7113e0ac3662
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
3
189f4a0bc653 don't try to pass bytes to pathlib.Path
Paul Fisher <paul@pfish.zone>
parents: 2
diff changeset
6 import os.path
0
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
7 import re
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
8 import shutil
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
9 import subprocess
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
10 import typing as t
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
11
1
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
12 import dulwich.refs
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
13 import mercurial.error as hgerr
0
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
14 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
15 from mercurial import registrar
0
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
16 from mercurial import wireprotoserver
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
17
1
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
18 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
19 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
20 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
21 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
22 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
23 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
24
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
25 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
26 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
27
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
28 PermissionCheck = t.Callable[
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
29 [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
30 None,
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
31 ]
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
32 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
33
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
34
4
5ad58438318a Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
35 def _is_gitty(repo: hgrepo.IRepo) -> t.TypeGuard[GittyRepo]:
5ad58438318a Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
36 """Ensures that we have hg-git installed and active."""
5ad58438318a Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
37 return hasattr(repo, 'githandler')
5ad58438318a Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
38
5ad58438318a Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
39
1
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
40 _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
41 """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
42
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
43
1
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
44 def _build_git_environ(
0
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
45 req_ctx: web_inner.requestcontext,
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
46 request: hgreq.parsedrequest,
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
47 ) -> dict[bytes, bytes]:
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
48 """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
49 fixed = {
1
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
50 k: v
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
51 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
52 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
53 }
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
54 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
55 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
56 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
57 return fixed
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
58
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
59
1
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
60 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
61 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
62 ) -> 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
63 """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
64 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
65 while line := output.readline():
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
66 if not line.rstrip(b'\r\n'):
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
67 # 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
68 # 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
69 break
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
70 parser.feed(line)
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
71 msg = parser.close()
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
72 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
73 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
74 byte_headers = {
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
75 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
76 }
0
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
77 return status, byte_headers, output
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
4
5ad58438318a Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
80 def _handle_git_protocol(
0
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
81 original: t.Callable[..., bool],
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
82 req_ctx: web_inner.requestcontext,
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
83 request: hgreq.parsedrequest,
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
84 response: hgreq.wsgiresponse,
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
85 check_permission: PermissionCheck,
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
86 ) -> bool:
4
5ad58438318a Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
87 """Intercepts requests from Git, if needed."""
1
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
88 repo = req_ctx.repo
4
5ad58438318a Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
89 if not _is_gitty(repo) or b'git-protocol' not in request.headers:
1
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
90 # 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
91 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
92 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
93 # 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
94 cgi_env = _build_git_environ(req_ctx, request)
5
c43ce246240b Really fix content-length for real for serious.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
95 content_length_hdr = request.headers.get(b'content-length', b'0')
c43ce246240b Really fix content-length for real for serious.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
96 try:
c43ce246240b Really fix content-length for real for serious.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
97 content_length = int(content_length_hdr)
c43ce246240b Really fix content-length for real for serious.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
98 except ValueError as ve:
c43ce246240b Really fix content-length for real for serious.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
99 raise hgerr.InputError(
c43ce246240b Really fix content-length for real for serious.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
100 f'Invalid content-length {content_length!r}'.encode()
c43ce246240b Really fix content-length for real for serious.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
101 ) from ve
1
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
102 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
103 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
104 )
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
105 call = subprocess.Popen(
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
106 http_backend,
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
107 close_fds=True,
4
5ad58438318a Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
108 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
109 stdout=subprocess.PIPE,
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
110 stderr=subprocess.DEVNULL,
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
111 env=cgi_env,
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
112 text=False,
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
113 )
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
114 assert call.stdout
4
5ad58438318a Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
115 assert call.stdin
5ad58438318a Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
116 # 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
117 with call.stdin:
5
c43ce246240b Really fix content-length for real for serious.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
118 if content_length:
c43ce246240b Really fix content-length for real for serious.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
119 shutil.copyfileobj(
c43ce246240b Really fix content-length for real for serious.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
120 request.bodyfh, call.stdin, length=content_length
c43ce246240b Really fix content-length for real for serious.
Paul Fisher <paul@pfish.zone>
parents: 4
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
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
123 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
124 response.status = status
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
125 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
126 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
127
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
128 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
129 with call, rest:
4
5ad58438318a Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
130 while more := rest.read(1024 * 1024):
1
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
131 yield more
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
132
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
133 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
134 response.sendresponse()
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
135 return True
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
136
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
137
4
5ad58438318a Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
138 def _clean_all_refs(refs: dulwich.refs.RefsContainer) -> None:
5ad58438318a Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
139 """Removes all refs from the Git repository."""
1
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
140 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
141 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
142
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
143
4
5ad58438318a Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
144 def _set_head(repo: GittyRepo) -> None:
1
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
145 """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
146 # 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
147 current = b'@'
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
148 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
149 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
150
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
151 # 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
152 git_current = current
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
153 if current == b'@':
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
154 # @ 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
155 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
156 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
157 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
158 # 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
159 return
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
160 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
161 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
162 # 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
163 # 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
164 try:
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
165 # 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
166 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
167 except KeyError:
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
168 # 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
169 branch = repo.dirstate.branch()
0
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
170 try:
1
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
171 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
172 except hgerr.RepoLookupError:
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
173 # 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
174 return
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
175 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
176 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
177 if not gitsha:
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
178 # 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
179 return
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
180 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
181 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
182
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
183
4
5ad58438318a Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
184 def _export_hook(ui: hgui.ui, repo: GittyRepo, **__: object) -> None:
5ad58438318a Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
185 """Exports to Git and sets up for serving."""
1
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
186 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
187 if never_export:
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
188 return
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
189 always_export = ui.configbool(b'git-serve', b'always-export', False)
3
189f4a0bc653 don't try to pass bytes to pathlib.Path
Paul Fisher <paul@pfish.zone>
parents: 2
diff changeset
190 if always_export or os.path.isdir(repo.githandler.gitdir):
4
5ad58438318a Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
191 _export_repo(repo)
1
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
192
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
193
4
5ad58438318a Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
194 def _export_repo(repo: GittyRepo) -> None:
5ad58438318a Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
195 """Do the actual exporting."""
5ad58438318a Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
196 _clean_all_refs(repo.githandler.git.refs)
1
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
197 repo.githandler.export_commits()
4
5ad58438318a Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
198 _set_head(repo)
1
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
199
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
200
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
201 # Interfacing with Mercurial
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
202
5
c43ce246240b Really fix content-length for real for serious.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
203 __version__ = '0.1.3'
1
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 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
206
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
207 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
208
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
209
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
210 @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
211 def git_serve_export(_: hgui.ui, repo: hgrepo.IRepo, **__: object) -> None:
4
5ad58438318a Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
212 if not _is_gitty(repo):
1
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
213 raise hgerr.Abort(b'this extension depends on the `hggit` extension')
4
5ad58438318a Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
214 _export_repo(repo)
1
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
215
0
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
216
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
217 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
218 extensions.wrapfunction(
4
5ad58438318a Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
219 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
220 )
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
221
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
222
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
223 def reposetup(ui: hgui.ui, _: hgrepo.IRepo) -> None:
4
5ad58438318a Fix issue where sometimes reads would be cut off.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
224 ui.setconfig(b'hooks', b'txnclose.__gitserve_internal__', _export_hook)
1
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
225
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
226
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
227 __all__ = ('__version__', 'cmdtable', 'command', 'uisetup', 'reposetup')