GitHubivarvong
ply
main
1// Issue #43: git never interprets the bytes of a name, a message, an ident or a ref. Every cell of
2// byte content × object field × operation
3// is run against git as the oracle (`ls-tree -z`, `cat-file`, `for-each-ref`, `merge-tree`, `check-attr` through
4// merge-tree's attr.tree) over git http-backend. A cell passes when ply returns the same bytes or names git does, or
5// when both refuse (git cannot store or read the value, and ply's refusal is typed). A ply refusal where git succeeds is
6// allowed only with a reason listed in ALLOWED_REFUSALS. Strings carry bytes as bytes.js decodeBytes maps them:
7// well-formed UTF-8 as itself (a BOM anywhere stays), every other byte as U+DC00 + byte; encodeBytes is the inverse.
8import { test, beforeAll as before, afterAll as after } from 'vitest';
9import assert from 'node:assert/strict';
10import { spawnSync } from 'node:child_process';
11import { cpSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
12import { join } from 'node:path';
13import { tmpdir } from 'node:os';
14import { tmpRoot, makeBareRepo, startGitServer } from './server.js';
15import { readFile, listTree, readCommit } from '../src/read.js';
16import { checkoutTree } from '../src/checkout.js';
17import { writeFiles } from '../src/checkout-node.js';
18import { DEFAULT_LIMITS } from '../src/limits.js';
19import { editFile } from '../src/edit.js';
20import { mergeBranch } from '../src/merge.js';
21import { decodeBytes, encodeBytes } from '../src/bytes.js';
22import { isPlyCode } from '../src/errors.js';
23import { Tracer } from '../src/otel.js';
24import { unwrap, unwrapAsync } from './unwrap.js';
26const O = (extra = {}) => ({ tracer: new Tracer(), ...extra });
28// ---- the space ----
29const BOM = [0xef, 0xbb, 0xbf];
30// Each content puts its bytes into a base string: at the front for the leading BOM, after the first byte otherwise.
31export const CONTENT = {
32 leadingBom: s => B(BOM, s),
33 midBom: s => B(s.slice(0, 1), BOM, s.slice(1)),
34 invalidUtf8: s => B(s.slice(0, 1), [0xff, 0x80], s.slice(1)),
35 overlong: s => B(s.slice(0, 1), [0xc0, 0xaf, 0xe0, 0x80, 0xae], s.slice(1)), // overlong '/' and '.'
36 nul: s => B(s.slice(0, 1), [0x00], s.slice(1)),
37 cr: s => B(s.slice(0, 1), [0x0d], s.slice(1)),
38 multibyte: s => B(s.slice(0, 1), 'é€🚀', s.slice(1)),
39 latin1: s => B(s.slice(0, 1), [0xe9, 0xe8], s.slice(1)),
40};
41export const FIELDS = ['treeName', 'commitMessage', 'authorName', 'refName', 'attributesPattern', 'tagName'];
42export const OPS = ['readFile', 'listTree', 'readCommit', 'checkoutTree', 'editFile', 'merge'];
44// A ply refusal where git succeeds, with the reason it is right.
45// A commit with a NUL in its message or an ident is an error to `git fsck --strict` (nulInCommit, nulInHeader), and a
46// commit read accepts exactly what fsck --strict accepts (SAFETY.md I38, issue #59), so every read refuses it, typed,
47// with git's msgId, where git's porcelain still prints it.
48const fsckNul = e => e.code === 'pack_corrupt' && /^commit: nul(InCommit|InHeader): /.test(e.message);
49const ALLOWED_REFUSALS = {
50 // APFS stores names as UTF-8 and answers EILSEQ to anything else; `git clone` fails on that file the same way.
51 // `git merge <annotated tag>` writes a mergetag header, which ply does not; it says so instead of merging without it.
52 merge: e => e.code === 'unsupported' && /is an annotated tag/.test(e.message),
53 checkoutTree: e => fsckNul(e) || (e.code === 'unsupported' && /filesystem refuses the name bytes/.test(e.message) && process.platform === 'darwin'),
54 readFile: fsckNul,
55 listTree: fsckNul,
56 readCommit: fsckNul,
57};
59function B(...parts) { return Buffer.concat(parts.map(p => (typeof p === 'string' ? Buffer.from(p, 'utf8') : Buffer.from(p)))); }
61// ---- git, bytes in and out ----
62const run = (cwd, args, { input, env } = {}) => spawnSync('git', args, { cwd, input, env: env ? { ...process.env, ...env } : process.env, maxBuffer: 1 << 26 });
63const gitOk = (cwd, args, opts) => { const r = run(cwd, args, opts); if (r.status !== 0) throw new Error(`git ${args.join(' ')}: ${r.stderr}`); return r.stdout; };
64const hash = (bare, type, data) => gitOk(bare, ['hash-object', '-w', '--literally', '-t', type, '--stdin'], { input: data }).toString().trim();
65const sortKey = e => (e.mode === '40000' ? B(e.name, '/') : e.name);
66const rawTree = entries => B(...[...entries].sort((a, b) => Buffer.compare(sortKey(a), sortKey(b))).flatMap(e => [B(`${e.mode} `), e.name, [0], Buffer.from(e.oid, 'hex')]));
67const IDENT_TAIL = ' <a@x> 1700000000 +0000';
68const rawCommit = ({ tree, parents = [], author = B('A U Thor'), message = B('m\n') }) => B(`tree ${tree}\n`, ...parents.map(p => `parent ${p}\n`), 'author ', author, IDENT_TAIL, '\ncommitter C', IDENT_TAIL, '\n\n', message);
69// ls-tree -z: [{ mode, type, oid, name: Buffer }]; throws when git cannot read the tree
70const lsTree = (bare, rev) => { const out = gitOk(bare, ['ls-tree', '-z', rev]); const rows = []; let p = 0; while (p < out.length) { const nul = out.indexOf(0, p); const line = out.subarray(p, nul); const tab = line.indexOf(9); const [mode, type, oid] = line.subarray(0, tab).toString().split(' '); rows.push({ mode, type, oid, name: Buffer.from(line.subarray(tab + 1)) }); p = nul + 1; } return rows; };
71const catCommit = (bare, rev) => { const raw = gitOk(bare, ['cat-file', 'commit', rev]); const blank = raw.indexOf('\n\n'); const head = raw.subarray(0, blank).toString('latin1').split('\n'); const field = k => head.filter(l => l.startsWith(k + ' ')).map(l => Buffer.from(l.slice(k.length + 1), 'latin1')); const author = field('author')[0]; const lt = author.indexOf(' <'); return { tree: field('tree')[0].toString(), parents: field('parent').map(String), authorName: author.subarray(0, lt), message: raw.subarray(blank + 2) }; };
72// Ref names never go through argv (it is UTF-8 to node): git lists them as bytes, and is asked by oid. for-each-ref
73// skips a name check-ref-format refuses ("ignoring ref with broken name"), so absence is git's refusal.
74const gitRefs = bare => { const out = gitOk(bare, ['for-each-ref', '--format=%(objectname) %(*objectname) %(refname)%00']); const m = new Map(); let p = 0; while (p < out.length) { const nul = out.indexOf(0, p); const line = out.subarray(p, nul); const [oid, peeled] = line.subarray(0, 82).toString().split(' '); m.set(Buffer.from(line.subarray(line.indexOf(32, 41) + 1)).toString('latin1'), { oid, commit: peeled || oid }); p = nul + 2; } return m; };
75const refAt = (bare, name) => { const r = gitRefs(bare).get(Buffer.from(unwrap(encodeBytes(name))).toString('latin1')); if (!r) throw new Error(`git: no ref ${JSON.stringify(name)} (for-each-ref lists no such name)`); return r.commit; };
76const refList = bare => [...gitRefs(bare)].map(([n, r]) => `${r.oid} ${n}`).join('\n');
77const bytesOf = s => Buffer.from(unwrap(encodeBytes(s)));
78const drain = async stream => { const parts = []; for await (const c of stream) parts.push(Buffer.from(c)); return Buffer.concat(parts); };
80// ---- one fixture per (content, field) ----
81// main: C0 (f.txt, g.txt[, .gitattributes]) → C1 (f.txt line 1). side: C0 → S1 (f.txt line 3, plus the field's bytes).
82// The field's bytes: treeName, a file of that name on side; commitMessage / authorName, S1's; refName, a branch at S1
83// (the read target); attributesPattern, a .gitattributes line `<pattern> merge=union` on both sides and a file the
84// pattern names that both sides change, so the merge is clean only when the pattern matches; tagName, an annotated tag
85// object at S1 whose `tag` header and ref carry the bytes (the read target).
86// Refs live in reftable where git has it (2.46: `git refs migrate`), so no filesystem refuses a ref name and every
87// host runs the cells git accepts (on APFS a loose ref file with a non-UTF-8 name is EILSEQ, which hid #43's push
88// report bug until Linux CI ran it). REF_FORMAT=files forces the files backend; expectations follow what git does.
89const REF_FORMAT = process.env.REF_FORMAT ?? (() => { const d = mkdtempSync(join(tmpdir(), 'ply-reftable-')); try { return run(d, ['init', '-q', '--bare']).status === 0 && run(d, ['refs', 'migrate', '--ref-format=reftable']).status === 0 ? 'reftable' : 'files'; } finally { rmSync(d, { recursive: true, force: true }); } })();
90const F_BASE = 'line1\nline2\nline3\n';
91function fixture(root, id, content, field) {
92 const bare = makeBareRepo(root, id);
93 if (REF_FORMAT === 'reftable') gitOk(bare, ['refs', 'migrate', '--ref-format=reftable']);
94 const blob = data => hash(bare, 'blob', B(data));
95 const hostile = s => CONTENT[content](s);
96 const f0 = blob(F_BASE), f1 = blob('LINE1\nline2\nline3\n'), fs1 = blob('line1\nline2\nLINE3\n'), g = blob('g\n');
97 const base = [{ mode: '100644', name: B('f.txt'), oid: f0 }, { mode: '100644', name: B('g.txt'), oid: g }];
98 const fx = { bare, field, content, target: 'refs/heads/side', from: 'refs/heads/side', extraRefs: [] };
99 let attrFile = null;
100 if (field === 'attributesPattern') {
101 const pattern = hostile('p.txt');
102 const named = Buffer.from(pattern.filter(b => b !== 0));
103 attrFile = { pattern, named, ga: blob(B(pattern, ' merge=union\n')), u0: blob('u0\n'), uo: blob('uo\n'), ut: blob('ut\n') };
104 base.push({ mode: '100644', name: B('.gitattributes'), oid: attrFile.ga }, { mode: '100644', name: named, oid: attrFile.u0 });
105 }
106 const t0 = hash(bare, 'tree', rawTree(base));
107 const c0 = hash(bare, 'commit', rawCommit({ tree: t0 }));
108 const swap = (entries, name, oid) => entries.map(e => (e.name.equals(B(name)) ? { ...e, oid } : e));
109 let mainEntries = swap(base, 'f.txt', f1), sideEntries = swap(base, 'f.txt', fs1);
110 if (attrFile) { mainEntries = swap(mainEntries, attrFile.named, attrFile.uo); sideEntries = swap(sideEntries, attrFile.named, attrFile.ut); }
111 if (field === 'treeName') sideEntries = [...sideEntries, { mode: '100644', name: hostile('n.txt'), oid: blob('hostile name\n') }];
112 const c1 = hash(bare, 'commit', rawCommit({ tree: hash(bare, 'tree', rawTree(mainEntries)), parents: [c0], message: B('main\n') }));
113 const s1 = hash(bare, 'commit', rawCommit({ tree: hash(bare, 'tree', rawTree(sideEntries)), parents: [c0], author: field === 'authorName' ? hostile('A U Thor') : B('A U Thor'), message: field === 'commitMessage' ? B(hostile('side commit'), '\n') : B('side\n') }));
114 const refs = [[B('refs/heads/main'), c1], [B('refs/heads/side'), s1]];
115 const peeled = new Map();
116 // a NUL ends the name in packed-refs: that ref cannot exist, and git refuses every operation on it
117 if (field === 'refName') { const name = B('refs/heads/', hostile('hr')); if (!name.includes(0)) refs.push([name, s1]); fx.target = fx.from = decodeBytes(name); }
118 if (field === 'tagName') {
119 const tagName = hostile('tg');
120 const tag = hash(bare, 'tag', B(`object ${s1}\ntype commit\ntag `, tagName, '\ntagger T', IDENT_TAIL, '\n\ntag\n'));
121 const name = B('refs/tags/', tagName.filter(b => b !== 0));
122 refs.push([name, tag]); peeled.set(name.toString('latin1'), s1); fx.target = fx.from = decodeBytes(name);
123 }
124 // packed-refs, not update-ref: a loose ref is a file, and the filesystem may refuse its name (APFS: EILSEQ)
125 refs.sort((a, b) => Buffer.compare(a[0], b[0]));
126 // reftable: one update-ref per name; a name git refuses to store (a CR) is simply absent, as it is to for-each-ref
127 if (REF_FORMAT === 'reftable') { for (const [n, o] of refs) run(bare, ['update-ref', '--stdin'], { input: B('update ', n, ` ${o}\n`) }); fx.refsBefore = refList(bare); return fx; }
128 writeFileSync(join(bare, 'packed-refs'), B('# pack-refs with: peeled fully-peeled sorted \n', ...refs.flatMap(([n, o]) => [`${o} `, n, '\n', ...(peeled.has(n.toString('latin1')) ? [`^${peeled.get(n.toString('latin1'))}\n`] : [])])));
129 fx.refsBefore = refList(bare);
130 return fx;
131}
133// ---- the oracle and ply, per operation ----
134// The target ref must be one git lists (check-ref-format); otherwise git refuses every operation on it.
135// Each returns { git, ply } normalized to comparable values, or throws (git's refusal, ply's refusal).
136const WHO = { name: 'P Ly', email: 'p@x' };
137const fieldIn = (fx, s) => decodeBytes(CONTENT[fx.content](s));
138const OPERATIONS = {
139 async readFile(fx, url) {
140 const rows = lsTree(fx.bare, revOf(fx)).filter(r => r.type === 'blob');
141 const git = rows.map(r => ({ name: r.name.toString('hex'), data: gitOk(fx.bare, ['cat-file', 'blob', r.oid]).toString('hex') }));
142 return { git, ply: async () => { const out = []; for (const r of rows) { const f = await unwrapAsync(readFile(url, { ref: fx.target, path: decodeBytes(r.name) }, O())); out.push({ name: r.name.toString('hex'), data: (await drain(f.stream)).toString('hex') }); } return out; } };
143 },
144 async listTree(fx, url) {
145 const git = lsTree(fx.bare, revOf(fx)).map(r => ({ name: r.name.toString('hex'), mode: r.mode, oid: r.oid }));
146 return { git, ply: async () => (await unwrapAsync(listTree(url, { ref: fx.target }, O()))).entries.map(e => ({ name: bytesOf(e.name).toString('hex'), mode: e.mode, oid: e.oid })) };
147 },
148 async readCommit(fx, url) {
149 const c = catCommit(fx.bare, revOf(fx));
150 const git = { tree: c.tree, parents: c.parents, author: c.authorName.toString('hex'), message: c.message.toString('hex') };
151 return { git, ply: async () => { const r = await unwrapAsync(readCommit(url, { ref: fx.target }, O())); return { tree: r.tree, parents: r.parents, author: bytesOf(r.author.name).toString('hex'), message: bytesOf(r.message).toString('hex') }; } };
152 },
153 async checkoutTree(fx, url) {
154 const rows = lsTree(fx.bare, revOf(fx)).filter(r => r.type === 'blob');
155 const git = rows.map(r => ({ path: r.name.toString('hex'), data: gitOk(fx.bare, ['cat-file', 'blob', r.oid]).toString('hex') })).sort(byPath);
156 return { git, ply: async () => {
157 const co = await unwrapAsync(checkoutTree(url, { ref: fx.target }, O()));
158 const files = []; for await (const f of co.files) files.push({ path: bytesOf(f.path).toString('hex'), data: (await drain(f.stream)).toString('hex') });
159 // and onto a filesystem: the names on disk are git's bytes
160 const dir = mkdtempSync(join(fx.bare, '..', 'co-'));
161 try {
162 const written = await writeFiles(dir, (await unwrapAsync(checkoutTree(url, { ref: fx.target }, O()))).files, { limits: DEFAULT_LIMITS, platform: process.platform });
163 if (!written.ok) throw written.error;
164 const disk = readdirSync(dir, { encoding: 'buffer' }).map(n => n.toString('hex')).sort();
165 assert.deepEqual(disk, files.map(f => f.path).sort(), 'names on disk');
166 } finally { rmSync(dir, { recursive: true, force: true }); }
167 return files.sort(byPath);
168 } };
169 },
170 async editFile(fx, url) {
171 // the edit is on the target branch (for a tag, on side); the caller's message / author carry the field's bytes
172 const ref = fx.field === 'tagName' ? 'refs/heads/side' : fx.target;
173 const message = fx.field === 'commitMessage' ? fieldIn(fx, 'edit message') : 'edit';
174 const author = fx.field === 'authorName' ? { ...WHO, name: fieldIn(fx, 'P Ly') } : WHO;
175 const old = refAt(fx.bare, ref);
176 gitCanUpdate(fx.bare, ref, old);
177 const oldRows = lsTree(fx.bare, old);
178 const fOld = oldRows.find(r => r.name.equals(B('f.txt'))).oid;
179 const newBlob = gitOk(fx.bare, ['hash-object', '--stdin'], { input: B(gitOk(fx.bare, ['cat-file', 'blob', fOld]).toString().replace('line2', 'EDITED')) }).toString().trim();
180 const git = { tree: oldRows.map(r => ({ name: r.name.toString('hex'), mode: r.mode, oid: r.name.equals(B('f.txt')) ? newBlob : r.oid })), parents: [old], author: bytesOf(author.name).toString('hex'), message: B(unwrap(encodeBytes(message)), '\n').toString('hex') };
181 gitCan(message, author.name);
182 return { git, ply: async () => {
183 const r = await unwrapAsync(editFile(url, { ref, path: 'f.txt', find: 'line2', replace: 'EDITED', author, message }, O()));
184 assert.equal(r.outcome ?? 'ok', 'ok');
185 const now = refAt(fx.bare, ref);
186 // no other ref appeared or moved (a ref name read as U+FFFD would push a new ref)
187 const others = list => list.split('\n').filter(l => !l.endsWith(` ${Buffer.from(unwrap(encodeBytes(ref))).toString('latin1')}`)).join('\n');
188 assert.equal(others(refList(fx.bare)), others(fx.refsBefore), 'refs other than the edited one');
189 const c = catCommit(fx.bare, now);
190 return { tree: lsTree(fx.bare, now).map(x => ({ name: x.name.toString('hex'), mode: x.mode, oid: x.oid })), parents: c.parents, author: c.authorName.toString('hex'), message: c.message.toString('hex') };
191 } };
192 },
193 async merge(fx, url) {
194 const message = fx.field === 'commitMessage' ? fieldIn(fx, 'merge message') : 'merge';
195 const author = fx.field === 'authorName' ? { ...WHO, name: fieldIn(fx, 'P Ly') } : WHO;
196 gitCan(message, author.name);
197 const r = run(fx.bare, ['-c', 'attr.tree=refs/heads/main', 'merge-tree', '--write-tree', 'refs/heads/main', refAt(fx.bare, fx.from)]);
198 if (r.status !== 0 && r.status !== 1) throw new Error(`git merge-tree: ${r.stderr}`);
199 const git = r.status === 0 ? { clean: true, tree: r.stdout.toString().split('\n')[0], message: B(unwrap(encodeBytes(message)), '\n').toString('hex'), author: bytesOf(author.name).toString('hex') } : { clean: false };
200 return { git, ply: async () => {
201 let m; try { m = await unwrapAsync(mergeBranch(url, { ref: 'refs/heads/main', from: fx.from, author, message }, O({ attributes: 'ours' }))); } catch (e) { if (e.code === 'merge_conflict') return { clean: false }; throw e; }
202 const c = catCommit(fx.bare, 'refs/heads/main');
203 assert.equal(m.outcome, 'merge');
204 return { clean: true, tree: c.tree, message: c.message.toString('hex'), author: c.authorName.toString('hex') };
205 } };
206 },
207};
208const byPath = (a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0);
209// the rev git reads: the target (a tag peels to its commit)
210const revOf = fx => refAt(fx.bare, fx.target);
211// Whether git can move the ref at all, asked of a copy of the server. A name argv can carry: a real `git push` of a child
212// commit (receive-pack's own checks: a broken ref elsewhere fails its connectivity check, "missing necessary objects").
213// Any other name: `update-ref --stdin`, which reads it as bytes (the loose ref file it writes may be refused by the
214// filesystem, APFS: EILSEQ, as a push would be).
215function gitCanUpdate(bare, ref, old) {
216 const a = mkdtempSync(join(bare, '..', 'cp-')), b = mkdtempSync(join(bare, '..', 'cp-'));
217 try {
218 cpSync(bare, a, { recursive: true }); cpSync(bare, b, { recursive: true });
219 const argvSafe = ref.isWellFormed() && /^[\x21-\x7e]+$/.test(ref);
220 const child = gitOk(a, ['commit-tree', `${old}^{tree}`, '-p', old, '-m', 'probe'], { env: { GIT_AUTHOR_NAME: 'a', GIT_AUTHOR_EMAIL: 'a@x', GIT_COMMITTER_NAME: 'a', GIT_COMMITTER_EMAIL: 'a@x' } }).toString().trim();
221 const r = argvSafe ? run(a, ['push', '-q', b, `${child}:${ref}`]) : run(b, ['update-ref', '--stdin'], { input: B('update ', unwrap(encodeBytes(ref)), ` ${old} ${old}\n`) });
222 if (r.status !== 0) throw new Error(`git ${argvSafe ? 'push' : 'update-ref'}: ${(r.stderr.toString().trim().split('\n').find(l => l.includes('rejected')) ?? r.stderr.toString().trim())}`);
223 } finally { rmSync(a, { recursive: true, force: true }); rmSync(b, { recursive: true, force: true }); }
224}
225// What git refuses to write from a caller: a NUL in a message (commit-tree: "a NUL byte in commit log message not
226// allowed") or in an ident (an environment variable cannot hold one); a ref name check-ref-format refuses.
227function gitCan(message, name) {
228 if (unwrap(encodeBytes(message)).includes(0)) throw new Error('git commit-tree: a NUL byte in commit log message not allowed');
229 if (unwrap(encodeBytes(name)).includes(0)) throw new Error('git: an ident cannot hold NUL');
230}
232// ---- run the product ----
233let root, cleanup, srv;
234const results = [];
235before(async () => { ({ root, cleanup } = tmpRoot()); srv = await startGitServer(root); });
236after(async () => { await srv.close(); cleanup(); });
238const verdict = async (fx, op) => {
239 const url = `${srv.url}/${fx.bare.split('/').pop()}`;
240 // The target ref must be a name git accepts (check-ref-format); otherwise git refuses every operation on it.
241 let oracle, gitErr = null;
242 try { oracle = await OPERATIONS[op](fx, url); } catch (e) { gitErr = e; }
243 let got, plyErr = null;
244 try { got = gitErr ? await plyAnyway(fx, op, url) : await oracle.ply(); } catch (e) { plyErr = e; }
245 if (plyErr && (!isPlyCode(plyErr.code) || plyErr.code === 'network')) return { v: 'untyped', why: String(plyErr?.stack ?? plyErr) };
246 if (gitErr && plyErr) return { v: 'refused-as-git', why: `${plyErr.code}: ${plyErr.message}`, git: String(gitErr.message).split('\n')[0] };
247 if (gitErr) return { v: 'git-refuses-ply-accepts', why: String(gitErr.message).split('\n')[0] };
248 if (plyErr) return ALLOWED_REFUSALS[op]?.(plyErr) ? { v: 'refused', why: `${plyErr.code}: ${plyErr.message}` } : { v: 'refused-unexplained', why: `${plyErr.code}: ${plyErr.message}` };
249 try { assert.deepEqual(got, oracle.git); return { v: 'same' }; } catch (e) { return { v: 'differs', why: e.message.slice(0, 600) }; }
250};
251// git refused: ply must refuse too. Run ply's operation with the same inputs and no oracle to compare against.
252async function plyAnyway(fx, op, url) {
253 const message = fx.field === 'commitMessage' ? fieldIn(fx, 'msg') : 'msg';
254 const author = fx.field === 'authorName' ? { ...WHO, name: fieldIn(fx, 'P Ly') } : WHO;
255 if (op === 'readFile') return (await unwrapAsync(readFile(url, { ref: fx.target, path: 'f.txt' }, O()))).size;
256 if (op === 'listTree') return (await unwrapAsync(listTree(url, { ref: fx.target }, O()))).entries.length;
257 if (op === 'readCommit') return (await unwrapAsync(readCommit(url, { ref: fx.target }, O()))).oid;
258 if (op === 'checkoutTree') { const co = await unwrapAsync(checkoutTree(url, { ref: fx.target }, O())); for await (const f of co.files) await drain(f.stream); return co.entries.length; }
259 if (op === 'editFile') return (await unwrapAsync(editFile(url, { ref: fx.field === 'tagName' ? 'refs/heads/side' : fx.target, path: 'f.txt', find: 'line2', replace: 'X', author, message }, O()))).outcome;
260 return (await unwrapAsync(mergeBranch(url, { ref: 'refs/heads/main', from: fx.from, author, message }, O({ attributes: 'ours' })))).outcome;
261}
263test(`git bytes: ${Object.keys(CONTENT).length} contents × ${FIELDS.length} fields × ${OPS.length} operations, every cell against git`, async () => {
264 let n = 0;
265 // CELL=content/field/op runs one cell and prints its whole verdict
266 const only = process.env.CELL?.split('/');
267 for (const content of Object.keys(CONTENT)) for (const field of FIELDS) for (const op of OPS) {
268 if (only && (only[0] !== content || only[1] !== field || only[2] !== op)) continue;
269 // a fresh fixture per cell: edits and merges move refs
270 const fx = fixture(root, `r${n++}`, content, field);
271 results.push({ content, field, op, ...(await verdict(fx, op)) });
272 if (only) console.log(results.at(-1));
273 }
274 const bad = results.filter(r => !['same', 'refused-as-git', 'refused'].includes(r.v));
275 const table = {};
276 for (const r of results) { const k = `${r.field}`; table[k] ??= {}; table[k][r.op] = (table[k][r.op] ?? '') + (r.v === 'same' ? '=' : r.v === 'refused-as-git' ? 'g' : r.v === 'refused' ? 'r' : 'X'); }
277 console.log(`# ref format: ${REF_FORMAT}`);
278 console.log(`# ${results.length} cells (contents in order ${Object.keys(CONTENT).join(' ')}; '=' same as git, 'g' both refuse, 'r' allowed refusal, 'X' fails)`);
279 for (const [field, ops] of Object.entries(table)) console.log(`# ${field.padEnd(18)} ${OPS.map(op => `${op}:${ops[op]}`).join(' ')}`);
280 const counts = {}; for (const r of results) counts[r.v] = (counts[r.v] ?? 0) + 1;
281 console.log(`# verdicts ${JSON.stringify(counts)}`);
282 for (const r of results.filter(x => x.v !== 'same')) console.log(`# ${r.v.padEnd(24)} ${r.content}/${r.field}/${r.op}: ${String(r.why ?? '').split('\n').slice(0, 3).join(' | ').slice(0, 300)}${r.git ? ` [git: ${r.git.slice(0, 120)}]` : ''}`);
283 if (!only) assert.equal(results.length, Object.keys(CONTENT).length * FIELDS.length * OPS.length);
284 assert.deepEqual(bad.map(r => `${r.content}/${r.field}/${r.op}: ${r.v}`), []);
285});
287// Every decoder is bytes.js's: a `new TextDecoder()` elsewhere would bring back the default that drops a leading BOM,
288// and a `new TextEncoder()` writes a lone surrogate as U+FFFD. bytes.js holds the three constructions this audit chose.
289test('src/ constructs TextDecoder and TextEncoder only in bytes.js', () => {
290 const dir = new URL('../src/', import.meta.url);
291 const found = [];
292 for (const f of readdirSync(dir)) {
293 if (!/\.[jt]s$/.test(f)) continue;
294 const text = readFileSync(new URL(f, dir), 'utf8');
295 for (const m of text.matchAll(/new Text(De|En)coder\(([^)]*)\)/g)) found.push(`${f}: ${m[0]}`);
296 }
297 assert.deepEqual(found, ["bytes.ts: new TextEncoder()", "bytes.ts: new TextDecoder('utf-8', { ignoreBOM: true })", "bytes.ts: new TextDecoder('utf-8', { fatal: true, ignoreBOM: true })"]);
298});
Diagnostics6 git calls · 29 ms
CallStatusSizeTime029 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=2 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 · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=11990 git.response.cache=HIT git.response.duration_ms=3 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 · 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=4347 git.response.cache=HIT git.response.duration_ms=2 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=3 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=19 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
fetch2008.2 KiB · hit19 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=8417 git.response.cache=HIT git.response.duration_ms=19 git.fetch.wants=497751b7cc87e7d955663600701e92dead81200b 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=5 git.pkt.data=4 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=8417 git.pkt.payload.max=8192 git.sideband.band1.bytes=8385 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
- a5e0f822cc59332c
- Request
- 106d3d84…
- 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