main
1// Real git as the oracle: `git http-backend` behind a tiny Node HTTP→CGI shim.
2import http from 'node:http';
3import { spawn, spawnSync, execFileSync } from 'node:child_process';
4import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
5import { tmpdir } from 'node:os';
6import { join } from 'node:path';
8export const git = (cwd, args, opts = {}) => execFileSync('git', args, { cwd, encoding: 'utf8', stdio: ['pipe', 'pipe', 'pipe'], ...opts });
9export const gitBuf = (cwd, args, input) => execFileSync('git', args, { cwd, input, stdio: ['pipe', 'pipe', 'pipe'], maxBuffer: 1 << 28 });
11// ---- git fsck as the oracle: the one definition every suite uses (self-tested in test/fsck-oracle.test.js) ----
12// git 2.50 writes fsck's verdict in these shapes, on two streams; an object problem never starts with "error:":
13// stderr `error in <type> <oid>: <msgId>: <text>` exit 1 (--strict turns WARN ids into errors)
14// `warning in <type> <oid>: <msgId>: <text>` exit 0 (INFO ids stay warnings even under --strict:
15// badFilemode, missingTaggerEntry, gitmodulesParse)
16// `error: <text>`, `fatal: <text>` (a damaged object file, a bad argument)
17// stdout `broken link from <type> <oid>` + an indented `to <type> <oid>` line, `missing <type> <oid>` exit 2
18// `dangling <type> <oid>` exit 0 (silenced by --no-dangling)
19// `notice:` lines (unborn HEAD, no refs) are the only output that says nothing about an object. A continuation line
20// (leading whitespace) is folded into the line before it. --connectivity-only skips every format check (a
21// duplicate-entry tree passes it with exit 0), so it is refused here.
22export function fsck(cwd, args = []) {
23 if (args.includes('--connectivity-only')) throw new Error('fsck --connectivity-only checks no object format; it is not an oracle');
24 const r = spawnSync('git', ['fsck', '--strict', ...args], { cwd, encoding: 'utf8', maxBuffer: 1 << 28 });
25 if (r.error) throw r.error;
26 const lines = [];
27 for (const l of `${r.stdout}\n${r.stderr}`.split('\n')) {
28 if (!l.trim() || l.startsWith('notice:')) continue;
29 if (/^\s/.test(l) && lines.length) lines[lines.length - 1] += ` ${l.trim()}`; else lines.push(l.trim());
30 }
31 return { status: r.status, lines };
32}
33// Every problem in the store: [] iff fsck --strict exits 0 and prints nothing but notices. Assert it with
34// `assert.deepEqual(fsckProblems(bare), [])`. `dangling: true` keeps dangling objects as problems.
35export function fsckProblems(cwd, { dangling = false, args = [] } = {}) {
36 const { status, lines } = fsck(cwd, [...(dangling ? [] : ['--no-dangling']), ...args]);
37 return status === 0 && !lines.length ? [] : [...lines, ...(status !== 0 ? [`(git fsck exit ${status})`] : [])];
38}
39// The problems git reports about one object, in a store that may hold other (bad) objects on purpose. The oid must be
40// full: an abbreviation can match another object's line. Exit bits beyond 1 (object) and 2 (connectivity) are a
41// store or git failure no line can be pinned to, so they throw.
42export function fsckProblemsOf(cwd, oid) {
43 if (!/^[0-9a-f]{40}$/.test(oid)) throw new Error(`fsckProblemsOf needs a full oid, got ${JSON.stringify(oid)}`);
44 const { status, lines } = fsck(cwd, ['--no-dangling']);
45 if (status === null || (status & ~3) !== 0) throw new Error(`git fsck failed (exit ${status}): ${lines.join(' | ')}`);
46 return lines.filter(l => l.includes(oid));
47}
49export function startGitServer(root, extraEnv = {}) {
50 const server = http.createServer((req, res) => {
51 const u = new URL(req.url, 'http://x');
52 const env = {
53 ...process.env, ...extraEnv,
54 GIT_PROJECT_ROOT: root, GIT_HTTP_EXPORT_ALL: '1',
55 PATH_INFO: u.pathname, REQUEST_METHOD: req.method, QUERY_STRING: u.search.slice(1),
56 CONTENT_TYPE: req.headers['content-type'] ?? '', REMOTE_USER: 'fuzz', REMOTE_ADDR: '127.0.0.1',
57 SERVER_PROTOCOL: 'HTTP/1.1', GATEWAY_INTERFACE: 'CGI/1.1',
58 HTTP_GIT_PROTOCOL: req.headers['git-protocol'] ?? '',
59 HTTP_CONTENT_ENCODING: req.headers['content-encoding'] ?? '',
60 };
61 if (req.headers['content-length']) env.CONTENT_LENGTH = req.headers['content-length'];
62 const child = spawn('git', ['http-backend'], { env, stdio: ['pipe', 'pipe', 'pipe'] });
63 // receive-pack may exit before it reads the whole body (a pack it refuses): EPIPE then means nothing, the response says it all
64 child.stdin.on('error', () => {});
65 req.pipe(child.stdin);
66 let head = Buffer.alloc(0), headerDone = false;
67 const stderr = [];
68 child.stderr.on('data', d => stderr.push(d));
69 child.stdout.on('data', d => {
70 if (headerDone) return void res.write(d);
71 head = Buffer.concat([head, d]);
72 const i = head.indexOf('\r\n\r\n') >= 0 ? head.indexOf('\r\n\r\n') : head.indexOf('\n\n');
73 if (i < 0) return;
74 const sepLen = head.indexOf('\r\n\r\n') >= 0 ? 4 : 2;
75 const headers = {}; let status = 200;
76 for (const line of head.subarray(0, i).toString().split(/\r?\n/)) {
77 const c = line.indexOf(':'); if (c < 0) continue;
78 const k = line.slice(0, c).trim(), v = line.slice(c + 1).trim();
79 if (k.toLowerCase() === 'status') status = parseInt(v, 10); else headers[k] = v;
80 }
81 res.writeHead(status, headers);
82 headerDone = true;
83 res.write(head.subarray(i + sepLen));
84 });
85 child.on('close', code => {
86 if (!req.complete) { req.unpipe(child.stdin); req.resume(); }
87 if (!headerDone) { res.writeHead(500); res.end(Buffer.concat(stderr).toString()); return; }
88 res.end();
89 });
90 });
91 // Outlive the client's idle sockets: with Node's 5 s default the server can close a kept-alive socket just as undici
92 // reuses it (slow oracle steps idle that long), surfacing as a spurious 'fetch failed'.
93 server.keepAliveTimeout = 120_000; server.headersTimeout = 121_000;
94 return new Promise(resolve => server.listen(0, '127.0.0.1', () => {
95 const { port } = server.address();
96 resolve({ url: `http://127.0.0.1:${port}`, close: () => new Promise(r => { server.close(r); server.closeAllConnections?.(); }), server });
97 }));
98}
100// Basic auth in front of `upstreamUrl`: a request without an Authorization header gets 401 + `WWW-Authenticate: Basic`
101// (what makes git send its credential); `accept(authorization)` decides the rest (false → 401, its body echoing the
102// header and its decoded form, as a careless server would). Every request's Authorization (or null) lands in `seen`.
103export function startAuthGate(upstreamUrl, accept = () => true) {
104 const up = new URL(upstreamUrl);
105 const seen = [];
106 const server = http.createServer((req, res) => {
107 const a = req.headers.authorization ?? null;
108 seen.push({ method: req.method, path: req.url.split('?')[0], authorization: a });
109 if (a === null || !accept(a)) {
110 const decoded = a?.startsWith('Basic ') ? Buffer.from(a.slice(6), 'base64').toString() : '';
111 res.writeHead(401, { 'WWW-Authenticate': 'Basic realm="gate"', 'content-type': 'text/plain' });
112 res.end(a === null ? 'authentication required' : `bad credentials ${a} (${decoded})`);
113 return;
114 }
115 const p = http.request({ host: up.hostname, port: up.port, path: req.url, method: req.method, headers: { ...req.headers, host: up.host } }, r => { res.writeHead(r.statusCode, r.headers); r.pipe(res); });
116 req.pipe(p);
117 });
118 server.keepAliveTimeout = 120_000; server.headersTimeout = 121_000;
119 return new Promise(resolve => server.listen(0, '127.0.0.1', () => resolve({
120 url: `http://127.0.0.1:${server.address().port}`, seen,
121 close: () => new Promise(r => { server.close(r); server.closeAllConnections?.(); }),
122 })));
123}
125// git itself, asynchronously (the gate runs in this process): no config, no credential helper, no prompt.
126// `extraEnv` reaches git and what it runs (GIT_SSH_COMMAND, a credential helper's file). `config` ([key, value]
127// pairs) reaches git through GIT_CONFIG_COUNT / GIT_CONFIG_KEY_n / GIT_CONFIG_VALUE_n after the defaults, never argv,
128// so a credential under test is in no process listing (a later `credential.helper=` would reset an earlier helper,
129// and env config is read before `-c`). `input` is written to stdin; `out` is stdout as raw bytes.
130export function gitAsync(cwd, args, extraEnv = {}, { config = [], input } = {}) {
131 const cfg = [['credential.helper', ''], ['user.name', 'g'], ['user.email', 'g@x'], ...config];
132 const env = {
133 PATH: process.env.PATH, HOME: cwd, GIT_CONFIG_NOSYSTEM: '1', GIT_CONFIG_GLOBAL: '/dev/null', GIT_TERMINAL_PROMPT: '0',
134 GIT_CONFIG_COUNT: String(cfg.length), ...Object.fromEntries(cfg.flatMap(([k, v], i) => [[`GIT_CONFIG_KEY_${i}`, k], [`GIT_CONFIG_VALUE_${i}`, v]])),
135 ...extraEnv,
136 };
137 return new Promise(resolve => {
138 const c = spawn('git', args, { cwd, env, stdio: [input === undefined ? 'ignore' : 'pipe', 'pipe', 'pipe'] });
139 const out = []; let stderr = '';
140 c.stdout.on('data', d => { out.push(d); }); c.stderr.on('data', d => { stderr += d; });
141 // git may exit before it reads all of stdin: EPIPE then means nothing, its status says it all
142 if (input !== undefined) { c.stdin.on('error', () => {}); c.stdin.end(input); }
143 c.on('close', status => { const buf = Buffer.concat(out); resolve({ status, stdout: buf.toString(), out: buf, stderr }); });
144 });
145}
147// git as the oracle, with no credential in any URL, argv or config value it is handed. Given a URL with userinfo, git
148// passes it to curl and compares what it asked for with CURLINFO_EFFECTIVE_URL; a libcurl that reports the effective
149// URL without the userinfo (GitHub's ubuntu-26.04 image 20261018.50: git 2.55.0, curl 8.18.0-1ubuntu2.7) looks like a
150// redirect to http.c's update_url_from_redirect, which rebuilds the credential from the userinfo-free URL: "warning:
151// redirecting to http://127.0.0.1:…/adv648.git/" then "fatal: could not read Username". So git decodes the userinfo
152// with its own parser (`git credential fill`: credential_from_url, the decoder http.c applies to a URL) and sends it
153// from a credential helper. The helper is set through GIT_CONFIG_* env; the bytes travel in a file (`%FF` decodes to
154// a byte no env string carries), and a run that took a redirect fails here, not as a missing credential later.
155const CRED_HELPER = '!f() { test "$1" = get || exit 0; cat "$PLY_CRED"; }; f';
156export async function gitWithCred(cwd, args, cred, config = []) { // cred: username=/password= lines as bytes, or null
157 const dir = mkdtempSync(join(tmpdir(), 'ply-cred-')), file = join(dir, 'cred');
158 writeFileSync(file, cred ?? '', { mode: 0o600 });
159 const r = await gitAsync(cwd, args, { PLY_CRED: file }, { config: [...(cred ? [['credential.helper', CRED_HELPER]] : []), ...config] });
160 rmSync(dir, { recursive: true, force: true });
161 if (/redirecting/.test(r.stderr)) throw new Error(`git ${args[0]} took a redirect: ${r.stderr}`);
162 return r;
163}
164// git's decoding of a URL's userinfo, ready for gitWithCred: { cred } (username=/password= lines for the helper) or
165// { refused: stderr }. A user alone gets an empty password: what git sends for `user@` (curl's `user:`, git 2.50
166// measured: no prompt). An empty user (`:pass@`, `:@`) is sent by git from a URL as Basic `:pass` (git 2.50 measured,
167// SAFETY I6d), but git's helper path sends nothing for an empty username (http.c init_curl_http_auth), so there the
168// bytes git decoded go out as `http.extraHeader`, framed `Basic base64(user ":" pass)` (RFC 7617).
169export async function gitUserinfoCred(cwd, url) {
170 const r = await gitAsync(cwd, ['credential', 'fill'], {}, { input: `url=${url}\n\n`, config: [['credential.helper', '!f() { test "$1" = get && echo password=; }; f']] });
171 if (r.status !== 0) return { refused: r.stderr };
172 const keep = []; let o = 0;
173 while (o < r.out.length) {
174 const e = r.out.indexOf(10, o) < 0 ? r.out.length - 1 : r.out.indexOf(10, o);
175 const line = r.out.subarray(o, e + 1);
176 if (/^(username|password)=/.test(line.toString('latin1'))) keep.push(line);
177 o = e + 1;
178 }
179 const field = k => { const l = keep.find(x => x.toString('latin1').startsWith(`${k}=`)); return l ? l.subarray(k.length + 1, l.at(-1) === 10 ? -1 : undefined) : null; };
180 const user = field('username'), pass = field('password');
181 if (!user || !pass) throw new Error(`git credential fill gave ${keep.length} fields`);
182 if (user.length > 0) return { cred: Buffer.concat(keep), config: [] };
183 return { cred: null, config: [['http.extraHeader', `Authorization: Basic ${Buffer.concat([user, Buffer.from(':'), pass]).toString('base64')}`]] };
184}
186// Background maintenance off for a repository real git serves, receives into or commits in. A test reads the object
187// store right after git returns (git fsck, ls-tree, a copy of the directory), and a repack still running then renames
188// and unlinks packs under that read: CI's git 2.56 failed `git fsck` after a push with "packfile … index not opened"
189// and ".tmp-…-pack-….pack index not opened" (ci runs 43067401434, 43070027886; receive.unpackLimit=1 keeps each push
190// a pack). What each key stops (git-config(1); builtin/receive-pack.c and builtin/gc.c at v2.50.0 and v2.56.0):
191// receive.autoGc=false receive-pack does not start `git maintenance run --auto` after it updates refs (`if
192// (auto_gc)` around prepare_auto_maintenance; the default is true).
193// maintenance.auto=false no porcelain (commit, fetch, merge, rebase, am, receive-pack) starts `git maintenance run
194// --auto`: prepare_auto_maintenance returns before spawning, whatever the strategy's tasks.
195// gc.auto=0 the gc task's own condition (need_to_gc: gc.auto <= 0 never runs), so a `git gc --auto` or
196// a `maintenance run --auto` started some other way does not repack through the gc task.
197// Environment config (GIT_CONFIG_COUNT) outranks all three; test/harness-maintenance.test.js observes them under it.
198export const NO_MAINTENANCE = [['gc.auto', '0'], ['maintenance.auto', 'false'], ['receive.autoGc', 'false']];
199export function noMaintenance(dir) { for (const [k, v] of NO_MAINTENANCE) git(dir, ['config', k, v]); }
200// A work repository (`git init -q -b main`, the directory made if absent) with background maintenance off: the one
201// place a test's non-bare repository is made, since `git commit` in it starts `git maintenance run --auto --detach`.
202export function initRepo(dir) { mkdirSync(dir, { recursive: true }); git(dir, ['init', '-q', '-b', 'main']); noMaintenance(dir); return dir; }
204// A bare repo configured for v2 partial fetch of arbitrary objects + push. `stock: true` leaves upload-pack at git's
205// defaults: no uploadpack.allowFilter (so `fetch=` advertises no `filter`, and upload-pack dies on one) and no
206// allowAnySHA1InWant, as a `git init --bare` behind `git http-backend` serves it; push and fsck-on-receive stay on.
207export function makeBareRepo(root, name, { stock = false } = {}) {
208 const dir = join(root, `${name}.git`);
209 git(root, ['init', '--bare', '-q', '-b', 'main', dir]);
210 noMaintenance(dir);
211 const upload = stock ? [] : [['uploadpack.allowFilter', 'true'], ['uploadpack.allowAnySHA1InWant', 'true'], ['protocol.version', '2']];
212 for (const [k, v] of [...upload, ['http.receivepack', 'true'], ['receive.fsckObjects', 'true'], ['transfer.fsckObjects', 'true']]) git(dir, ['config', k, v]);
213 return dir;
214}
216export function tmpRoot() {
217 const root = mkdtempSync(join(tmpdir(), 'ply-'));
218 return { root, cleanup: () => rmSync(root, { recursive: true, force: true }) };
219}
221// Materialize a tree spec {path: Uint8Array|string|{mode, data}} in a work repo and push to bare. Returns head oid.
222export function commitFiles(work, bare, files, msg = 'seed', { exec = [] } = {}) {
223 const fs = require_fs();
224 for (const [p, v] of Object.entries(files)) {
225 const full = join(work, p);
226 fs.mkdirSync(join(full, '..'), { recursive: true });
227 const data = v && typeof v === 'object' && 'data' in v ? v.data : v;
228 fs.writeFileSync(full, data);
229 if ((v && v.mode === '100755') || exec.includes(p)) fs.chmodSync(full, 0o755);
230 }
231 git(work, ['add', '-A']);
232 git(work, ['-c', 'user.name=seed', '-c', 'user.email=seed@x', 'commit', '-q', '--allow-empty', '-m', msg]);
233 git(work, ['push', '-q', bare, 'HEAD:refs/heads/main']);
234 return git(work, ['rev-parse', 'HEAD']).trim();
235}
236import { createRequire } from 'node:module';
237const require_fs = () => createRequire(import.meta.url)('node:fs');
Diagnostics4 git calls · 467 ms
CallStatusSizeTime0467 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200145 B · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=145 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=145 git.pkt.payload.max=76 git.ls_refs.count=2 git.ls_refs.head=refs/heads/main
fetchblob:none · deepen 1020038 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=461 git.response.bytes=38434 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=19912563f540228593df0c489082e5e2508e21e4 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=6 git.pkt.data=4 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=38434 git.pkt.payload.max=38339 git.sideband.band1.bytes=38338 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=63 git.pack.bytes=38338 git.pack.ofs_delta=0 git.pack.ref_delta=8 git.pack.chain.max=8 git.pack.trailer=0bc36ab7adf82d3109177f11f1ea93f1ce071f77 git.pack.full=55Open pack map
fetch2006.6 KiB461 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=6806 git.response.cache=MISS git.response.duration_ms=461 git.fetch.wants=8560f75d407ba1dfe6ec3c70a8df867f969440e5 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=6806 git.pkt.payload.max=6785 git.sideband.band1.bytes=6784 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/ply.git
- Ref
- refs/heads/main
- Commit
- 19912563f540228593df0c489082e5e2508e21e4
- cf-ray
- a5e20478eedac69c
- Request
- 1f89abd8…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format