GitHubivarvong
ply
main
1// commitChanges with a 40-hex base on a target that exists at that commit, while another writer DELETES the target
2// between two of the call's requests. Both paths: the single streamed edit and the in-memory change list.
3//
4// Expected values are hand-derived from the contract (README, "One commit from a change list"), not from the code:
5// - no `head`: the call is last-writer-wins. Whatever request the delete lands before, the call ends with its
6// commit on the ref. The ref was absent when that commit was pushed, so the call created it: created true,
7// oldOid null, and the commit's parent is the base commit (a 40-hex base is that commit). A delete that lands
8// after the call's first read of the target (N >= 2) costs one redo, reported as git.commit.attempt 1.
9// - `head` pinned to the base commit: the caller asked for exactly that tip. The ref is not there, so every
10// window is cas_conflict (expected the pin, actual null: deleted) and the ref stays deleted.
11// - the two paths answer the same race the same way (one expectation, both lists).
12// Judge of the repository: git in the bare repo (rev-parse, cat-file).
13// The other writer is injected by a fetchImpl wrapper that runs `git update-ref -d` just before request N, so every
14// window between two requests is covered by a counter, not by timing.
15import { test, beforeAll as before, afterAll as after } from 'vitest';
16import assert from 'node:assert/strict';
17import { join } from 'node:path';
18import { cpSync, writeFileSync } from 'node:fs';
19import { git, tmpRoot, makeBareRepo, startGitServer, initRepo } from './server.js';
20import { commitChanges } from '../src/changes.js';
21import { Tracer, arrayExporter, flattenLine } from '../src/otel.js';
23const AUTHOR = { name: 'Ada', email: 'ada@x', tz: '+0000', time: 1700000000 };
24const REF = 'refs/heads/feature';
25const EDIT = [{ kind: 'edit', path: 'plain.txt', find: 'plain', replace: 'edited' }];
26const LISTS = [['single edit (streamed)', EDIT], ['two changes (in memory)', [...EDIT, { kind: 'write', path: 'extra.txt', content: 'e\n' }]]];
28let root, cleanup, srv, tpl, baseCommit, n = 0;
29before(async () => {
30 ({ root, cleanup } = tmpRoot());
31 srv = await startGitServer(root);
32 const seed = join(root, 'seed');
33 initRepo(seed);
34 for (const [k, v] of [['user.name', 's'], ['user.email', 's@x'], ['commit.gpgsign', 'false']]) git(seed, ['config', k, v]);
35 writeFileSync(join(seed, 'plain.txt'), 'plain\n');
36 git(seed, ['add', '-A']); git(seed, ['commit', '-q', '-m', 'seed']);
37 git(seed, ['branch', 'feature', 'main']);
38 tpl = makeBareRepo(root, 'tpl'); git(seed, ['push', '-q', tpl, 'main', 'feature']);
39 baseCommit = git(seed, ['rev-parse', 'main']).trim();
40});
41after(async () => { await srv.close(); cleanup(); });
43const tipOf = bare => { try { return git(bare, ['rev-parse', '--verify', '-q', REF]).trim(); } catch { return null; } };
45// One run: the target exists at the base commit; the other writer deletes it just before request N.
46async function raceAt(list, pin, N) {
47 const name = `del${n++}`; const bare = join(root, `${name}.git`);
48 cpSync(tpl, bare, { recursive: true });
49 assert.equal(tipOf(bare), baseCommit);
50 let seen = 0, fired = false;
51 const fetchImpl = async (...args) => {
52 seen++;
53 if (seen === N) { git(bare, ['update-ref', '-d', REF]); fired = true; }
54 return fetch(...args);
55 };
56 const exporter = arrayExporter();
57 const r = await commitChanges(`${srv.url}/${name}.git`, { ref: REF, base: baseCommit, author: AUTHOR, message: 'm', changes: list }, { tracer: new Tracer({ exporter }), fetchImpl, ...(pin ? { head: baseCommit } : {}) });
58 const span = exporter.lines.flatMap(flattenLine).find(s => s.name === 'git.commit');
59 return { r, fired, bare, span };
60}
62for (const [what, list] of LISTS) {
63 test(`no head: a target deleted mid-call is committed again on the base, created: ${what}`, async () => {
64 const windows = [];
65 for (let N = 1; N <= 16; N++) {
66 // oxlint-disable-next-line no-await-in-loop -- one run per window, in order
67 const { r, fired, bare, span } = await raceAt(list, false, N);
68 if (!fired) break; // the call finished in fewer than N requests: every window is covered
69 assert.equal(r.ok ? 'ok' : `${r.error.code}: ${r.error.message}`, 'ok', `window ${N} (ref deleted just before request ${N})`);
70 assert.equal(r.value.outcome, 'commit', `window ${N}: outcome`);
71 assert.equal(r.value.created, true, `window ${N}: created`);
72 assert.equal(r.value.oldOid, null, `window ${N}: oldOid`);
73 assert.equal(r.value.parent, baseCommit, `window ${N}: parent`);
74 const tip = tipOf(bare);
75 assert.equal(tip, r.value.oid, `window ${N}: the ref is at the reported commit`);
76 assert.equal(git(bare, ['rev-parse', `${tip}^`]).trim(), baseCommit, `window ${N}: git's parent of the tip`);
77 assert.equal(git(bare, ['cat-file', '-p', `${tip}:plain.txt`]), 'edited\n', `window ${N}: the edit is in the commit`);
78 // the first request is the call's read of the target: a delete before it is simply an absent ref (no redo)
79 assert.equal(span.attrs['git.commit.attempt'], N === 1 ? 0 : 1, `window ${N}: the redo is reported`);
80 windows.push(N);
81 }
82 // the first read and at least one later window were exercised (a call is never one request)
83 assert.ok(windows.length >= 2, `windows covered: ${windows.join(' ')}`);
84 });
86 test(`head pinned to the base: a target deleted mid-call is cas_conflict and stays deleted: ${what}`, async () => {
87 const windows = [];
88 for (let N = 1; N <= 16; N++) {
89 // oxlint-disable-next-line no-await-in-loop -- one run per window, in order
90 const { r, fired, bare } = await raceAt(list, true, N);
91 if (!fired) break;
92 const got = r.ok ? `ok(created=${r.value.created},oldOid=${r.value.oldOid})` : r.error.code;
93 assert.equal(got, 'cas_conflict', `window ${N} (ref deleted just before request ${N})`);
94 assert.equal(r.error.expected, baseCommit, `window ${N}: expected is the pin`);
95 assert.equal(r.error.actual, null, `window ${N}: actual is null (deleted)`);
96 assert.equal(tipOf(bare), null, `window ${N}: the ref stays deleted`);
97 windows.push(N);
98 }
99 assert.ok(windows.length >= 2, `windows covered: ${windows.join(' ')}`);
100 });
101}
Diagnostics6 git calls · 80 ms
CallStatusSizeTime080 ms
discover200191 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=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 KiB3 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=11990 git.response.cache=UPDATING git.response.duration_ms=3 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 · hit3 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=3 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=3 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=2 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
fetch2002.4 KiB69 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=69 git.response.bytes=2474 git.response.cache=MISS git.response.duration_ms=69 git.fetch.wants=152e4ab7e83581e97e2361912a54ad5d4638d2ea 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=2474 git.pkt.payload.max=2447 git.sideband.band1.bytes=2447 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
- a5e147292e35a140
- Request
- 797a8b24…
- 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