GitHubivarvong
ply
main
1// commitChanges against git, over generated repositories and generated change lists.
2//
3// Repository: one generated template directory planted at sibling and nested paths (one tree oid at several paths), its
4// `sub/` directory planted again elsewhere (twins inside twins), plus a few files of their own. Asserted by git's own
5// hashing before any case runs.
6// Change list: write, create, edit, delete, rename, mode change, delete and rename of whole directories, across and
7// inside the twins; names and contents come from small pools so the same new file often lands under two twins (the
8// two rebuilt directories are then one new tree, which a pack must carry once). A third of the lists hold one change:
9// that is commitChanges' streamed path.
10// Oracle: the same list applied with git add / git rm / git mv in a copy of the seed work tree, then
11// `git ls-tree -r -t` of git's commit against `git ls-tree -r -t` of the ref ply pushed, byte for byte; the outcome
12// (commit or noop) is predicted from git's tree oid; fsck of the receiving repository must be clean. Nothing expected
13// here is computed by ply. The generator keeps a model of the file set (path -> bytes) only to emit lists that are
14// valid for both sides; an edit's expected bytes are the model's (a token that occurs once, replaced by split/join).
15// Servers: `default` is git's own receive-pack defaults (no fsck: a wrong tree is accepted silently, so the ls-tree
16// comparison is the judge); `strict` is receive.fsckObjects=true + receive.unpackLimit=1 (index-pack --strict, which
17// refuses a pack that names one object twice).
18//
19// Deterministic: splitmix32 seeded from CHANGES_GEN_SEED (default below). Every failure prints the line that reruns
20// that one case. A wider sweep: CHANGES_GEN_REPOS=40 CHANGES_GEN_LISTS=12 npx vitest run test/changes-generated.test.js
21import { test, beforeAll as before, afterAll as after } from 'vitest';
22import assert from 'node:assert/strict';
23import { join, dirname } from 'node:path';
24import { cpSync, mkdirSync, writeFileSync, chmodSync, rmdirSync } from 'node:fs';
25import { git, tmpRoot, makeBareRepo, startGitServer, fsckProblems, noMaintenance } from './server.js';
26import { Rng } from './fuzz/prng.js';
27import { commitChanges } from '../src/changes.js';
28import { Tracer } from '../src/otel.js';
30const SEED = Number(process.env.CHANGES_GEN_SEED ?? 20261111);
31const REPOS = Number(process.env.CHANGES_GEN_REPOS ?? 3);
32const LISTS = Number(process.env.CHANGES_GEN_LISTS ?? 4);
33const ONLY = process.env.CHANGES_GEN_ONLY ?? ''; // "<repo>.<list>": rerun one case
34const AUTHOR = { name: 'Ada', email: 'ada@x', tz: '+0000', time: 1700000000 };
35const SERVERS = [
36 ['default', [['receive.fsckObjects', 'false'], ['transfer.fsckObjects', 'false']]],
37 ['strict', [['receive.fsckObjects', 'true'], ['receive.unpackLimit', '1']]],
38];
39const NAMES = ['alpha.txt', 'beta.md', 'gamma.json', 'run.sh', 'new.txt', 'same.txt'];
40const DIRS = ['sub', 'lib', 'x'];
41const BODIES = ['same\n', 'one\n', 'two\ntwo\n', '{}\n'];
43// ---- the generator: a model of the file set, Map<path, { content, exec }> ----
44const dirsOf = files => {
45 const out = new Set();
46 for (const p of files.keys()) for (let d = dirname(p); d !== '.'; d = dirname(d)) out.add(d);
47 return [...out].sort();
48};
49const isDir = (files, p) => [...files.keys()].some(f => f.startsWith(`${p}/`));
50// a path is free for a new file when nothing is there and no ancestor is a file
51const free = (files, p) => {
52 if (files.has(p) || isDir(files, p)) return false;
53 for (let d = dirname(p); d !== '.'; d = dirname(d)) if (files.has(d)) return false;
54 return true;
55};
57// a rename's destination directory must exist, in ply (not_found) as in `git mv` ("destination directory does not
58// exist"); the oracle runs `git mv` unassisted, so git itself refuses a list that breaks this
59const parentExists = (files, p) => dirname(p) === '.' || isDir(files, dirname(p));
61function genRepo(rng) {
62 // the template: 2-4 files and a sub/ with 1-2 files; every file carries one token an edit can find exactly once
63 const template = new Map();
64 let token = 0;
65 const body = () => `line ${rng.int(0, 3)}\nTOKEN${token++}\n`;
66 for (const name of NAMES.slice(0, rng.int(2, 4))) template.set(name, { content: body(), exec: name === 'run.sh' || rng.bool(0.2) });
67 for (const name of NAMES.slice(0, rng.int(1, 2))) template.set(`sub/${name}`, { content: body(), exec: false });
68 const twins = ['a', 'b', 'c', 'n/a', 'n/deep/b'].slice(0, rng.int(3, 5));
69 const subTwins = ['s', 'n/s'].slice(0, rng.int(1, 2));
70 const files = new Map();
71 for (const d of twins) for (const [p, f] of template) files.set(`${d}/${p}`, { ...f });
72 for (const d of subTwins) for (const [p, f] of template) if (p.startsWith('sub/')) files.set(`${d}/${p.slice(4)}`, { ...f });
73 files.set('top.txt', { content: body(), exec: false });
74 files.set('tool.sh', { content: body(), exec: true });
75 return { files, twins, subTwins };
76}
78function genChange(rng, files) {
79 const paths = [...files.keys()].sort();
80 const dirs = dirsOf(files);
81 const newPath = () => {
82 const dir = rng.bool(0.85) && dirs.length > 0 ? rng.pick(dirs) : '';
83 const leaf = rng.bool(0.25) ? `${rng.pick(DIRS)}/${rng.pick(NAMES)}` : rng.pick(NAMES);
84 return dir === '' ? leaf : `${dir}/${leaf}`;
85 };
86 const mode = () => rng.weighted([[undefined, 5], ['100644', 2], ['100755', 2]]);
87 const withMode = (c, m) => (m === undefined ? c : { ...c, mode: m });
88 const kind = rng.weighted([['write-new', 4], ['write-over', 4], ['create', 2], ['edit', 3], ['delete', 3], ['rename', 3], ['chmod', 2], ['rename-dir', 1], ['delete-dir', 1]]);
89 if (kind === 'write-new' || kind === 'create') {
90 const path = newPath();
91 if (!free(files, path)) return null;
92 const m = mode();
93 const content = rng.pick(BODIES);
94 files.set(path, { content, exec: m === '100755' });
95 return withMode({ kind: kind === 'create' ? 'create' : 'write', path, content }, m);
96 }
97 if (paths.length === 0) return null;
98 if (kind === 'write-over') {
99 const path = rng.pick(paths);
100 const m = mode();
101 // one time in four the same bytes: with no mode that is a no-op, as `git add` of an unchanged file is
102 const content = rng.bool(0.25) ? files.get(path).content : rng.pick(BODIES);
103 files.set(path, { content, exec: m === undefined ? files.get(path).exec : m === '100755' });
104 return withMode({ kind: 'write', path, content }, m);
105 }
106 if (kind === 'chmod') {
107 const path = rng.pick(paths);
108 const f = files.get(path);
109 files.set(path, { content: f.content, exec: !f.exec });
110 return { kind: 'write', path, content: f.content, mode: f.exec ? '100644' : '100755' };
111 }
112 if (kind === 'edit') {
113 const path = rng.pick(paths);
114 const f = files.get(path);
115 const found = /TOKEN\d+/u.exec(f.content);
116 if (found === null || f.content.split(found[0]).length !== 2) return null;
117 const replace = `EDIT${rng.int(0, 2)}`;
118 files.set(path, { content: f.content.split(found[0]).join(replace), exec: f.exec });
119 return { kind: 'edit', path, find: found[0], replace };
120 }
121 if (kind === 'delete') {
122 const path = rng.pick(paths);
123 files.delete(path);
124 return { kind: 'delete', path };
125 }
126 if (kind === 'rename') {
127 const from = rng.pick(paths);
128 const to = newPath();
129 if (!free(files, to) || !parentExists(files, to)) return null;
130 files.set(to, files.get(from));
131 files.delete(from);
132 return { kind: 'rename', from, to };
133 }
134 if (dirs.length === 0) return null;
135 const dir = rng.pick(dirs);
136 const under = paths.filter(p => p.startsWith(`${dir}/`));
137 if (kind === 'delete-dir') {
138 for (const p of under) files.delete(p);
139 return { kind: 'delete', path: dir };
140 }
141 const to = rng.bool(0.5) ? `moved${rng.int(0, 1)}` : `${rng.pick(dirs)}/moved`;
142 if (to === dir || to.startsWith(`${dir}/`) || !free(files, to) || !parentExists(files, to)) return null;
143 for (const p of under) { files.set(`${to}/${p.slice(dir.length + 1)}`, files.get(p)); files.delete(p); }
144 return { kind: 'rename', from: dir, to };
145}
147function genList(rng, seedFiles) {
148 const files = new Map(seedFiles);
149 const want = rng.bool(0.34) ? 1 : rng.int(2, 7);
150 const changes = [];
151 for (let tries = 0; changes.length < want && tries < 200; tries++) {
152 const c = genChange(rng, files);
153 if (c !== null) changes.push(c);
154 }
155 return { changes, files };
156}
158// ---- the oracle: git's own commands in a copy of the seed work tree ----
159// git removes a directory from the work tree when `git rm` empties it, not when `git mv` does; an empty directory
160// left behind would turn a later `git mv x <that name>` into a move INTO it. Trees have no empty directories.
161const prune = (work, p) => { for (let d = dirname(p); d !== '.'; d = dirname(d)) { try { rmdirSync(join(work, d)); } catch { return; } } };
162function byGit(seedWork, work, changes, model) {
163 cpSync(seedWork, work, { recursive: true });
164 for (const c of changes) {
165 if (c.kind === 'write' || c.kind === 'create') {
166 mkdirSync(dirname(join(work, c.path)), { recursive: true });
167 writeFileSync(join(work, c.path), c.content); // an existing file keeps its mode; a new one is 0644
168 if (c.mode !== undefined) chmodSync(join(work, c.path), c.mode === '100755' ? 0o755 : 0o644);
169 git(work, ['add', '--', c.path]);
170 } else if (c.kind === 'edit') {
171 writeFileSync(join(work, c.path), model.at(c)); // git has no find/replace: the model's bytes after this edit
172 git(work, ['add', '--', c.path]);
173 } else if (c.kind === 'delete') {
174 // -f: git rm refuses a path whose staged content differs from HEAD (an earlier change of this list) without it
175 git(work, ['rm', '-r', '-q', '-f', '--', c.path]);
176 } else {
177 git(work, ['mv', '--', c.from, c.to]);
178 prune(work, c.from);
179 }
180 }
181 git(work, ['commit', '-q', '--allow-empty', '-m', 'by git']);
182 return { listing: git(work, ['ls-tree', '-r', '-t', 'HEAD']), tree: git(work, ['rev-parse', 'HEAD^{tree}']).trim() };
183}
185// The bytes each edit must leave, replayed from the seed model by the list alone (no ply code).
186function editBytes(seedFiles, changes) {
187 const files = new Map([...seedFiles].map(([p, f]) => [p, f.content]));
188 const out = new Map();
189 for (const c of changes) {
190 if (c.kind === 'write' || c.kind === 'create') files.set(c.path, c.content);
191 else if (c.kind === 'edit') { files.set(c.path, files.get(c.path).split(c.find).join(c.replace)); out.set(c, files.get(c.path)); }
192 else if (c.kind === 'delete') { for (const p of [...files.keys()]) if (p === c.path || p.startsWith(`${c.path}/`)) files.delete(p); }
193 else for (const p of [...files.keys()]) {
194 if (p !== c.from && !p.startsWith(`${c.from}/`)) continue;
195 files.set(c.to + p.slice(c.from.length), files.get(p));
196 files.delete(p);
197 }
198 }
199 return { at: c => out.get(c) };
200}
202let root, cleanup, srv, n = 0;
203const repos = [];
205before(async () => {
206 ({ root, cleanup } = tmpRoot());
207 srv = await startGitServer(root);
208 const rng = new Rng(SEED);
209 for (let r = 0; r < REPOS; r++) {
210 const repoRng = rng.fork();
211 const listRngs = Array.from({ length: LISTS }, () => rng.fork());
212 const { files, twins, subTwins } = genRepo(repoRng);
213 const work = join(root, `seed${r}`);
214 git(root, ['-c', 'core.autocrlf=false', 'init', '-q', '-b', 'main', work]); noMaintenance(work);
215 for (const [k, v] of [['user.name', 's'], ['user.email', 's@x'], ['commit.gpgsign', 'false'], ['core.autocrlf', 'false'], ['core.fileMode', 'true']]) git(work, ['config', k, v]);
216 for (const [p, f] of files) {
217 mkdirSync(dirname(join(work, p)), { recursive: true });
218 writeFileSync(join(work, p), f.content);
219 if (f.exec) chmodSync(join(work, p), 0o755);
220 }
221 git(work, ['add', '-A']);
222 git(work, ['commit', '-q', '-m', 'seed']);
223 // the fixture is what the test is about: by git's own hashing, the twins are one tree oid and so are the sub twins
224 assert.equal(new Set(twins.map(d => git(work, ['rev-parse', `HEAD:${d}`]).trim())).size, 1, `seed ${SEED} repo ${r}: twins differ`);
225 assert.equal(new Set([`${twins[0]}/sub`, ...subTwins].map(d => git(work, ['rev-parse', `HEAD:${d}`]).trim())).size, 1, `seed ${SEED} repo ${r}: sub twins differ`);
226 const bares = new Map();
227 for (const [name, cfg] of SERVERS) {
228 const bare = makeBareRepo(root, `tpl${r}-${name}`);
229 for (const [k, v] of cfg) git(bare, ['config', k, v]);
230 git(work, ['push', '-q', bare, 'main']);
231 bares.set(name, bare);
232 }
233 repos.push({ work, bares, files, listRngs, seedTree: git(work, ['rev-parse', 'HEAD^{tree}']).trim() });
234 }
235});
236after(async () => { await srv.close(); cleanup(); });
238for (let r = 0; r < REPOS; r++) {
239 for (let l = 0; l < LISTS; l++) {
240 if (ONLY !== '' && ONLY !== `${r}.${l}`) continue;
241 for (const [server] of SERVERS) {
242 test(`generated change list ${r}.${l} matches git [${server}] (seed ${SEED})`, async () => {
243 const repo = repos[r];
244 // a fresh generator per run of the case, so the two servers (and a rerun) see the same list
245 const { changes } = genList(new Rng(repo.listRngs[l].s), repo.files);
246 const rerun = `CHANGES_GEN_SEED=${SEED} CHANGES_GEN_REPOS=${REPOS} CHANGES_GEN_LISTS=${LISTS} CHANGES_GEN_ONLY=${r}.${l} npx vitest run test/changes-generated.test.js`;
247 const where = `\nFAILING SEED ${SEED} case ${r}.${l} [${server}]\nrerun: ${rerun}\nchanges: ${JSON.stringify(changes)}\n`;
248 const want = byGit(repo.work, join(root, `g${n++}`), changes, editBytes(repo.files, changes));
249 const name = `t${n++}`;
250 const bare = join(root, `${name}.git`);
251 cpSync(repo.bares.get(server), bare, { recursive: true });
252 const res = await commitChanges(`${srv.url}/${name}.git`, { ref: 'refs/heads/main', author: AUTHOR, message: 'generated', changes }, { tracer: new Tracer() });
253 // git's tree decides the outcome: the seed's tree again is nothing to commit
254 assert.equal(res.ok ? `ok ${res.value.outcome}` : `${res.error.code}: ${res.error.message}`, want.tree === repo.seedTree ? 'ok noop' : 'ok commit', where);
255 assert.equal(git(bare, ['ls-tree', '-r', '-t', 'refs/heads/main']), want.listing, where);
256 assert.equal(res.value.tree, want.tree, where);
257 assert.deepEqual(fsckProblems(bare), [], where);
258 });
259 }
260 }
261}
Diagnostics6 git calls · 117 ms
CallStatusSizeTime0117 ms
discover200191 B1 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 · hit1 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=1 git.response.bytes=11990 git.response.cache=HIT git.response.duration_ms=1 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=3 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 · 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=5323b9266eea5ded637285d1c5320699f12be76eOpen 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=109 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=be7e0453974a5fe832f9065a33bb7e3434c10e9bOpen pack map
fetch2005.0 KiB109 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=5085 git.response.cache=MISS git.response.duration_ms=109 git.fetch.wants=fe5b88f3a2de7cd517df2d20b37b9794c3d2e5fd 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=5085 git.pkt.payload.max=5058 git.sideband.band1.bytes=5058 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
- a5e0f82908db332c
- Request
- e1fc7886…
- 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