1
"""gitd — smart-HTTP git server plus the AIMB-X web UI.
3
Protocol surface (exactly what git clients expect):
4
GET /<owner>/<repo>.git/info/refs?service=git-upload-pack|git-receive-pack
5
POST /<owner>/<repo>.git/git-upload-pack
6
POST /<owner>/<repo>.git/git-receive-pack
8
Auth is HTTP Basic: any username, password = an `ag_usr_…` token (Bearer works for the
9
web UI and API). Public repositories are readable anonymously; writes always need the
10
owner's token. A repository is created on first push.
12
from __future__ import annotations
25
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
26
from pathlib import Path
27
from urllib.parse import parse_qs, quote, unquote, urlparse
30
from .store import Store, dumps
32
GIT = shutil.which("git") or "/usr/bin/git"
33
DATA = Path(os.environ.get("GITD_DATA", "/var/lib/gitd")).resolve()
35
DB = Path(os.environ.get("GITD_DB", str(DATA / "gitd.kilat")))
36
ADMIN_TOKEN = os.environ.get("GITD_ADMIN_TOKEN", "")
37
DEFAULT_VIS = os.environ.get("GITD_DEFAULT_VISIBILITY", "private")
38
MAX_BODY = int(os.environ.get("GITD_MAX_BODY", str(512 * 1024 * 1024)))
39
NAME_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$")
41
_store: Store | None = None
42
_lock = threading.Lock()
49
DATA.mkdir(parents=True, exist_ok=True)
50
REPOS.mkdir(parents=True, exist_ok=True)
55
def repo_dir(owner: str, name: str) -> Path:
56
return REPOS / owner / f"{name}.git"
59
def git(repo: Path, *args: str, timeout: int = 120) -> str:
60
r = subprocess.run([GIT, "-C", str(repo), *args], capture_output=True, text=True, timeout=timeout)
62
raise RuntimeError(r.stderr.strip()[:300] or "git failed")
66
def pkt(s: bytes) -> bytes:
67
return b"%04x" % (len(s) + 4) + s
70
class Handler(BaseHTTPRequestHandler):
71
server_version = "gitd/0.1"
72
protocol_version = "HTTP/1.1"
74
# ---------- helpers ----------
75
def _send(self, code: int, body: bytes, ctype: str = "text/plain; charset=utf-8",
76
extra: dict | None = None) -> None:
77
self.send_response(code)
78
self.send_header("Content-Type", ctype)
79
self.send_header("Content-Length", str(len(body)))
80
self.send_header("Cache-Control", "no-cache, max-age=0, must-revalidate")
81
for k, v in (extra or {}).items():
82
self.send_header(k, v)
84
if self.command != "HEAD":
85
self.wfile.write(body)
87
def _html(self, code: int, page: str) -> None:
88
self._send(code, page.encode(), "text/html; charset=utf-8")
90
def _json(self, code: int, obj) -> None:
91
self._send(code, json.dumps(obj).encode(), "application/json; charset=utf-8")
93
def _auth(self) -> dict | None:
94
h = self.headers.get("Authorization", "")
96
if h.startswith("Basic "):
98
raw = base64.b64decode(h[6:]).decode("utf-8", "replace")
99
token = raw.split(":", 1)[1] if ":" in raw else raw
100
except (binascii.Error, ValueError):
102
elif h.startswith("Bearer "):
103
token = h[7:].strip()
105
q = parse_qs(urlparse(self.path).query)
106
token = (q.get("token") or [""])[0]
107
return store().user_by_token(token) if token else None
109
def _is_admin(self) -> bool:
110
h = self.headers.get("Authorization", "")
112
if h.startswith("Bearer "):
113
token = h[7:].strip()
114
q = parse_qs(urlparse(self.path).query)
115
token = token or (q.get("token") or [""])[0]
116
return bool(ADMIN_TOKEN) and token == ADMIN_TOKEN
118
def _base_url(self) -> str:
119
host = self.headers.get("X-Forwarded-Host") or self.headers.get("Host") or "localhost"
120
proto = self.headers.get("X-Forwarded-Proto") or ("http" if host.startswith("127.") else "https")
121
return f"{proto}://{host}"
123
def _can_read(self, repo: dict | None, user: dict | None) -> bool:
126
if repo["visibility"] == "public":
130
return bool(user) and user["name"] == repo["owner"]
132
def _can_write(self, owner: str, user: dict | None) -> bool:
135
return bool(user) and user["name"] == owner
137
def _read_body(self) -> bytes:
138
te = (self.headers.get("Transfer-Encoding") or "").lower()
142
line = self.rfile.readline().strip()
146
size = int(line.split(b";")[0], 16)
148
raise ValueError("bad chunk")
150
self.rfile.readline()
152
out += self.rfile.read(size)
153
self.rfile.readline()
155
n = int(self.headers.get("Content-Length") or 0)
157
raise ValueError("body too large")
158
return self.rfile.read(n) if n else b""
160
def log_message(self, fmt: str, *args) -> None:
161
sys.stdout.write("%s %s\n" % (time.strftime("%Y-%m-%dT%H:%M:%S"), fmt % args))
163
# ---------- routes ----------
164
def do_HEAD(self) -> None: # noqa: N802
167
def do_GET(self) -> None: # noqa: N802
168
u = urlparse(self.path)
169
q = parse_qs(u.query)
173
if path == "/healthz":
175
return self._json(200, {"ok": True, "service": "gitd", "repos": st["repos"],
176
"users": st["users"], "engine": store().k.stats()})
177
if path == "/static/app.css":
178
return self._send(200, web.CSS.encode(), "text/css; charset=utf-8")
180
m = re.match(r"^/([^/]+)/([^/]+?)\.git/info/refs$", path)
182
return self._advertise(m.group(1), m.group(2), (q.get("service") or [""])[0], user)
184
if path in ("/", "/repos"):
186
for r in store().repos():
187
if not self._can_read(r, user):
189
d = repo_dir(r["owner"], r["name"])
193
r["branches"] = len(git(d, "for-each-ref", "--format=%(refname)", "refs/heads")
195
r["last_commit"] = git(d, "log", "-1", "--format=%s", "--all").strip()[:60]
199
return self._html(200, web.page_repos(repos, store().stats(),
200
user["name"] if user else None))
203
return self._html(200, web.page_about())
205
m = re.match(r"^/u/([^/]+)$", path)
207
owner = unquote(m.group(1))
208
repos = [r for r in store().repos() if r["owner"] == owner and self._can_read(r, user)]
209
st = {"users": store().stats()["users"], "repos": len(repos)}
210
return self._html(200, web.page_repos(repos, st, owner))
212
m = re.match(r"^/r/([^/]+)/([^/]+)$", path)
214
return self._repo_page(unquote(m.group(1)), unquote(m.group(2)),
215
(q.get("ref") or [""])[0], (q.get("path") or [""])[0], user)
217
return self._html(404, web.page_404())
219
def do_POST(self) -> None: # noqa: N802
220
path = urlparse(self.path).path
221
if path == "/admin/repo":
222
return self._admin_repo()
223
m = re.match(r"^/([^/]+)/([^/]+?)\.git/(git-upload-pack|git-receive-pack)$", path)
225
return self._json(404, {"error": "not found"})
226
return self._rpc(m.group(1), m.group(2), m.group(3))
228
def _admin_repo(self) -> None:
229
"""Metadata writes must happen inside this process: kilat is single-writer."""
230
if not self._is_admin():
231
return self._json(403, {"error": "admin token required"})
233
body = json.loads(self._read_body() or b"{}")
234
owner, name = body["owner"], body["name"]
235
except Exception as e:
236
return self._json(400, {"error": f"bad request: {e}"})
237
row = store().add_repo(owner, name, body.get("visibility") or
238
(store().get_repo(owner, name) or {}).get("visibility", DEFAULT_VIS),
239
body.get("description", ""))
242
# ---------- git smart HTTP ----------
243
def _advertise(self, owner: str, name: str, service: str, user: dict | None) -> None:
244
if not NAME_RE.match(owner) or not NAME_RE.match(name):
245
return self._send(400, b"bad repository name")
246
if service not in ("git-upload-pack", "git-receive-pack"):
247
return self._send(400, b"bad service")
248
repo = store().get_repo(owner, name)
249
d = repo_dir(owner, name)
251
if service == "git-receive-pack":
252
if not self._can_write(owner, user):
253
return self._send(401, b"authentication required\n",
254
extra={"WWW-Authenticate": 'Basic realm="gitd"'})
256
# create-on-first-push, like a git host that has no web form
257
d.mkdir(parents=True, exist_ok=True)
258
subprocess.run([GIT, "init", "--bare", "--quiet", str(d)], check=True)
259
if not store().get_repo(owner, name): # jangan timpa visibility yang sudah diatur
260
store().add_repo(owner, name, DEFAULT_VIS)
262
if not self._can_read(repo, user):
263
return self._send(401, b"authentication required\n",
264
extra={"WWW-Authenticate": 'Basic realm="gitd"'})
266
return self._send(404, b"repository not found\n")
268
env = os.environ.copy()
269
proto = self.headers.get("Git-Protocol")
271
env["GIT_PROTOCOL"] = proto
273
env["REMOTE_USER"] = user["name"]
274
sub = service[4:] if service.startswith("git-") else service # "git receive-pack"
275
out = subprocess.run([GIT, sub, "--stateless-rpc", "--advertise-refs", str(d)],
276
capture_output=True, env=env, timeout=120)
277
if out.returncode != 0:
278
return self._send(500, out.stderr[:400])
279
body = pkt(f"# service={service}\n".encode()) + b"0000" + out.stdout
280
self._send(200, body, f"application/x-{service}-advertisement")
282
def _rpc(self, owner: str, name: str, service: str) -> None:
284
d = repo_dir(owner, name)
286
return self._send(404, b"repository not found\n")
287
if service == "git-receive-pack" and not self._can_write(owner, user):
288
return self._send(401, b"authentication required\n",
289
extra={"WWW-Authenticate": 'Basic realm="gitd"'})
290
if service == "git-upload-pack" and not self._can_read(store().get_repo(owner, name), user):
291
return self._send(401, b"authentication required\n",
292
extra={"WWW-Authenticate": 'Basic realm="gitd"'})
294
body = self._read_body()
295
except ValueError as e:
296
return self._send(413, str(e).encode())
298
env = os.environ.copy()
299
proto = self.headers.get("Git-Protocol")
301
env["GIT_PROTOCOL"] = proto
303
env["REMOTE_USER"] = user["name"]
304
sub = service[4:] if service.startswith("git-") else service
305
out = subprocess.run([GIT, sub, "--stateless-rpc", str(d)], input=body,
306
capture_output=True, env=env, timeout=600)
307
if out.returncode != 0:
308
self.log_message("git %s failed: %s", service, out.stderr.decode("utf-8", "replace")[:300])
309
return self._send(500, out.stderr[:400])
310
if service == "git-receive-pack":
312
repo = store().get_repo(owner, name) or {}
313
repo["last_push"] = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
314
store().k.put("repos", f"{owner}/{name}", dumps(repo))
315
self._send(200, out.stdout, f"application/x-{service}-result")
317
# ---------- web repo page ----------
318
def _repo_page(self, owner: str, name: str, ref: str, path: str, user: dict | None) -> None:
319
repo = store().get_repo(owner, name)
320
d = repo_dir(owner, name)
321
if not repo or not d.exists() or not self._can_read(repo, user):
322
return self._html(404, web.page_404())
325
for line in git(d, "for-each-ref", "--format=%(refname:short)", "refs/heads").splitlines():
328
sha = git(d, "rev-parse", "--short", line).strip()
329
subj = git(d, "log", "-1", "--format=%s", line).strip()[:60]
330
branches.append({"name": line, "sha": sha, "subject": subj})
331
ref = ref or (branches[0]["name"] if branches else "")
334
for line in git(d, "log", "-12", "--format=%h%x09%an%x09%ad%x09%s",
335
"--date=iso-strict", ref).splitlines():
336
sha, author, date, subj = (line.split("\t") + ["", "", "", ""])[:4]
337
commits.append({"sha": sha, "author": author, "date": date, "subject": subj})
338
tree, blob = [], None
340
target = f"{ref}:{path}" if path else ref
341
kind = subprocess.run([GIT, "-C", str(d), "cat-file", "-t", target],
342
capture_output=True, text=True).stdout.strip()
343
if path and kind == "blob":
344
raw = subprocess.run([GIT, "-C", str(d), "cat-file", "-p", target],
345
capture_output=True, timeout=30)
346
if raw.returncode == 0 and len(raw.stdout) < 400_000 and b"\x00" not in raw.stdout[:2048]:
347
blob = {"path": path, "bytes": len(raw.stdout),
348
"text": raw.stdout.decode("utf-8", "replace")}
349
parent = path.rsplit("/", 1)[0] if "/" in path else ""
350
lsref = f"{ref}:{parent}" if parent else ref
351
for line in git(d, "ls-tree", "-l", lsref).splitlines():
352
meta, fname = line.split("\t", 1)
353
mode, typ, obj, size = meta.split()[:4]
354
tree.append({"path": f"{parent}/{fname}" if parent else fname,
355
"type": "tree" if typ == "tree" else "blob",
356
"size_h": "" if typ == "tree" else _human(int(size or 0))})
357
elif kind == "tree" or not path:
358
for line in git(d, "ls-tree", "-l", target).splitlines():
359
meta, fname = line.split("\t", 1)
360
mode, typ, obj, size = meta.split()[:4]
361
tree.append({"path": f"{path}/{fname}" if path else fname,
362
"type": "tree" if typ == "tree" else "blob",
363
"size_h": "" if typ == "tree" else _human(int(size or 0))})
365
return self._html(500, web.page_404())
366
clone = f"{self._base_url()}/{quote(owner)}/{quote(name)}.git"
367
self._html(200, web.page_repo(repo, branches, commits, tree, ref, path, blob, clone))
370
def _fix_head(d: Path) -> None:
371
"""Setelah push, HEAD harus menunjuk branch yang benar-benar ada; kalau tidak,
372
`git clone` berhasil tetapi checkout kosong (bare repo default ke master)."""
374
branches = [b for b in git(d, "for-each-ref", "--format=%(refname)", "refs/heads").splitlines() if b]
377
cur = subprocess.run([GIT, "-C", str(d), "symbolic-ref", "HEAD"],
378
capture_output=True, text=True).stdout.strip()
379
if cur not in branches:
381
subprocess.run([GIT, "-C", str(d), "symbolic-ref", "HEAD", branches[0]], check=True)
386
def _human(n: int) -> str:
387
for unit in ("B", "KB", "MB"):
389
return f"{n:.0f} {unit}" if unit == "B" else f"{n:.1f} {unit}"
394
def serve(host: str, port: int) -> None:
396
httpd = ThreadingHTTPServer((host, port), Handler)
397
httpd.daemon_threads = True
398
print(f"gitd listening on http://{host}:{port} · repos {REPOS}")
400
httpd.serve_forever()
401
except KeyboardInterrupt:
407
def main(argv=None) -> int:
408
ap = argparse.ArgumentParser(prog="gitd-server")
409
ap.add_argument("--host", default=os.environ.get("GITD_HOST", "127.0.0.1"))
410
ap.add_argument("--port", type=int, default=int(os.environ.get("GITD_PORT", "8189")))
411
a = ap.parse_args(argv)
412
serve(a.host, a.port)
416
if __name__ == "__main__":
417
raise SystemExit(main())