main
ply4 git calls342 ms59 KiB2 cachedprotocol v2
1008 lines73 KiBRaw
1// The ply Worker (src/index.js), bundled by `cf build` exactly as `cf deploy` uploads it and run under workerd, against git
2// http-backend (test/server.js). Every endpoint is judged by git; plus the bearer gate, the remote allowlist, the
3// credential forwarding (verbatim and per host), credential non-leakage (responses, traces, error bodies, logs) and the
4// writes switch. Skipped if workerd is not installed.
5import { test, before, after } from 'node:test';
6import assert from 'node:assert/strict';
7import http from 'node:http';
8import { spawn } from 'node:child_process';
9import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
10import { join } from 'node:path';
11import { fileURLToPath } from 'node:url';
12import { copyCfBundle } from './cf-bundle.js';
13import { git, gitBuf, tmpRoot, makeBareRepo, startGitServer, commitFiles } from './server.js';
14import { startProxy } from './proxy.js';
16/** Null and a missing value take the fallback. An empty string and 0 stay. */
17function fill(value, fallback) {
18 return value === null || value === void 0 ? fallback : value;
21/** One property. A null receiver and a missing key are holes. A stored null stays null. */
22function slot(receiver, key) {
23 if (receiver === null || receiver === void 0) return void 0;
24 if (typeof receiver !== 'object' && typeof receiver !== 'function') return void 0;
25 return receiver[key];
28function closeServer(s, done) {
29 const closeAll = s.closeAllConnections;
30 if (closeAll != null) closeAll.call(s);
31 s.close(done);
34const WORKERD = [process.env.WORKERD, fileURLToPath(new URL('../node_modules/.bin/workerd', import.meta.url))].filter(Boolean).find(p => existsSync(p));
35const skip = !WORKERD && 'workerd not installed';
36const PORT = 18900 + (process.pid % 50), PORT_RO = PORT + 50;
37const PLY = 'test-ply-token-' + 'x'.repeat(20);
38const SECRET = 'gitcred_' + 'S3cr3tT0k3n'.repeat(3); // the git credential the auth proxy expects
39const EXPECT = `Bearer ${SECRET}`;
40let root, cleanup, srv, authSrv, echoSrv, artProxy, redirSrv, raySrv, proc, logs = '';
41const A = { name: 'w', email: 'w@x' };
43// a git host that requires `Authorization: Bearer SECRET` (401 without, 403 with a wrong one) in front of http-backend
44function authProxy(upstream) {
45 const up = new URL(upstream);
46 const s = http.createServer((req, res) => {
47 const a = req.headers.authorization;
48 if (!a) { res.writeHead(401, { 'www-authenticate': 'Basic realm="git"' }); return void res.end('Authentication required'); }
49 if (a !== EXPECT && a !== `Basic ${Buffer.from(`x-access-token:${SECRET}`).toString('base64')}`) { res.writeHead(403); return void res.end('Forbidden'); }
50 const u = 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); });
51 req.pipe(u);
52 });
53 return new Promise(ok => s.listen(0, '127.0.0.1', () => ok({ url: `http://127.0.0.1:${s.address().port}`, close: () => new Promise(r => { closeServer(s, r); }) })));
55// a hostile host that echoes the credential back in a 500 body. Its info/refs is a plain v2 advertisement, so the
56// transport's first GET (followRedirects 'initial' advertises first) succeeds and the echo arrives on the POST.
57const V2_ADVERTISEMENT = '000eversion 2\n0013ls-refs=unborn\n0019fetch=shallow filter\n0000';
58function echoServer() {
59 const s = http.createServer((req, res) => {
60 if (req.method === 'GET' && fill(req.url, '').includes('/info/refs')) { res.writeHead(200, { 'content-type': 'application/x-git-upload-pack-advertisement' }); return void res.end(V2_ADVERTISEMENT); }
61 res.writeHead(500, { 'content-type': 'text/plain' }); res.end(`echo ${req.headers.authorization} ${Buffer.from(fill(String(req.headers.authorization).split(' ')[1], ''), 'base64').toString()}`);
62 });
63 return new Promise(ok => s.listen(0, '127.0.0.1', () => ok({ url: `http://127.0.0.1:${s.address().port}`, close: () => new Promise(r => { closeServer(s, r); }) })));
66// A host that has moved repositories: `/moved/<p>` 307s to `/<p>` (same origin, relative Location), `/away/<p>` 307s to
67// another allowlisted origin, `/loop/<p>` 307s to itself, `/postmoved/<p>` serves GETs from `/<p>` and 307s every POST
68// there; anything else is proxied to http-backend. `hits` records each request (path, method, whether it carried
69// Authorization) so a test can see what was and was not dialled.
70function redirector(upstream) {
71 const up = new URL(upstream);
72 const hits = [];
73 const s = http.createServer((req, res) => {
74 hits.push({ path: req.url, method: req.method, auth: !!req.headers.authorization });
75 const m = /^\/(moved|away|loop|postmoved)(\/.*)$/.exec(req.url);
76 let path = req.url;
77 if (m && m[1] === 'postmoved' && req.method !== 'POST') path = m[2];
78 else if (m) {
79 const location = m[1] === 'moved' || m[1] === 'postmoved' ? m[2] : m[1] === 'away' ? `${upstream}${m[2]}` : req.url;
80 req.resume(); res.writeHead(307, { location }); return void res.end();
81 }
82 const u = http.request({ host: up.hostname, port: up.port, path, method: req.method, headers: { ...req.headers, host: up.host } }, r => { res.writeHead(r.statusCode, r.headers); r.pipe(res); });
83 req.pipe(u);
84 });
85 return new Promise(ok => s.listen(0, '127.0.0.1', () => ok({ url: `http://127.0.0.1:${s.address().port}`, hits, close: () => new Promise(r => { closeServer(s, r); }) })));
88// git http-backend with a cf-ray on every response. `beforeReceive` runs before a receive-pack POST is forwarded,
89// so a test can move the ref and make git itself reject the push.
90function rayProxy(upstream) {
91 const up = new URL(upstream);
92 const seen = [];
93 let n = 0;
94 let beforeReceive = null;
95 const s = http.createServer((req, res) => {
96 const chunks = [];
97 req.on('data', c => chunks.push(c));
98 req.on('end', () => {
99 const body = Buffer.concat(chunks);
100 const ray = `${(++n).toString(16).padStart(16, '0')}-IAD`;
101 const path = fill(req.url, '');
102 const hit = { ray, method: fill(req.method, 'GET'), path, head: body.subarray(0, 64).toString('latin1'), hook: 'skip' };
103 seen.push(hit);
104 const forward = () => {
105 const u = http.request({ host: up.hostname, port: up.port, path: req.url, method: req.method, headers: { ...req.headers, host: up.host } }, r => {
106 const headers = { ...r.headers, 'cf-ray': ray };
107 res.writeHead(fill(r.statusCode, 502), headers);
108 r.pipe(res);
109 });
110 u.on('error', () => { if (!res.headersSent) { res.writeHead(502, { 'cf-ray': ray, 'content-type': 'text/plain' }); res.end('upstream'); } });
111 u.end(body);
112 };
113 Promise.resolve().then(() => {
114 if (!(beforeReceive && req.method === 'POST' && path.split('?')[0].endsWith('/git-receive-pack'))) return;
115 hit.hook = 'run';
116 return beforeReceive();
117 }).then(forward, err => {
118 hit.hook = `err:${err}`;
119 if (!res.headersSent) res.writeHead(500, { 'cf-ray': ray, 'content-type': 'text/plain' });
120 res.end(`ray hook: ${err}`);
121 });
122 });
123 });
124 return new Promise(ok => s.listen(0, '127.0.0.1', () => ok({
125 url: `http://127.0.0.1:${s.address().port}`, seen,
126 setBeforeReceive(fn) { beforeReceive = fn; },
127 close: () => new Promise(r => { closeServer(s, r); }),
128 })));
131before(async () => {
132 if (!WORKERD) return;
133 ({ root, cleanup } = tmpRoot());
134 srv = await startGitServer(root);
135 authSrv = await authProxy(srv.url);
136 echoSrv = await echoServer();
137 redirSrv = await redirector(srv.url);
138 const filterOf = b => fill(slot(/filter ([^\n]+)\n/.exec(Buffer.from(b).toString('latin1')), '1'), null);
139 artProxy = await startProxy(srv.url, ctx => (ctx.stage === 'fetch' && filterOf(ctx.reqBody) && filterOf(ctx.reqBody) !== 'blob:none' ? { type: 'status', status: 400, contentType: 'text/plain', body: 'Unsupported Git fetch filter; only blob:none is supported.' } : { type: 'pass' }));
140 raySrv = await rayProxy(srv.url);
141 const out = join(root, 'wk'); mkdirSync(out, { recursive: true });
142 copyCfBundle(join(out, 'worker.mjs'));
143 const allow = [srv.url, authSrv.url, echoSrv.url, artProxy.url, redirSrv.url, raySrv.url].join(',');
144 const worker = writes => `(modules = [ (name = "worker.mjs", esModule = embed "worker.mjs") ], compatibilityDate = "2026-10-18", globalOutbound = "internet",
145 bindings = [ (name = "PLY_TOKEN", text = "${PLY}"), (name = "WRITES", text = "${writes}"), (name = "ALLOW_REMOTE_ORIGINS", text = "${allow}") ])`;
146 writeFileSync(join(out, 'config.capnp'), `using Workerd = import "/workerd/workerd.capnp";
147const config :Workerd.Config = (
148 services = [ (name = "rw", worker = ${worker('on')}), (name = "ro", worker = ${worker('off')}), (name = "internet", network = (allow = ["private", "public", "local"])) ],
149 sockets = [ (name = "http", address = "127.0.0.1:${PORT}", http = (), service = "rw"), (name = "http-ro", address = "127.0.0.1:${PORT_RO}", http = (), service = "ro") ],
150);
151`);
152 proc = spawn(WORKERD, ['serve', join(out, 'config.capnp'), '--verbose'], { stdio: ['ignore', 'pipe', 'pipe'] });
153 proc.stdout.on('data', d => { logs += d; }); proc.stderr.on('data', d => { logs += d; });
154 for (let i = 0; i < 80; i++) { try { if ((await fetch(`http://127.0.0.1:${PORT}/`)).ok) break; } catch { } await new Promise(r => setTimeout(r, 100)); }
155});
156after(async () => { if (!WORKERD) return; proc.kill(); await Promise.all([srv.close(), authSrv.close(), echoSrv.close(), artProxy.close(), redirSrv.close(), raySrv.close()]); cleanup(); });
158const W = (path, { method = 'GET', body, headers = {}, port = PORT, auth = true } = {}) => fetch(`http://127.0.0.1:${port}${path}`, { method, headers: { ...(auth ? { authorization: `Bearer ${PLY}` } : {}), ...(body ? { 'content-type': 'application/json' } : {}), ...headers }, body: body ? JSON.stringify(body) : void 0 });
159const qs = o => new URLSearchParams(Object.entries(o).filter(([, v]) => v !== void 0)).toString();
160let n = 0;
161function repo(files, host = () => srv.url) {
162 const name = `r${n++}`; const bare = makeBareRepo(root, name); const work = join(root, `w${name}`); mkdirSync(work); git(work, ['init', '-q', '-b', 'main']);
163 const head = commitFiles(work, bare, files, 'seed');
164 return { name, bare, work, head, remote: `${host()}/${name}.git` };
167test('GET / is public usage text; everything else needs the PLY bearer (missing or wrong → 401)', { skip }, async () => {
168 const u = await W('/', { auth: false }); assert.equal(u.status, 200); const usage = await u.text(); assert.match(usage, /Authorization: Bearer <PLY_TOKEN>/); assert.match(usage, /GET +\/v1\/commit\?remote=&ref=/);
169 // a browser (Accept: text/html) is sent to the viewer; the usage text stays for curl and fetch
170 const html = await fetch(`http://127.0.0.1:${PORT}/`, { redirect: 'manual', headers: { accept: 'text/html,application/xhtml+xml,*/*;q=0.8' } });
171 assert.equal(html.status, 302); assert.equal(html.headers.get('location'), '/view'); assert.equal(html.headers.get('cache-control'), 'no-store'); assert.match(String(html.headers.get('vary')), /Accept/i); await html.arrayBuffer();
172 for (const [m, p] of [['GET', '/v1/refs'], ['GET', '/v1/read'], ['GET', '/v1/tree'], ['GET', '/v1/discover'], ['GET', '/v1/commit'], ['POST', '/v1/edit'], ['POST', '/v1/branch'], ['DELETE', '/v1/branch'], ['POST', '/v1/merge'], ['POST', '/v1/rebase'], ['POST', '/v1/cherry-pick'], ['POST', '/v1/revert'], ['POST', '/v1/tag'], ['DELETE', '/v1/tag'], ['GET', '/nope']]) {
173 for (const headers of [{}, { authorization: `Bearer ${PLY}x` }, { authorization: `Bearer ${PLY.slice(0, -1)}` }, { authorization: `Basic ${btoa(`x:${PLY}`)}` }]) {
174 const r = await W(`${p}?remote=${encodeURIComponent(srv.url + '/x.git')}`, { method: m, headers, auth: false });
175 assert.equal(r.status, 401, `${m} ${p} ${JSON.stringify(headers)}`); assert.equal((await r.json()).code, 'unauthorized');
176 assert.match(fill(r.headers.get('www-authenticate'), ''), /Bearer/);
177 }
178 }
179 assert.equal((await W('/nope')).status, 404);
180 assert.equal((await W('/v1/read', { method: 'POST' })).status, 405);
181});
183function assertSecurityHeaders(headers, where) {
184 assert.equal(headers.get('x-content-type-options'), 'nosniff', where);
185 assert.equal(headers.get('referrer-policy'), 'no-referrer', where);
186 const csp = fill(headers.get('content-security-policy'), '');
187 assert.match(csp, /frame-ancestors 'none'/, where);
188 assert.match(csp, /script-src 'none'/, where);
189 assert.equal(csp.includes('unsafe-inline'), false, where);
192test('security headers are set on every route', { skip }, async () => {
193 const routes = [
194 ['GET', '/', false],
195 ['GET', '/nope', true],
196 ['GET', '/v1/refs', false],
197 ['GET', `/v1/refs?${qs({ remote: 'https://127.0.0.1.nip.io/x.git' })}`, true],
198 ['POST', '/v1/edit', true],
199 ['GET', '/view', false],
200 ['HEAD', '/view', false],
201 ['GET', '/view.css', false],
202 ['GET', '/view/evil.example/a/b', false],
203 ['GET', '/view/github.com/a/%2f../b', false],
204 ['POST', '/view', false],
205 ];
206 for (const [method, path, auth] of routes) {
207 const res = await W(path, { method, auth, body: method === 'POST' && path === '/v1/edit' ? { remote: 'https://127.0.0.1/x.git' } : void 0 });
208 assertSecurityHeaders(res.headers, `${method} ${path}`);
209 await res.arrayBuffer();
210 }
211});
213test('remote allowlist: https only, no IP literals / private names / userinfo / ports / query; nothing is fetched', { skip }, async () => {
214 const bad = ['http://example.com/x.git', 'https://127.0.0.1/x.git', 'https://10.0.0.1/x.git', 'https://169.254.169.254/x.git', 'https://192.168.1.1/x.git', 'https://[::1]/x.git', 'https://[fd00::1]/x.git',
215 'https://2130706433/x.git', 'https://0x7f000001/x.git', 'https://localhost/x.git', 'https://foo.localhost/x.git', 'https://git.internal/x.git', 'https://intranet/x.git', 'https://user:pw@github.com/x.git',
216 'https://github.com:8443/x.git', 'https://github.com/x.git?a=b', 'ftp://github.com/x.git', 'not a url', '',
217 'https://127.0.0.1.nip.io/x.git', 'https://169.254.169.254.nip.io/latest', 'https://foo.localtest.me/x.git',
218 'https://127-0-0-1.sslip.io/x.git', 'https://github.com./x.git', 'https://0177.0.0.1/x.git', 'https://[::ffff:127.0.0.1]/x.git'];
219 for (const remote of bad) {
220 const r = await W(`/v1/refs?${qs({ remote, trace: '1' })}`);
221 const b = await r.json();
222 assert.equal(r.status, 400, `${remote}: ${JSON.stringify(b)}`); assert.equal(b.code, 'bad_request'); assert.equal(b.retry, 'no');
223 assert.deepEqual(b.trace, [], 'refused before any span (no request was made)');
224 }
225 const w = await W('/v1/edit', { method: 'POST', body: { remote: 'https://127.0.0.1/x.git', path: 'a', find: 'b', replace: 'c', author: A, message: 'm' } });
226 assert.equal(w.status, 400);
227});
229test('read endpoints equal git: refs, discover, tree, read (bytes, headers, content-length, streamed 3 MiB)', { skip }, async () => {
230 const big = Buffer.alloc(3 << 20); for (let i = 0; i < big.length; i += 5) big[i] = i & 255;
231 const r = repo({ 'README.md': '# hello\n', 'a/b/c.txt': 'nested\n', 'empty': '', 'big.bin': big });
232 git(r.work, ['tag', '-a', '-m', 't', 'v1']); git(r.work, ['push', '-q', r.bare, 'refs/tags/v1']);
233 const refs = await (await W(`/v1/refs?${qs({ remote: r.remote })}`)).json();
234 assert.equal(refs.head, 'refs/heads/main'); assert.equal(refs.refs['refs/heads/main'], r.head);
235 assert.equal(refs.refs['refs/tags/v1'], git(r.bare, ['rev-parse', 'v1']).trim()); assert.equal(refs.peeled['refs/tags/v1'], r.head);
236 const pre = await (await W(`/v1/refs?${qs({ remote: r.remote, prefix: 'refs/tags/' })}`)).json();
237 assert.deepEqual(Object.keys(pre.refs), ['refs/tags/v1']); assert.equal(pre.head, 'refs/heads/main');
238 const d = await (await W(`/v1/discover?${qs({ remote: r.remote })}`)).json();
239 assert.equal(d.version, 2); assert.match(d.agent, /^git\//); assert.equal(d.objectFormat, 'sha1');
240 const tr = await (await W(`/v1/tree?${qs({ remote: r.remote, path: 'a/b' })}`)).json();
241 assert.deepEqual(tr.entries, [{ name: 'c.txt', mode: '100644', oid: git(r.bare, ['rev-parse', 'main:a/b/c.txt']).trim(), type: 'blob' }]);
242 const top = await (await W(`/v1/tree?${qs({ remote: r.remote, ref: 'v1' })}`)).json();
243 assert.equal(top.tree, git(r.bare, ['rev-parse', 'v1^{tree}']).trim()); assert.equal(top.entries.length, 4);
244 for (const path of ['README.md', 'a/b/c.txt', 'empty', 'big.bin']) {
245 const res = await W(`/v1/read?${qs({ remote: r.remote, path })}`);
246 assert.equal(res.status, 200, path);
247 const got = Buffer.from(await res.arrayBuffer()), want = gitBuf(r.bare, ['show', `main:${path}`]);
248 assert.ok(got.equals(want), `${path} bytes`);
249 assert.equal(res.headers.get('content-length'), String(want.length), `${path} content-length`);
250 assert.equal(res.headers.get('content-type'), 'application/octet-stream');
251 assert.equal(res.headers.get('x-ply-commit'), r.head); assert.equal(res.headers.get('x-ply-blob'), git(r.bare, ['rev-parse', `main:${path}`]).trim());
252 }
253 const tr1 = await W(`/v1/read?${qs({ remote: r.remote, path: 'README.md', trace: '1' })}`);
254 {
255 const timing = tr1.headers.get('x-ply-trace') || '';
256 assert.ok(/ls-refs;dur=\d+/.test(timing) || /ply\.cas\.ref;dur=\d+/.test(timing), timing);
257 }
258 await tr1.arrayBuffer();
259 // 'README.md/' names a directory: git answers "path 'README.md/' does not exist" for a blob, and ply refuses it before
260 // any request (ply#316, #300-8; it used to drop the slash and read README.md).
261 for (const [path, code, status] of [['nope', 'not_found', 404], ['a/b', 'bad_request', 400], ['README.md/x', 'not_found', 404], ['../x', 'bad_request', 400], ['README.md/', 'bad_request', 400]]) {
262 const res = await W(`/v1/read?${qs({ remote: r.remote, path })}`); const b = await res.json();
263 assert.equal(res.status, status, path); assert.equal(b.code, code); assert.equal(b.retry, 'no');
264 if (path === 'a/b') assert.match(b.message, /GET \/v1\/tree/);
265 assert.equal(res.headers.get('x-ply-trace'), null);
266 }
267 const nr = await W(`/v1/read?${qs({ remote: r.remote, path: 'README.md', ref: 'nope' })}`); assert.equal(nr.status, 404);
268 const trace = await (await W(`/v1/tree?${qs({ remote: r.remote, trace: '1' })}`)).json();
269 const names = trace.trace.map(s => s.name);
270 assert.ok(
271 (names.includes('git.upload-pack ls-refs') && names.includes('git.ls_tree')) ||
272 (names.some(n => n.startsWith('ply.cas.'))),
273 JSON.stringify(names),
274 );
275});
277test('GET /v1/commit equals git log / cat-file (HEAD, short, full, tag, oid, merge); not_found / bad_request shapes; blob:none fallback', { skip }, async () => {
278 const r = repo({ 'f.txt': 'one\n' });
279 git(r.work, ['tag', '-a', '-m', 't', 'v1']);
280 git(r.work, ['checkout', '-q', '-b', 'side']); writeFileSync(join(r.work, 's.txt'), 's\n'); git(r.work, ['add', '-A']);
281 git(r.work, ['commit', '-q', '-m', 'side: Zoë\n\nline two\nline three'], { env: { ...process.env, GIT_AUTHOR_NAME: 'Zoë Ångström', GIT_AUTHOR_EMAIL: 'z@ë.x', GIT_AUTHOR_DATE: '1700000000 +0530', GIT_COMMITTER_NAME: 'C', GIT_COMMITTER_EMAIL: 'c@x', GIT_COMMITTER_DATE: '1700000100 -0800' } });
282 git(r.work, ['checkout', '-q', 'main']); writeFileSync(join(r.work, 'm.txt'), 'm\n'); git(r.work, ['add', '-A']);
283 git(r.work, ['-c', 'user.name=m', '-c', 'user.email=m@x', 'commit', '-q', '-m', 'main']);
284 git(r.work, ['-c', 'user.name=m', '-c', 'user.email=m@x', 'merge', '-q', '--no-ff', '-m', 'merge side', 'side']);
285 git(r.work, ['push', '-q', r.bare, 'main', 'side', 'refs/tags/v1']);
286 const want = (spec, bare = r.bare) => {
287 const [oid, tree, parents, an, ae, ad, cn, ce, cd] = git(bare, ['log', '-1', '--date=raw', `--format=%H%x1f%T%x1f%P%x1f%an%x1f%ae%x1f%ad%x1f%cn%x1f%ce%x1f%cd`, spec]).replace(/\n$/, '').split('\x1f');
288 const raw = gitBuf(bare, ['cat-file', 'commit', oid]).toString(); const id = (name, email, d) => { const [epoch, tz] = d.split(' '); return { name, email, time: new Date(Number(epoch) * 1000).toISOString().replace('.000Z', 'Z'), epoch: Number(epoch), tz }; };
289 return { oid, tree, parents: parents ? parents.split(' ') : [], author: id(an, ae, ad), committer: id(cn, ce, cd), message: raw.slice(raw.indexOf('\n\n') + 2), signed: false };
290 };
291 const side = git(r.bare, ['rev-parse', 'side']).trim();
292 for (const [ref, spec, name] of [[void 0, 'main', 'refs/heads/main'], ['main', 'main', 'refs/heads/main'], ['refs/heads/side', 'side', 'refs/heads/side'], ['v1', 'v1^{commit}', 'refs/tags/v1'], [side, side, side]]) {
293 const res = await W(`/v1/commit?${qs({ remote: r.remote, ref })}`);
294 const b = await res.json();
295 assert.equal(res.status, 200, JSON.stringify(b)); assert.match(res.headers.get('content-type'), /application\/json/);
296 assert.deepEqual(b, { ref: name, ...want(spec) }, String(ref));
297 }
298 const m = await (await W(`/v1/commit?${qs({ remote: r.remote })}`)).json();
299 assert.equal(m.parents.length, 2); assert.equal(m.parents[1], side);
300 const shortRes = await W(`/v1/commit?${qs({ remote: r.remote, ref: side.slice(0, 8) })}`);
301 const shortBody = await shortRes.json();
302 assert.equal(shortRes.status, 200, JSON.stringify(shortBody));
303 assert.equal(shortBody.oid, side);
304 assert.equal(shortRes.headers.get('cache-control'), 'no-store', 'an abbreviated oid is not a full oid');
305 // /v1 is a credentialed API: every response is no-store with no Vary, even tree/read/commit at a full oid (an
306 // immutable answer), errors included
307 for (const [path, extra] of [['/v1/commit', {}], ['/v1/tree', {}], ['/v1/read', { path: 's.txt' }], ['/v1/read', { path: 's.txt', trace: '1' }], ['/v1/refs', {}], ['/v1/discover', {}], ['/v1/read', { path: 'no-such-file' }], ['/v1/commit', { ref: 'f'.repeat(40) }]]) {
308 const res = await W(`${path}?${qs({ remote: r.remote, ref: side, ...extra })}`); await res.arrayBuffer();
309 const what = `${path} ${JSON.stringify(extra)} → ${res.status}`;
310 if (!extra.ref && extra.path !== 'no-such-file') assert.equal(res.status, 200, what);
311 assert.equal(res.headers.get('cache-control'), 'no-store', what); assert.equal(res.headers.get('vary'), null, what);
312 }
313 const noAuth = await W(`/v1/read?${qs({ remote: r.remote, ref: side, path: 's.txt' })}`, { auth: false }); await noAuth.arrayBuffer();
314 assert.equal(noAuth.status, 401); assert.equal(noAuth.headers.get('cache-control'), 'no-store');
315 // refs and discover ignore ref=, so an abbreviated oid there does not cost them an abbrev ls-refs
316 for (const path of ['/v1/refs', '/v1/discover']) {
317 const abbrev = await W(`${path}?${qs({ remote: r.remote, ref: 'abcdef1' })}`); await abbrev.arrayBuffer();
318 assert.equal(abbrev.status, 200, `${path} ignores an abbreviated ref`);
319 }
320 const failRes = await W(`/v1/commit?${qs({ remote: r.remote, ref: 'f'.repeat(40) })}`); await failRes.arrayBuffer();
321 assert.equal(failRes.headers.get('cache-control'), 'no-store', 'an error is no-store');
322 const noneRes = await W(`/v1/commit?${qs({ remote: r.remote, ref: 'abcdef1' })}`);
323 const noneBody = await noneRes.json();
324 assert.equal(noneRes.status, 400, JSON.stringify(noneBody));
325 assert.equal(noneBody.code, 'bad_request');
326 assert.match(noneBody.message, /40-hex/);
327 const usage = await W('/');
328 assert.equal(usage.status, 200);
329 const usageText = await usage.text();
330 assert.match(usageText, /entries:\[\{name, mode, oid, type\}\]/);
331 assert.match(usageText, /GET \/view/);
332 assert.match(usageText, /GET \/r\/<name>/);
333 assert.match(fill(usage.headers.get('x-ply-build'), ''), /^ply=[0-9a-f]{40} ply-web=[0-9a-f]{40}$/);
334 assert.doesNotMatch(fill(usage.headers.get('x-ply-version'), ''), /ply=/, 'x-ply-version stays the Workers version id');
335 const s = await (await W(`/v1/commit?${qs({ remote: r.remote, ref: 'side' })}`)).json();
336 assert.deepEqual(s.author, { name: 'Zoë Ångström', email: 'z@ë.x', time: '2023-11-14T22:13:20Z', epoch: 1700000000, tz: '+0530' }); assert.equal(s.message, 'side: Zoë\n\nline two\nline three\n');
337 for (const [ref, status, code] of [['nope', 404, 'not_found'], ['f'.repeat(40), 404, 'not_found'], [git(r.bare, ['rev-parse', 'main:f.txt']).trim(), 404, 'not_found'], ['a..b', 400, 'bad_request']]) {
338 const res = await W(`/v1/commit?${qs({ remote: r.remote, ref })}`); const b = await res.json();
339 assert.equal(res.status, status, `${ref}: ${JSON.stringify(b)}`); assert.equal(b.code, code); assert.equal(b.retry, 'no');
340 }
341 const missing = await W(`/v1/commit?${qs({ remote: `${srv.url}/does-not-exist.git` })}`); assert.equal(missing.status, 404); assert.equal((await missing.json()).code, 'not_found');
342 assert.equal((await W('/v1/commit')).status, 400);
343 assert.equal((await W(`/v1/commit?${qs({ remote: r.remote })}`, { method: 'POST' })).status, 405);
344 const tr = await (await W(`/v1/commit?${qs({ remote: r.remote, trace: '1' })}`)).json();
345 const fetches = tr.trace.filter(x => x.name === 'git.upload-pack fetch');
346 const casCommit = tr.trace.filter(x => x.name === 'ply.cas.commit' || x.name === 'ply.cas.ref');
347 if (fetches.length === 1) {
348 assert.equal(fetches[0].attrs['git.fetch.filter'], 'tree:0'); assert.equal(fetches[0].attrs['git.fetch.deepen'], 1);
349 } else {
350 assert.ok(casCommit.length >= 1, JSON.stringify(tr.trace.map(x => x.name)));
351 }
352 // Artifacts mode: tree:0 refused → blob:none, same answer
353 const a = repo({ 'x.txt': 'x\n' }, () => artProxy.url);
354 const ab = await (await W(`/v1/commit?${qs({ remote: a.remote, trace: '1' })}`)).json();
355 const { trace, ...rest } = ab;
356 assert.deepEqual(rest, { ref: 'refs/heads/main', ...want('main', a.bare) }); assert.equal(rest.oid, a.head);
357 // CAS CommitCache hides upload-pack attrs on the front-door trace; body equality is the contract.
358 assert.ok(
359 trace.some(x => x.attrs && x.attrs['git.fetch.filter.fallback'] === 'blob:none') ||
360 trace.some(x => x.name === 'ply.cas.commit' || x.name === 'ply.cas.ref'),
361 JSON.stringify(trace.map(x => x.name)),
362 );
363});
365test('a server that refuses tree:0 (Artifacts mode): reads and an edit still match git via the blob:none fallback', { skip }, async () => {
366 const r = repo({ 'd/e/f.txt': 'deep MARK\n', 'x.txt': 'x\n' }, () => artProxy.url);
367 const res = await W(`/v1/read?${qs({ remote: r.remote, path: 'd/e/f.txt', trace: '1' })}`);
368 assert.equal(res.status, 200); assert.equal(await res.text(), 'deep MARK\n');
369 const tr = await (await W(`/v1/tree?${qs({ remote: r.remote, path: 'd', trace: '1' })}`)).json();
370 assert.equal(tr.tree, git(r.bare, ['rev-parse', 'main:d']).trim());
371 const e = await (await W('/v1/edit?trace=1', { method: 'POST', body: { remote: r.remote, path: 'd/e/f.txt', find: 'MARK', replace: 'EDITED', author: A, message: 'm' } })).json();
372 assert.equal(git(r.bare, ['rev-parse', 'main']).trim(), e.newCommit, JSON.stringify(e).slice(0, 400));
373 assert.equal(gitBuf(r.bare, ['show', 'main:d/e/f.txt']).toString(), 'deep EDITED\n');
374 assert.ok(e.trace.some(s => s.attrs['git.fetch.filter.fallback'] === 'blob:none'));
375});
377test('empty repo lifecycle: first commit through history, with no git seeding', { skip }, async () => {
378 const name = `e${n++}`;
379 const bare = makeBareRepo(root, name);
380 // srv's origin was already discovered by the missing-repo commit read above. raySrv is a different origin
381 // (it proxies the same http-backend) and this isolate has not asked it for ls-refs=unborn yet.
382 const remote = `${raySrv.url}/${name}.git`;
383 const post = (p, body, method = 'POST') => W(p + (p.includes('?') ? '' : ''), { method, body: { remote, ...body } }).then(async res => ({ status: res.status, body: await res.json() }));
384 const ZERO = '0'.repeat(40);
385 assert.equal(git(bare, ['symbolic-ref', 'HEAD']).trim(), 'refs/heads/main');
386 assert.throws(() => git(bare, ['rev-parse', '--verify', 'HEAD']));
387 const emptyRes = await W(`/v1/commit?${qs({ remote, trace: '1' })}`);
388 const emptyBody = await emptyRes.json();
389 const { trace: coldTrace, ...empty } = emptyBody;
390 assert.deepEqual(empty, { empty: true, ref: 'refs/heads/main', oid: null, tree: null, parents: [], author: null, committer: null, message: null, signed: false });
391 // CAS RefCache owns discover/ls-refs; the front door sees ply.cas.ref (and must not invent a git.head span).
392 const coldNames = coldTrace.map(s => s.name);
393 assert.equal(coldTrace.some(s => s.name === 'git.head'), false, JSON.stringify(coldNames));
394 assert.ok(
395 coldTrace.some(s => s.name === 'ply.cas.ref') || coldTrace.filter(s => s.name === 'git.upload-pack discover').length === 1,
396 JSON.stringify(coldNames),
397 );
398 assert.match(fill(emptyRes.headers.get('x-ply-request-id'), ''), /\S/);
399 assert.match(fill(emptyRes.headers.get('x-ply-build'), ''), /^ply=[0-9a-f]{40} ply-web=[0-9a-f]{40}$/);
400 assert.match(fill(emptyRes.headers.get('server-timing'), ''), /total;dur=\d+/);
401 const warmRes = await W(`/v1/commit?${qs({ remote, ref: 'main', trace: '1' })}`);
402 const warmBody = await warmRes.json();
403 const { trace: warmTrace, ...warm } = warmBody;
404 assert.deepEqual(warm, empty);
405 const warmNames = warmTrace.map(s => s.name);
406 assert.equal(warmTrace.some(s => s.name === 'git.head'), false, JSON.stringify(warmNames));
407 assert.ok(
408 warmTrace.some(s => s.name === 'ply.cas.ref') || warmTrace.filter(s => s.name === 'git.upload-pack discover').length >= 1,
409 JSON.stringify(warmNames),
410 );
411 assert.notEqual(warmRes.headers.get('x-ply-request-id'), emptyRes.headers.get('x-ply-request-id'));
412 assert.match(fill(warmRes.headers.get('server-timing'), ''), /total;dur=\d+/);
413 assert.equal((await W(`/v1/commit?${qs({ remote, ref: 'nope' })}`)).status, 404);
414 const refs = await (await W(`/v1/refs?${qs({ remote })}`)).json();
415 assert.equal(refs.head, null); assert.equal(refs.empty, true); assert.equal(refs.unborn, 'refs/heads/main');
416 let x = await post('/v1/branch', { ref: 'tmp' });
417 assert.equal(x.status, 400, JSON.stringify(x.body)); assert.match(x.body.message, /from is required when the remote HEAD is unborn \(refs\/heads\/main\)/);
418 x = await post('/v1/branch', { ref: 'tmp', from: ZERO });
419 assert.equal(x.status, 400, JSON.stringify(x.body)); assert.match(x.body.message, /zero oid/);
420 assert.throws(() => git(bare, ['rev-parse', '--verify', '-q', 'refs/heads/tmp']));
421 x = await post('/v1/merge', { from: 'main' });
422 assert.equal(x.status, 400, JSON.stringify(x.body)); assert.match(x.body.message, /ref is required when the remote HEAD is unborn \(refs\/heads\/main\)/);
423 x = await post('/v1/edit', { base: ZERO, path: 'a.txt', content: 'a\n', author: A, message: 'no' });
424 assert.equal(x.status, 400, JSON.stringify(x.body)); assert.match(x.body.message, /zero oid/);
425 const author = { ...A, tz: '+0530', time: 1700000000 };
426 x = await post('/v1/edit?trace=1', { message: 'init', author, changes: [
427 { op: 'create', path: 'README.md', content: 'hello\n' },
428 { op: 'create', path: 'src/a.txt', content: 'v1\n' },
429 { op: 'create', path: 'src/keep.txt', content: 'keep\n' },
430 { op: 'create', path: 'old.txt', content: 'x\n' },
431 { op: 'create', path: 'bin/run', content: '#!/bin/sh\n', mode: '100755' },
432 { op: 'create', path: 'link', content: 'README.md', mode: '120000' },
433 ] });
434 assert.equal(x.status, 200, JSON.stringify(x.body));
435 assert.equal(x.body.outcome, 'commit'); assert.equal(x.body.ref, 'refs/heads/main'); assert.equal(x.body.oldOid, null); assert.equal(x.body.oldCommit, null); assert.equal(x.body.noop, false);
436 const c0 = x.body.oid;
437 assert.equal(x.body.newCommit, c0);
438 assert.equal(git(bare, ['rev-parse', 'main']).trim(), c0);
439 assert.ok(x.body.trace.some(s => s.name === 'git.commit' || s.name === 'git.head'), JSON.stringify(x.body.trace == null ? void 0 : x.body.trace.map(s => s.name)));
440 assert.equal(x.body.trace.filter(s => s.name === 'git.upload-pack discover').length, 0, 'HEAD capability was cached by the commit reads');
441 const raw0 = gitBuf(bare, ['cat-file', 'commit', c0]).toString();
442 assert.equal(raw0.split('\n').some(l => l.startsWith('parent ')), false);
443 assert.match(raw0, /author w <w@x> 1700000000 \+0530\n/);
444 assert.match(raw0, /committer w <w@x> 1700000000 \+0530\n/);
445 assert.match(git(bare, ['ls-tree', '-r', c0]), /100755 blob [0-9a-f]{40}\tbin\/run/);
446 assert.match(git(bare, ['ls-tree', '-r', c0]), /120000 blob [0-9a-f]{40}\tlink/);
447 assert.equal(gitBuf(bare, ['cat-file', '-p', `${c0}:link`]).toString(), 'README.md');
448 const born = await (await W(`/v1/commit?${qs({ remote })}`)).json();
449 assert.equal(born.oid, c0); assert.equal(born.parents.length, 0); assert.equal(born.author.tz, '+0530'); assert.equal(born.author.epoch, 1700000000); assert.equal(born.empty, void 0);
450 const bornRefs = await (await W(`/v1/refs?${qs({ remote })}`)).json();
451 assert.equal(bornRefs.head, 'refs/heads/main'); assert.equal(bornRefs.refs['refs/heads/main'], c0); assert.equal(bornRefs.empty, void 0);
452 x = await post('/v1/edit', { message: 'second', author: { ...A, time: 1700000100 }, committer: { name: 'c', email: 'c@x', tz: '-0800', time: 1700000100 }, changes: [
453 { op: 'edit', path: 'src/a.txt', find: 'v1', replace: 'v2' },
454 { op: 'delete', path: 'src/keep.txt' },
455 { op: 'create', path: 'ADDED.txt', content: 'added\n' },
456 { op: 'rename', path: 'old.txt', to: 'new.txt' },
457 ] });
458 assert.equal(x.status, 200, JSON.stringify(x.body));
459 const c1 = x.body.oid;
460 assert.equal(x.body.oldOid, c0); assert.equal(git(bare, ['rev-parse', 'main']).trim(), c1);
461 const raw1 = gitBuf(bare, ['cat-file', 'commit', c1]).toString();
462 assert.match(raw1, new RegExp(`parent ${c0}\\n`));
463 assert.match(raw1, /author w <w@x> 1700000100 \+0000\n/);
464 assert.match(raw1, /committer c <c@x> 1700000100 -0800\n/);
465 // commit-fresh-after-write: tip must be c1 (parents length 1), never a stale c0 RefCache HIT.
466 const freshRes = await W(`/v1/commit?${qs({ remote })}`);
467 const fresh = await freshRes.json();
468 assert.equal(freshRes.status, 200, JSON.stringify(fresh));
469 assert.equal(fresh.oid, c1, JSON.stringify(fresh));
470 assert.deepEqual(fresh.parents, [c0], JSON.stringify(fresh));
471 assert.ok(freshRes.headers.get('x-artifacts-ray'), 'fill rays required on /v1/commit');
472 assert.equal(gitBuf(bare, ['show', 'main:src/a.txt']).toString(), 'v2\n');
473 assert.equal(gitBuf(bare, ['show', 'main:ADDED.txt']).toString(), 'added\n');
474 assert.equal(gitBuf(bare, ['show', 'main:new.txt']).toString(), 'x\n');
475 assert.throws(() => git(bare, ['cat-file', '-e', 'main:src/keep.txt']));
476 assert.throws(() => git(bare, ['cat-file', '-e', 'main:old.txt']));
477 x = await post('/v1/branch', { ref: 'other', from: c0 });
478 assert.equal(x.status, 201, JSON.stringify(x.body)); assert.equal(x.body.outcome, 'created'); assert.equal(x.body.oid, c0); assert.equal(x.body.oldOid, null);
479 x = await post('/v1/edit', { ref: 'other', message: 'other same', author: A, changes: [
480 { op: 'edit', path: 'src/a.txt', find: 'v1', replace: 'v2' },
481 { op: 'delete', path: 'src/keep.txt' },
482 { op: 'create', path: 'ADDED.txt', content: 'added\n' },
483 { op: 'rename', path: 'old.txt', to: 'new.txt' },
484 ] });
485 assert.equal(x.status, 200, JSON.stringify(x.body));
486 const otherTip = x.body.oid;
487 assert.equal(git(bare, ['rev-parse', 'other^{tree}']).trim(), git(bare, ['rev-parse', `${c1}^{tree}`]).trim());
488 x = await post('/v1/cherry-pick', { ref: 'other', commit: c1, committer: A });
489 assert.equal(x.status, 200, JSON.stringify(x.body)); assert.equal(x.body.outcome, 'empty'); assert.equal(x.body.noop, true); assert.equal(x.body.oid, otherTip);
490 assert.equal(git(bare, ['rev-parse', 'other']).trim(), otherTip);
491 x = await post('/v1/cherry-pick', { ref: 'other', commit: c1, committer: A, allowEmpty: true });
492 assert.equal(x.status, 200, JSON.stringify(x.body)); assert.equal(x.body.outcome, 'cherry_pick'); assert.equal(x.body.noop, false);
493 const emptyPick = x.body.oid;
494 assert.notEqual(emptyPick, otherTip);
495 assert.equal(git(bare, ['rev-parse', 'other']).trim(), emptyPick);
496 assert.equal(git(bare, ['rev-parse', 'other^{tree}']).trim(), git(bare, ['rev-parse', `${otherTip}^{tree}`]).trim());
497 assert.equal(git(bare, ['rev-parse', 'other^']).trim(), otherTip);
498 // The same patch, plus an add that exists only on the target. ply keeps the rename guard, so this is 409, not empty.
499 x = await post('/v1/branch', { ref: 'wider', from: c0 });
500 assert.equal(x.status, 201, JSON.stringify(x.body));
501 x = await post('/v1/edit', { ref: 'wider', message: 'wider', author: A, changes: [
502 { op: 'edit', path: 'src/a.txt', find: 'v1', replace: 'v2' },
503 { op: 'delete', path: 'src/keep.txt' },
504 { op: 'create', path: 'ADDED.txt', content: 'added\n' },
505 { op: 'rename', path: 'old.txt', to: 'new.txt' },
506 { op: 'create', path: 'extra.txt', content: 'only here\n' },
507 ] });
508 assert.equal(x.status, 200, JSON.stringify(x.body));
509 const widerTip = x.body.oid;
510 x = await post('/v1/cherry-pick', { ref: 'wider', commit: c1, committer: A });
511 assert.equal(x.status, 409, JSON.stringify(x.body));
512 assert.equal(x.body.code, 'merge_conflict');
513 assert.equal(x.body.retry, 'no');
514 assert.equal(git(bare, ['rev-parse', 'wider']).trim(), widerTip);
515 x = await post('/v1/cherry-pick', { ref: 'wider', commit: c1, committer: A, allowEmpty: true });
516 assert.equal(x.status, 409, JSON.stringify(x.body));
517 assert.equal(x.body.code, 'merge_conflict');
518 assert.equal(git(bare, ['rev-parse', 'wider']).trim(), widerTip);
519 x = await post('/v1/branch', { ref: 'feat', from: c1 });
520 assert.equal(x.status, 201); assert.equal(x.body.oid, c1);
521 x = await post('/v1/edit', { ref: 'feat', message: 'feat file', author: A, changes: [{ op: 'create', path: 'feat.txt', content: 'feat\n' }] });
522 assert.equal(x.status, 200, JSON.stringify(x.body));
523 const feat = x.body.oid;
524 x = await post('/v1/cherry-pick', { commit: feat, committer: A });
525 assert.equal(x.status, 200, JSON.stringify(x.body)); assert.equal(x.body.outcome, 'cherry_pick');
526 assert.equal(gitBuf(bare, ['show', 'main:feat.txt']).toString(), 'feat\n');
527 const picked = x.body.oid;
528 x = await post('/v1/revert', { commit: picked, committer: A });
529 assert.equal(x.status, 200, JSON.stringify(x.body)); assert.equal(x.body.outcome, 'revert');
530 assert.throws(() => git(bare, ['cat-file', '-e', 'main:feat.txt']));
531 const mainNow = x.body.oid;
532 x = await post('/v1/branch', { ref: 'side', from: mainNow });
533 assert.equal(x.status, 201); assert.equal(x.body.oid, mainNow);
534 x = await post('/v1/edit', { ref: 'side', path: 'README.md', find: 'hello', replace: 'hello side', author: A, message: 'side readme' });
535 assert.equal(x.status, 200, JSON.stringify(x.body));
536 const side = x.body.oid;
537 x = await post('/v1/edit', { path: 'ADDED.txt', find: 'added', replace: 'added main', author: A, message: 'main added' });
538 assert.equal(x.status, 200, JSON.stringify(x.body));
539 const mainMoved = x.body.oid;
540 assert.equal(git(bare, ['rev-parse', 'side']).trim(), side);
541 assert.equal(git(bare, ['rev-parse', 'main']).trim(), mainMoved);
542 x = await post('/v1/merge', { from: side, author: { ...A, tz: '+0530', time: 1700000200 }, committer: { name: 'c', email: 'c@x', tz: '-0800', time: 1700000300 }, message: 'merge side' });
543 assert.equal(x.status, 200, JSON.stringify(x.body)); assert.equal(x.body.outcome, 'merge');
544 assert.equal(git(bare, ['rev-parse', 'main']).trim(), x.body.oid);
545 assert.equal(gitBuf(bare, ['show', 'main:README.md']).toString(), 'hello side\n');
546 assert.equal(gitBuf(bare, ['show', 'main:ADDED.txt']).toString(), 'added main\n');
547 const merged = gitBuf(bare, ['cat-file', 'commit', x.body.oid]).toString();
548 assert.match(merged, /author w <w@x> 1700000200 \+0530\n/);
549 assert.match(merged, /committer c <c@x> 1700000300 -0800\n/);
550 x = await post('/v1/branch', { ref: 'reb', from: c0 });
551 assert.equal(x.status, 201);
552 x = await post('/v1/edit', { ref: 'reb', message: 'reb file', author: A, changes: [{ op: 'create', path: 'reb.txt', content: 'reb\n' }] });
553 assert.equal(x.status, 200, JSON.stringify(x.body));
554 const rebBefore = git(bare, ['rev-parse', 'reb']).trim();
555 x = await post('/v1/rebase', { ref: 'reb', onto: ZERO, committer: A });
556 assert.equal(x.status, 400, JSON.stringify(x.body)); assert.match(x.body.message, /zero oid/);
557 assert.equal(git(bare, ['rev-parse', 'reb']).trim(), rebBefore);
558 x = await post('/v1/rebase', { ref: 'reb', onto: 'main', committer: { name: 'c', email: 'c@x', tz: '-0800', time: 1700000400 }, author: { ...A, tz: '+0530', time: 1700000500 } });
559 assert.equal(x.status, 200, JSON.stringify(x.body)); assert.equal(x.body.outcome, 'rebase');
560 assert.equal(gitBuf(bare, ['show', 'reb:reb.txt']).toString(), 'reb\n');
561 assert.equal(git(bare, ['merge-base', 'reb', 'main']).trim(), git(bare, ['rev-parse', 'main']).trim());
562 assert.match(gitBuf(bare, ['cat-file', 'commit', x.body.oid]).toString(), /committer c <c@x> 1700000400 -0800\n/);
563 x = await post('/v1/edit', { ref: 'from-oid', base: c0, message: 'from oid', author: A, changes: [{ op: 'create', path: 'from-oid.txt', content: 'p\n' }] });
564 assert.equal(x.status, 200, JSON.stringify(x.body));
565 const fromOid = x.body.oid;
566 assert.equal(x.body.oldOid, null);
567 assert.match(gitBuf(bare, ['cat-file', 'commit', fromOid]).toString(), new RegExp(`parent ${c0}\\n`));
568 const mainAt = git(bare, ['rev-parse', 'main']).trim();
569 x = await post('/v1/merge', { from: c0, author: A });
570 assert.equal(x.status, 200, JSON.stringify(x.body)); assert.equal(x.body.outcome, 'up_to_date');
571 assert.equal(git(bare, ['rev-parse', 'main']).trim(), mainAt);
572 x = await post('/v1/branch', { ref: 'sq', from: c0 });
573 assert.equal(x.status, 201, JSON.stringify(x.body));
574 x = await post('/v1/edit', { ref: 'sq', message: 'sq one', author: A, changes: [{ op: 'create', path: 'sq.txt', content: 'one\n' }] });
575 assert.equal(x.status, 200, JSON.stringify(x.body));
576 x = await post('/v1/edit', { ref: 'sq', message: 'sq two', author: A, changes: [{ op: 'edit', path: 'sq.txt', find: 'one', replace: 'two' }] });
577 assert.equal(x.status, 200, JSON.stringify(x.body));
578 x = await post('/v1/rebase', {
579 ref: 'sq', onto: mainAt, squash: true, message: 'squashed\n',
580 committer: { name: 'c', email: 'c@x', tz: '-0800', time: 1700000600 },
581 author: { name: 'a', email: 'a@x', tz: '+0530', time: 1700000700 },
582 });
583 assert.equal(x.status, 200, JSON.stringify(x.body)); assert.equal(x.body.outcome, 'squash');
584 assert.equal(git(bare, ['rev-parse', 'sq^']).trim(), mainAt);
585 assert.equal(gitBuf(bare, ['show', 'sq:sq.txt']).toString(), 'two\n');
586 const squashed = gitBuf(bare, ['cat-file', 'commit', x.body.oid]).toString();
587 assert.match(squashed, /author a <a@x> 1700000700 \+0530\n/);
588 assert.match(squashed, /committer c <c@x> 1700000600 -0800\n/);
589 assert.match(squashed, /\nsquashed\n$/);
590 x = await post('/v1/cherry-pick', { commit: ZERO, committer: A });
591 assert.equal(x.status, 400, JSON.stringify(x.body));
592 x = await post('/v1/tag', { name: 'v1', target: c0 });
593 assert.equal(x.status, 201, JSON.stringify(x.body)); assert.equal(x.body.outcome, 'tag'); assert.equal(x.body.oid, c0);
594 assert.equal(git(bare, ['rev-parse', 'refs/tags/v1']).trim(), c0);
595 x = await post('/v1/tag', { name: 'v2', target: c1, annotated: true, message: 'version two\n', tagger: author });
596 assert.equal(x.status, 201, JSON.stringify(x.body)); assert.equal(x.body.outcome, 'tag'); assert.equal(x.body.commit, c1);
597 assert.equal(git(bare, ['cat-file', '-t', x.body.oid]).trim(), 'tag');
598 assert.equal(git(bare, ['rev-parse', 'v2^{}']).trim(), c1);
599 x = await post('/v1/tag', { name: 'v1', target: c0 });
600 assert.equal(x.status, 409); assert.equal(x.body.code, 'ref_exists'); assert.equal(x.body.retry, 'no'); assert.equal(x.body.oid, c0);
601 x = await post('/v1/branch', { ref: 'tmp', from: c0 });
602 assert.equal(x.status, 201); assert.equal(x.body.oid, c0);
603 x = await post('/v1/branch', { ref: 'tmp', from: c0 });
604 assert.equal(x.status, 409); assert.equal(x.body.code, 'ref_exists'); assert.equal(x.body.retry, 'no'); assert.equal(x.body.oid, c0);
605 x = await post('/v1/branch', { ref: 'tmp', oid: c1 }, 'DELETE');
606 assert.equal(x.status, 409); assert.equal(x.body.code, 'stale_expected'); assert.equal(x.body.retry, 'fresh'); assert.equal(x.body.expected, c1); assert.equal(x.body.actual, c0); assert.equal(x.body.oid, c0);
607 assert.equal(git(bare, ['rev-parse', 'tmp']).trim(), c0);
608 x = await post('/v1/branch', { ref: 'tmp', oid: c0 }, 'DELETE');
609 assert.equal(x.status, 200); assert.equal(x.body.outcome, 'deleted'); assert.equal(x.body.deleted, c0);
610 assert.throws(() => git(bare, ['rev-parse', '--verify', '-q', 'refs/heads/tmp']));
611 x = await post('/v1/tag', { name: 'v1', oid: c1 }, 'DELETE');
612 assert.equal(x.status, 409); assert.equal(x.body.code, 'stale_expected'); assert.equal(x.body.actual, c0);
613 x = await post('/v1/tag', { name: 'v1', oid: c0 }, 'DELETE');
614 assert.equal(x.status, 200); assert.equal(x.body.deleted, c0);
615 assert.equal(git(bare, ['fsck', '--strict', '--no-dangling']).trim(), '');
616});
618test('writes equal git: edit, branch create/delete, merge, rebase, cherry-pick, revert; CAS → 409; conflicts → 409/422', { skip }, async () => {
619 const r = repo({ 'f.txt': 'one MARK\n', 'x.txt': 'x1\nx2\nx3\n', 'y.txt': 'y\n' });
620 const post = (p, body, method = 'POST') => W(p, { method, body: { remote: r.remote, ...body } }).then(async res => ({ status: res.status, body: await res.json() }));
621 // edit (HEAD branch by default), then a stale head → 409 and nothing pushed
622 let x = await post('/v1/edit', { path: 'f.txt', find: 'MARK', replace: 'EDIT', author: A, message: 'edit' });
623 assert.equal(x.status, 200, JSON.stringify(x.body)); assert.equal(x.body.ref, 'refs/heads/main'); assert.equal(x.body.noop, false);
624 const c1 = x.body.newCommit; assert.equal(git(r.bare, ['rev-parse', 'main']).trim(), c1); assert.equal(gitBuf(r.bare, ['show', 'main:f.txt']).toString(), 'one EDIT\n');
625 x = await post('/v1/edit', { path: 'f.txt', find: 'EDIT', replace: 'AGAIN', author: A, message: 'stale', head: r.head });
626 assert.equal(x.status, 409); assert.equal(x.body.code, 'cas_conflict'); assert.equal(x.body.retry, 'fresh'); assert.equal(git(r.bare, ['rev-parse', 'main']).trim(), c1);
627 x = await post('/v1/edit', { path: 'f.txt', find: 'EDIT', replace: 'AGAIN', author: A, message: 'fresh', head: c1 });
628 assert.equal(x.status, 200); const c2 = x.body.newCommit;
629 x = await post('/v1/edit', { path: 'f.txt', find: 'nothere', replace: 'z', author: A, message: 'm' });
630 assert.equal(x.status, 422); assert.equal(x.body.code, 'match_count'); assert.equal(x.body.matches, 0);
631 x = await post('/v1/edit', { path: 'f.txt', find: 'AGAIN', replace: 'AGAIN', author: A, message: 'noop' });
632 assert.equal(x.status, 200); assert.equal(x.body.noop, true); assert.equal(git(r.bare, ['rev-parse', 'main']).trim(), c2);
633 // edit creating a branch from main
634 x = await post('/v1/edit', { ref: 'feat', base: 'main', path: 'x.txt', find: 'x2', replace: 'x2 feat', author: A, message: 'feat' });
635 assert.equal(x.status, 200); const f1 = x.body.newCommit; assert.equal(git(r.bare, ['rev-parse', 'feat']).trim(), f1);
636 // branch create (409 if it exists), delete with CAS (stale → 409), delete current
637 x = await post('/v1/branch', { ref: 'tmp', from: 'main' }); assert.equal(x.status, 201); assert.equal(x.body.oid, c2); assert.equal(git(r.bare, ['rev-parse', 'tmp']).trim(), c2);
638 x = await post('/v1/branch', { ref: 'tmp', from: 'main' }); assert.equal(x.status, 409); assert.equal(x.body.code, 'ref_exists'); assert.equal(x.body.retry, 'no'); assert.equal(x.body.oid, c2);
639 x = await post('/v1/branch', { ref: 'tmp', oid: r.head }, 'DELETE'); assert.equal(x.status, 409); assert.equal(x.body.code, 'stale_expected'); assert.equal(x.body.retry, 'fresh'); assert.equal(x.body.expected, r.head); assert.equal(x.body.actual, c2); assert.equal(x.body.oid, c2);
640 x = await post('/v1/branch', { ref: 'tmp', oid: c2 }, 'DELETE'); assert.equal(x.status, 200); assert.equal(x.body.deleted, c2);
641 assert.throws(() => git(r.bare, ['rev-parse', '--verify', '-q', 'refs/heads/tmp']));
642 x = await post('/v1/branch', { ref: 'tmp' }, 'DELETE'); assert.equal(x.status, 404);
643 // main moves on y.txt; merge feat → a merge commit whose tree is git merge-tree's
644 x = await post('/v1/edit', { path: 'y.txt', find: 'y', replace: 'y main', author: A, message: 'main y' }); const m1 = x.body.newCommit;
645 x = await post('/v1/merge', { from: 'feat', author: A, message: 'merge feat' });
646 assert.equal(x.status, 200, JSON.stringify(x.body)); assert.equal(x.body.outcome, 'merge');
647 assert.equal(x.body.tree, git(r.bare, ['merge-tree', '--write-tree', m1, f1]).trim().split('\n')[0]);
648 assert.equal(git(r.bare, ['rev-parse', 'main']).trim(), x.body.oid); const mg = x.body.oid;
649 // feat2 from the pre-merge main, rebased onto main
650 x = await post('/v1/edit', { ref: 'feat2', base: 'main', path: 'f.txt', find: 'AGAIN', replace: 'AGAIN2', author: A, message: 'feat2' });
651 const f2 = x.body.newCommit;
652 x = await post('/v1/edit', { path: 'y.txt', find: 'y main', replace: 'y main2', author: A, message: 'main moves' }); const m2 = x.body.newCommit;
653 x = await post('/v1/rebase', { ref: 'feat2', committer: A });
654 assert.equal(x.status, 200, JSON.stringify(x.body)); assert.equal(x.body.outcome, 'rebase'); assert.equal(x.body.commits, 1);
655 assert.equal(git(r.bare, ['rev-parse', 'feat2^{tree}']).trim(), git(r.bare, ['merge-tree', '--write-tree', `--merge-base=${mg}`, m2, f2]).trim().split('\n')[0]);
656 assert.equal(git(r.bare, ['rev-parse', 'feat2^']).trim(), m2);
657 // cherry-pick the rebased commit onto main, then revert it
658 const picked = git(r.bare, ['rev-parse', 'feat2']).trim();
659 x = await post('/v1/cherry-pick', { commit: picked, committer: A }); assert.equal(x.status, 200, JSON.stringify(x.body));
660 assert.equal(x.body.tree, git(r.bare, ['rev-parse', 'feat2^{tree}']).trim());
661 x = await post('/v1/revert', { commit: x.body.oid, committer: A }); assert.equal(x.status, 200);
662 assert.equal(x.body.tree, git(r.bare, ['rev-parse', `${m2}^{tree}`]).trim());
663 // a content conflict → 409 merge_conflict with the paths
664 await post('/v1/edit', { ref: 'c1', base: 'main', path: 'x.txt', find: 'x1', replace: 'x1 left', author: A, message: 'l' });
665 await post('/v1/edit', { path: 'x.txt', find: 'x1', replace: 'x1 right', author: A, message: 'r' });
666 x = await post('/v1/merge', { from: 'c1', author: A }); assert.equal(x.status, 409); assert.equal(x.body.code, 'merge_conflict'); assert.deepEqual(x.body.conflicts, ['x.txt']);
667 // bad input
668 x = await post('/v1/edit', { path: 'f.txt', find: 'a', replace: 'b', message: 'no author' }); assert.equal(x.status, 400);
669 x = await post('/v1/cherry-pick', { commit: 'nothex', committer: A }); assert.equal(x.status, 400);
670 const raw = await W('/v1/edit', { method: 'POST', body: void 0, headers: { 'content-type': 'application/json' } }); assert.equal(raw.status, 400);
671 // an oversized declared body is refused from its Content-Length, before it is read
672 const huge = await new Promise((ok, no) => { const q = http.request({ host: '127.0.0.1', port: PORT, path: '/v1/edit', method: 'POST', headers: { authorization: `Bearer ${PLY}`, 'content-length': String((1 << 20) + 10) } }, res => { res.resume(); ok(res.statusCode); }); q.on('error', no); q.write('{'); });
673 assert.equal(huge, 413);
674 assert.equal(git(r.bare, ['fsck', '--strict', '--no-dangling']).trim(), '');
675});
677// ply sends a pack past its held size (32 KiB) as a ReadableStream (ply#172); the Worker's guarded fetch forwards it.
678// A new file of 64 KiB of random bytes in base64 (no base to delta against) deflates to more than 32 KiB, so this push
679// is streamed.
680test('a write whose pack is past 32 KiB is streamed to the remote and equals git', { skip }, async () => {
681 const big = Buffer.from(crypto.getRandomValues(new Uint8Array(64 * 1024))).toString('base64');
682 const r = repo({ 'f.txt': 'one\n' });
683 const res = await W('/v1/edit', { method: 'POST', body: { remote: r.remote, path: 'big.txt', find: '', replace: big, author: A, message: 'big' } });
684 const body = await res.json();
685 assert.equal(res.status, 200, JSON.stringify(body));
686 assert.equal(git(r.bare, ['rev-parse', 'main']).trim(), body.newCommit);
687 assert.equal(gitBuf(r.bare, ['show', 'main:big.txt']).toString(), big);
688 git(r.bare, ['fsck', '--strict', '--no-dangling']);
689});
691test('writes switch: WRITES=off → 501 on every write endpoint, reads still served', { skip }, async () => {
692 const r = repo({ 'f.txt': 'MARK\n' });
693 for (const [m, p] of [['POST', '/v1/edit'], ['POST', '/v1/branch'], ['DELETE', '/v1/branch'], ['POST', '/v1/merge'], ['POST', '/v1/rebase'], ['POST', '/v1/cherry-pick'], ['POST', '/v1/revert'], ['POST', '/v1/tag'], ['DELETE', '/v1/tag']]) {
694 const res = await W(p, { method: m, port: PORT_RO, body: { remote: r.remote, path: 'f.txt', find: 'MARK', replace: 'X', author: A, message: 'm' } });
695 assert.equal(res.status, 501, `${m} ${p}`); assert.equal((await res.json()).code, 'unsupported');
696 }
697 assert.equal(git(r.bare, ['rev-parse', 'main']).trim(), r.head);
698 const rd = await W(`/v1/read?${qs({ remote: r.remote, path: 'f.txt' })}`, { port: PORT_RO }); assert.equal(await rd.text(), 'MARK\n');
699});
701test('git credentials: Git-Authorization verbatim, Git-Token mapped; upstream 401/403 mirrored; never in responses, traces, errors or logs', { skip }, async () => {
702 const r = repo({ 'f.txt': 'secret MARK\n' }, () => authSrv.url);
703 const ok = await W(`/v1/read?${qs({ remote: r.remote, path: 'f.txt' })}`, { headers: { 'git-authorization': EXPECT } });
704 assert.equal(ok.status, 200); assert.equal(await ok.text(), 'secret MARK\n');
705 const tok = await W(`/v1/tree?${qs({ remote: r.remote, trace: '1' })}`, { headers: { 'git-token': SECRET } }); // non-GitHub host → Bearer
706 assert.equal(tok.status, 200); const tokBody = await tok.text();
707 const none = await W(`/v1/refs?${qs({ remote: r.remote })}`); const noneB = await none.json();
708 assert.equal(none.status, 401); assert.equal(noneB.code, 'auth'); assert.equal(noneB.upstreamStatus, 401);
709 const wrong = await W(`/v1/refs?${qs({ remote: r.remote, trace: '1' })}`, { headers: { 'git-authorization': `Bearer ${SECRET}x` } });
710 assert.equal(wrong.status, 403); const wrongB = await wrong.text();
711 const both = await W(`/v1/refs?${qs({ remote: r.remote })}`, { headers: { 'git-authorization': EXPECT, 'git-token': SECRET } }); assert.equal(both.status, 400);
712 const e = await W('/v1/edit?trace=1', { method: 'POST', headers: { 'git-authorization': EXPECT }, body: { remote: r.remote, path: 'f.txt', find: 'MARK', replace: 'EDIT', author: A, message: 'm' } });
713 assert.equal(e.status, 200); const eB = await e.text();
714 assert.equal(gitBuf(r.bare, ['show', 'main:f.txt']).toString(), 'secret EDIT\n');
715 // a hostile upstream echoing the credential in its error body, under every credential form
716 const echoes = [];
717 for (const headers of [{ 'git-authorization': EXPECT }, { 'git-token': SECRET }, { 'git-authorization': `Basic ${btoa(`oauth2:${SECRET}`)}` }]) {
718 const res = await W(`/v1/read?${qs({ remote: `${echoSrv.url}/x.git`, path: 'f', trace: '1' })}`, { headers });
719 const b = await res.text(); echoes.push(b);
720 assert.equal(res.status, 502, b); assert.match(b, /upstream_5xx/); assert.match(b, /\[redacted\]/);
721 }
722 await new Promise(r => setTimeout(r, 300)); // let workerd flush its log lines
723 const forms = [SECRET, btoa(`x-access-token:${SECRET}`), btoa(`oauth2:${SECRET}`), SECRET.slice(0, 16)];
724 const haystack = { tokBody, wrongB, eB, echoes: echoes.join('\n'), logs };
725 for (const [where, text] of Object.entries(haystack)) for (const f of forms) assert.ok(!text.includes(f), `credential form ${f.slice(0, 6)}… found in ${where}`);
726 assert.match(logs, /"msg":"ply.request"/, 'the structured request log lines were captured (the grep is not vacuous)');
727 assert.ok(!logs.includes(PLY), 'the PLY token is not logged');
728});
730// git's http.followRedirects=initial (ply-web#117): ply follows a 3xx only on the first GET info/refs and rewrites the
731// base; every later request, the push included, goes to the new base. A 3xx on a POST is an error naming the target.
732// createGuardedFetch (src/remote.ts) never follows a 3xx: another origin is refused before ply sees it (400
733// bad_request, one dial), and a loop is ply's `http` error past its three hops (400). Under workerd, end to end, with
734// the credential attached.
735test('redirects: git\'s rule (initial info/refs followed and the base rewritten; a POST redirect is an error), cross-origin refused before ply, loops cut', { skip }, async () => {
736 const r = repo({ 'f.txt': 'one MARK\n' });
737 const moved = `${redirSrv.url}/moved/${r.name}.git`;
738 const token = 'redirtoken_' + 'Z9'.repeat(10);
739 const H = { 'git-token': token };
740 const paths = () => redirSrv.hits.map(h => [h.method, h.path.split('?')[0]]);
741 redirSrv.hits.length = 0;
742 let res = await W(`/v1/refs?${qs({ remote: moved })}`, { headers: H });
743 let body = await res.json();
744 assert.equal(res.status, 200, JSON.stringify(body)); assert.equal(body.refs['refs/heads/main'], r.head);
745 assert.deepEqual(paths(), [['GET', `/moved/${r.name}.git/info/refs`], ['GET', `/${r.name}.git/info/refs`], ['POST', `/${r.name}.git/git-upload-pack`]], 'the info/refs GET was followed once and the POST went to the rewritten base');
746 assert.ok(redirSrv.hits.every(h => h.auth), 'the same-origin hop keeps the credential');
747 res = await W(`/v1/read?${qs({ remote: moved, path: 'f.txt' })}`, { headers: H });
748 assert.equal(res.status, 200); assert.equal(await res.text(), 'one MARK\n');
749 redirSrv.hits.length = 0;
750 res = await W('/v1/edit', { method: 'POST', headers: H, body: { remote: moved, path: 'f.txt', find: 'MARK', replace: 'MOVED', author: A, message: 'via redirect' } });
751 body = await res.json();
752 assert.equal(res.status, 200, JSON.stringify(body)); assert.equal(git(r.bare, ['rev-parse', 'main']).trim(), body.newCommit);
753 assert.equal(gitBuf(r.bare, ['show', 'main:f.txt']).toString(), 'one MOVED\n');
754 assert.ok(redirSrv.hits.some(h => h.method === 'POST' && h.path === `/${r.name}.git/git-receive-pack`), 'the push went to the rewritten base');
755 assert.deepEqual(redirSrv.hits.filter(h => h.path.startsWith('/moved/')), [], 'ply remembers the settled base from the reads above: the edit asks nothing of the old base');
757 // A remote that redirects only its POSTs: git cannot use it (http.followRedirects=initial), and neither can /v1.
758 const before = git(r.bare, ['rev-parse', 'main']).trim();
759 for (const req of [[`/v1/refs?${qs({ remote: `${redirSrv.url}/postmoved/${r.name}.git` })}`, { headers: H }], ['/v1/edit', { method: 'POST', headers: H, body: { remote: `${redirSrv.url}/postmoved/${r.name}.git`, path: 'f.txt', find: 'MOVED', replace: 'NOPE', author: A, message: 'post redirect' } }]]) {
760 redirSrv.hits.length = 0;
761 res = await W(...req);
762 body = await res.json();
763 assert.equal(res.status, 400, JSON.stringify(body)); assert.equal(body.code, 'http');
764 assert.match(body.message, new RegExp(`HTTP 307 git-upload-pack: redirected to ${redirSrv.url}/${r.name}\\.git/git-upload-pack`), 'the error names the target');
765 assert.equal(redirSrv.hits.some(h => h.method === 'POST' && !h.path.startsWith('/postmoved/')), false, 'the redirected POST was not re-sent');
766 assert.equal(JSON.stringify(body).includes(token), false);
767 }
768 assert.equal(git(r.bare, ['rev-parse', 'main']).trim(), before, 'nothing was pushed');
770 for (const [kind, code, msg, dials] of [['away', 'bad_request', /redirect host is not an allowlisted host/, 1], ['loop', 'http', /HTTP 307 git-upload-pack advertisement: redirected to .*\/loop\//, 4]]) {
771 redirSrv.hits.length = 0;
772 res = await W(`/v1/refs?${qs({ remote: `${redirSrv.url}/${kind}/${r.name}.git`, trace: '1' })}`, { headers: H });
773 body = await res.json();
774 assert.equal(res.status, 400, `${kind}: ${JSON.stringify(body)}`); assert.equal(body.code, code); assert.match(body.message, msg);
775 assert.equal(redirSrv.hits.length, dials, `${kind}: hops dialled`);
776 assert.ok(redirSrv.hits.every(h => h.method === 'GET'), `${kind}: only info/refs GETs`);
777 assert.equal(JSON.stringify(body).includes(token), false);
778 }
779 assert.equal(logs.includes(token), false);
780});
782// ply remembers how a remote's initial exchange settled (per isolate): a second /v1 request for the same remote sends its
783// POST straight to the settled base, with no GET info/refs, redirected or not.
784test('redirect memo: a second /v1 request for the same remote in the isolate makes no info/refs GET', { skip }, async () => {
785 const r = repo({ 'f.txt': 'memo\n' });
786 const paths = () => redirSrv.hits.map(h => [h.method, h.path.split('?')[0]]);
787 for (const [remote, first] of [
788 [`${redirSrv.url}/moved/${r.name}.git`, [['GET', `/moved/${r.name}.git/info/refs`], ['GET', `/${r.name}.git/info/refs`], ['POST', `/${r.name}.git/git-upload-pack`]]],
789 [`${redirSrv.url}/${r.name}.git`, [['GET', `/${r.name}.git/info/refs`], ['POST', `/${r.name}.git/git-upload-pack`]]],
790 ]) {
791 redirSrv.hits.length = 0;
792 let res = await W(`/v1/refs?${qs({ remote })}`);
793 assert.equal(res.status, 200, await res.text());
794 assert.deepEqual(paths(), first, `${remote}: the first request settles the initial exchange`);
795 redirSrv.hits.length = 0;
796 res = await W(`/v1/refs?${qs({ remote })}`);
797 const body = await res.json();
798 assert.equal(res.status, 200, JSON.stringify(body)); assert.equal(body.refs['refs/heads/main'], r.head);
799 assert.deepEqual(paths(), [['POST', `/${r.name}.git/git-upload-pack`]], `${remote}: the second request makes no GET`);
800 }
801});
803const RAY_ENTRY = /^([a-z-]+)=([0-9a-f]{16}-[A-Z]{3}|-|no-response);dur=(\d+)$/;
804const RAY_SPAN = {
805 'git.upload-pack ls-refs': 'ls-refs',
806 'git.upload-pack discover': 'ls-refs',
807 'git.upload-pack fetch': 'fetch',
808 'git.receive-pack': 'receive-pack',
809 'git.receive-pack advertise': 'receive-pack',
810};
812function phaseOfHit(hit) {
813 const path = hit.path.split('?')[0];
814 if (path.endsWith('/info/refs')) {
815 const service = new URL(hit.path, 'http://ray.invalid').searchParams.get('service');
816 if (service === 'git-upload-pack') return 'ls-refs';
817 if (service === 'git-receive-pack') return 'receive-pack';
818 return null;
819 }
820 if (hit.method === 'POST' && path.endsWith('/git-receive-pack')) return 'receive-pack';
821 if (hit.method === 'POST' && path.endsWith('/git-upload-pack')) {
822 if (hit.head.includes('command=ls-refs')) return 'ls-refs';
823 if (hit.head.includes('command=fetch')) return 'fetch';
824 }
825 return null;
828function parseRayHeader(header) {
829 assert.ok(header, 'x-artifacts-ray');
830 const entries = [];
831 let overflow = null;
832 for (const part of header.split(', ')) {
833 const over = /^overflow=(\d+)$/.exec(part);
834 if (over) { overflow = Number(over[1]); continue; }
835 const m = RAY_ENTRY.exec(part);
836 assert.ok(m, part);
837 entries.push({ phase: m[1], ray: m[2], dur: Number(m[3]) });
838 }
839 return { entries, overflow };
842function assertRaysMatch(header, hits, timing) {
843 const parsed = parseRayHeader(header);
844 const want = hits.flatMap(hit => {
845 const phase = phaseOfHit(hit);
846 return phase ? [{ phase, ray: hit.ray, advert: hit.method === 'GET' }] : [];
847 });
848 assert.deepEqual(parsed.entries.map(e => ({ phase: e.phase, ray: e.ray })), want.map(w => ({ phase: w.phase, ray: w.ray })), header);
849 parsed.entries.forEach((e, i) => { e.advert = want[i].advert; });
850 assert.equal(parsed.overflow, null);
851 assert.equal(header.includes('/'), false, header);
852 const st = fill(timing, '');
853 for (const phase of new Set(parsed.entries.map(e => e.phase))) assert.match(st, new RegExp(`${phase};dur=\\d+`));
854 assert.match(st, /total;dur=\d+/);
855 return parsed;
858// Each span is stamped with the exchange it read. A transport advertises (GET info/refs) before its first POST, inside
859// that POST's span (followRedirects 'initial'): a span that ended on a POST skips that preamble.
860function assertSpanRays(trace, all) {
861 const spans = trace.filter(s => RAY_SPAN[s.name]);
862 const entries = [];
863 let next = 0;
864 for (const s of spans) {
865 while (s.attrs['http.request.method'] === 'POST' && all[next] && all[next].advert && next + 1 < all.length) next++;
866 entries.push(all[next++]);
867 }
868 assert.equal(next, all.length, 'every exchange is a span or the preamble of one');
869 assert.deepEqual(spans.map(s => RAY_SPAN[s.name]), entries.map(e => e.phase), JSON.stringify(spans.map(s => s.name)));
870 spans.forEach((s, i) => {
871 assert.equal(s.attrs['git.upstream.phase'], entries[i].phase);
872 assert.equal(s.attrs['git.upstream.cf_ray'], entries[i].ray);
873 assert.equal(s.attrs['git.upstream.dur_ms'], entries[i].dur);
874 });
877test('x-artifacts-ray: a read, an empty-repo first commit, a delta push, and a CAS 409', { skip }, async () => {
878 const r = repo({ 'README.md': 'one\n' }, () => raySrv.url);
879 const noauth = await W(`/v1/read?${qs({ remote: r.remote, path: 'README.md' })}`, { auth: false });
880 assert.equal(noauth.status, 401);
881 assert.equal(noauth.headers.get('x-artifacts-ray'), null);
882 await noauth.arrayBuffer();
884 const fromRead = raySrv.seen.length;
885 const read = await W(`/v1/read?${qs({ remote: r.remote, path: 'README.md' })}`);
886 assert.equal(read.status, 200);
887 assert.equal(Buffer.from(await read.arrayBuffer()).toString(), 'one\n');
888 const readRayHdr = read.headers.get('x-artifacts-ray');
889 // Fill rays required: parent observeUpstream (fallback) or CAS-stamped fill rays on the cached response.
890 const readRays = assertRaysMatch(readRayHdr, raySrv.seen.slice(fromRead), read.headers.get('server-timing'));
891 assert.ok(readRays.entries.some(e => e.phase === 'ls-refs'));
892 assert.ok(readRays.entries.some(e => e.phase === 'fetch'));
894 const fromTree = raySrv.seen.length;
895 const treeRes = await W(`/v1/tree?${qs({ remote: r.remote, trace: '1' })}`);
896 const tree = await treeRes.json();
897 assert.equal(treeRes.status, 200, JSON.stringify(tree));
898 const treeRayHdr = treeRes.headers.get('x-artifacts-ray');
899 const treeRays = assertRaysMatch(treeRayHdr, raySrv.seen.slice(fromTree), treeRes.headers.get('server-timing'));
900 if (Array.isArray(tree.trace) && tree.trace.some(s => String(s.name).startsWith('ply.cas.'))) {
901 // CAS compose: span pairing differs (ply.cas.* instead of git.*); header still required.
902 assert.ok(treeRays.entries.length >= 1, treeRayHdr);
903 } else {
904 assertSpanRays(tree.trace, treeRays.entries);
905 }
907 const fromMiss = raySrv.seen.length;
908 const miss = await W(`/v1/read?${qs({ remote: r.remote, path: 'nope' })}`);
909 const missBody = await miss.json();
910 assert.equal(miss.status, 404);
911 assert.equal(missBody.code, 'not_found');
912 const missRayHdr = miss.headers.get('x-artifacts-ray');
913 const missRays = assertRaysMatch(missRayHdr, raySrv.seen.slice(fromMiss), miss.headers.get('server-timing'));
914 assert.ok(missRays.entries.some(e => e.phase === 'ls-refs' || e.phase === 'fetch'));
916 const name = `ray${n++}`;
917 const bare = makeBareRepo(root, name);
918 const remote = `${raySrv.url}/${name}.git`;
919 const author = { name: 'w', email: 'w@x', tz: '+0000', time: 1700000000 };
920 const post = async body => {
921 const from = raySrv.seen.length;
922 const res = await W('/v1/edit?trace=1', { method: 'POST', body: { remote, ...body } });
923 const json = await res.json();
924 return { res, json, hits: raySrv.seen.slice(from) };
925 };
927 const first = await post({ message: 'init', author, changes: [{ op: 'create', path: 'a.txt', content: 'v1\n' }] });
928 assert.equal(first.res.status, 200, JSON.stringify(first.json));
929 assert.equal(first.json.outcome, 'commit');
930 assert.equal(first.json.oldOid, null);
931 assert.equal(gitBuf(bare, ['cat-file', 'commit', first.json.oid]).toString().split('\n').some(l => l.startsWith('parent ')), false);
932 const firstRays = assertRaysMatch(first.res.headers.get('x-artifacts-ray'), first.hits, first.res.headers.get('server-timing'));
933 assert.ok(firstRays.entries.some(e => e.phase === 'receive-pack'));
934 assertSpanRays(first.json.trace, firstRays.entries);
936 const delta = await post({ message: 'second', author, changes: [{ op: 'edit', path: 'a.txt', find: 'v1', replace: 'v2' }] });
937 assert.equal(delta.res.status, 200, JSON.stringify(delta.json));
938 assert.match(gitBuf(bare, ['cat-file', 'commit', delta.json.oid]).toString(), new RegExp(`parent ${first.json.oid}\\n`));
939 const deltaRays = assertRaysMatch(delta.res.headers.get('x-artifacts-ray'), delta.hits, delta.res.headers.get('server-timing'));
940 assert.ok(deltaRays.entries.some(e => e.phase === 'receive-pack'));
941 assertSpanRays(delta.json.trace, deltaRays.entries);
943 raySrv.setBeforeReceive(() => {
944 const parent = git(bare, ['rev-parse', 'HEAD']).trim();
945 const tree = git(bare, ['rev-parse', 'HEAD^{tree}']).trim();
946 const neu = git(bare, ['commit-tree', tree, '-p', parent, '-m', 'raced'], {
947 env: { ...process.env, GIT_AUTHOR_NAME: 'w', GIT_AUTHOR_EMAIL: 'w@x', GIT_COMMITTER_NAME: 'w', GIT_COMMITTER_EMAIL: 'w@x' },
948 }).trim();
949 git(bare, ['update-ref', 'refs/heads/main', neu, parent]);
950 });
951 try {
952 const cas = await post({ message: 'conflict', author, changes: [{ op: 'edit', path: 'a.txt', find: 'v2', replace: 'v3' }] });
953 assert.equal(cas.res.status, 409, JSON.stringify({ code: cas.json.code, message: cas.json.message, hooks: cas.hits.filter(h => h.path.includes('receive-pack')).map(h => ({ path: h.path, method: h.method, hook: h.hook, ray: h.ray })) }));
954 assert.ok(cas.json.code === 'cas_conflict' || cas.json.code === 'not_fast_forward', cas.json.code);
955 const casRays = assertRaysMatch(cas.res.headers.get('x-artifacts-ray'), cas.hits, cas.res.headers.get('server-timing'));
956 const pushed = casRays.entries.filter(e => e.phase === 'receive-pack');
957 assert.ok(pushed.length >= 1, cas.res.headers.get('x-artifacts-ray'));
958 assert.notEqual(pushed.at(-1).ray, '-');
959 assert.notEqual(pushed.at(-1).ray, 'no-response');
960 assertSpanRays(cas.json.trace, casRays.entries);
961 assert.equal(gitBuf(bare, ['show', 'main:a.txt']).toString(), 'v2\n');
962 } finally {
963 raySrv.setBeforeReceive(null);
964 }
965});
967// ply #277: operation() turns a throw from its body into bad_request 'callback' (or 'ambiguous' after a push handover).
968// The Worker's conflicts are returned values, so each documented 409 holds, including a conflict learnt from a definite
969// cas_conflict push rejection (a racer moves the ref between ls-refs and receive-pack). A push whose outcome is unknown
970// is still ply's ambiguous (502, retry read).
971test('write conflicts are 409 values: pre-check and post-push definite rejection; an unknown push is ambiguous', { skip }, async () => {
972 const r = repo({ 'a.txt': 'a\n' }, () => raySrv.url);
973 const { bare, head } = r;
974 const post = async (path, body, method = 'POST') => { const res = await W(path, { method, body: { remote: r.remote, ...body } }); return { status: res.status, body: await res.json() }; };
975 const env = { ...process.env, GIT_AUTHOR_NAME: 'w', GIT_AUTHOR_EMAIL: 'w@x', GIT_COMMITTER_NAME: 'w', GIT_COMMITTER_EMAIL: 'w@x' };
976 const other = git(bare, ['commit-tree', git(bare, ['rev-parse', 'HEAD^{tree}']).trim(), '-p', head, '-m', 'other'], { env }).trim();
977 const refExists = (x, oid) => { assert.equal(x.status, 409, JSON.stringify(x.body)); assert.equal(x.body.code, 'ref_exists'); assert.equal(x.body.retry, 'no'); assert.equal(x.body.oid, oid); };
978 const stale = (x, expected, actual) => {
979 assert.equal(x.status, 409, JSON.stringify(x.body)); assert.equal(x.body.code, 'stale_expected'); assert.equal(x.body.retry, 'fresh');
980 assert.equal(x.body.expected, expected); assert.equal(x.body.actual, actual); assert.equal(x.body.oid, actual);
981 };
982 // pre-check conflicts (no push)
983 refExists(await post('/v1/branch', { ref: 'main', from: head }), head);
984 let x = await post('/v1/tag', { name: 't0', target: head }); assert.equal(x.status, 201, JSON.stringify(x.body));
985 refExists(await post('/v1/tag', { name: 't0', target: head }), head);
986 stale(await post('/v1/tag', { name: 't0', oid: other }, 'DELETE'), other, head);
987 x = await post('/v1/branch', { ref: 'b0', from: head }); assert.equal(x.status, 201);
988 stale(await post('/v1/branch', { ref: 'b0', oid: other }, 'DELETE'), other, head);
989 const A = { name: 'a', email: 'a@x', time: 1767225600, tz: '+0000' };
990 refExists(await post('/v1/edit', { ref: 'b0', base: 'main', path: 'a.txt', find: 'a', replace: 'b', author: A, message: 'm' }), head);
991 refExists(await post('/v1/edit', { ref: 'b0', base: 'main', changes: [{ op: 'create', path: 'n.txt', content: 'n\n' }], committer: A, author: A, message: 'm' }), head);
992 // post-push definite rejection: a racer moves (or creates) the ref after ls-refs, so git rejects the CAS
993 const race = (ref, to, from = '') => raySrv.setBeforeReceive(() => { raySrv.setBeforeReceive(null); git(bare, from ? ['update-ref', ref, to, from] : ['update-ref', ref, to, '0'.repeat(40)]); });
994 try {
995 race('refs/heads/b1', other); refExists(await post('/v1/branch', { ref: 'b1', from: head }), other);
996 race('refs/tags/t1', other); refExists(await post('/v1/tag', { name: 't1', target: head }), other);
997 race('refs/heads/b0', other, head); stale(await post('/v1/branch', { ref: 'b0' }, 'DELETE'), head, other);
998 race('refs/tags/t0', other, head); stale(await post('/v1/tag', { name: 't0', oid: head }, 'DELETE'), head, other);
999 race('refs/heads/b2', other); refExists(await post('/v1/edit', { ref: 'b2', base: 'main', path: 'a.txt', find: 'a', replace: 'b', author: A, message: 'm' }), other);
1000 race('refs/heads/b3', other); refExists(await post('/v1/edit', { ref: 'b3', base: 'main', changes: [{ op: 'create', path: 'n.txt', content: 'n\n' }], committer: A, author: A, message: 'm' }), other);
1001 // an unknown push outcome: the receive-pack POST is answered with a 500 after the request was handed over
1002 raySrv.setBeforeReceive(() => { raySrv.setBeforeReceive(null); throw new Error('dropped'); });
1003 x = await post('/v1/branch', { ref: 'b4', from: head });
1004 assert.equal(x.body.code, 'ambiguous', JSON.stringify(x.body)); assert.equal(x.status, 502); assert.equal(x.body.retry, 'read');
1005 } finally {
1006 raySrv.setBeforeReceive(null);
1008});
Diagnostics4 git calls · 342 ms
CallStatusSizeTime0342 ms
discover200152 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
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 · hit4 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=4
git.response.bytes=145
git.response.cache=HIT
git.response.duration_ms=4
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 1020040 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=41349
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=526b305930b8ef382f2d7a1f9c0238730a585921
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=41349
git.pkt.payload.max=41254
git.sideband.band1.bytes=41253
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=61
git.pack.bytes=41253
git.pack.ofs_delta=0
git.pack.ref_delta=0
git.pack.chain.max=0
git.pack.trailer=ab60efef4b7243678c75de64a4386986ee885c1c
git.pack.full=61
Open pack map
fetch20018 KiB335 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=334
git.response.bytes=18382
git.response.cache=MISS
git.response.duration_ms=335
git.fetch.wants=d6e4f3ff91aed5a63c4b752eecfbc96e87b371f6
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=18382
git.pkt.payload.max=18361
git.sideband.band1.bytes=18360
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-web.git
Ref
refs/heads/main
Commit
526b305930b8ef382f2d7a1f9c0238730a585921
cf-ray
a5e2c5f178cb4afc
Request
d2f4e5af…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format