Artifactspublic-v0

ply

main
ply4 git calls261 ms43 KiB2 cachedprotocol v2
199 lines13 KiBRaw
1// encodeTree is the one way ply writes a tree, and it must not write one `git fsck --strict` refuses.
2// Every expectation here is git's: `git fsck --strict` over a fresh repository holding one hand-built tree object
3// (written with `git hash-object --literally`, which checks nothing) is the verdict on each cell, `git mktree` builds
4// git's own tree from the same entries, and `git ls-tree` reads back what was written. Where git has no say (a mode
5// or an oid that is not a string) the expected refusal is derived by hand and said in a comment.
6import { test } from 'vitest';
7import assert from 'node:assert/strict';
8import { spawnSync } from 'node:child_process';
9import { mkdtempSync, rmSync } from 'node:fs';
10import { tmpdir } from 'node:os';
11import { join } from 'node:path';
12import { encodeTree, encodeEntries, treeSort } from '../src/objects.js';
14const BLOB = 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'; // git's empty blob
15const TREE = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'; // git's empty tree
16const NULL = '0'.repeat(40);
17const TYPE_OF = { 100644: 'blob', 100755: 'blob', 120000: 'blob', 40000: 'tree', 160000: 'commit' };
19function git(dir, args, input) {
20 const r = spawnSync('git', ['-C', dir, ...args], { input, maxBuffer: 1 << 28 });
21 return { code: r.status ?? 1, out: `${r.stdout ?? ''}`, err: `${r.stderr ?? ''}` };
23function withRepo(fn) {
24 const dir = mkdtempSync(join(tmpdir(), 'ply-enc-tree-'));
25 try {
26 assert.equal(git(dir, ['init', '-q', '-b', 'main']).code, 0);
27 return fn(dir);
28 } finally {
29 rmSync(dir, { recursive: true, force: true });
30 }
32// One tree entry's bytes, written by hand from the format (`<mode> <name>\0<20 raw oid bytes>`): not ply's encoder.
33const rawEntry = (mode, name, oid) => Buffer.concat([Buffer.from(`${mode} `, 'latin1'), Buffer.from(name, 'utf8'), Buffer.from([0]), Buffer.from(oid, 'hex')]);
34// git's verdict on one tree object: `git fsck --strict` over a fresh repository that holds the tree and the empty
35// blob and tree its entries point at (so a refusal is about the tree, never a broken link). hash-object's own check is
36// not this verdict: it refuses INFO-level ids (gitattributesSymlink) that fsck --strict accepts and cannot see the
37// ones fsck decides at the end of the run (gitmodulesBlob, gitattributesBlob).
38const seed = dir => {
39 assert.equal(git(dir, ['hash-object', '-t', 'blob', '-w', '--stdin'], '').out.trim(), BLOB);
40 assert.equal(git(dir, ['hash-object', '-t', 'tree', '-w', '--stdin'], '').out.trim(), TREE);
41};
42const fsckAccepts = bytes => withRepo(dir => {
43 seed(dir);
44 const w = git(dir, ['hash-object', '-t', 'tree', '-w', '--stdin', '--literally'], bytes);
45 assert.equal(w.code, 0, w.err);
46 return git(dir, ['fsck', '--strict']);
47});
49// name, mode, oid: one entry each. Git decides which are refused.
50const CELLS = [
51 ['a', '100644', BLOB], ['a b', '100755', BLOB], ['-', '120000', BLOB], ['d', '40000', TREE], ['sub', '160000', BLOB],
52 ['é', '100644', BLOB], ['日本', '40000', TREE], ['...', '100644', BLOB], ['git~2', '100644', BLOB], ['.gitx', '100644', BLOB],
53 ['.gitmodules', '100644', BLOB], ['.gitattributes', '100644', BLOB], ['.gitattributes', '120000', BLOB], ['.gitignore', '120000', BLOB],
54 // fsck.c fsck_tree, one cell per message id
55 ['', '100644', BLOB], ['a/b', '100644', BLOB], ['a/', '40000', TREE], ['a\0b', '100644', BLOB], ['.', '100644', BLOB], ['..', '40000', TREE],
56 ['.git', '40000', TREE], ['.GIT', '100644', BLOB], ['.Git', '100644', BLOB], ['.git ', '100644', BLOB], ['.git.', '100644', BLOB], ['git~1', '100644', BLOB],
57 ['GIT~1', '40000', TREE], ['.g‌it', '100644', BLOB], ['.git::$INDEX_ALLOCATION', '100644', BLOB], ['a\\.git', '100644', BLOB],
58 ['.gitmodules', '120000', BLOB], ['.gitmodules', '40000', TREE], ['.gitmodules', '160000', 'c'.repeat(40)], ['.GITMODULES.', '120000', BLOB], ['gitmod~1', '120000', BLOB],
59 ['.gitmodu‌les', '120000', BLOB], ['.gitattributes', '40000', TREE], ['.gitattributes', '160000', 'c'.repeat(40)],
60 ['a', '100644', NULL], ['d', '40000', NULL], ['sub', '160000', NULL],
61 ['x'.repeat(4096), '100644', BLOB], ['x'.repeat(4097), '100644', BLOB],
62];
64test('encodeTree refuses exactly the single entries git fsck refuses, and writes git\'s bytes for the rest', () => {
65 {
66 const wrong = [];
67 let refused = 0;
68 for (const [name, mode, oid] of CELLS) {
69 const want = fsckAccepts(rawEntry(mode, name, oid));
70 if (want.code !== 0) refused += 1;
71 const got = encodeTree([{ mode, name, oid }]);
72 const label = `${mode} ${JSON.stringify(name.length > 40 ? `${name.slice(0, 8)}…(${name.length})` : name)} ${oid === NULL ? 'null-oid' : ''}`;
73 if (got.ok !== (want.code === 0)) { wrong.push(`${label}: git ${want.code === 0 ? 'accepts' : `refuses (${want.err.trim().split('\n')[0]})`}, ply ${got.ok ? 'encodes' : `refuses (${got.error.message})`}`); continue; }
74 if (got.ok) assert.deepEqual([...got.value], [...rawEntry(mode, name, oid)], label);
75 else assert.equal(got.error.code, 'bad_request', label);
76 }
77 assert.ok(refused >= 25, `git refused only ${refused} cells: the oracle is not judging`);
78 assert.deepEqual(wrong, []);
79 }
80});
82// Modes, at the encoder itself (encodeEntries: what pushTree, commitChanges, merge and memtree call with a typed entry).
83// The type says Mode; a JavaScript caller's value is whatever it is. Git decides each string cell: the entry's bytes are
84// written by hand with that mode (UTF-8) and `git fsck --strict` accepts or refuses the tree. No cell holds a space or a
85// NUL: those bytes would move the mode/name boundary and git would be judging a different entry.
86const MODE_CELLS = [
87 ['100644', BLOB], ['100755', BLOB], ['120000', BLOB], ['40000', TREE], ['160000', BLOB],
88 // fsck zeroPaddedFilemode, badFilemode (100664 is an error under --strict), and modes decode_tree_entry cannot read
89 ['0100644', BLOB], ['040000', TREE], ['100664', BLOB], ['100000', BLOB], ['644', BLOB], ['0', BLOB], ['', BLOB], ['abc', BLOB], ['100648', BLOB],
90 // not ASCII: fullwidth digits (3 bytes each in UTF-8, 1 UTF-16 unit), one fullwidth digit among ASCII, an astral digit
91 ['100644', BLOB], ['100644', BLOB], ['𝟏00644', BLOB], ['é', BLOB],
92];
93// Git's verdict on a mode has three values, and ply encodes only the first: `clean` (fsck --strict accepts and says
94// nothing: the five modes git writes), `notice` (fsck --strict accepts and reports badFilemode, an INFO id that
95// --strict does not raise: 100664, 100000, 644; git never writes these, canon_mode rewrites them), `refused`.
96test('encodeEntries checks the mode itself: it encodes exactly the modes git fsck --strict passes without a badFilemode notice, with git\'s bytes, and never throws', () => {
97 const wrong = [];
98 const seen = { clean: [], notice: [], refused: [] };
99 for (const [mode, oid] of MODE_CELLS) {
100 const raw = Buffer.concat([Buffer.from(`${mode} a`, 'utf8'), Buffer.from([0]), Buffer.from(oid, 'hex')]);
101 const want = fsckAccepts(raw);
102 const verdict = want.code !== 0 ? 'refused' : /badFilemode/.test(`${want.out}${want.err}`) ? 'notice' : 'clean';
103 seen[verdict].push(mode);
104 const label = `mode ${JSON.stringify(mode)}`;
105 let got;
106 try { got = encodeEntries([{ mode, name: 'a', oid }]); } catch (x) { wrong.push(`${label}: encodeEntries throws ${x}`); continue; }
107 if (got.ok !== (verdict === 'clean')) { wrong.push(`${label}: git ${verdict} (${`${want.err}${want.out}`.trim().split('\n')[0]}), ply ${got.ok ? `encodes ${JSON.stringify(Buffer.from(got.value).toString('latin1').slice(0, 12))}` : `refuses (${got.error.message})`}`); continue; }
108 if (got.ok) assert.deepEqual([...got.value], [...raw], label);
109 else assert.deepEqual([got.error.code, got.error.what, got.error.entry], ['bad_request', 'mode', 'a'], label);
110 }
111 // the oracle is judging: git passes its own five modes clean, and only those
112 assert.deepEqual(seen.clean, ['100644', '100755', '120000', '40000', '160000']);
113 assert.deepEqual(seen.notice, ['100664', '100000', '644']);
114 assert.equal(seen.refused.length, MODE_CELLS.length - 8);
115 assert.deepEqual(wrong, []);
116});
118test('encodeEntries checks every field itself: a mode or an oid that is not what the type says is bad_request, never a throw or a written tree', () => {
119 // By hand: a tree entry's mode is one of five ASCII strings and its id is 20 bytes, written from 40 lowercase hex
120 // digits. Nothing else has an encoding, so each of these has exactly one honest outcome: bad_request naming the field.
121 const cyclic = {}; cyclic.self = cyclic;
122 const cells = [
123 ['mode', { mode: 100644, name: 'a', oid: BLOB }], ['mode', { mode: undefined, name: 'a', oid: BLOB }], ['mode', { mode: null, name: 'a', oid: BLOB }],
124 ['mode', { mode: {}, name: 'a', oid: BLOB }], ['mode', { mode: 10n, name: 'a', oid: BLOB }], ['mode', { mode: cyclic, name: 'a', oid: BLOB }], ['mode', { mode: ['100644'], name: 'a', oid: BLOB }],
125 ['oid', { mode: '100644', name: 'a', oid: 'abc' }], ['oid', { mode: '100644', name: 'a', oid: BLOB.toUpperCase() }], ['oid', { mode: '100644', name: 'a', oid: `${BLOB}0` }],
126 ['oid', { mode: '100644', name: 'a', oid: BLOB.slice(1) }], ['oid', { mode: '100644', name: 'a', oid: `${BLOB.slice(0, 39)}g` }], ['oid', { mode: '100644', name: 'a', oid: 5 }],
127 ['oid', { mode: '100644', name: 'a', oid: undefined }], ['oid', { mode: '100644', name: 'a', oid: '1'.repeat(40) }],
128 // a name that is not a string, with a bad mode beside it: quoting the name must not throw either (a BigInt, a cycle)
129 ['mode', { mode: '644', name: 5n, oid: BLOB }], ['mode', { mode: '644', name: cyclic, oid: BLOB }],
130 ];
131 const wrong = [];
132 for (const [what, entry] of cells) {
133 for (const [fn, encode] of [['encodeEntries', encodeEntries], ['encodeTree', encodeTree]]) {
134 const label = `${fn} ${what} mode:${typeof entry.mode} name:${typeof entry.name} oid:${typeof entry.oid === 'string' ? entry.oid.length : typeof entry.oid}`;
135 let got;
136 try { got = encode([entry]); } catch (x) { wrong.push(`${label}: throws ${x}`); continue; }
137 if (got.ok) { wrong.push(`${label}: encodes ${got.value.length} bytes`); continue; }
138 // a name that is not a string is refused for the name by the encoder and for the mode by treeEntryOf: either names a field
139 if (got.error.code !== 'bad_request' || (got.error.what !== what && got.error.what !== 'tree entry name')) wrong.push(`${label}: ${got.error.code} what=${got.error.what}`);
140 }
141 }
142 assert.deepEqual(wrong, []);
143});
145test('encodeTree of one entry never writes a tree git reads as other entries (a NUL in the name)', () => {
146 withRepo(dir => {
147 // "a", NUL, 20 chosen bytes, then a second entry's mode and name: the caller's one name frames two entries
148 const name = `a\0${'B'.repeat(20)}100644 evil`;
149 const got = encodeTree([{ mode: '100644', name, oid: BLOB }]);
150 if (!got.ok) { assert.equal(got.error.code, 'bad_request'); assert.equal(got.error.entry, name); return; }
151 const written = git(dir, ['hash-object', '-t', 'tree', '-w', '--stdin', '--literally'], got.value);
152 assert.equal(written.code, 0, written.err);
153 const listed = git(dir, ['ls-tree', '-z', written.out.trim()]).out.split('\0').filter(Boolean);
154 assert.deepEqual(listed, [`100644 blob ${BLOB}\ta`], 'one entry in, one entry out');
155 });
156});
158// A small deterministic generator (mulberry32): the corpus is the same on every run
159function rng(seed) {
160 let a = seed >>> 0;
161 return () => { a = (a + 0x6d2b79f5) >>> 0; let t = a; t = Math.imul(t ^ (t >>> 15), t | 1); t ^= t + Math.imul(t ^ (t >>> 7), t | 61); return ((t ^ (t >>> 14)) >>> 0) / 4294967296; };
163// Names that are prefixes of each other, bytes on both sides of '/' (0x2f): '-' '.' below it, '0' above; non-ASCII
164const PIECES = ['a', 'ab', 'a.', 'a-', 'a0', 'a+', 'A', 'b', '.x', '-', '0', 'é', 'é', '日本', '😀', 'z', '~', ' ', '_', 'a b', 'ÿ', 'aa'];
165function corpus(seed, count) {
166 const next = rng(seed);
167 const pick = list => list[Math.floor(next() * list.length)];
168 const trees = [];
169 for (let t = 0; t < count; t++) {
170 const names = new Set();
171 const n = 1 + Math.floor(next() * 40);
172 while (names.size < n) names.add(Array.from({ length: 1 + Math.floor(next() * 3) }, () => pick(PIECES)).join(''));
173 trees.push([...names].map(name => { const mode = pick(['100644', '100755', '120000', '40000', '40000', '160000']); return { mode, name, oid: mode === '40000' ? TREE : BLOB }; }));
174 }
175 return trees;
178test('a generated corpus: ply encodes the tree git mktree builds, git fsck accepts it, and treeSort is git ls-tree\'s order', () => {
179 withRepo(dir => {
180 for (const entries of corpus(20261111, 150)) {
181 const stdin = entries.map(e => `${e.mode} ${TYPE_OF[e.mode]} ${e.oid}\t${e.name}\0`).join('');
182 const made = git(dir, ['mktree', '-z', '--missing'], stdin);
183 assert.equal(made.code, 0, made.err);
184 const want = made.out.trim();
185 const enc = encodeTree(entries);
186 assert.ok(enc.ok, enc.ok ? '' : enc.error.message);
187 const hashed = git(dir, ['hash-object', '-t', 'tree', '-w', '--stdin'], enc.value);
188 assert.equal(hashed.code, 0, `git refuses what ply encoded: ${hashed.err}`);
189 assert.equal(hashed.out.trim(), want, 'the oid git mktree gives the same entries');
190 const order = git(dir, ['ls-tree', '-z', '--name-only', want]).out.split('\0').filter(Boolean);
191 const sorted = treeSort(entries);
192 assert.ok(sorted.ok);
193 assert.deepEqual(sorted.value.map(e => e.name), order);
194 }
195 seed(dir);
196 const all = git(dir, ['fsck', '--strict']);
197 assert.equal(all.code, 0, `git fsck --strict over every tree ply encoded: ${all.err}`);
198 });
199});
Diagnostics4 git calls · 261 ms
CallStatusSizeTime0261 ms
discover200152 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
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 · 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=145
git.response.cache=HIT
git.response.duration_ms=3
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 · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=255
git.response.bytes=38434
git.response.cache=HIT
git.response.duration_ms=3
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=55
Open pack map
fetch2004.7 KiB255 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=4844
git.response.cache=MISS
git.response.duration_ms=255
git.fetch.wants=5bd672781a87c7887a7b44785bf2ba5c5f5e2fc9
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=4844
git.pkt.payload.max=4823
git.sideband.band1.bytes=4822
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
a5e1f9f88ee224ce
Request
a4b48472…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format