GitHubivarvong

ply

main
ply6 git calls182 ms28 KiB4 cachedprotocol v2
162 lines15 KiBRaw
1// receive-pack parity: our push vs `git push` on identical repos, over the product of server configs × hooks ×
2// command kinds × capabilities. Oracle per cell: same accept/reject, same ref state, same stored objects,
3// same server message, hook stderr visible on band 2 when side-band is requested, fsck clean.
4import { test, beforeAll as before, afterAll as after } from 'vitest';
5import assert from 'node:assert/strict';
6import { join } from 'node:path';
7import { mkdirSync, writeFileSync, chmodSync, cpSync } from 'node:fs';
8import { git, gitBuf, tmpRoot, makeBareRepo, startGitServer, fsckProblems, initRepo } from './server.js';
9import { GitHttp, ZERO_OID } from '../src/client.js';
10import { Tracer, arrayExporter, flattenLine } from '../src/otel.js';
11import { TYPE } from '../src/objects.js';
12import { makeDelta } from '../src/pack.js';
13import { unwrap, unwrapAsync } from './unwrap.js';
15let root, cleanup, srv, n = 0;
16before(async () => { ({ root, cleanup } = tmpRoot()); srv = await startGitServer(root); });
17after(async () => { await srv.close(); cleanup(); });
19const HOOK = {
20 none: null,
21 'pre-receive-reject': ['pre-receive', '#!/bin/sh\necho "policy: nope" >&2\nexit 1\n'],
22 'pre-receive-accept-noisy': ['pre-receive', '#!/bin/sh\necho "hello from hook" >&2\necho "line two" >&2\nexit 0\n'],
23 'update-reject-feature': ['update', '#!/bin/sh\ncase "$1" in refs/heads/feature*) echo "feature branches are frozen" >&2; exit 1;; esac\nexit 0\n'],
24 // echoes the push options it received (GIT_PUSH_OPTION_COUNT / GIT_PUSH_OPTION_n) so the test can see them arrive
25 'pre-receive-echo-options': ['pre-receive', '#!/bin/sh\necho "opts=$GIT_PUSH_OPTION_COUNT:$GIT_PUSH_OPTION_0:$GIT_PUSH_OPTION_1" >&2\nexit 0\n'],
26};
27const CONFIG = {
28 default: {},
29 'deny-nonff': { 'receive.denyNonFastForwards': 'true' },
30 'deny-deletes': { 'receive.denyDeletes': 'true' },
31 'index-pack': { 'receive.unpackLimit': '1' }, // every push goes through index-pack --fix-thin --strict
32 'unpack-objects': { 'receive.unpackLimit': '1000' },
33 'no-fsck': { 'receive.fsckObjects': 'false', 'transfer.fsckObjects': 'false' },
34 'push-options': { 'receive.advertisePushOptions': 'true' },
35};
37// Build a twin: bare repo pair (ours / git's) seeded identically, plus a work clone holding the candidate commits.
38function twins(cfg, hook) {
39 const id = n++;
40 const seedWork = join(root, `w${id}`); mkdirSync(seedWork); initRepo(seedWork);
41 const env = { ...process.env, 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' };
42 writeFileSync(join(seedWork, 'f.txt'), 'base content\nMARK\n'); git(seedWork, ['add', '-A']); git(seedWork, ['commit', '-q', '-m', 'base'], { env });
43 const base = git(seedWork, ['rev-parse', 'HEAD']).trim();
44 writeFileSync(join(seedWork, 'f.txt'), 'base content\nEDITED\n'); git(seedWork, ['commit', '-q', '-am', 'ff'], { env });
45 const ff = git(seedWork, ['rev-parse', 'HEAD']).trim();
46 git(seedWork, ['checkout', '-q', base]); writeFileSync(join(seedWork, 'g.txt'), 'other'); git(seedWork, ['add', '-A']); git(seedWork, ['commit', '-q', '-m', 'diverged'], { env });
47 const diverged = git(seedWork, ['rev-parse', 'HEAD']).trim();
48 git(seedWork, ['checkout', '-q', '-b', 'main2', ff]);
49 const mk = tag => { const bare = makeBareRepo(root, `${tag}${id}`); git(seedWork, ['push', '-q', bare, `${base}:refs/heads/main`, `${base}:refs/heads/feature/x`]); for (const [k, v] of Object.entries(cfg)) git(bare, ['config', k, v]); if (hook) { const [name, body] = hook; const p = join(bare, 'hooks', name); writeFileSync(p, body); chmodSync(p, 0o755); } return bare; };
50 return { base, ff, diverged, work: seedWork, ours: mk('o'), theirs: mk('t'), id };
52const refsOf = bare => git(bare, ['for-each-ref', '--format=%(refname) %(objectname)']).trim();
53const objectsOf = bare => git(bare, ['cat-file', '--batch-check', '--batch-all-objects', '--unordered']).trim().split('\n').map(l => l.split(' ')[0]).sort().join('\n');
54const objectsFor = (work, commit) => { // full commit + tree + blobs for a one-commit push (what our pipeline would send, non-delta)
55 const out = [];
56 for (const l of git(work, ['rev-list', '--objects', '--no-object-names', commit, '-n1', `^${commit}~1`]).trim().split('\n')) {
57 const type = git(work, ['cat-file', '-t', l]).trim();
58 out.push({ type: TYPE[type.toUpperCase()], data: new Uint8Array(gitBuf(work, ['cat-file', type, l])) });
59 }
60 return out;
61};
62const gitPush = (work, bare, refspecs, extra = []) => { try { const out = git(work, ['push', '--porcelain', ...extra, bare, ...refspecs]); return { ok: true, out }; } catch (e) { return { ok: false, out: (e.stdout || '') + (e.stderr || ''), code: e.status }; } };
64// PUSH_MATRIX=full runs every cell (~20 min: each cell is a real `git push` plus ours). Default: a seeded 1-in-8 sample
65// (~3 min) whose seed rotates daily, so a week of `npm test` covers the matrix and any day's failure is reproducible with
66// PUSH_MATRIX_SEED=<n>. Cells are deterministic in (config, hook, command, caps), so sampling loses nothing but time.
67const FULL = process.env.PUSH_MATRIX === 'full';
68const SEED = +(process.env.PUSH_MATRIX_SEED ?? Math.floor(Date.now() / 86400000));
69const sampled = (() => { let x = SEED >>> 0 || 1; return label => { let h = x; for (const ch of label) h = Math.imul(h ^ ch.charCodeAt(0), 16777619) >>> 0; return FULL || (h % 8) === 0; }; })();
71test(`push matrix: configs × hooks × commands × caps, parity with git push (${FULL ? 'full' : `sample seed=${SEED}`})`, async () => {
72 const stats = { cells: 0, ok: 0, rejected: 0, skipped: 0 };
73 for (const [ck, cfg] of Object.entries(CONFIG)) for (const [hk, hook] of Object.entries(HOOK)) {
74 const cmds = {
75 'ff-update': t => ({ ours: { commands: [{ ref: 'refs/heads/main', oldOid: t.base, newOid: t.ff }], objects: objectsFor(t.work, t.ff) }, git: [`${t.ff}:refs/heads/main`] }),
76 'nonff-update': t => ({ ours: { commands: [{ ref: 'refs/heads/main', oldOid: t.base, newOid: t.diverged }], objects: objectsFor(t.work, t.diverged) }, git: [`+${t.diverged}:refs/heads/main`] }),
77 'create': t => ({ ours: { commands: [{ ref: 'refs/heads/new', oldOid: ZERO_OID, newOid: t.ff }], objects: objectsFor(t.work, t.ff) }, git: [`${t.ff}:refs/heads/new`] }),
78 'delete': t => ({ ours: { commands: [{ ref: 'refs/heads/feature/x', oldOid: t.base, newOid: ZERO_OID }], objects: [] }, git: [':refs/heads/feature/x'] }),
79 'multi': t => ({ ours: { commands: [{ ref: 'refs/heads/main', oldOid: t.base, newOid: t.ff }, { ref: 'refs/heads/feature/x', oldOid: t.base, newOid: t.ff }], objects: objectsFor(t.work, t.ff) }, git: [`${t.ff}:refs/heads/main`, `${t.ff}:refs/heads/feature/x`] }),
80 'multi-atomic': t => ({ ours: { commands: [{ ref: 'refs/heads/main', oldOid: t.base, newOid: t.ff }, { ref: 'refs/heads/feature/x', oldOid: t.base, newOid: t.ff }], objects: objectsFor(t.work, t.ff), atomic: true }, git: [`${t.ff}:refs/heads/main`, `${t.ff}:refs/heads/feature/x`], extra: ['--atomic'] }),
81 'stale-old': t => ({ ours: { commands: [{ ref: 'refs/heads/main', oldOid: t.diverged, newOid: t.ff }], objects: objectsFor(t.work, t.ff) }, git: [`${t.ff}:refs/heads/main`], extra: [`--force-with-lease=refs/heads/main:${t.diverged}`] }),
82 'thin-blob-delta': t => { const objs = objectsFor(t.work, t.ff); const oldBlob = git(t.work, ['rev-parse', `${t.base}:f.txt`]).trim(), newBlob = git(t.work, ['rev-parse', `${t.ff}:f.txt`]).trim(); const base = new Uint8Array(gitBuf(t.work, ['cat-file', 'blob', oldBlob])), target = new Uint8Array(gitBuf(t.work, ['cat-file', 'blob', newBlob])); const i = objs.findIndex(o => o.type === TYPE.BLOB && o.data.length === target.length); objs[i] = { type: TYPE.BLOB, data: makeDelta(base, target).delta, base: { tag: 'ref', oid: oldBlob } }; return { ours: { commands: [{ ref: 'refs/heads/main', oldOid: t.base, newOid: t.ff }], objects: objs }, git: [`${t.ff}:refs/heads/main`] }; },
83 };
84 for (const [kk, mkCmd] of Object.entries(cmds)) for (const caps of [{ sideband: false }, { sideband: true }, { sideband: false, reportV2: true }, { sideband: true, reportV2: true }, { sideband: true, pushOptions: ['ci.skip', 'reviewer=alice'] }]) { // sideband is explicit per cell: the default (on) is checked separately below
85 const label = `${ck}/${hk}/${kk}/${Object.entries(caps).filter(([, v]) => v).map(([k]) => k).join('+') || 'plain'}`;
86 if (!sampled(label)) { stats.skipped++; continue; }
87 stats.cells++;
88 const t = twins(cfg, hook);
89 const { ours, git: refspecs, extra = [] } = mkCmd(t);
90 const exporter = arrayExporter();
91 const client = unwrap(GitHttp.create(`${srv.url}/o${t.id}.git`, { tracer: new Tracer({ exporter }) }));
92 let res, err;
93 try { res = await unwrapAsync(client.push({ ...ours, ...caps })); } catch (e) { err = e; }
94 const g = gitPush(t.work, t.theirs, refspecs, [...extra, ...(caps.pushOptions ?? []).flatMap(o => ['-o', o])]);
95 // 1. accept/reject parity
96 if (caps.pushOptions && ck !== 'push-options') { // git: "the receiving end does not support push options" (client-side); ours: server rejects the capability
97 assert.ok(!g.ok, `${label}: git should refuse -o without advertisePushOptions`);
98 assert.equal(err?.code, 'unsupported', `${label}: ours ${err?.code} ${err?.message}`); // detected from the advertisement before sending anything
99 assert.equal(refsOf(t.ours), refsOf(t.theirs), `${label}: refs differ`);
100 stats.rejected++; continue;
101 }
102 assert.equal(!err, g.ok, `${label}: ours ${err ? 'rejected (' + err.code + ': ' + err.message + ')' : 'ok'} vs git ${g.ok ? 'ok' : 'rejected'}\n${g.out}`);
103 // 2. ref state parity
104 assert.equal(refsOf(t.ours), refsOf(t.theirs), `${label}: refs differ`);
105 // 3. stored objects parity (an accepted push stores the same objects; a rejected push may leave dangling ones on both)
106 if (!err) assert.equal(objectsOf(t.ours), objectsOf(t.theirs), `${label}: objects differ`);
107 // 4. reason parity: git's porcelain "!" line carries the server's reason
108 if (err) {
109 stats.rejected++;
110 // git porcelain: one "!" line per rejected ref with the server's reason; a client-side lease refusal never reaches the server
111 const gitRejects = new Map([...g.out.matchAll(/^!\s+\S*:(\S+)\s+\[remote rejected\]\s+\((.*)\)/gm)].map(m => [m[1], m[2]]));
112 const ourRejects = new Map(err.report?.refs?.filter(r => !r.ok).map(r => [r.ref, r.reason]) ?? []);
113 if (/stale info/.test(g.out)) assert.ok([...ourRejects.values()].every(r => /^(failed to update ref|pre-receive hook declined|hook declined|atomic push failure|atomic transaction failed|bad ref|incorrect old value provided|reference does not exist)$/.test(r)), `${label}: ${[...ourRejects.values()]}`);
114 else assert.deepEqual([...ourRejects].sort(), [...gitRejects].sort(), `${label}: per-ref reasons`);
115 const reasons = [...ourRejects.values()];
116 // the class table of test/space/push-ng.space.js: 'bad ref' (denyNonFastForwards could not parse a stale, unknown old oid) and
117 // git >= 2.51's batched texts are cas; receive-pack's own 'non-fast-forward' (denyNonFastForwards) is not_fast_forward
118 const isNff = r => r === 'non-fast-forward';
119 const isCas = r => /^(failed to update ref|failed to lock|atomic push failure|atomic transaction failed|bad ref|incorrect old value provided|reference does not exist)$/.test(r) || /fetch first|already exists/.test(r);
120 const expectCas = reasons.length && reasons.every(r => isCas(r) || isNff(r)) && reasons.some(r => r !== 'atomic push failure');
121 // a non-atomic push git applied in part is push_partial, naming what landed (SAFETY.md I33)
122 const landed = err.report?.refs?.filter(r => r.ok).map(r => r.ref) ?? [];
123 if (landed.length && !ours.atomic) { assert.equal(err.code, 'push_partial', `${label}: ${err.message}`); assert.deepEqual(err.landed, landed, label); }
124 else assert.equal(err.code, expectCas ? (reasons.some(isNff) ? 'not_fast_forward' : 'cas_conflict') : 'push_rejected', `${label}: code for ${JSON.stringify(reasons)}`);
125 } else stats.ok++;
126 // 5. hook output on band 2 when requested
127 if (caps.sideband && hk === 'pre-receive-accept-noisy') assert.match((err?.hookOutput ?? res.hookOutput), /hello from hook\r?\nline two/, `${label}: band 2`);
128 if (!caps.sideband) assert.equal((err?.hookOutput ?? res?.hookOutput ?? ''), '', `${label}: no band 2 without side-band`);
129 // 6. report-status-v2 is accepted; git 2.43 emits `option` lines only through proc-receive, so none are expected here
130 if (caps.reportV2 && !err) assert.deepEqual(res.report.refs.map(r => ({ ...r.options })), res.report.refs.map(() => ({})), label);
131 // 6b. push options: only valid when the server advertises them; git refuses client-side otherwise, we get the server's refusal
132 if (caps.pushOptions && !err && hk === 'pre-receive-echo-options') assert.match(res.hookOutput, /opts=2:ci\.skip:reviewer=alice/, `${label}: options reached the hook`); // only this hook prints them
133 // 7. server state is sound
134 assert.deepEqual(fsckProblems(t.ours), [], label);
135 // 8. the receive-pack span records the outcome
136 const span = exporter.lines.flatMap(flattenLine).find(s => s.name === 'git.receive-pack');
137 assert.equal(span.attrs['git.push.ok'], !err, label);
138 }
139 }
140 console.log(` cells=${stats.cells} accepted=${stats.ok} rejected=${stats.rejected} skipped=${stats.skipped}${FULL ? "" : " (PUSH_MATRIX=full for all)"}`);
141 assert.ok(stats.cells > 0);
142});
144test('push default: side-band-64k is requested unless disabled, and band 2 lands in git.push.remote.messages', async () => {
145 const bare = makeBareRepo(root, 'sbdef'); const work = join(root, 'wsbdef'); mkdirSync(work); initRepo(work);
146 const { commitFiles } = await import('./server.js');
147 const head = commitFiles(work, bare, { 'f.txt': 'x\n' }, 'seed');
148 const hooks = join(bare, 'hooks'); mkdirSync(hooks, { recursive: true });
149 writeFileSync(join(hooks, 'pre-receive'), '#!/bin/sh\necho "from hook" >&2\nexit 0\n'); chmodSync(join(hooks, 'pre-receive'), 0o755);
150 const ex = arrayExporter();
151 const c = unwrap(GitHttp.create(`${srv.url}/sbdef.git`, { tracer: new Tracer({ exporter: ex }) }));
152 const blob = new TextEncoder().encode('y\n');
153 const treeData = unwrap((await import('../src/objects.js')).encodeTree([{ mode: '100644', name: 'f.txt', oid: (await import('../src/objects.js')).hashObject('blob', blob) }]));
154 const commitData = unwrap((await import('../src/objects.js')).encodeCommit({ tree: (await import('../src/objects.js')).hashObject('tree', treeData), parents: [head], author: 'a <a@x> 1 +0000', message: 'm\n' }));
155 const newOid = (await import('../src/objects.js')).hashObject('commit', commitData);
156 const r = await unwrapAsync(c.push({ ref: 'refs/heads/main', oldOid: head, newOid, objects: [{ type: TYPE.COMMIT, data: commitData }, { type: TYPE.TREE, data: treeData }, { type: TYPE.BLOB, data: blob }] }));
157 assert.equal(r.hookOutput.trim(), 'from hook');
158 const span = ex.lines.flatMap(flattenLine).find(s => s.name === 'git.receive-pack');
159 assert.ok(span.attrs['git.push.caps'].includes('side-band-64k'));
160 assert.deepEqual(span.attrs['git.push.remote.messages'], ['from hook']);
161 assert.equal(git(bare, ['rev-parse', 'main']).trim(), newOid);
162});
Diagnostics6 git calls · 182 ms
CallStatusSizeTime0182 ms
discover200191 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=1
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 KiB · 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=11990
git.response.cache=HIT
git.response.duration_ms=2
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 · hit1 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=1
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=10
Open pack map
fetchtree:0200919 B · hit3 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=3
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=5323b9266eea5ded637285d1c5320699f12be76e
Open pack map
fetchtree:02005.6 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=173
git.response.bytes=5724
git.response.cache=HIT
git.response.duration_ms=2
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=be7e0453974a5fe832f9065a33bb7e3434c10e9b
Open pack map
fetch2005.4 KiB173 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=5508
git.response.cache=MISS
git.response.duration_ms=173
git.fetch.wants=e6fea99e5986b10d21eee5b488845f41c1ef0abc
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=5508
git.pkt.payload.max=5481
git.sideband.band1.bytes=5481
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
a5e0f8429b43332c
Request
35372bf9…
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