annotate src/git_serve/__init__.py @ 3:189f4a0bc653

don't try to pass bytes to pathlib.Path
author Paul Fisher <paul@pfish.zone>
date Sat, 14 Feb 2026 18:53:30 -0500
parents 871dcb2a2aeb
children 5ad58438318a
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
1
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
10 import threading
0
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
11 import typing as t
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
12
1
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
13 import dulwich.refs
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
14 import mercurial.error as hgerr
0
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
15 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
16 from mercurial import registrar
0
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
17 from mercurial import wireprotoserver
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
18
1
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
19 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
20 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
21 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
22 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
23 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
24 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
25
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
26 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
27 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
28
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
29 PermissionCheck = t.Callable[
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
30 [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
31 None,
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
32 ]
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
33 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
34
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
35
1
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
36 _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
37 """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
38
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
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 def _build_git_environ(
0
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
41 req_ctx: web_inner.requestcontext,
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
42 request: hgreq.parsedrequest,
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
43 ) -> dict[bytes, bytes]:
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
44 """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
45 fixed = {
1
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
46 k: v
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
47 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
48 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
49 }
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
50 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
51 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
52 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
53 return fixed
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
54
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
55
1
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
56 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
57 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
58 ) -> 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
59 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
60 while line := output.readline():
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
61 if not line.rstrip(b'\r\n'):
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
62 # 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
63 # 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
64 break
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
65 parser.feed(line)
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
66 msg = parser.close()
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
67 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
68 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
69 byte_headers = {
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
70 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
71 }
0
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
72 return status, byte_headers, output
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
73
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 _feeds = 0
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
76 _feeder_lock = threading.Lock()
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
77
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 def feed_count() -> int:
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
80 global _feeds
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
81 with _feeder_lock:
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
82 _feeds += 1
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
83 return _feeds
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
84
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
85
1
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
86 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
87 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
88
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
89
0
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
90 def handle_git_protocol(
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
91 original: t.Callable[..., bool],
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
92 req_ctx: web_inner.requestcontext,
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
93 request: hgreq.parsedrequest,
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
94 response: hgreq.wsgiresponse,
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
95 check_permission: PermissionCheck,
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
96 ) -> bool:
1
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
97 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
98 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
99 # 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
100 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
101 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
102 # 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
103 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
104 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
105 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
106 )
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
107 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
108 call = subprocess.Popen(
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
109 http_backend,
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
110 close_fds=True,
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
111 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
112 stdout=subprocess.PIPE,
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
113 stderr=subprocess.DEVNULL,
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
114 env=cgi_env,
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
115 text=False,
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
116 )
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
117 assert call.stdout
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
118
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
119 def feed():
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
120 try:
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
121 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
122 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
123 except (OSError, BrokenPipeError):
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
124 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
125
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
126 if is_post:
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
127 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
128 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
129 response.status = status
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
130 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
131 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
132
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
133 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
134 with call, rest:
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
135 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
136 yield more
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
137
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
138 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
139 response.sendresponse()
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
140 return True
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
141
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 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
144 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
145 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
146
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 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
149 """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
150 # 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
151 current = b'@'
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
152 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
153 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
154
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
155 # 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
156 git_current = current
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
157 if current == b'@':
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
158 # @ 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
159 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
160 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
161 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
162 # 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
163 return
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
164 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
165 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
166 # 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
167 # 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
168 try:
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
169 # 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
170 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
171 except KeyError:
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
172 # 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
173 branch = repo.dirstate.branch()
0
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
174 try:
1
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
175 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
176 except hgerr.RepoLookupError:
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
177 # 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
178 return
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
179 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
180 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
181 if not gitsha:
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
182 # 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
183 return
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
184 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
185 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
186
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 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
189 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
190 if never_export:
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
191 return
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
192 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
193 if always_export or os.path.isdir(repo.githandler.gitdir):
1
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
194 export_repo(repo)
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
195
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
196
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
197 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
198 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
199 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
200 set_head(repo)
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
201
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
202
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
203 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
204 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
205
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 # Interfacing with Mercurial
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
208
3
189f4a0bc653 don't try to pass bytes to pathlib.Path
Paul Fisher <paul@pfish.zone>
parents: 2
diff changeset
209 __version__ = '0.1.1'
1
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 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
212
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
213 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
214
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
215
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
216 @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
217 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
218 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
219 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
220 export_repo(repo)
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
221
0
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
222
c1dc9d21fa57 First cut at serving hg repos with git.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
223 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
224 extensions.wrapfunction(
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
225 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
226 )
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
227
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 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
230 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
231
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
232
a39dd69b8972 Create a more-or-less real package and make it work (?)
Paul Fisher <paul@pfish.zone>
parents: 0
diff changeset
233 __all__ = ('__version__', 'cmdtable', 'command', 'uisetup', 'reposetup')