main
1// commitChanges over directories that share one tree oid (template, locale and package-copy directories). The
2// in-memory tree must hold one mutable node per PATH: a node shared per oid applies a change under every twin.
3// Judge: git. The same change list is applied in a clone with git add / git rm / git mv, and `git ls-tree -r` of
4// ply's pushed commit must equal git's byte for byte, on three receive-pack configurations a host may run:
5// strict-unpack : receive.fsckObjects=true, small push -> unpack-objects --strict (the suite's default bare repo)
6// index-pack : receive.fsckObjects=true, receive.unpackLimit=1 -> index-pack --strict
7// no-fsck : receive.fsckObjects=false (git's own default), where a wrong tree is accepted silently
8import { test, beforeAll as before, afterAll as after } from 'vitest';
9import assert from 'node:assert/strict';
10import { join, dirname } from 'node:path';
11import { cpSync, mkdirSync, writeFileSync, chmodSync } from 'node:fs';
12import { git, tmpRoot, makeBareRepo, startGitServer, fsckProblems, noMaintenance } from './server.js';
13import { commitChanges } from '../src/changes.js';
14import { Tracer } from '../src/otel.js';
16const AUTHOR = { name: 'Ada', email: 'ada@x', tz: '+0000', time: 1700000000 };
17const CONFIGS = [
18 ['strict-unpack', []],
19 ['index-pack', [['receive.unpackLimit', '1']]],
20 ['no-fsck', [['receive.fsckObjects', 'false'], ['transfer.fsckObjects', 'false']]],
21];
22// a/, b/, c/ and n/a/ are one tree oid; so are their sub/ directories (twins inside twins), at two depths.
23const TWINS = ['a', 'b', 'c', 'n/a'];
24const LISTS = {
25 'write under one twin': [{ kind: 'write', path: 'a/only-in-a.txt', content: 'A\n' }],
26 'write different files under two twins': [
27 { kind: 'write', path: 'a/only-in-a.txt', content: 'A\n' }, { kind: 'write', path: 'b/only-in-b.txt', content: 'B\n' },
28 ],
29 'write different files under three twins': [
30 { kind: 'write', path: 'a/1.txt', content: '1\n' }, { kind: 'write', path: 'b/2.txt', content: '2\n' }, { kind: 'write', path: 'c/3.txt', content: '3\n' },
31 ],
32 'overwrite one file under one twin': [{ kind: 'write', path: 'b/config.json', content: '{"b":1}\n' }],
33 'delete under one twin': [{ kind: 'delete', path: 'a/config.json' }],
34 'delete under one twin, write under another': [{ kind: 'delete', path: 'a/README.md' }, { kind: 'write', path: 'b/z.txt', content: 'z\n' }],
35 'rename between twins': [{ kind: 'rename', from: 'a/README.md', to: 'b/moved.md' }],
36 'rename inside one twin': [{ kind: 'rename', from: 'c/config.json', to: 'c/settings.json' }],
37 'nested twins: write in one, delete in another': [
38 { kind: 'write', path: 'a/sub/new.txt', content: 'new\n' }, { kind: 'delete', path: 'n/a/sub/leaf.txt' },
39 ],
40 'nested twin at another depth': [{ kind: 'write', path: 'n/a/only-nested.txt', content: 'N\n' }, { kind: 'delete', path: 'c/sub/leaf.txt' }],
41 // the two rebuilt directories are again one tree: the pack must carry that new tree once
42 'the same new file under two twins': [
43 { kind: 'write', path: 'a/same.txt', content: 'same\n' }, { kind: 'write', path: 'b/same.txt', content: 'same\n' },
44 ],
45 'edit under one twin, create under another': [
46 { kind: 'edit', path: 'a/README.md', find: 'template', replace: 'edited' }, { kind: 'create', path: 'c/x.txt', content: 'x\n' },
47 ],
48 'delete a whole twin, then write into the next': [
49 { kind: 'delete', path: 'a/README.md' }, { kind: 'delete', path: 'a/config.json' }, { kind: 'delete', path: 'a/sub/leaf.txt' },
50 { kind: 'write', path: 'b/sub/kept.txt', content: 'k\n' },
51 ],
52};
54let root, cleanup, srv, seed, n = 0;
55const templates = new Map();
57before(async () => {
58 ({ root, cleanup } = tmpRoot());
59 srv = await startGitServer(root);
60 seed = join(root, 'seed');
61 git(root, ['-c', 'core.autocrlf=false', 'init', '-q', '-b', 'main', seed]); noMaintenance(seed);
62 for (const [k, v] of [['user.name', 's'], ['user.email', 's@x'], ['commit.gpgsign', 'false'], ['core.autocrlf', 'false']]) git(seed, ['config', k, v]);
63 for (const d of TWINS) {
64 mkdirSync(join(seed, d, 'sub'), { recursive: true });
65 writeFileSync(join(seed, d, 'README.md'), 'template\n');
66 writeFileSync(join(seed, d, 'config.json'), '{}\n');
67 writeFileSync(join(seed, d, 'sub', 'leaf.txt'), 'leaf\n');
68 }
69 writeFileSync(join(seed, 'run.sh'), '#!/bin/sh\n');
70 chmodSync(join(seed, 'run.sh'), 0o755);
71 git(seed, ['add', '-A']);
72 git(seed, ['commit', '-q', '-m', 'seed']);
73 // the fixture is what the test is about: the twins are one tree oid, by git's own hashing
74 const oids = new Set(TWINS.map(d => git(seed, ['rev-parse', `HEAD:${d}`]).trim()));
75 assert.equal(oids.size, 1);
76 for (const [name, cfg] of CONFIGS) {
77 const bare = makeBareRepo(root, `tpl-${name}`);
78 for (const [k, v] of cfg) git(bare, ['config', k, v]);
79 git(seed, ['push', '-q', bare, 'main']);
80 templates.set(name, bare);
81 }
82});
83after(async () => { await srv.close(); cleanup(); });
85// The same list by git's own commands in a clone of the seed: the oracle's `ls-tree -r`.
86function byGit(changes) {
87 const work = join(root, `g${n++}`);
88 cpSync(seed, work, { recursive: true });
89 for (const c of changes) {
90 if (c.kind === 'write' || c.kind === 'create') {
91 mkdirSync(dirname(join(work, c.path)), { recursive: true });
92 writeFileSync(join(work, c.path), c.content);
93 git(work, ['add', '--', c.path]);
94 } else if (c.kind === 'edit') {
95 // git has no find/replace: the expected bytes are written out here ('template\n' with the one match replaced)
96 writeFileSync(join(work, c.path), 'edited\n');
97 git(work, ['add', '--', c.path]);
98 } else if (c.kind === 'delete') git(work, ['rm', '-q', '--', c.path]);
99 else git(work, ['mv', c.from, c.to]);
100 }
101 git(work, ['commit', '-q', '-m', 'by git']);
102 return git(work, ['ls-tree', '-r', 'HEAD']);
103}
105for (const [label, changes] of Object.entries(LISTS)) {
106 for (const [config] of CONFIGS) {
107 test(`twin directories: ${label} [${config}]`, async () => {
108 const name = `t${n++}`;
109 const bare = join(root, `${name}.git`);
110 cpSync(templates.get(config), bare, { recursive: true });
111 const want = byGit(changes);
112 const r = await commitChanges(`${srv.url}/${name}.git`, { ref: 'refs/heads/main', author: AUTHOR, message: label, changes }, { tracer: new Tracer() });
113 assert.equal(r.ok ? `ok ${r.value.outcome}` : `${r.error.code}: ${r.error.message}`, 'ok commit');
114 assert.equal(git(bare, ['ls-tree', '-r', 'refs/heads/main']), want);
115 assert.deepEqual(fsckProblems(bare), []);
116 });
117 }
118}
120// Rewriting a file's content does not change its mode in git: `git add` of new bytes over run.sh (100755) keeps
121// 100755. A `write` with no mode does the same; an explicit mode wins; a path with no regular file there is 100644.
122test('write with no mode keeps the mode of the regular file it replaces, as git add does', async () => {
123 const name = `t${n++}`;
124 const bare = join(root, `${name}.git`);
125 cpSync(templates.get('strict-unpack'), bare, { recursive: true });
126 const work = join(root, `g${n++}`);
127 cpSync(seed, work, { recursive: true });
128 writeFileSync(join(work, 'run.sh'), '#!/bin/sh\necho hi\n');
129 writeFileSync(join(work, 'a/README.md'), 'plain\n');
130 writeFileSync(join(work, 'new.sh'), 'new\n');
131 git(work, ['add', '-A']);
132 git(work, ['commit', '-q', '-m', 'by git']);
133 const want = git(work, ['ls-tree', '-r', 'HEAD']);
134 assert.match(want, /^100755 blob [0-9a-f]{40}\trun\.sh$/mu);
135 const changes = [
136 { kind: 'write', path: 'run.sh', content: '#!/bin/sh\necho hi\n' },
137 { kind: 'write', path: 'a/README.md', content: 'plain\n' },
138 { kind: 'write', path: 'new.sh', content: 'new\n' },
139 ];
140 const r = await commitChanges(`${srv.url}/${name}.git`, { ref: 'refs/heads/main', author: AUTHOR, message: 'modes', changes }, { tracer: new Tracer() });
141 assert.equal(r.ok ? `ok ${r.value.outcome}` : `${r.error.code}: ${r.error.message}`, 'ok commit');
142 assert.equal(git(bare, ['ls-tree', '-r', 'refs/heads/main']), want);
143 // an explicit mode always wins: by hand, run.sh becomes 100644 and a/config.json 100755
144 const again = await commitChanges(`${srv.url}/${name}.git`, {
145 ref: 'refs/heads/main', author: AUTHOR, message: 'explicit',
146 changes: [{ kind: 'write', path: 'run.sh', content: 'x\n', mode: '100644' }, { kind: 'write', path: 'a/config.json', content: '{}\n', mode: '100755' }],
147 }, { tracer: new Tracer() });
148 assert.equal(again.ok, true);
149 assert.equal(git(bare, ['ls-tree', 'refs/heads/main', 'run.sh']).slice(0, 6), '100644');
150 assert.equal(git(bare, ['ls-tree', 'refs/heads/main', 'a/config.json']).slice(0, 6), '100755');
151});
Diagnostics4 git calls · 224 ms
CallStatusSizeTime0224 ms
discover200152 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 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 · 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=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 · 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=38434 git.response.cache=HIT git.response.duration_ms=2 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
fetch2002.8 KiB220 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=220 git.response.bytes=2857 git.response.cache=MISS git.response.duration_ms=220 git.fetch.wants=f75c26c585406ad1305b03b22173d808ea2c38bf 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=2857 git.pkt.payload.max=2836 git.sideband.band1.bytes=2835 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
- a5e2bac1f97ccf46
- Request
- 694c47e5…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format