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}
Diagnostics4 git calls · 378 ms
CallStatusSizeTime0378 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 B3 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=145 git.response.cache=UPDATING git.response.duration_ms=3 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 · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=38434 git.response.cache=HIT git.response.duration_ms=4 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.3 KiB371 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=371 git.response.bytes=2377 git.response.cache=MISS git.response.duration_ms=371 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=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=2377 git.pkt.payload.max=2356 git.sideband.band1.bytes=2355 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
- a5e285b11f804af4
- Request
- 16104f69…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format