GitHubivarvong
ply
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});
Diagnostics7 git calls · 133 ms
CallStatusSizeTime0133 ms
discover200191 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=191 git.server.agent=git/github-9922ec520325-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=191 git.pkt.payload.max=36
ls-refs20012 KiB98 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=89 git.response.bytes=11990 git.response.cache=EXPIRED git.response.duration_ms=98 git.protocol.version=2 git.pkt.frames=152 git.pkt.data=151 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=11990 git.pkt.payload.max=122 git.ls_refs.count=151 git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 102004.2 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=4347 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 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=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4347 git.pkt.payload.max=4247 git.sideband.band1.bytes=4247 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=10 git.pack.bytes=4247 git.pack.ofs_delta=0 git.pack.ref_delta=0 git.pack.chain.max=0 git.pack.trailer=a6caf8e129bea82c9bae4cbc7c130c9205add400 git.pack.full=10Open pack map
fetchtree:0200919 B · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=919 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=800d30ba8923860bfc460ca884b7f9ba22083588 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=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=919 git.pkt.payload.max=892 git.sideband.band1.bytes=892 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=892 git.pack.trailer=5323b9266eea5ded637285d1c5320699f12be76eOpen pack map
fetchtree:02005.6 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=5724 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=58fabb6768b3f737ee9cd8bac647bdc27405fb4d 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=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=5724 git.pkt.payload.max=5697 git.sideband.band1.bytes=5697 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=5697 git.pack.trailer=be7e0453974a5fe832f9065a33bb7e3434c10e9bOpen pack map
fetchtree:02001.4 KiB · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=1428 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=74d7bb09987bbe78a9c0ea8f43a1b5a4e079f538 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=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1428 git.pkt.payload.max=1401 git.sideband.band1.bytes=1401 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1401 git.pack.trailer=65ea1a3aeef333a80a2dfa3f441e629d90497e8eOpen pack map
fetch2003.0 KiB · hit23 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=23 git.response.bytes=3031 git.response.cache=HIT git.response.duration_ms=23 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=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=3031 git.pkt.payload.max=3004 git.sideband.band1.bytes=3004 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://github.com/ivarvong/ply
- Ref
- refs/heads/main
- Commit
- 19912563f540228593df0c489082e5e2508e21e4
- cf-ray
- a5e181459bf73014
- Request
- 912d6d52…
- Protocol
- v2 · sha1
- Agent
- git/github-9922ec520325-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format