main
1// The ref-name space (refname.space.js, #73): every cell's name asked of real git three ways — check-ref-format, and a
2// real receive-pack's answer to a create and to a delete of it — and ply must agree on every cell: validateRef with
3// check-ref-format, validatePushRef with receive-pack ("funny refname" or not), and push() refuses before sending
4// exactly the names receive-pack calls funny.
5import { test } from 'vitest';
6import assert from 'node:assert/strict';
7import { spawn, spawnSync } from 'node:child_process';
8import { mkdtempSync, rmSync } from 'node:fs';
9import { tmpdir } from 'node:os';
10import { join } from 'node:path';
11import { cells, COMPONENTS, DEPTHS } from './refname.space.js';
12import { validateRef, validatePushRef } from '../../src/validate.js';
13import { pkt, FLUSH } from '../../src/pktline.js';
14import { concat, te } from '../../src/bytes.js';
15import { runPush } from './harness.js';
16import { createHash } from 'node:crypto';
17import { unwrap } from '../unwrap.js';
19const ENV = { ...Object.fromEntries(Object.entries(process.env).filter(([k]) => !k.startsWith('GIT_'))), GIT_CONFIG_NOSYSTEM: '1', GIT_CONFIG_GLOBAL: '/dev/null', GIT_AUTHOR_NAME: 'a', GIT_AUTHOR_EMAIL: 'a@x', GIT_COMMITTER_NAME: 'a', GIT_COMMITTER_EMAIL: 'a@x', GIT_AUTHOR_DATE: '1700000000 +0000', GIT_COMMITTER_DATE: '1700000000 +0000' };
20const ZERO = '0'.repeat(40);
21const accepts = f => { try { f(); return true; } catch (e) { if (e.code !== 'bad_request') throw e; return false; } };
23// git check-ref-format <name>, for every name, a few at a time
24async function checkRefFormat(names) {
25 const out = Array.from({ length: names.length });
26 let next = 0;
27 const one = i => new Promise((res, rej) => {
28 const p = spawn('git', ['check-ref-format', names[i]], { env: ENV, stdio: 'ignore' });
29 p.on('error', rej); p.on('close', code => { out[i] = code === 0; res(); });
30 });
31 await Promise.all(Array.from({ length: 16 }, async () => { while (next < names.length) await one(next++); }));
32 return out;
33}
35// One real `git receive-pack --stateless-rpc` answering one command per name, as a client sends them: true where git
36// answered "ng <name> funny refname". Every other answer (ok, a D/F conflict between two cells' names) is not funny.
37function receivePackFunny(dir, names, { oldOid, newOid }) {
38 const lines = names.map((n, i) => unwrap(pkt(concat([te.encode(`${oldOid} ${newOid} `), te.encode(n), te.encode(i === 0 ? '\0report-status delete-refs\n' : '\n')]))));
39 const head = new Uint8Array(12); head.set(te.encode('PACK')); head[7] = 2; // version 2, 0 objects
40 const pack = newOid === ZERO ? [] : [head, createHash('sha1').update(head).digest()];
41 const r = spawnSync('git', ['receive-pack', '--stateless-rpc', dir], { env: ENV, input: concat([...lines, FLUSH, ...pack]), maxBuffer: 1 << 26 });
42 assert.equal(r.status, 0, `receive-pack: ${r.stderr}`);
43 // the report: pkt lines "unpack ok", then one "ok <ref>" / "ng <ref> <reason>" per command, in command order
44 const buf = r.stdout, report = [];
45 for (let i = 0; i < buf.length;) {
46 const len = parseInt(buf.subarray(i, i + 4).toString(), 16);
47 if (len === 0) { i += 4; continue; }
48 report.push(buf.subarray(i + 4, i + len)); i += len;
49 }
50 assert.equal(report[0].toString(), 'unpack ok\n');
51 assert.equal(report.length, names.length + 1, 'one status per command');
52 return names.map((n, i) => {
53 const line = report[i + 1], ref = Buffer.from(te.encode(n));
54 const status = line.subarray(0, 3).toString();
55 assert.ok((status === 'ok ' || status === 'ng ') && line.subarray(3, 3 + ref.length).equals(ref), `status ${i} is for ${JSON.stringify(n)}: ${JSON.stringify(line.toString())}`);
56 return status === 'ng ' && line.subarray(3 + ref.length).toString() === ' funny refname\n';
57 });
58}
60test('ref names: ply agrees with git check-ref-format and receive-pack on every cell', { timeout: 300_000 }, async () => {
61 const all = cells(), names = all.map(c => c.name);
62 assert.equal(new Set(names).size, names.length, 'cells are distinct names');
63 const root = mkdtempSync(join(tmpdir(), 'ply-refname-'));
64 try {
65 const dir = join(root, 'srv.git');
66 spawnSync('git', ['init', '-q', '--bare', dir], { env: ENV });
67 const tree = spawnSync('git', ['mktree'], { cwd: dir, env: ENV, input: '' }).stdout.toString().trim();
68 const C = spawnSync('git', ['commit-tree', tree, '-m', 'c'], { cwd: dir, env: ENV }).stdout.toString().trim();
69 assert.match(C, /^[0-9a-f]{40}$/);
70 const t0 = performance.now();
71 const format = await checkRefFormat(names);
72 const t1 = performance.now();
73 const createFunny = receivePackFunny(dir, names, { oldOid: ZERO, newOid: C });
74 const deleteFunny = receivePackFunny(dir, names, { oldOid: C, newOid: ZERO });
75 const ms = performance.now() - t0;
76 const wrong = [], mainWrong = [], tally = { format: 0, create: 0, delete: 0 };
77 for (let i = 0; i < all.length; i++) {
78 const n = names[i], id = `${all[i].id} ${JSON.stringify(n)}`;
79 const git = { format: format[i], create: !createFunny[i], delete: !deleteFunny[i] };
80 const ply = { format: accepts(() => unwrap(validateRef(n))), create: accepts(() => unwrap(validatePushRef(n, false))), delete: accepts(() => unwrap(validatePushRef(n, true))) };
81 for (const k of Object.keys(git)) {
82 if (git[k]) tally[k]++;
83 if (git[k] !== ply[k]) wrong.push(`${id} ${k}: git ${git[k] ? 'accepts' : 'refuses'}, ply ${ply[k] ? 'accepts' : 'refuses'}`);
84 }
85 // what main (validateRef alone before every push) did with the same cell
86 for (const k of ['create', 'delete']) if (ply.format !== git[k]) mainWrong.push(`${id} ${k}`);
87 }
88 console.log(` refname: ${all.length} cells (${COMPONENTS.length} components at depths ${DEPTHS.join('/')} + a U+0001–U+00FF sweep) × 3 questions, git ${spawnSync('git', ['--version']).stdout.toString().trim()} in ${(ms / 1000).toFixed(1)} s (check-ref-format ${((t1 - t0) / 1000).toFixed(1)} s); git accepts: format ${tally.format}, create ${tally.create}, delete ${tally.delete}; ply disagrees ${wrong.length}; validateRef-before-push (main) disagreed ${mainWrong.length}`);
89 for (const w of wrong.slice(0, 20)) console.log(` ${w}`);
90 for (const w of mainWrong) console.log(` main: ${w}`);
91 assert.deepEqual(wrong, []);
92 // push() refuses exactly git's funny names before sending a byte, and lets the rest through to the server
93 const sample = all.filter((_, i) => i % 7 === 0 || createFunny[i] !== !format[i] || deleteFunny[i] !== !format[i]);
94 for (const c of sample) {
95 const i = all.indexOf(c);
96 for (const [del, funny] of [[false, createFunny[i]], [true, deleteFunny[i]]]) {
97 const got = await runPush({ commands: [{ ref: c.name, oldOid: del ? C : ZERO, newOid: del ? ZERO : C }], opts: { sideband: false }, limits: { maxRefs: 8, maxReportLines: 16, maxPackBytes: 1 << 20 } }, { drop: 'never' });
98 const refusedBeforeSend = got.tag === 'preSend' && got.code === 'bad_request';
99 assert.equal(refusedBeforeSend, funny, `${c.id} ${JSON.stringify(c.name)} ${del ? 'delete' : 'create'}: push() ${JSON.stringify(got)}, receive-pack ${funny ? 'funny' : 'not funny'}`);
100 }
101 }
102 } finally { rmSync(root, { recursive: true, force: true }); }
103});
Diagnostics4 git calls · 334 ms
CallStatusSizeTime0334 ms
discover200152 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 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 · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=145 git.response.cache=HIT git.response.duration_ms=2 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 · hit4 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=38434 git.response.cache=HIT git.response.duration_ms=4 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
fetch2003.0 KiB328 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=328 git.response.bytes=3034 git.response.cache=MISS git.response.duration_ms=328 git.fetch.wants=9fdb5c2888c2a1999686c1b9d0d596e9dfa7fa6e 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=3034 git.pkt.payload.max=3013 git.sideband.band1.bytes=3012 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
- a5e2cb29aa057929
- Request
- f68b48fc…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format