Artifactspublic-v0

ply

main
ply4 git calls340 ms49 KiB2 cachedprotocol v2
610 lines38 KiBRaw
1// pushRepo (ply/repo-node): a local repository, with its history, pushed to `git http-backend` with no git on the
2// client side (git builds the fixture and judges; pushRepo reads `.git` itself). Two servers: the default fixture and a
3// stock one (upload-pack at git's defaults). The local repository has packed and loose objects, packed and loose refs,
4// an annotated and a lightweight tag, an executable, a symlink, a gitlink (not followed) and a file edited in every
5// commit, so the pack carries OFS deltas.
6//
7// After every push: the objects sent are exactly `git rev-list --objects <new> --not <what the remote had>` (what git
8// push sends), the captured pack passes `git index-pack --strict` on its own (non-thin) or `--fix-thin` against the
9// remote (thin), the remote passes `git fsck --strict`, and its refs equal the local refs pushed. At the end a
10// `git clone --mirror` of the remote passes fsck and has the same refs. The pack size is measured against
11// `git pack-objects --revs --thin --delta-base-offset`, which is what `git push` runs.
12import { test, beforeAll as before, afterAll as after } from 'vitest';
13import assert from 'node:assert/strict';
14import { join } from 'node:path';
15import { mkdirSync, writeFileSync, symlinkSync, chmodSync, rmSync, appendFileSync, mkdtempSync } from 'node:fs';
16import { execFileSync } from 'node:child_process';
17import { deflateSync } from 'node:zlib';
18import { readBody } from './body.js';
19import { git, gitBuf, gitAsync, tmpRoot, makeBareRepo, startGitServer, fsckProblems, initRepo } from './server.js';
20import { pushRepo, readConfig } from '../src/repo-node.js';
21import { encodeDelta, planDeltas } from '../src/delta.js';
22import { applyDelta, buildPack, encodeOfsDistance, parsePack, resolvePack, readOfsDelta } from '../src/pack.js';
23import { hashObject, TYPE } from '../src/objects.js';
24import { DEFAULT_LIMITS, withLimits } from '../src/limits.js';
25import { Tracer, arrayExporter, flattenLine } from '../src/otel.js';
27const ZERO = '0'.repeat(40);
28let root, cleanup, srv, n = 0;
29before(async () => { ({ root, cleanup } = tmpRoot()); srv = await startGitServer(root); });
30after(async () => { await srv.close(); cleanup(); });
32const ID = ['-c', 'user.name=Local', '-c', 'user.email=local@example.com'];
33const remoteRepo = stock => { const name = `r${n++}`; const bare = makeBareRepo(root, name, { stock }); return { bare, url: `${srv.url}/${name}.git` }; };
34const rev = (dir, r) => git(dir, ['rev-parse', r]).trim();
35const refsOf = dir => new Map(git(dir, ['for-each-ref', '--format=%(refname) %(objectname)']).trim().split('\n').filter(Boolean).map(l => l.split(' ')));
36const objectsOf = (dir, args) => new Set(git(dir, ['rev-list', '--objects', ...args]).trim().split('\n').filter(Boolean).map(l => l.slice(0, 40)));
37const fsck = bare => assert.deepEqual(fsckProblems(bare), []);
38// What the remote lacks: everything reachable from `tips` minus everything reachable from the remote's refs, as an exact
39// set difference. `rev-list --objects tips --not remoteTips` is not that: it excludes only the trees of the boundary
40// commits, so a blob an older commit already holds and a newer one brings back is counted again (on this repository's
41// own history, tsconfig.isolated.json at 5ce754b: rev-list 1783, the remote lacks 1782, pushRepo sends 1782).
42const wouldSend = (local, tips, remoteTips) => {
43 const want = objectsOf(local, tips);
44 if (remoteTips.length) for (const o of objectsOf(local, remoteTips)) want.delete(o);
45 return want;
46};
47// requests by service, and every receive-pack body (the pack is what follows the commands' flush)
48const counting = () => {
49 const c = { 'git-upload-pack': 0, 'git-receive-pack': 0, packs: [] };
50 const fetchImpl = async (url, given) => {
51 const kind = String(url).split('/').pop();
52 c[kind] = (c[kind] ?? 0) + 1;
53 // a pack past INFLATE_CHUNK is a streamed body (src/packwrite.ts): read once, forwarded as its bytes
54 const { bytes: body, init, streamed } = await readBody(given);
55 if (kind === 'git-receive-pack') { const at = body.indexOf('0000PACK'); c.packs.push(at < 0 ? null : body.subarray(at + 4)); c.streamed = (c.streamed ?? 0) + (streamed ? 1 : 0); }
56 return fetch(url, init);
57 };
58 return { c, fetchImpl };
59};
60const traced = () => { const ex = arrayExporter(); return { tracer: new Tracer({ exporter: ex }), spans: () => ex.lines.flatMap(flattenLine) }; };
61// The pack's objects as git index-pack --strict reads it: in a fresh repository (a self-contained pack), or in `dir`
62// (the remote after the push: --strict checks every object the pack names is there). Never --fix-thin: a thin base
63// would fail it.
64function indexPack(pack, into = null) {
65 const dir = into ?? mkdtempSync(join(root, 'ip-'));
66 if (into === null) git(dir, ['init', '-q', '--bare']);
67 const out = execFileSync('git', ['index-pack', '--strict', '--stdin', '-v'], { cwd: dir, input: pack, stdio: ['pipe', 'pipe', 'pipe'] });
68 const idx = join(dir, 'objects', 'pack', `pack-${out.toString().trim().split(/\s+/).pop()}.idx`);
69 const rows = git(dir, ['verify-pack', '-v', idx]).trim().split('\n').filter(l => /^[0-9a-f]{40} /.test(l));
70 return { oids: new Set(rows.map(l => l.slice(0, 40))), deltas: rows.filter(l => l.split(/\s+/).length >= 7).length };
73// ---- the local repository ----
74const lines = (seed, count) => Array.from({ length: count }, (_, i) => `export const v${i} = ${(i * 2654435761 + seed) >>> 0}; // line ${i}`);
75function makeLocal(name = `local${n++}`) {
76 const dir = join(root, name);
77 initRepo(dir);
78 const src = lines(1, 300);
79 const commit = (msg, edit) => {
80 edit();
81 git(dir, ['add', '-A']);
82 git(dir, [...ID, 'commit', '-q', '-m', msg]);
83 };
84 const put = (p, d) => { mkdirSync(join(dir, p, '..'), { recursive: true }); writeFileSync(join(dir, p), d); };
85 commit('one', () => { put('src/main.js', `${src.join('\n')}\n`); put('README.md', '# local\n'); put('bin/run', '#!/bin/sh\necho run\n'); chmodSync(join(dir, 'bin/run'), 0o755); symlinkSync('src/main.js', join(dir, 'link')); });
86 for (let i = 2; i <= 6; i++) {
87 commit(`edit ${i}`, () => {
88 src[(i * 41) % src.length] = `// changed in ${i}`;
89 src.splice((i * 17) % src.length, 0, `// inserted in ${i}`);
90 put('src/main.js', `${src.join('\n')}\n`);
91 put(`docs/note${i}.md`, `note ${i}\n${lines(i, 20).join('\n')}\n`);
92 });
93 if (i === 3) {
94 git(dir, ['branch', 'old']);
95 git(dir, ['tag', 'lw']);
96 git(dir, ['repack', '-adq']); // the history so far is packed; what follows is loose
97 git(dir, ['pack-refs', '--all']);
98 }
99 }
100 // a gitlink: in the tree, never followed (its commit is not in this repository)
101 git(dir, ['update-index', '--add', '--cacheinfo', `160000,${'c0ffee'.padEnd(40, '0')},vendor/sub`]);
102 git(dir, [...ID, 'commit', '-q', '-m', 'submodule']);
103 git(dir, [...ID, 'tag', '-a', 'v1', '-m', 'release 1', 'main~1']);
104 git(dir, ['branch', 'topic', 'main~2']);
105 return dir;
108for (const stock of [false, true]) {
109 test(`pushRepo ${stock ? 'stock' : 'default'}: first push, incremental, no-op, non-fast-forward, force, tags, delete, dry run, all — judged by git`, async () => {
110 const local = makeLocal();
111 const { bare, url } = remoteRepo(stock);
112 const push = async (spec, opts = {}) => { const k = counting(); const r = await pushRepo(local, url, spec, { fetchImpl: k.fetchImpl, ...opts }); return { r, k }; };
113 const sent = (k, i = 0) => indexPack(k.c.packs[i], bare);
115 // 1. an empty remote: `old` (three commits) as main; the pack is self-contained and deltified
116 let { r, k } = await push({ refspecs: ['old:main'] });
117 assert.ok(r.ok, r.ok || r.error.message);
118 assert.deepEqual(r.value.updates, [{ ref: 'refs/heads/main', oldOid: ZERO, newOid: rev(local, 'old'), kind: 'create' }]);
119 let want = wouldSend(local, ['old'], []);
120 let pack = sent(k);
121 assert.deepEqual(pack.oids, want, 'the objects git push would send');
122 assert.equal(r.value.objects, want.size);
123 assert.ok(pack.deltas > 0 && pack.deltas === r.value.deltas, `deltas: ${pack.deltas} in the pack, ${r.value.deltas} planned`);
124 assert.equal(k.c['git-receive-pack'], 1);
125 fsck(bare);
126 assert.equal(rev(bare, 'refs/heads/main'), rev(local, 'old'));
128 // 2. incremental: main on top sends main --not old, nothing the remote has
129 ({ r, k } = await push({ refspecs: ['main'] }));
130 assert.ok(r.ok, r.ok || r.error.message);
131 assert.equal(r.value.updates[0].kind, 'update');
132 want = wouldSend(local, ['main'], [rev(local, 'old')]);
133 pack = sent(k);
134 assert.deepEqual(pack.oids, want);
135 assert.ok(![...pack.oids].includes('c0ffee'.padEnd(40, '0')), 'the gitlink is not followed');
136 fsck(bare);
137 assert.equal(rev(bare, 'refs/heads/main'), rev(local, 'main'));
138 const gitPackBytes = execFileSync('git', ['pack-objects', '--stdout', '--revs', '--thin', '--delta-base-offset', '-q'], { cwd: local, input: `main\n^old\n` }).length;
139 console.log(` ${stock ? 'stock' : 'default'}: incremental ${r.value.objects} objects, ${r.value.deltas} deltas, ${r.value.packBytes} B (git push: ${gitPackBytes} B, thin)`);
141 // 3. again: up to date, no receive-pack request
142 const { tracer, spans } = traced();
143 ({ r, k } = await push({ refspecs: ['main'] }, { tracer }));
144 assert.ok(r.ok);
145 assert.equal(r.value.upToDate, true);
146 assert.deepEqual(r.value.current, ['refs/heads/main']);
147 assert.deepEqual(r.value.updates, []);
148 assert.equal(k.c['git-receive-pack'], 0, 'no receive-pack request');
149 assert.ok(!spans().some(s => s.name === 'git.receive-pack' || s.name === 'git.push_repo.read'), 'no push and no object read');
150 assert.equal(spans().find(s => s.name === 'git.push_repo').attrs['git.push_repo.outcome'], 'up_to_date');
152 // 4. an older commit over main: refused locally, nothing sent; `+` and force push it
153 ({ r, k } = await push({ refspecs: ['old:main'] }));
154 assert.equal(r.ok, false);
155 assert.equal(r.error.code, 'not_fast_forward');
156 assert.match(r.error.message, /refs\/heads\/main \(non-fast-forward\)/);
157 assert.equal(k.c['git-receive-pack'], 0);
158 assert.equal(rev(bare, 'refs/heads/main'), rev(local, 'main'));
159 ({ r } = await push({ refspecs: ['+old:main'] }));
160 assert.ok(r.ok);
161 assert.deepEqual(r.value.updates, [{ ref: 'refs/heads/main', oldOid: rev(local, 'main'), newOid: rev(local, 'old'), kind: 'force' }]);
162 assert.equal(r.value.objects, 0);
163 assert.equal(rev(bare, 'refs/heads/main'), rev(local, 'old'));
164 ({ r } = await push({ refspecs: ['main'] }));
165 assert.ok(r.ok);
166 assert.equal(r.value.updates[0].kind, 'update');
167 ({ r } = await push({ refspecs: ['old:main'], force: true }));
168 assert.ok(r.ok);
169 assert.equal(r.value.updates[0].kind, 'force');
170 ({ r } = await push({ refspecs: ['main'] }));
171 assert.ok(r.ok);
172 fsck(bare);
174 // a tip this repository does not have (someone else pushed): git's "fetch first"
175 const other = join(root, `other${n++}`);
176 git(root, ['clone', '-q', bare, other]);
177 writeFileSync(join(other, 'theirs.txt'), 'theirs\n');
178 git(other, ['add', '-A']); git(other, [...ID, 'commit', '-q', '-m', 'theirs']); git(other, ['push', '-q', 'origin', 'HEAD:refs/heads/ahead']);
179 ({ r, k } = await push({ refspecs: ['main:refs/heads/ahead'] }));
180 assert.equal(r.error.code, 'not_fast_forward');
181 assert.match(r.error.message, /refs\/heads\/ahead \(fetch first\)/);
182 assert.equal(k.c['git-receive-pack'], 0);
184 // 5. tags: the annotated tag's object is the only new object; the lightweight tag names a commit the remote has
185 ({ r, k } = await push({ tags: true }));
186 assert.ok(r.ok, r.ok || r.error.message);
187 assert.deepEqual(r.value.updates.map(u => [u.ref, u.kind]).sort(), [['refs/tags/lw', 'create'], ['refs/tags/v1', 'create']]);
188 assert.deepEqual(sent(k).oids, new Set([rev(local, 'refs/tags/v1')]));
189 assert.equal(git(bare, ['cat-file', '-t', 'refs/tags/v1']).trim(), 'tag');
190 assert.equal(rev(bare, 'refs/tags/v1'), rev(local, 'refs/tags/v1'));
191 fsck(bare);
192 // a tag that moved locally: git refuses it (already exists) unless forced
193 git(local, [...ID, 'tag', '-f', '-a', 'v1', '-m', 'release 1 again', 'main']);
194 ({ r, k } = await push({ tags: true }));
195 assert.equal(r.error.code, 'not_fast_forward');
196 assert.match(r.error.message, /refs\/tags\/v1 \(already exists\)/);
197 assert.equal(k.c['git-receive-pack'], 0);
198 ({ r } = await push({ refspecs: ['+v1'] }));
199 assert.ok(r.ok, r.ok || r.error.message);
200 assert.equal(r.value.updates[0].kind, 'force');
201 assert.equal(rev(bare, 'refs/tags/v1'), rev(local, 'refs/tags/v1'));
203 // 6. delete with :dst (a short name the remote has)
204 ({ r } = await push({ refspecs: ['topic'] }));
205 assert.ok(r.ok);
206 assert.equal(r.value.objects, 0, 'topic is an ancestor of main: nothing new');
207 assert.equal(rev(bare, 'refs/heads/topic'), rev(local, 'topic'));
208 const del = traced();
209 ({ r, k } = await push({ refspecs: [':topic'] }, { tracer: del.tracer }));
210 assert.ok(r.ok, r.ok || r.error.message);
211 assert.ok(!del.spans().some(s => s.name === 'git.push_repo.read'), 'a delete reads no objects');
212 assert.deepEqual(r.value.updates, [{ ref: 'refs/heads/topic', oldOid: rev(local, 'topic'), newOid: ZERO, kind: 'delete' }]);
213 assert.deepEqual(k.c.packs, [null], 'a delete sends no pack');
214 assert.ok(!refsOf(bare).has('refs/heads/topic'));
215 ({ r } = await push({ refspecs: [':nope'] }));
216 assert.equal(r.error.code, 'not_found');
217 assert.equal(r.error.miss.kind, 'ref');
219 // 7. dry run: the same decisions, no receive-pack request, the remote unchanged
220 git(local, ['checkout', '-q', '-b', 'feature']);
221 writeFileSync(join(local, 'feature.txt'), 'feature\n');
222 git(local, ['add', '-A']); git(local, [...ID, 'commit', '-q', '-m', 'feature']);
223 ({ r, k } = await push({ dryRun: true }));
224 assert.ok(r.ok, r.ok || r.error.message);
225 assert.equal(r.value.dryRun, true);
226 assert.deepEqual(r.value.updates, [{ ref: 'refs/heads/feature', oldOid: ZERO, newOid: rev(local, 'feature'), kind: 'create' }]);
227 assert.equal(r.value.objects, wouldSend(local, ['feature'], [rev(local, 'main')]).size);
228 assert.equal(r.value.packBytes, 0);
229 assert.equal(k.c['git-receive-pack'], 0);
230 assert.ok(!refsOf(bare).has('refs/heads/feature'));
231 // the default refspec is the current branch, to the same name
232 ({ r } = await push({}));
233 assert.ok(r.ok);
234 assert.equal(rev(bare, 'refs/heads/feature'), rev(local, 'feature'));
236 // 8. all: every branch, one request; then the mirror has exactly the local refs
237 ({ r, k } = await push({ all: true }));
238 assert.ok(r.ok, r.ok || r.error.message);
239 assert.deepEqual(r.value.updates.map(u => u.ref).sort(), ['refs/heads/old', 'refs/heads/topic']);
240 assert.equal(k.c['git-receive-pack'], 1);
241 const mirror = join(root, `mirror${n++}`);
242 git(root, ['clone', '-q', '--mirror', '--no-local', bare, mirror]);
243 fsck(mirror);
244 const remoteRefs = refsOf(mirror);
245 const localRefs = refsOf(local);
246 for (const [ref, oid] of remoteRefs) if (ref !== 'refs/heads/ahead') assert.equal(oid, localRefs.get(ref), ref);
247 for (const [ref, oid] of localRefs) assert.equal(remoteRefs.get(ref), oid, ref);
248 assert.equal(git(mirror, ['count-objects', '-v']).match(/in-pack: (\d+)/)[1], String(objectsOf(mirror, ['--all']).size));
249 });
252test('pushRepo thin: the first version of each path deltas against the remote\'s copy (REF_DELTA), and receive-pack accepts it', async () => {
253 const local = makeLocal();
254 for (const stock of [false, true]) {
255 const { bare, url } = remoteRepo(stock);
256 let r = await pushRepo(local, url, { refspecs: ['old:main'], thin: true });
257 assert.ok(r.ok);
258 assert.equal(r.value.thinDeltas, 0, 'nothing on the remote yet');
259 const k = counting();
260 r = await pushRepo(local, url, { refspecs: ['main'], thin: true }, { fetchImpl: k.fetchImpl });
261 assert.ok(r.ok, r.ok || r.error.message);
262 assert.ok(r.value.thinDeltas > 0, 'src/main.js deltas against the remote\'s version');
263 const thick = await pushRepo(local, url, { refspecs: ['main'], dryRun: true });
264 assert.equal(thick.value.upToDate, true);
265 fsck(bare);
266 // the pack is thin: index-pack needs --fix-thin and the remote's objects to complete it
267 const fixed = execFileSync('git', ['index-pack', '--strict', '--stdin', '--fix-thin'], { cwd: bare, input: k.c.packs[0] }).toString();
268 assert.match(fixed, /^pack\t[0-9a-f]{40}/);
269 assert.equal(rev(bare, 'refs/heads/main'), rev(local, 'main'));
270 }
271});
273test('pushRepo pack size: this repository\'s own history, main~50 then main, against git push\'s pack', async () => {
274 // The issue's measurement (#186). Skipped where the checkout has no history to push.
275 const repo = join(import.meta.dirname, '..');
276 let count = 0;
277 try { count = Number(git(repo, ['rev-list', '--count', 'HEAD']).trim()); } catch { count = 0; }
278 if (count < 60 || git(repo, ['rev-parse', '--is-shallow-repository']).trim() === 'true') return;
279 const local = join(root, `self${n++}`);
280 git(root, ['clone', '-q', '--no-local', '--single-branch', repo, local]);
281 git(local, ['checkout', '-q', '-B', 'main', rev(repo, 'HEAD')]);
282 for (const t of git(local, ['tag', '-l']).trim().split('\n').filter(Boolean)) git(local, ['tag', '-d', t]);
283 const { url } = remoteRepo(true);
284 const base = rev(local, 'HEAD~50');
285 const gitPack = revs => execFileSync('git', ['pack-objects', '--stdout', '--revs', '--thin', '--delta-base-offset', '-q'], { cwd: local, input: `${revs.join('\n')}\n`, maxBuffer: 1 << 30 }).length;
286 const first = await pushRepo(local, url, { refspecs: [`${base}:refs/heads/main`] });
287 assert.ok(first.ok, first.ok || first.error.message);
288 const firstGit = gitPack([base]);
289 const incr = await pushRepo(local, url, { refspecs: ['main'], dryRun: false, thin: true });
290 assert.ok(incr.ok, incr.ok || incr.error.message);
291 const incrGit = gitPack(['main', `^${base}`]);
292 console.log(` this repository: first push ${first.value.objects} objects ${first.value.packBytes} B (git ${firstGit} B, ×${(first.value.packBytes / firstGit).toFixed(2)}); incremental thin ${incr.value.objects} objects ${incr.value.packBytes} B (git ${incrGit} B, ×${(incr.value.packBytes / incrGit).toFixed(2)})`);
293 assert.equal(first.value.objects, wouldSend(local, [base], []).size);
294 assert.equal(incr.value.objects, wouldSend(local, ['main'], [base]).size);
295 assert.ok(first.value.packBytes < 1.5 * firstGit, 'first push well under 2× git');
296 assert.ok(incr.value.packBytes < 1.5 * incrGit, 'incremental (thin) well under 2× git');
297}, 120_000);
299// ---- refusals ----
300const NOWHERE = 'http://127.0.0.1:9/never.git';
301const refused = async (dir, spec, what, opts = {}) => {
302 const r = await pushRepo(dir, NOWHERE, spec, opts);
303 assert.equal(r.ok, false);
304 assert.equal(r.error.code, 'bad_request', r.error.message);
305 assert.equal(r.error.what, what, r.error.message);
306 return r.error;
307};
309test('pushRepo refuses, before any request, what it does not read: not a repository, a bad spec, SHA-256, reftable, a partial clone, alternates, shallow, a bad ref', async () => {
310 const local = makeLocal();
311 const empty = join(root, `empty${n++}`); mkdirSync(empty);
312 await refused(empty, {}, 'dir');
313 await refused('', {}, 'dir');
314 await refused(local, { all: true, refspecs: ['main'] }, 'spec');
315 await refused(local, { refspecs: 'main' }, 'spec');
316 await refused(local, { dryRun: 'yes' }, 'spec');
317 // sha256: git's own repository format
318 const sha256 = join(root, `sha256-${n++}`);
319 git(root, ['init', '-q', '--object-format=sha256', sha256]);
320 assert.match((await refused(sha256, {}, 'objectFormat')).message, /sha256/);
321 // the format extensions, as git writes them
322 const copy = what => { const d = join(root, `${what}${n++}`); git(root, ['clone', '-q', '--no-local', local, d]); return d; };
323 const reftable = copy('reftable');
324 appendFileSync(join(reftable, '.git', 'config'), '[extensions]\n\trefStorage = reftable\n');
325 await refused(reftable, {}, 'refStorage');
326 const partial = copy('partial');
327 appendFileSync(join(partial, '.git', 'config'), '[extensions]\n\tpartialClone = origin\n');
328 await refused(partial, {}, 'partialClone');
329 const shared = join(root, `shared${n++}`);
330 git(root, ['clone', '-q', '--shared', local, shared]);
331 await refused(shared, {}, 'alternates');
332 const shallow = join(root, `shallow${n++}`);
333 git(root, ['clone', '-q', '--depth', '1', `file://${local}`, shallow]);
334 await refused(shallow, {}, 'shallow');
335 const badRef = copy('badref');
336 writeFileSync(join(badRef, '.git', 'refs', 'heads', 'broken'), 'not an id\n');
337 await refused(badRef, {}, 'ref');
338 // a symbolic ref that ends nowhere is left out, as git keeps it
339 const dangling = copy('dangling');
340 mkdirSync(join(dangling, '.git', 'refs', 'remotes', 'gone'), { recursive: true });
341 writeFileSync(join(dangling, '.git', 'refs', 'remotes', 'gone', 'HEAD'), 'ref: refs/remotes/gone/main\n');
342 const { url } = remoteRepo(false);
343 const ok = await pushRepo(dangling, url, { refspecs: ['main'] });
344 assert.ok(ok.ok, ok.ok || ok.error.message);
345 // limits.maxRefs bounds the refs read
346 const many = await pushRepo(local, NOWHERE, {}, { limits: withLimits({ maxRefs: 2 }) });
347 assert.equal(many.error.code, 'limit_exceeded');
348 assert.equal(many.error.what, 'local refs');
349 // a bare repository and a work tree whose .git is a file (a linked worktree) are read the same way
350 const bareLocal = join(root, `bare${n++}`);
351 git(root, ['clone', '-q', '--bare', local, bareLocal]);
352 const wt = join(root, `wt${n++}`);
353 git(local, ['worktree', 'add', '-q', wt, 'topic']);
354 for (const [dir, ref] of [[bareLocal, 'main'], [wt, 'topic']]) {
355 const remote = remoteRepo(false);
356 const r = await pushRepo(dir, remote.url, { refspecs: [ref] });
357 assert.ok(r.ok, r.ok || r.error.message);
358 assert.equal(rev(remote.bare, `refs/heads/${ref}`), rev(local, ref));
359 fsck(remote.bare);
360 }
361});
363test('pushRepo refspecs: git\'s syntax and resolution, and what it refuses', async () => {
364 const local = makeLocal();
365 const { bare, url } = remoteRepo(false);
366 const go = spec => pushRepo(local, url, spec);
367 const bad = async (spec, re) => { const r = await go(spec); assert.equal(r.ok, false, JSON.stringify(spec)); assert.equal(r.error.code, 'bad_request', r.error.message); assert.match(r.error.message, re); };
368 await bad({ refspecs: ['a:b:c'] }, /not src, src:dst/);
369 await bad({ refspecs: ['main:'] }, /not src, src:dst/);
370 await bad({ refspecs: ['nope'] }, /does not match any local ref/);
371 git(local, ['branch', 'dup']); git(local, ['tag', 'dup']);
372 await bad({ refspecs: ['dup'] }, /matches more than one ref/);
373 await bad({ refspecs: [rev(local, 'old')] }, /needs a destination/);
374 await bad({ refspecs: [`${rev(local, 'old')}:short`] }, /is not a full refname/);
375 await bad({ refspecs: ['main:x', 'old:x'] }, /destination of two refspecs/);
376 await bad({ refspecs: ['main:refs/heads/a b'] }, /not src, src:dst/);
377 await bad({ refspecs: ['main:refs/heads/bad..name'] }, /ref/);
378 // a 40-hex id to a full name; HEAD to its branch; a short destination the remote has; the same refspec twice
379 let r = await go({ refspecs: [`${rev(local, 'old')}:refs/heads/from-id`, 'HEAD', 'HEAD'] });
380 assert.ok(r.ok, r.ok || r.error.message);
381 assert.deepEqual(r.value.updates.map(u => u.ref).sort(), ['refs/heads/from-id', 'refs/heads/main']);
382 r = await go({ refspecs: ['old:from-id'] });
383 assert.ok(r.ok);
384 assert.deepEqual(r.value.current, ['refs/heads/from-id']);
385 // a detached HEAD has no default
386 git(local, ['checkout', '-q', '--detach', 'old']);
387 await bad({}, /HEAD is not a branch/);
388 r = await go({ refspecs: ['HEAD:refs/heads/detached'] });
389 assert.ok(r.ok);
390 assert.equal(rev(bare, 'refs/heads/detached'), rev(local, 'old'));
391 fsck(bare);
392});
394test('pushRepo fails closed: limits, a missing or corrupt object, a tree entry of the wrong type', async () => {
395 // loose objects only, so the store's own bounds decide
396 const loose = join(root, `loose${n++}`);
397 initRepo(loose);
398 writeFileSync(join(loose, 'big.txt'), 'x'.repeat(5000));
399 writeFileSync(join(loose, 'small.txt'), 'small\n');
400 git(loose, ['add', '-A']); git(loose, [...ID, 'commit', '-q', '-m', 'one']);
401 const { url } = remoteRepo(false);
402 const limited = async (limits, what) => { const r = await pushRepo(loose, url, {}, { limits: withLimits(limits) }); assert.equal(r.ok, false); assert.equal(r.error.code, 'limit_exceeded', r.error.message); assert.equal(r.error.what, what); };
403 await limited({ maxInflatedTotal: 1000 }, 'local objects bytes');
404 await limited({ maxPackObjects: 2 }, 'local objects');
405 await limited({ maxObjectSize: 4990 }, 'object size');
406 // packed, the pack's own bounds decide (parsePack, resolvePack), still limit_exceeded
407 const packed = makeLocal();
408 const p = await pushRepo(packed, url, { refspecs: ['old'] }, { limits: withLimits({ maxInflatedTotal: 2000 }) });
409 assert.equal(p.error.code, 'limit_exceeded');
410 // a missing object
411 const blob = rev(loose, 'HEAD:small.txt');
412 rmSync(join(loose, '.git', 'objects', blob.slice(0, 2), blob.slice(2)));
413 let r = await pushRepo(loose, url, {});
414 assert.equal(r.error.code, 'bad_request');
415 assert.equal(r.error.what, 'object');
416 assert.match(r.error.message, /small\.txt\) is missing from the local repository/);
417 // a loose object whose bytes do not hash to its name
418 writeFileSync(join(loose, '.git', 'objects', blob.slice(0, 2), blob.slice(2)), deflateSync(Buffer.from('blob 6\0SMALL\n')));
419 r = await pushRepo(loose, url, {});
420 assert.equal(r.error.code, 'pack_corrupt');
421 assert.match(r.error.message, /bad header or hash/);
422 // a tree entry that says tree and names a blob
423 const wrong = join(root, `wrong${n++}`);
424 initRepo(wrong);
425 const b = git(wrong, ['hash-object', '-w', '--stdin'], { input: 'blob\n' }).trim();
426 const tree = gitBuf(wrong, ['hash-object', '-t', 'tree', '-w', '--stdin', '--literally'], Buffer.concat([Buffer.from('40000 dir\0'), Buffer.from(b, 'hex')])).toString().trim();
427 const c = git(wrong, [...ID, 'commit-tree', tree, '-m', 'wrong']).trim();
428 git(wrong, ['update-ref', 'refs/heads/main', c]);
429 r = await pushRepo(wrong, url, {});
430 assert.equal(r.error.code, 'pack_corrupt');
431 assert.match(r.error.message, new RegExp(`${b} is a blob, not a tree`));
432});
434// ---- the delta generator and the pack encoding ----
435test('encodeDelta: applyDelta (and git index-pack) rebuild the target; null when the delta would not be smaller', async () => {
436 let seed = 7;
437 const rnd = () => (seed = (Math.imul(seed, 1103515245) + 12345) >>> 0) / 2 ** 32;
438 for (let t = 0; t < 400; t++) {
439 const base = Uint8Array.from({ length: Math.floor(rnd() * 4000) }, () => Math.floor(rnd() * (rnd() < 0.5 ? 3 : 256)));
440 const out = [...base];
441 for (let e = 0; e < 1 + rnd() * 6; e++) {
442 const p = Math.floor(rnd() * out.length), k = rnd();
443 if (k < 0.3) out.splice(p, Math.floor(rnd() * 60));
444 else if (k < 0.6) out.splice(p, 0, ...Array.from({ length: Math.floor(rnd() * 200) }, () => Math.floor(rnd() * 256)));
445 else out.splice(p, 0, ...base.subarray(Math.floor(rnd() * base.length), Math.floor(rnd() * base.length)));
446 }
447 const target = Uint8Array.from(out);
448 const d = encodeDelta(base, target, Infinity);
449 if (d === null) { assert.ok(base.length < 16 || target.length < 16); continue; }
450 assert.deepEqual(Buffer.from(applyDelta(base, d).value.out), Buffer.from(target));
451 const capped = encodeDelta(base, target, d.length);
452 assert.equal(capped, null, 'a delta of maxSize bytes or more is null');
453 }
454 // a chain of 70 versions of one path: depth bounded by maxDepth, OFS bases before their deltas, git accepts it
455 const objs = [];
456 const text = Array.from({ length: 300 }, (_, i) => `line ${i} ${'x'.repeat(i % 40)}\n`);
457 for (let v = 0; v < 70; v++) { text[(v * 37) % 300] = `changed ${v}\n`; objs.push({ type: TYPE.BLOB, data: Buffer.from(text.join('')), path: 'a.txt' }); }
458 const plan = planDeltas(objs, { maxDepth: 10 });
459 assert.ok(plan.ofs > 50);
460 const depth = [];
461 for (const [i, o] of plan.objects.entries()) { const ofs = o.base?.tag === 'ofs'; assert.ok(o.base === undefined || (ofs && o.base.index < i)); depth.push(ofs ? depth[o.base.index] + 1 : 0); }
462 assert.ok(Math.max(...depth) <= 10);
463 const { pack } = await buildPack(plan.objects);
464 const resolved = resolvePack(parsePack(pack).value);
465 assert.ok(objs.every(o => resolved.value.objects.has(hashObject('blob', o.data))));
466 const verify = indexPack(pack);
467 assert.equal(verify.oids.size, new Set(objs.map(o => hashObject('blob', o.data))).size);
468 assert.equal(verify.deltas, plan.ofs);
469 assert.equal(planDeltas(objs, { maxDepth: 0 }).ofs, 0);
470 // the OFS distance encoding round-trips through the reader
471 for (const d of [1, 127, 128, 16511, 16512, 2113663, 2113664, 1 << 30]) {
472 const enc = encodeOfsDistance(d);
473 assert.equal(readOfsDelta(enc, 0).value.off, d, String(d));
474 }
475});
477test('readConfig: sections, subsections, case, comments, a bare key', () => {
478 const c = readConfig('[core]\n\tbare = false ; comment\n[Extensions]\n\tObjectFormat = sha256\n[remote "origin"]\n\turl = x\n# c\n[x]\n\tflag\n');
479 assert.equal(c.get('core.bare'), 'false');
480 assert.equal(c.get('extensions.objectformat'), 'sha256');
481 assert.equal(c.get('remote.origin.url'), 'x');
482 assert.equal(c.get('x.flag'), 'true');
483});
485// ---- git is the judge of how a local repository is read: the same push, from the same directory, by git 2.50 ----
486// git's verdict: its exit status, its stderr, and the commands `git push --dry-run --porcelain` reports, in the shape
487// of pushRepo's dry run. Each new oid is git's own reading of the source ref it printed (rev-parse), so nothing
488// expected here comes from ply. Only creates are compared (the remote is empty): porcelain's `*` flag.
489const byRef = (a, b) => (a.ref < b.ref ? -1 : 1);
490const gitDryRun = async (dir, url, args) => {
491 const r = await gitAsync(dir, ['push', '--dry-run', '--porcelain', url, ...args]);
492 const rows = r.stdout.split('\n').filter(l => l.includes('\t')).map(l => l.split('\t'));
493 const updates = rows.map(([flag, pair]) => { const [src, dst] = pair.split(':'); return { ref: dst, newOid: rev(dir, src), kind: flag === '*' ? 'create' : flag }; });
494 return { status: r.status, stderr: r.stderr, updates: updates.sort(byRef) };
495};
496const plyDryRun = async (dir, url, spec) => {
497 const r = await pushRepo(dir, url, { ...spec, dryRun: true });
498 return r.ok ? { ok: true, updates: r.value.updates.map(u => ({ ref: u.ref, newOid: u.newOid, kind: u.kind })).sort(byRef) } : { ok: false, error: r.error };
499};
500// both sides on one push: git performs it, and pushRepo's commands are git's
501const sameAsGit = async (dir, url, args, spec) => {
502 const g = await gitDryRun(dir, url, args);
503 assert.equal(g.status, 0, `git push ${args.join(' ')}: ${g.stderr}`);
504 const p = await plyDryRun(dir, url, spec);
505 assert.ok(p.ok, p.ok || `pushRepo ${JSON.stringify(spec)}: ${p.error.code}: ${p.error.message}`);
506 assert.deepEqual(p.updates, g.updates, `git push ${args.join(' ')}`);
507 return g.updates;
508};
509// a work tree with one commit: main, topic and a lightweight tag v1, all loose
510const smallLocal = () => {
511 const dir = join(root, `small${n++}`);
512 git(root, ['init', '-q', '-b', 'main', dir]);
513 writeFileSync(join(dir, 'f'), 'one\n');
514 git(dir, ['add', '-A']);
515 git(dir, [...ID, 'commit', '-q', '-m', 'one']);
516 git(dir, ['branch', 'topic']);
517 git(dir, ['tag', 'v1']);
518 return dir;
519};
521test('pushRepo vs git: a file under refs/ that is not a ref (Finder\'s .DS_Store, a dot directory, a name check-ref-format refuses) is ignored', async () => {
522 const local = smallLocal();
523 const { url } = remoteRepo(false);
524 const refsDir = join(local, '.git', 'refs');
525 writeFileSync(join(refsDir, 'heads', '.DS_Store'), '\x00\x00\x00\x01Bud1');
526 writeFileSync(join(refsDir, '.DS_Store'), '\x00\x00\x00\x01Bud1');
527 mkdirSync(join(refsDir, 'heads', '.hidden'));
528 writeFileSync(join(refsDir, 'heads', '.hidden', 'x'), 'junk\n');
529 writeFileSync(join(refsDir, 'tags', 'bad~name'), 'junk\n');
530 const head = rev(local, 'main');
531 assert.deepEqual(await sameAsGit(local, url, ['main'], { refspecs: ['main'] }), [{ ref: 'refs/heads/main', newOid: head, kind: 'create' }]);
532 assert.deepEqual((await sameAsGit(local, url, ['--all'], { all: true })).map(u => u.ref), ['refs/heads/main', 'refs/heads/topic']);
533 assert.deepEqual((await sameAsGit(local, url, ['--tags'], { tags: true })).map(u => u.ref), ['refs/tags/v1']);
534});
536test('pushRepo vs git: a short source is ambiguous as git counts it (remote.c count_refspec_match): a branch or a tag wins over refs/remotes', async () => {
537 const local = smallLocal();
538 const { url } = remoteRepo(false);
539 // a branch named as a remote is (refs/remotes/foo/HEAD), a tag named as a remote-tracking ref, and a remote alone
540 git(local, ['branch', 'foo']);
541 git(local, ['update-ref', 'refs/remotes/foo/main', 'HEAD']);
542 git(local, ['symbolic-ref', 'refs/remotes/foo/HEAD', 'refs/remotes/foo/main']);
543 git(local, ['tag', 'bar']);
544 git(local, ['update-ref', 'refs/remotes/bar', 'HEAD']);
545 git(local, ['update-ref', 'refs/remotes/baz/main', 'HEAD']);
546 git(local, ['symbolic-ref', 'refs/remotes/baz/HEAD', 'refs/remotes/baz/main']);
547 assert.deepEqual((await sameAsGit(local, url, ['foo'], { refspecs: ['foo'] })).map(u => u.ref), ['refs/heads/foo']);
548 assert.deepEqual((await sameAsGit(local, url, ['bar'], { refspecs: ['bar'] })).map(u => u.ref), ['refs/tags/bar']);
549 assert.deepEqual((await sameAsGit(local, url, ['baz:refs/heads/from-baz'], { refspecs: ['baz:refs/heads/from-baz'] })).map(u => u.ref), ['refs/heads/from-baz']);
550 // two strong matches (a branch and a tag) stay ambiguous on both sides
551 git(local, ['branch', 'dup']);
552 git(local, ['tag', 'dup']);
553 const g = await gitDryRun(local, url, ['dup']);
554 assert.notEqual(g.status, 0);
555 assert.match(g.stderr, /src refspec dup matches more than one/);
556 const p = await plyDryRun(local, url, { refspecs: ['dup'] });
557 assert.equal(p.ok, false);
558 assert.equal(p.error.code, 'bad_request');
559 assert.match(p.error.message, /matches more than one ref \(refs\/tags\/dup, refs\/heads\/dup\)/);
560});
562test('pushRepo vs git: a ref directly under refs/ (refs/stash) is not a match for a short source (remote.c one_local_ref): the branch of that name is the source', async () => {
563 const local = smallLocal();
564 const { url } = remoteRepo(false);
565 // a repository that has run git stash and has a branch named stash: refs/stash is the stash commit, the branch is main's
566 writeFileSync(join(local, 'f'), 'stashed\n');
567 git(local, [...ID, 'stash']);
568 git(local, ['branch', 'stash']);
569 const [stashed, head] = [rev(local, 'refs/stash'), rev(local, 'refs/heads/main')];
570 assert.notEqual(stashed, head);
571 // every expected oid below is git's: sameAsGit compares with git push's own source, and the literal is rev-parse's
572 assert.deepEqual(await sameAsGit(local, url, ['stash'], { refspecs: ['stash'] }), [{ ref: 'refs/heads/stash', newOid: head, kind: 'create' }]);
573 assert.deepEqual(await sameAsGit(local, url, ['stash:refs/heads/s2'], { refspecs: ['stash:refs/heads/s2'] }), [{ ref: 'refs/heads/s2', newOid: head, kind: 'create' }]);
574 // the same for any one-level ref: refs/x (the stash commit) beside a branch x
575 git(local, ['update-ref', 'refs/x', stashed]);
576 git(local, ['branch', 'x']);
577 assert.deepEqual(await sameAsGit(local, url, ['x'], { refspecs: ['x'] }), [{ ref: 'refs/heads/x', newOid: head, kind: 'create' }]);
578 // git still reads a one-level ref as the source when it is named in full, or when nothing git counts matches the name
579 assert.deepEqual(await sameAsGit(local, url, ['refs/x:refs/heads/from-x'], { refspecs: ['refs/x:refs/heads/from-x'] }), [{ ref: 'refs/heads/from-x', newOid: stashed, kind: 'create' }]);
580 git(local, ['update-ref', 'refs/lone', stashed]);
581 assert.deepEqual(await sameAsGit(local, url, ['lone:refs/heads/from-lone'], { refspecs: ['lone:refs/heads/from-lone'] }), [{ ref: 'refs/heads/from-lone', newOid: stashed, kind: 'create' }]);
582});
584test('pushRepo vs git: a loose ref that is a symbolic link is the file it points at; a link that points nowhere is not a ref', async () => {
585 const local = smallLocal();
586 const { url } = remoteRepo(false);
587 const outside = join(root, `outside-ref${n++}`);
588 writeFileSync(outside, `${rev(local, 'main')}\n`);
589 symlinkSync(outside, join(local, '.git', 'refs', 'heads', 'linked'));
590 symlinkSync(join(root, 'nowhere'), join(local, '.git', 'refs', 'heads', 'dangling'));
591 // a link whose target text is a ref name and that resolves on disk nowhere (the legacy symlink form of a symbolic ref)
592 symlinkSync('refs/heads/topic', join(local, '.git', 'refs', 'heads', 'alias'));
593 assert.deepEqual(await sameAsGit(local, url, ['linked'], { refspecs: ['linked'] }), [{ ref: 'refs/heads/linked', newOid: rev(local, 'main'), kind: 'create' }]);
594 const all = await sameAsGit(local, url, ['--all'], { all: true });
595 assert.ok(all.some(u => u.ref === 'refs/heads/linked'), 'git push --all sends the linked branch');
596 // a link whose target text is a ref name and that does resolve on disk (to refs/x/refs/heads/topic, at the second
597 // commit): git reads it as a symbolic ref to refs/heads/topic (the first commit), never as the file it points at
598 writeFileSync(join(local, 'f'), 'two\n');
599 git(local, [...ID, 'commit', '-q', '-a', '-m', 'two']);
600 git(local, ['update-ref', 'refs/x/refs/heads/topic', 'main']);
601 symlinkSync('refs/heads/topic', join(local, '.git', 'refs', 'x', 'alias2'));
602 assert.notEqual(rev(local, 'topic'), rev(local, 'main'));
603 assert.deepEqual(await sameAsGit(local, url, ['refs/x/alias2:refs/heads/via-link'], { refspecs: ['refs/x/alias2:refs/heads/via-link'] }), [{ ref: 'refs/heads/via-link', newOid: rev(local, 'topic'), kind: 'create' }]);
604 // the one stated difference: git walks into a linked directory (the push below succeeds for git); ply refuses it by name
605 symlinkSync(join(local, '.git', 'refs', 'tags'), join(local, '.git', 'refs', 'heads', 'dirlink'));
606 assert.equal((await gitDryRun(local, url, ['main'])).status, 0);
607 const refused = await plyDryRun(local, url, { refspecs: ['main'] });
608 assert.equal(refused.ok, false);
609 assert.deepEqual([refused.error.code, refused.error.what, refused.error.path], ['bad_request', 'ref', 'refs/heads/dirlink']);
610});
Diagnostics4 git calls · 340 ms
CallStatusSizeTime0340 ms
discover200152 B2 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 · 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 · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=335
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
fetch20011 KiB335 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=11245
git.response.cache=MISS
git.response.duration_ms=335
git.fetch.wants=bf8efe3a079c44bbe29876efa0a0c4eab81e4229
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=11245
git.pkt.payload.max=11224
git.sideband.band1.bytes=11223
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
a5e30699493e15db
Request
d6c7d09d…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format