GitHubivarvong
ply
main
1// commitChanges creating a ref from a base, while another writer creates that same ref AT THE BASE COMMIT between two
2// of the call's requests. The compare-and-swap is the one the call resolved when it read the target (its first
3// request): "absent" pushes with the zero old oid, on the streamed single-edit path as on the in-memory path.
4//
5// Expected values are hand-derived from the contract (README, "One commit from a change list"), not from the code:
6// - the call's first request reads the target ref. A ref the other writer made BEFORE it (N = 1) is seen:
7// a ref base, or head pinned to the zero id, is cas_conflict; a 40-hex base equal to that tip with no pin is an
8// ordinary commit on the existing ref (created false, oldOid the tip).
9// - a ref made AFTER it (N >= 2) is not what the call read. The call must not report it created the ref:
10// cas_conflict, expected the zero id, actual the oid that won.
11// Judge of the repository: git rev-parse in the bare repo (the ref is where the other writer put it).
12// The other writer is injected by a fetchImpl wrapper that runs `git update-ref` just before request N, so every
13// window between two requests is covered by a counter, not by timing.
14import { test, beforeAll as before, afterAll as after } from 'vitest';
15import assert from 'node:assert/strict';
16import { join } from 'node:path';
17import { cpSync, writeFileSync } from 'node:fs';
18import { git, tmpRoot, makeBareRepo, startGitServer, initRepo } from './server.js';
19import { commitChanges } from '../src/changes.js';
20import { Tracer } from '../src/otel.js';
22const AUTHOR = { name: 'Ada', email: 'ada@x', tz: '+0000', time: 1700000000 };
23const ZERO = '0'.repeat(40);
24const EDIT = [{ kind: 'edit', path: 'plain.txt', find: 'plain', replace: 'edited' }];
25const LISTS = [['single edit (streamed)', EDIT], ['two changes (in memory)', [...EDIT, { kind: 'write', path: 'extra.txt', content: 'e\n' }]]];
27let root, cleanup, srv, tpl, baseCommit, n = 0;
28before(async () => {
29 ({ root, cleanup } = tmpRoot());
30 srv = await startGitServer(root);
31 const seed = join(root, 'seed');
32 initRepo(seed);
33 for (const [k, v] of [['user.name', 's'], ['user.email', 's@x'], ['commit.gpgsign', 'false']]) git(seed, ['config', k, v]);
34 writeFileSync(join(seed, 'plain.txt'), 'plain\n');
35 git(seed, ['add', '-A']); git(seed, ['commit', '-q', '-m', 'seed']);
36 git(seed, ['tag', '-a', '-m', 'v1', 'v1', 'main']); git(seed, ['tag', 'lw', 'main']);
37 tpl = makeBareRepo(root, 'tpl'); git(seed, ['push', '-q', tpl, 'main', 'v1', 'lw']);
38 baseCommit = git(seed, ['rev-parse', 'main']).trim();
39 // the annotated tag is a tag object, not the commit: the base the call must peel
40 assert.notEqual(git(seed, ['rev-parse', 'v1']).trim(), baseCommit);
41 assert.equal(git(seed, ['rev-parse', 'v1^{commit}']).trim(), baseCommit);
42});
43after(async () => { await srv.close(); cleanup(); });
45// One run: the other writer creates refs/heads/feature at the base commit just before request N.
46async function raceAt(base, list, pin, N) {
47 const name = `race${n++}`; const bare = join(root, `${name}.git`);
48 cpSync(tpl, bare, { recursive: true });
49 let seen = 0, fired = false;
50 const fetchImpl = async (...args) => {
51 seen++;
52 if (seen === N) { git(bare, ['update-ref', 'refs/heads/feature', baseCommit]); fired = true; }
53 return fetch(...args);
54 };
55 const r = await commitChanges(`${srv.url}/${name}.git`, { ref: 'refs/heads/feature', base, author: AUTHOR, message: 'm', changes: list }, { tracer: new Tracer(), fetchImpl, ...(pin ? { head: ZERO } : {}) });
56 return { r, fired, bare, requests: seen };
57}
59for (const baseKind of ['refs/heads/main', 'refs/tags/lw', 'refs/tags/v1', '40-hex commit']) {
60 for (const [what, list] of LISTS) {
61 for (const pin of [false, true]) {
62 test(`a ref created by another writer between requests is cas_conflict: base ${baseKind}, ${what}${pin ? ', head pinned to absent' : ''}`, async () => {
63 const base = baseKind === '40-hex commit' ? baseCommit : baseKind;
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 } = await raceAt(base, list, pin, N);
68 if (!fired) break; // the call finished in fewer than N requests: every window is covered
69 const tip = git(bare, ['rev-parse', 'refs/heads/feature']).trim();
70 const seenBeforeFirstRead = N === 1 && baseKind === '40-hex commit' && !pin;
71 if (seenBeforeFirstRead) {
72 // the ref existed at the base commit before the call read it: an ordinary commit on an existing ref
73 assert.equal(r.ok ? 'ok' : `${r.error.code}: ${r.error.message}`, 'ok', `window ${N}`);
74 assert.equal(r.value.created, false, `window ${N}: created`);
75 assert.equal(r.value.oldOid, baseCommit, `window ${N}: oldOid`);
76 assert.equal(git(bare, ['rev-parse', `${tip}^`]).trim(), baseCommit, `window ${N}: parent`);
77 windows.push(`${N}:ok`);
78 continue;
79 }
80 const got = r.ok ? `ok(created=${r.value.created},oldOid=${r.value.oldOid})` : r.error.code;
81 assert.equal(got, 'cas_conflict', `window ${N} (ref created just before request ${N})`);
82 assert.equal(r.error.expected, ZERO, `window ${N}: expected`);
83 assert.equal(r.error.actual, baseCommit, `window ${N}: actual is the oid that won`);
84 assert.equal(tip, baseCommit, `window ${N}: the ref is where the other writer put it`);
85 windows.push(`${N}:cas_conflict`);
86 }
87 // the first read and at least one later window were exercised (a call is never one request)
88 assert.ok(windows.length >= 2, `windows covered: ${windows.join(' ')}`);
89 });
90 }
91 }
92}
Diagnostics6 git calls · 94 ms
CallStatusSizeTime094 ms
discover200191 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=1 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 KiB2 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=11990 git.response.cache=UPDATING git.response.duration_ms=2 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 · hit1 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=1 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 · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=84 git.response.bytes=5724 git.response.cache=HIT git.response.duration_ms=3 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.3 KiB84 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2378 git.response.cache=MISS git.response.duration_ms=84 git.fetch.wants=320a5001fb723753a367505ba7d50a7b583c2067 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=2378 git.pkt.payload.max=2351 git.sideband.band1.bytes=2351 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
- a5e0fc3b7cea332c
- Request
- bf7a3bf4…
- 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