main
1// Git compliance: everything we validate or produce, decided by git itself over enumerated input spaces.
2// idents → `git fsck --strict` on a commit carrying the ident
3// refs → `git check-ref-format`
4// paths → tree built from the path, then `git fsck --strict` (protectNTFS/HFS defaults)
5// trees → our encodeTree hash == `git write-tree` over all name pairs × modes
6// commits → parseCommit on every commit shape git can write (merges, gpgsig, mergetag, encoding, extra headers)
7import { test, beforeAll as before, afterAll as after } from 'vitest';
8import assert from 'node:assert/strict';
9import { execFileSync } from 'node:child_process';
10import { join } from 'node:path';
11import { mkdirSync, writeFileSync } from 'node:fs';
12import { git, gitBuf, tmpRoot, fsck, initRepo } from './server.js';
13import { validateIdent, validateRef, validatePath } from '../src/validate.js';
14import { encodeCommit, parseCommit, hashObject, encodeTree, parseTree, treeSort } from '../src/objects.js';
15import { DEFAULT_LIMITS } from '../src/limits.js';
16import { unwrap } from './unwrap.js';
18let root, cleanup, repo, emptyTree, blob;
19before(() => { ({ root, cleanup } = tmpRoot()); repo = join(root, 'r'); mkdirSync(repo); git(repo, ['init', '-q']); emptyTree = git(repo, ['hash-object', '-t', 'tree', '--stdin', '-w'], { input: '' }).trim(); blob = git(repo, ['hash-object', '-w', '--stdin'], { input: 'x' }).trim(); });
20after(() => cleanup());
22// fsck verdicts for every object written so far, from one `git fsck --strict` over the whole store: per object, the
23// msgId of the first line naming it (fsckProblemsOf's lines, whatever fsck exits with), or 'ok'. An object's verdict
24// depends only on its own bytes (fsck-oracle.test.js: one bad object does not taint a good one in the same store), so
25// one scan answers every case. One fsck per case re-scanned a store that grew with every case: quadratic, 259 s of CI
26// for the ident test alone.
27function fsckAll() {
28 const { status, lines } = fsck(repo, ['--no-dangling']);
29 if (status === null || (status & ~3) !== 0) throw new Error(`git fsck failed (exit ${status}): ${lines.join(' | ')}`);
30 const first = new Map();
31 for (const line of lines) for (const oid of new Set(line.match(/[0-9a-f]{40}/g) ?? [])) if (!first.has(oid)) first.set(oid, line);
32 return oid => {
33 const line = first.get(oid);
34 return line ? line.match(/^(?:error|warning) in \w+ [0-9a-f]{40}: (\w+):/)?.[1] ?? line : 'ok';
35 };
36}
37const writeLiterally = (type, data) => { try { return gitBuf(repo, ['hash-object', '-t', type, '-w', '--stdin', '--literally'], data).toString().trim(); } catch { return null; } };
38// The same, for many objects in one hash-object (--stdin-paths) instead of a process each. If git refuses the batch,
39// every object is written alone, so an object git will not write is null exactly as writeLiterally has it.
40let batches = 0;
41function writeAllLiterally(type, datas) {
42 const dir = join(root, `batch-${batches++}`); mkdirSync(dir);
43 const paths = datas.map((d, i) => { const p = join(dir, String(i)); writeFileSync(p, d); return p; });
44 let oids;
45 try { oids = gitBuf(repo, ['hash-object', '-t', type, '-w', '--literally', '--stdin-paths'], paths.join('\n') + '\n').toString().trim().split('\n'); } catch { oids = []; }
46 return oids.length === datas.length ? oids : datas.map(d => writeLiterally(type, d));
47}
49const strings = (alphabet, maxLen) => { const out = ['']; let layer = ['']; for (let n = 1; n <= maxLen; n++) { layer = layer.flatMap(s => alphabet.map(c => s + c)); out.push(...layer); } return out; };
51test('ident validation == git fsck --strict (exhaustive over an alphabet, name and email independently)', () => {
52 const alpha = ['a', ' ', '<', '>', '\n', '\0', '.', '\t', '\r', '"', "'", '@', ','];
53 const cases = [];
54 const check = (name, email) => {
55 let ours; try { unwrap(validateIdent({ name, email, time: 1700000000, tz: '+0000' })); ours = 'ok'; } catch { ours = 'reject'; }
56 const data = Buffer.from(`tree ${emptyTree}\nauthor ${name} <${email}> 1700000000 +0000\ncommitter ${name} <${email}> 1700000000 +0000\n\nm\n`, 'utf8');
57 cases.push({ name, email, ours, data });
58 };
59 for (const s of strings(alpha, 3)) { check(s, 'a@x'); check('A', s); }
60 for (const [name, email] of [["O'Brien", 'o@x'], ['Dr. Who, Jr.', 'w@x'], [' lead', 'l@x'], ['trail ', 't@x'], ['日本語 名', 'n@x'], ['', ''], ['x', 'a b @ c']]) check(name, email);
61 writeAllLiterally('commit', cases.map(k => k.data)).forEach((oid, i) => { cases[i].oid = oid; });
62 const fsck = fsckAll(), n = cases.length, disagree = [];
63 for (const { name, email, ours, oid } of cases) {
64 const theirs = oid ? fsck(oid) : 'unwritable';
65 // a NUL splits the header for git ("unterminated header"); for us it is a plain reject. Both reject.
66 if ((ours === 'ok') !== (theirs === 'ok')) disagree.push({ name, email, ours, theirs });
67 }
68 assert.deepEqual(disagree, [], `${disagree.length}/${n} disagreements`);
69 console.log(` idents: ${n} cases, 0 disagreements`);
70});
72test('ref validation == git check-ref-format (exhaustive over an alphabet)', () => {
73 const alpha = ['a', '/', '.', '~', '^', ':', '?', '*', '[', '\\', ' ', '@', '{', '}', '-', '\x7f', '\x01', 'l', 'o', 'c', 'k'];
74 let n = 0; const disagree = [];
75 const tails = [...strings(alpha, 2), '.lock', 'a.lock', 'a.lock/b', 'lock', '@{', 'a@{b', 'a..b', 'a/.b', '.a', 'a/', 'a//b', 'a.', 'a...', 'x.lock.y'];
76 for (const t of tails) for (const ref of [`refs/heads/${t}`, `refs/${t}`, t]) {
77 if (ref.includes('\n') || ref.includes('\0')) continue; // cannot be passed as an argv string; we reject both regardless
78 n++;
79 let ours; try { unwrap(validateRef(ref)); ours = 'ok'; } catch { ours = 'reject'; }
80 let theirs; try { execFileSync('git', ['check-ref-format', ref], { stdio: 'pipe' }); theirs = 'ok'; } catch { theirs = 'reject'; }
81 // we additionally require a full refs/ name (bare "main" is rejected by us, accepted by check-ref-format)
82 if (!ref.startsWith('refs/')) { if (ours !== 'reject') disagree.push({ ref, ours, theirs: 'n/a (we require refs/)' }); continue; }
83 if (ours !== theirs) disagree.push({ ref, ours, theirs });
84 }
85 assert.deepEqual(disagree, [], `${disagree.length}/${n} disagreements`);
86 console.log(` refs: ${n} cases, 0 disagreements`);
87});
89test('path validation == git tree acceptance + fsck --strict (components over an alphabet, .git variants, NTFS/HFS)', () => {
90 const alpha = ['a', '.', 'g', 'i', 't', '~', '1', ' ', 'G', 'I', 'T', '\\'];
91 const comps = [...new Set([...strings(alpha, 2).filter(s => s.length), ...strings(['.', 'g', 'i', 't', 'G'], 4).filter(s => s.length), '.git', '.GIT', '.Git', 'git~1', 'GIT~1', '.gi\u200ct', '.g\u200ci\u200dt', '.git ', '.git.', '.git..', '.git . ', 'gi7eba~1', '.GIT~1', 'x', '..', '.', 'nul\0x', 'a\nb', '.gitmodules', '.git\u200e', '\ufeff.git', '.giT'])]; // 'a/b' is two components for us, one (invalid) name for a tree
92 const cases = [];
93 for (const c of comps) {
94 const path = `d/${c}`;
95 const ours = validatePath(path, DEFAULT_LIMITS).ok ? 'ok' : 'reject';
96 // git side: build the tree object directly (bypassing index normalization) and fsck it
97 const data = Buffer.concat([Buffer.from(`100644 ${c}\0`, 'utf8'), Buffer.from(blob, 'hex')]);
98 cases.push({ c, ours, data, writable: !c.includes('\0') && !c.includes('/') });
99 }
100 const writable = cases.filter(k => k.writable);
101 writeAllLiterally('tree', writable.map(k => k.data)).forEach((oid, i) => { writable[i].oid = oid; });
102 const fsck = fsckAll(), n = cases.length, disagree = [];
103 for (const { c, ours, oid } of cases) {
104 const theirs = oid ? fsck(oid) : 'reject';
105 if ((ours === 'ok') !== (theirs === 'ok')) disagree.push({ c: JSON.stringify(c), ours, theirs });
106 }
107 assert.deepEqual(disagree, [], `${disagree.length}/${n} disagreements`);
108 console.log(` paths: ${n} components, 0 disagreements`);
109});
111test('tree encoding == git write-tree over all name pairs × {file, dir} (sort order with dir-as-slash)', () => {
112 const names = ['a', 'a-', 'a.', 'a0', 'a_', 'ab', 'A', 'a~', 'b', 'a b', '-a', '.a', '한', 'aa', 'a.b', 'a-b'];
113 let n = 0;
114 for (let i = 0; i < names.length; i++) for (let j = i + 1; j < names.length; j++) for (const [mi, mj] of [['100644', '100644'], ['40000', '100644'], ['100644', '40000'], ['40000', '40000'], ['120000', '100755']]) {
115 const sub = join(root, `t${n++}`); mkdirSync(sub); git(sub, ['init', '-q']);
116 const entries = [[names[i], mi], [names[j], mj]].map(([name, mode]) => ({ mode, name, oid: mode === '40000' ? emptyTreeIn(sub) : blobIn(sub, name) }));
117 // git: mktree with entries (any order) → oid
118 const mk = git(sub, ['mktree'], { input: entries.map(e => `${e.mode.padStart(6, '0')} ${e.mode === '40000' ? 'tree' : 'blob'} ${e.oid}\t${e.name}`).join('\n') + '\n' }).trim();
119 assert.equal(hashObject('tree', unwrap(encodeTree(entries))), mk, `${names[i]}(${mi}) vs ${names[j]}(${mj})`);
120 assert.equal(hashObject('tree', unwrap(encodeTree([entries[1], entries[0]]))), mk, 'order-independent');
121 }
122 console.log(` trees: ${n} pairs, all equal to git mktree`);
123});
124const emptyTreeIn = sub => git(sub, ['hash-object', '-t', 'tree', '--stdin', '-w'], { input: '' }).trim();
125const blobIn = (sub, s) => git(sub, ['hash-object', '-w', '--stdin'], { input: s }).trim();
127test('parseCommit recovers tree/parents/author/committer/message from every commit shape git writes', () => {
128 const sub = join(root, 'c'); mkdirSync(sub); initRepo(sub);
129 const env = { ...process.env, GIT_AUTHOR_NAME: 'A', GIT_AUTHOR_EMAIL: 'a@x', GIT_COMMITTER_NAME: 'C', GIT_COMMITTER_EMAIL: 'c@x', GIT_AUTHOR_DATE: '1700000000 +0100', GIT_COMMITTER_DATE: '1700000001 -0230' };
130 writeFileSync(join(sub, 'f'), '1'); git(sub, ['add', 'f']); git(sub, ['commit', '-q', '-m', 'root'], { env });
131 git(sub, ['checkout', '-q', '-b', 'b1']); writeFileSync(join(sub, 'g'), '2'); git(sub, ['add', 'g']); git(sub, ['commit', '-q', '-m', 'b1'], { env });
132 git(sub, ['checkout', '-q', '-b', 'b2', 'main']); writeFileSync(join(sub, 'h'), '3'); git(sub, ['add', 'h']); git(sub, ['commit', '-q', '-m', 'b2\n\nbody line\n\nmore'], { env });
133 git(sub, ['checkout', '-q', 'main']); git(sub, ['merge', '-q', '--no-ff', '-m', 'octo', 'b1', 'b2'], { env });
134 const tree = git(sub, ['rev-parse', 'HEAD^{tree}']).trim(), p = git(sub, ['rev-parse', 'HEAD']).trim();
135 // hand-made shapes git accepts: gpgsig (multi-line continuation), mergetag, encoding, unknown header, empty message, no trailing newline
136 const shapes = [
137 `tree ${tree}\nparent ${p}\nauthor A <a@x> 1 +0000\ncommitter C <c@x> 2 +0000\ngpgsig -----BEGIN PGP SIGNATURE-----\n \n iQEzBAABCAAdFiEE\n =abcd\n -----END PGP SIGNATURE-----\n\nsigned\n`,
138 `tree ${tree}\nparent ${p}\nauthor A <a@x> 1 +0000\ncommitter C <c@x> 2 +0000\nmergetag object ${p}\n type commit\n tag v9\n tagger T <t@x> 3 +0000\n \n v9\n\nmerge tag\n`,
139 `tree ${tree}\nauthor A <a@x> 1 +0000\ncommitter C <c@x> 2 +0000\nencoding ISO-8859-1\n\nlatin\n`,
140 `tree ${tree}\nauthor A <a@x> 1 +0000\ncommitter C <c@x> 2 +0000\nx-custom header value\n\n`,
141 `tree ${tree}\nauthor A <a@x> 1 +0000\ncommitter C <c@x> 2 +0000\n\nno trailing newline`,
142 `tree ${tree}\nauthor A <a@x> 1 +0000\ncommitter C <c@x> 2 +0000\n`,
143 ];
144 const all = [...git(sub, ['rev-list', '--all']).trim().split('\n'), ...shapes.map(s => gitBuf(sub, ['hash-object', '-t', 'commit', '-w', '--stdin', '--literally'], Buffer.from(s)).toString().trim())];
145 for (const oid of all) {
146 const data = new Uint8Array(gitBuf(sub, ['cat-file', 'commit', oid]));
147 const c = unwrap(parseCommit(data));
148 const [t, ...parents] = git(sub, ['rev-list', '--parents', '-n1', oid]).trim().split(' ').slice(1).length ? [git(sub, ['rev-parse', `${oid}^{tree}`]).trim(), ...git(sub, ['rev-list', '--parents', '-n1', oid]).trim().split(' ').slice(1)] : [git(sub, ['rev-parse', `${oid}^{tree}`]).trim()];
149 assert.equal(c.tree, t, oid); assert.deepEqual(c.parents, parents, oid);
150 assert.equal(c.author, git(sub, ['log', '-1', '--format=%an <%ae> %at %ai', oid]).trim().replace(/ \d{4}-\d\d-\d\d \d\d:\d\d:\d\d /, ' '), oid);
151 // message oracle from the raw object: git's header parser stops at the first empty line, and the
152 // message is everything after it; with no empty line the message is empty (git's %s and %b are both '').
153 // %B is consulted only when the empty line exists: without one, pretty.c leaves message_off at the
154 // NUL terminator and %B prints from message_off + 1, one byte past the buffer (stray bytes, ~half of
155 // runs on homebrew git 2.50.0; issue #33).
156 const end = data.findIndex((b, i) => b === 10 && (i === 0 || data[i - 1] === 10)); // empty line: "\n" at 0 or "\n\n"
157 const message = end < 0 ? '' : Buffer.from(data.subarray(end + 1)).toString('utf8');
158 assert.equal(c.message ?? '', message, oid);
159 if (end < 0) assert.equal(git(sub, ['log', '-1', '--format=%s%b', oid]), '\n', oid);
160 else assert.equal(message.replace(/\n+$/, ''), git(sub, ['log', '-1', '--format=%B', oid]).replace(/\n+$/, ''), oid); // %B appends newlines of its own
161 // re-encoding a commit we did not write must be byte-identical when it has only standard headers
162 // parse ∘ encode is the identity for every shape, including multi-line continuation headers (gpgsig, mergetag)
163 assert.deepEqual([...unwrap(encodeCommit(c))], [...data], `roundtrip ${oid}`);
164 }
165 console.log(` commits: ${all.length} shapes parsed consistently with git log`);
166});
Diagnostics4 git calls · 362 ms
CallStatusSizeTime0362 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 · hit5 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=4 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
fetch2005.0 KiB353 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=353 git.response.bytes=5072 git.response.cache=MISS git.response.duration_ms=353 git.fetch.wants=4b14b799c48c53cd33db3a5e435ab7b463a0c369 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=5072 git.pkt.payload.max=5051 git.sideband.band1.bytes=5050 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
- a5e1c327199536cd
- Request
- fe11e3e2…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format