Artifactspublic-v0

ply

main
ply4 git calls231 ms48 KiB2 cachedprotocol v2
591 lines37 KiBRaw
1// runTransfer's result is the class of what happened at the destination (the only commit point). The oracle is git
2// http-backend (fsck on) and git itself reading the bare repository; the faults are injected in a fetch wrapper:
3// - a push whose response is dropped after the server applied it: the refs show it, the run goes on to done
4// - a push that never gets an answer and never lands, with maxSteps: the run is `ambiguous`, never `paused`
5// - the final push applied, its response dropped, and the read after it failing: `ambiguous`, never the read's error
6// - the final push acknowledged, the first settle read replaying the refs from before it (a lagging ls-refs) and
7// the second failing: `done` with `tempLeft: false` on the report alone
8// - two runners on one plan at once, 20 times: both end done, the destination is the planned head, fsck clean
9// - two runners on one plan, gated so the second's push of every step reaches git only after the first's landed, on
10// a destination whose pre-receive hook refuses a push of what is already there: both end done; with the loser's
11// reads failing after its last refusal, the loser is `ambiguous` (never paused, aborted or the refusal)
12// - one runner whose push of every step k is overtaken: before its k-th receive-pack POST is forwarded, step k is
13// landed through a second clean transport. Git itself then refuses the runner's push because the ref moved (a lost
14// compare-and-swap, no hook), or a pre-receive hook installed for that one request declines it: done either way
15// - atomic off, the temp ref's delete refused by an update hook while the create lands: done with tempLeft
16// - a real AbortSignal fired during the final push: after git applied it, `ambiguous`, never `aborted`; before the
17// request was forwarded (fetch rejects with the body unpulled, git never saw it), `aborted` as it is, not a push
18// (README, Resumable transfer; #272); git shows which of the two happened and a resume ends done
19// - a sweep deleting the temp ref just before the final push: atomic off, the branch is published and the run is
20// done; atomic on, git refuses the whole push, nothing is published and the run reports a definite refusal
21// - a receive-pack POST whose fetch rejects before pulling the body (a connect error), at the first, second and
22// final step, with and without another transport landing that step first: the request never reached git, so it is
23// not a push (README, Resumable transfer): the run ends with that `network` error, no settle read, pushes unchanged
24// Each expectation is the fault the wrapper injected and what git shows in the bare repository, not ply's output.
25import { test, beforeAll as before, afterAll as after } from 'vitest';
26import assert from 'node:assert/strict';
27import { mkdirSync, writeFileSync, chmodSync, rmSync } from 'node:fs';
28import { join } from 'node:path';
29import { randomFillSync } from 'node:crypto';
30import { git, gitBuf, tmpRoot, makeBareRepo, startGitServer, fsckProblems, initRepo } from './server.js';
31import { scanPackIndex } from '../src/packscanindex.js';
32import { resolvePackRange } from '../src/packrangeresolve.js';
33import { rangeFromBytes } from '../src/rangesource.js';
34import { createTransport } from '../src/http.js';
35import { planTransfer } from '../src/transferplan.js';
36import { runTransfer, transferStep, sweepTransfers } from '../src/transfer.js';
37import * as entry from '../src/index.js';
38import { Tracer, arrayExporter, flattenLine } from '../src/otel.js';
39import { unwrap } from './unwrap.js';
41const must = r => { if (r.ok) return r.value; throw Object.assign(new Error(r.error.message), { code: r.error.code }); };
42const streamOf = bytes => new ReadableStream({ start(c) { c.enqueue(bytes); c.close(); } });
43const REF = 'refs/heads/main';
44const BUDGET = { maxRequestBytes: 200_000, maxObjects: 300, maxObjectBytes: 1 << 20, maxCompressedObjectBytes: 1 << 20, reserveBytes: 0, reserveObjects: 0 };
45const FAST = { reads: 3, delayMs: 0 };
47let root, cleanup, srv, n = 0, src, from;
48before(async () => {
49 ({ root, cleanup } = tmpRoot());
50 srv = await startGitServer(root);
51 const work = join(root, 'source');
52 mkdirSync(work, { recursive: true });
53 initRepo(work);
54 for (let i = 0; i < 10; i++) {
55 const blob = new Uint8Array(40_000 + i * 500);
56 randomFillSync(blob);
57 writeFileSync(join(work, `r${i}.bin`), blob);
58 }
59 git(work, ['add', '.']);
60 git(work, ['-c', 'user.name=Ann Author', '-c', 'user.email=ann@x', 'commit', '-q', '-m', 'one']);
61 const head = git(work, ['rev-parse', 'HEAD']).trim();
62 const objs = git(work, ['rev-list', '--objects', '--no-walk', head]);
63 const pack = new Uint8Array(gitBuf(work, ['pack-objects', '--stdout', '-q'], objs));
64 src = { commit: new Uint8Array(gitBuf(work, ['cat-file', 'commit', head])) };
65 const p = must(await scanPackIndex(streamOf(pack)));
66 const source = rangeFromBytes(pack);
67 from = { source, pack: p, resolved: must(await resolvePackRange(p, source)) };
68});
69after(async () => { await srv?.close(); cleanup?.(); });
71// A fetch that injects faults by request: `push(i)` for the i-th receive-pack POST (0-based), `read()` for every
72// upload-pack POST (ls-refs). 'drop': the server gets the whole request and answers, the answer is thrown away (the
73// client sees a network failure after the server applied the push); 'lose': the request never reaches the server.
74function faulty({ push = () => 'pass', read = () => 'pass' } = {}) {
75 const seen = { pushes: 0, dropped: 0, lost: 0, readsFailed: 0 };
76 const fetchImpl = async (input, init) => {
77 const url = typeof input === 'string' ? input : input.url;
78 const method = init?.method ?? (typeof input === 'string' ? 'GET' : input.method);
79 if (method === 'POST' && url.endsWith('/git-upload-pack') && read() === 'fail') { seen.readsFailed++; throw new TypeError('injected: read failed'); }
80 if (method !== 'POST' || !url.endsWith('/git-receive-pack')) return fetch(input, init);
81 const what = push(seen.pushes++);
82 if (what === 'lose') {
83 // read the body so the client has sent the whole push, then fail without the server ever seeing it
84 const body = init?.body ?? (typeof input === 'string' ? null : input.body);
85 if (body && typeof body.getReader === 'function') { const r = body.getReader(); for (;;) { const { done } = await r.read(); if (done) break; } }
86 seen.lost++;
87 throw new TypeError('injected: request lost');
88 }
89 const res = await fetch(input, init);
90 if (what !== 'drop') return res;
91 await res.arrayBuffer(); // the server has applied the push and written its whole report
92 seen.dropped++;
93 throw new TypeError('injected: response dropped');
94 };
95 return { fetchImpl, seen };
97function repo(options = {}) {
98 const name = `o${n++}`;
99 const bare = makeBareRepo(root, name);
100 return { bare, url: `${srv.url}/${name}.git`, t: unwrap(createTransport(`${srv.url}/${name}.git`, options)) };
102const plan = (opts = {}) => must(planTransfer(from.pack, from.resolved, { budget: BUDGET, ref: REF, commit: src.commit, ...opts }));
103// The destination as git sees it.
104const refsOf = bare => git(bare, ['for-each-ref', '--format=%(refname) %(objectname)']).trim();
105const lsRemote = bare => git(bare, ['ls-remote', bare]).trim();
106function assertPublished(bare, p, { temp = false } = {}) {
107 assert.equal(refsOf(bare), temp ? `${REF} ${p.head}\n${p.tempRef} ${git(bare, ['rev-parse', p.tempRef]).trim()}` : `${REF} ${p.head}`);
108 assert.match(lsRemote(bare), new RegExp(`^${p.head}\\t${REF}$`, 'm'));
109 assert.deepEqual(fsckProblems(bare, { args: ['--strict'] }), []);
112test('a push applied whose response is dropped: the refs show it, the run goes on to done', async () => {
113 const p = plan();
114 assert.ok(p.steps.length >= 3);
115 for (const nth of [1, p.steps.length - 1]) {
116 const f = faulty({ push: i => (i === nth ? 'drop' : 'pass') });
117 const { bare, t } = repo({ fetchImpl: f.fetchImpl });
118 const run = await runTransfer(t, from, p, src.commit, { settle: FAST, ref: REF });
119 assert.equal(f.seen.dropped, 1);
120 assert.equal(run.ok, true, run.ok ? '' : `${run.error.code}: ${run.error.message}`);
121 // every step was sent exactly once (the dropped one landed: git shows the head), so pushes is the step count
122 assert.deepEqual(run.value, { tag: 'done', head: p.head, pushes: p.steps.length, tempLeft: false });
123 assertPublished(bare, p);
124 }
125});
127test('maxSteps: a push that gets no answer and does not land is ambiguous, never paused; a pause names its last push', async () => {
128 const p = plan();
129 // every push is lost before the server: the client sent it whole and has no report (the protocol's unknown outcome)
130 const f = faulty({ push: () => 'lose' });
131 const { bare, t } = repo({ fetchImpl: f.fetchImpl });
132 for (const maxSteps of [1, 1, 2]) {
133 const before = f.seen.lost;
134 const run = await runTransfer(t, from, p, src.commit, { maxSteps, settle: FAST, ref: REF });
135 assert.equal(f.seen.lost - before, maxSteps, 'every push the run was allowed was sent and lost');
136 assert.equal(run.ok, false, `a run whose only pushes have an unknown outcome reported ${JSON.stringify(run.ok ? run.value : null)}`);
137 assert.equal(run.error.code, 'ambiguous');
138 assert.equal(refsOf(bare), '', 'nothing landed');
139 }
140 // a clean run that stops at maxSteps: paused, and the pause says its last push landed (git: the temp ref moved)
141 const clean = repo();
142 const paused = must(await runTransfer(clean.t, from, p, src.commit, { maxSteps: 1, settle: FAST, ref: REF }));
143 assert.deepEqual(paused, { tag: 'paused', step: 1, pushes: 1, reason: 'max_steps', lastPush: 'acknowledged', lastStep: 'landed' });
144 assert.equal(refsOf(clean.bare), `${p.tempRef} ${p.steps[0].temp}`);
145 // no push at all
146 assert.deepEqual(must(await runTransfer(clean.t, from, p, src.commit, { maxSteps: 0, ref: REF })), { tag: 'paused', step: 1, pushes: 0, reason: 'max_steps', lastPush: 'none', lastStep: 'none' });
147});
149test('the final push applied, its response dropped, the read after it failing: ambiguous, never the read\'s error', async () => {
150 const p = plan();
151 const final = p.steps.length - 1;
152 let failReads = false;
153 const f = faulty({ push: i => { if (i !== final) return 'pass'; failReads = true; return 'drop'; }, read: () => (failReads ? 'fail' : 'pass') });
154 const { bare, t } = repo({ fetchImpl: f.fetchImpl });
155 const run = await runTransfer(t, from, p, src.commit, { settle: FAST, ref: REF });
156 assert.equal(f.seen.dropped, 1);
157 assert.ok(f.seen.readsFailed >= 1);
158 assertPublished(bare, p); // git: the branch is published
159 assert.equal(run.ok, false);
160 assert.equal(run.error.code, 'ambiguous', `the push landed and its outcome was unknown to the client; the run said ${run.error.code}: ${run.error.message}`);
161 assert.equal(run.error.cause?.code, 'ambiguous', 'carries the push\'s own outcome');
162 // a clean resume: done, nothing sent
163 failReads = false;
164 assert.deepEqual(must(await runTransfer(t, from, p, src.commit, { ref: REF })), { tag: 'done', head: p.head, pushes: 0, tempLeft: false });
165});
167test('the final push acknowledged, the first settle read lagging (it does not show the push) and the next failing: done, tempLeft false, on the report alone', async () => {
168 const p = plan();
169 const final = p.steps.length - 1;
170 // every ls-refs answer is kept; after the final push's report, read 0 replays the answer from before the push (a
171 // host's ls-refs lagging a push it acknowledged) and every later read fails
172 let last = null, pushes = 0, after = -1;
173 const seen = { stale: 0, failed: 0 };
174 const fetchImpl = async (input, init) => {
175 const url = typeof input === 'string' ? input : input.url;
176 const method = init?.method ?? (typeof input === 'string' ? 'GET' : input.method);
177 if (method === 'POST' && url.endsWith('/git-receive-pack')) {
178 const res = await fetch(input, init);
179 if (pushes++ === final) { const body = await res.arrayBuffer(); after = 0; return new Response(body, { status: res.status, headers: res.headers }); }
180 return res;
181 }
182 if (method === 'POST' && url.endsWith('/git-upload-pack')) {
183 if (after === 0) { after++; seen.stale++; return new Response(last.body, { status: last.status, headers: last.headers }); }
184 if (after > 0) { seen.failed++; throw new TypeError('injected: read failed'); }
185 const res = await fetch(input, init);
186 last = { body: await res.clone().arrayBuffer(), status: res.status, headers: res.headers };
187 return res;
188 }
189 return fetch(input, init);
190 };
191 const { bare, t } = repo({ fetchImpl });
192 const run = await runTransfer(t, from, p, src.commit, { settle: FAST, ref: REF });
193 assert.deepEqual([seen.stale, seen.failed], [1, 1], 'read 0 lagged (the refs before the final push), read 1 failed, no read after');
194 assertPublished(bare, p); // git: the final push landed, the temp ref is gone
195 assert.deepEqual(must(run), { tag: 'done', head: p.head, pushes: p.steps.length, tempLeft: false });
196});
198test('two runners on one plan at once, 20 times: both end done; the destination is the planned head', async () => {
199 const p = plan();
200 for (let rep = 0; rep < 20; rep++) {
201 const { bare, url } = repo();
202 const runs = await Promise.all([0, 1].map(() => runTransfer(unwrap(createTransport(url)), from, p, src.commit, { ref: REF })));
203 assertPublished(bare, p);
204 for (const r of runs) {
205 assert.equal(r.ok, true, `rep ${rep}: a runner reported ${r.ok ? '' : `${r.error.code}: ${r.error.message}`} though git shows the transfer complete`);
206 assert.equal(r.value.tag, 'done');
207 assert.equal(r.value.head, p.head);
208 }
209 }
210}, 240_000);
212// Two runners, one plan, one destination, one interleaving (no clock): per step k, runner A's push is forwarded only
213// once runner B's push of k is waiting at B's gate (so both chose step k from the same refs), and B's push is forwarded
214// only once A's has its whole answer (so git has applied A's). The destination's pre-receive hook refuses a push whose
215// new value the ref already has, a refusal that is not a compare-and-swap reason ('pre-receive hook declined':
216// push_rejected). So every push B makes is refused by real git while the refs show that step landed.
217function gatedRunners(steps, { failReadsAfterLast = false } = {}) {
218 const defer = () => { let open; const p = new Promise(r => { open = r; }); return { p, open }; };
219 const arrived = Array.from({ length: steps }, defer); // B's push k is at its gate
220 const answered = Array.from({ length: steps }, defer); // A's push k has its whole report
221 const seen = { a: 0, b: 0, reports: [], readsFailed: 0 };
222 const kind = (input, init) => {
223 const url = typeof input === 'string' ? input : input.url;
224 const method = init?.method ?? (typeof input === 'string' ? 'GET' : input.method);
225 if (method !== 'POST') return 'other';
226 if (url.endsWith('/git-receive-pack')) return 'push';
227 return url.endsWith('/git-upload-pack') ? 'read' : 'other';
228 };
229 // the whole report read (the server has applied or refused the push) before anyone is told; the caller gets `res`
230 const whole = async res => new TextDecoder().decode(await res.clone().arrayBuffer());
231 const a = async (input, init) => {
232 if (kind(input, init) !== 'push') return fetch(input, init);
233 const k = seen.a++;
234 await arrived[k].p;
235 const res = await fetch(input, init);
236 await whole(res);
237 answered[k].open();
238 return res;
239 };
240 const b = async (input, init) => {
241 const what = kind(input, init);
242 if (what === 'read' && failReadsAfterLast && seen.reports.length === steps) { seen.readsFailed++; throw new TypeError('injected: read failed'); }
243 if (what !== 'push') return fetch(input, init);
244 const k = seen.b++;
245 arrived[k].open();
246 await answered[k].p;
247 const res = await fetch(input, init);
248 seen.reports.push(await whole(res));
249 return res;
250 };
251 // a runner that ends early must not leave the other waiting at a gate
252 const release = { a: () => answered.forEach(g => g.open()), b: () => arrived.forEach(g => g.open()) };
253 return { a, b, seen, release };
255// pre-receive: refuse a command whose new value is what the ref already is (a delete is never that).
256const REFUSE_LANDED = `#!/bin/sh
257while read old new ref; do
258 [ "$new" = 0000000000000000000000000000000000000000 ] && continue
259 cur=$(git rev-parse -q --verify "$ref" 2>/dev/null)
260 if [ "$cur" = "$new" ]; then echo "already there: $ref" >&2; exit 1; fi
261done
262exit 0
263`;
264async function raceGated(p, opts) {
265 const { bare, url } = repo();
266 mkdirSync(join(bare, 'hooks'), { recursive: true });
267 writeFileSync(join(bare, 'hooks', 'pre-receive'), REFUSE_LANDED);
268 chmodSync(join(bare, 'hooks', 'pre-receive'), 0o755);
269 const g = gatedRunners(p.steps.length, opts);
270 const [runA, runB] = await Promise.all([
271 runTransfer(unwrap(createTransport(url, { fetchImpl: g.a })), from, p, src.commit, { settle: FAST, ref: REF }).finally(g.release.a),
272 runTransfer(unwrap(createTransport(url, { fetchImpl: g.b })), from, p, src.commit, { settle: FAST, ref: REF }).finally(g.release.b),
273 ]);
274 return { bare, g, runA, runB };
276const say = r => (r.ok ? JSON.stringify(r.value) : `${r.error.code}: ${r.error.message}`);
277const DECLINED = /ng refs\/[^\n]*pre-receive hook declined/u;
279test('two gated runners, the loser\'s every push refused by git after the winner\'s landed: both done, refs as planned', async () => {
280 const p = plan();
281 const n = p.steps.length;
282 assert.ok(n >= 3);
283 const { bare, g, runA, runB } = await raceGated(p);
284 // git: the transfer is complete and nothing else is there
285 assertPublished(bare, p);
286 // the protocol: git refused every push the loser made (its own report-status); the winner made one push per step
287 assert.equal(g.seen.a, n);
288 assert.equal(g.seen.reports.length, g.seen.b);
289 for (const report of g.seen.reports) assert.match(report, DECLINED);
290 // the winner pushed every step once; the loser pushed every step once, each refused, each shown landed by the refs
291 assert.deepEqual(runA.ok && runA.value, { tag: 'done', head: p.head, pushes: n, tempLeft: false }, `winner: ${say(runA)}`);
292 assert.equal(runB.ok, true, `git shows every step the loser pushed landed and the transfer complete; the loser said ${say(runB)}`);
293 assert.deepEqual(runB.value, { tag: 'done', head: p.head, pushes: n, tempLeft: false });
294 assert.equal(g.seen.b, n);
295});
297test('two gated runners, the loser\'s reads failing after its last refused push: ambiguous with the refusal as cause, never paused or aborted', async () => {
298 const p = plan();
299 const n = p.steps.length;
300 const { bare, g, runA, runB } = await raceGated(p, { failReadsAfterLast: true });
301 assertPublished(bare, p);
302 assert.deepEqual(runA.ok && runA.value, { tag: 'done', head: p.head, pushes: n, tempLeft: false }, `winner: ${say(runA)}`);
303 assert.equal(g.seen.b, n);
304 assert.match(g.seen.reports[n - 1], DECLINED);
305 assert.ok(g.seen.readsFailed >= 1);
306 // the loser's last push was refused and it could not read the refs: where the destination stands is unknown to it
307 assert.equal(runB.ok, false, `the loser could not read the destination after a refused push; it said ${say(runB)}`);
308 assert.equal(runB.error.code, 'ambiguous', say(runB));
309 assert.equal(runB.error.cause?.code, 'push_rejected', 'carries the push\'s own outcome');
310});
312test('atomic off, the temp ref\'s delete refused by a hook while the create lands: done, tempLeft', async () => {
313 const p = plan({ atomic: false });
314 const { bare, t } = repo();
315 // update hook: refuse deleting anything under refs/ply/ (new value all zeros), allow everything else
316 const hook = join(bare, 'hooks', 'update');
317 mkdirSync(join(bare, 'hooks'), { recursive: true });
318 writeFileSync(hook, '#!/bin/sh\ncase "$1" in refs/ply/*) [ "$3" = 0000000000000000000000000000000000000000 ] && exit 1 ;; esac\nexit 0\n');
319 chmodSync(hook, 0o755);
320 const run = await runTransfer(t, from, p, src.commit, { settle: FAST, ref: REF });
321 assertPublished(bare, p, { temp: true }); // git: the branch at the planned head, the temp ref still there
322 assert.equal(run.ok, true, run.ok ? '' : `the branch is published; the run said ${run.error.code}: ${run.error.message}`);
323 assert.deepEqual(run.value, { tag: 'done', head: p.head, pushes: p.steps.length, tempLeft: true });
324});
326// One runner, overtaken at every step (no clock, no second runner to schedule): before the runner's k-th receive-pack
327// POST is forwarded, `before(k)` runs (it lands step k through a second, clean transport), then the request goes to
328// git and its whole report is read before the runner sees it. The runner's k-th push is step k: it reads the refs
329// after every push, and they show the step the clean transport landed.
330const isPush = (input, init) => {
331 const url = typeof input === 'string' ? input : input.url;
332 const method = init?.method ?? (typeof input === 'string' ? 'GET' : input.method);
333 return method === 'POST' && url.endsWith('/git-receive-pack');
334};
335function overtaken(before, around = { enter() {}, leave() {} }) {
336 const seen = { pushes: 0, reports: [] };
337 const fetchImpl = async (input, init) => {
338 if (!isPush(input, init)) return fetch(input, init);
339 await before(seen.pushes++);
340 around.enter();
341 try {
342 const res = await fetch(input, init);
343 // the hook (if any) stays until git has written its whole report: git runs it after the pack is in
344 seen.reports.push(new TextDecoder().decode(await res.clone().arrayBuffer()));
345 return res;
346 } finally { around.leave(); }
347 };
348 return { fetchImpl, seen };
350const NG = /ng refs\/\S+ /u;
352test('a runner whose every push is overtaken by another transport landing that step: git refuses each push (the ref moved, or a hook declines), the refs show the step, the run is done', async () => {
353 const p = plan();
354 const n = p.steps.length;
355 assert.ok(n >= 3, 'batch steps and a final step');
356 for (const how of ['cas', 'hook']) {
357 const { bare, url } = repo();
358 const clean = unwrap(createTransport(url));
359 const hook = join(bare, 'hooks', 'pre-receive');
360 const around = how === 'hook'
361 ? { enter() { mkdirSync(join(bare, 'hooks'), { recursive: true }); writeFileSync(hook, '#!/bin/sh\nexit 1\n'); chmodSync(hook, 0o755); }, leave() { rmSync(hook, { force: true }); } }
362 : undefined;
363 const landed = [];
364 const o = overtaken(async k => {
365 // the other writer: step k through a clean transport, and git's own view of the ref it moved
366 landed.push(must(await transferStep(clean, from, p, k, { commit: src.commit, ref: REF })).step);
367 assert.equal(refsOf(bare), k < n - 1 ? `${p.tempRef} ${p.steps[k].temp}` : `${REF} ${p.head}`);
368 }, around);
369 const run = await runTransfer(unwrap(createTransport(url, { fetchImpl: o.fetchImpl })), from, p, src.commit, { settle: FAST, ref: REF });
370 assert.equal(run.ok, true, `${how}: git refused the runner's push of a step the other transport had landed (the refs show it); the runner said ${say(run)}`);
371 // the other transport landed every step, in order, before the runner's push of it reached git
372 assert.deepEqual(landed, Array.from({ length: n }, (_, k) => k), how);
373 // the protocol: git refused every push the runner made, in its own report-status
374 assert.equal(o.seen.reports.length, n, how);
375 for (const report of o.seen.reports) {
376 assert.match(report, NG, how);
377 // 'cas': no hook exists, the refusal is git's own (the ref is not at the old value the command names)
378 if (how === 'hook') assert.match(report, DECLINED); else assert.doesNotMatch(report, /hook declined/u);
379 }
380 assertPublished(bare, p);
381 assert.deepEqual(run.value, { tag: 'done', head: p.head, pushes: n, tempLeft: false }, how);
382 }
383});
385test('a pause after this run\'s push was refused and another runner landed the step says both: lastPush refused, lastStep landed', async () => {
386 const p = plan();
387 // refused: step 0 is landed through a clean transport before the runner's push of it reaches git (no hook: git's own
388 // compare-and-swap refusal). The two facts are the test's own: the report git sent the runner, and git's refs.
389 const refused = repo();
390 const clean = unwrap(createTransport(refused.url));
391 const o = overtaken(async k => { must(await transferStep(clean, from, p, k, { commit: src.commit, ref: REF })); });
392 const paused = must(await runTransfer(unwrap(createTransport(refused.url, { fetchImpl: o.fetchImpl })), from, p, src.commit, { maxSteps: 1, settle: FAST, ref: REF }));
393 assert.equal(o.seen.reports.length, 1);
394 assert.match(o.seen.reports[0], NG, 'git refused this run\'s push');
395 assert.equal(refsOf(refused.bare), `${p.tempRef} ${p.steps[0].temp}`, 'git shows step 0 landed');
396 assert.deepEqual(paused, { tag: 'paused', step: 1, pushes: 1, reason: 'max_steps', lastPush: 'refused', lastStep: 'landed' });
397 // unanswered: the push applied, its report dropped before the runner saw it; git shows the step landed
398 const f = faulty({ push: () => 'drop' });
399 const dropped = repo({ fetchImpl: f.fetchImpl });
400 const unanswered = must(await runTransfer(dropped.t, from, p, src.commit, { maxSteps: 1, settle: FAST, ref: REF }));
401 assert.equal(f.seen.dropped, 1);
402 assert.equal(refsOf(dropped.bare), `${p.tempRef} ${p.steps[0].temp}`);
403 assert.deepEqual(unanswered, { tag: 'paused', step: 1, pushes: 1, reason: 'max_steps', lastPush: 'unanswered', lastStep: 'landed' });
404 // the package entry exports the runner. This is a value check only: the LastPush and LastStep type exports and the
405 // paused shape are pinned by test/types/entry.typetest.ts (compiled by tsconfig.check.json in `npm run typecheck`)
406 assert.equal(entry.runTransfer, runTransfer);
407});
409test('a real AbortSignal fired during the final push: ambiguous once sent, aborted when the body was never pulled; git decides what landed', async () => {
410 const p = plan();
411 const n = p.steps.length;
412 // 'applied': the signal fires once git has written its whole report for the final push (it landed);
413 // 'unsent': it fires before the final push is forwarded, so fetch rejects without pulling the body (git never saw it):
414 // README, Resumable transfer: a step that fails before any push request is made, `aborted` included, is not a push
415 // and ends with that error as it is (#272).
416 for (const when of ['applied', 'unsent']) {
417 const controller = new AbortController();
418 const seen = { pushes: 0, fired: 0 };
419 const fetchImpl = async (input, init) => {
420 if (!isPush(input, init) || seen.pushes++ !== n - 1) return fetch(input, init);
421 if (when === 'unsent') { seen.fired++; controller.abort(); return fetch(input, init); }
422 const res = await fetch(input, init);
423 await res.clone().arrayBuffer();
424 seen.fired++;
425 controller.abort();
426 return res;
427 };
428 const { bare, url } = repo();
429 const run = await runTransfer(unwrap(createTransport(url, { fetchImpl, signal: controller.signal })), from, p, src.commit, { settle: FAST, ref: REF });
430 assert.equal(seen.fired, 1, when);
431 assert.equal(controller.signal.aborted, true);
432 // git decides what landed: the branch published, or the temp ref still at the last batch step
433 if (when === 'applied') assertPublished(bare, p);
434 else assert.equal(refsOf(bare), `${p.tempRef} ${p.steps[n - 2].temp}`);
435 assert.equal(run.ok, false, `${when}: the final push's outcome is not known to the runner; it said ${say(run)}`);
436 assert.equal(run.error.code, when === 'applied' ? 'ambiguous' : 'aborted', `${when}: ${say(run)}`);
437 // a resume without the signal: nothing to send when it had landed, the one final push when it had not
438 const resumed = must(await runTransfer(unwrap(createTransport(url)), from, p, src.commit, { settle: FAST, ref: REF }));
439 assert.deepEqual(resumed, { tag: 'done', head: p.head, pushes: when === 'applied' ? 0 : 1, tempLeft: false }, when);
440 assertPublished(bare, p);
441 }
442});
444test('a sweep deleting the temp ref just before the final push: atomic off, published and done; atomic on, nothing published and a definite refusal', async () => {
445 for (const atomic of [false, true]) {
446 const p = plan({ atomic });
447 const n = p.steps.length;
448 const { bare, url } = repo();
449 const clean = unwrap(createTransport(url));
450 const swept = [];
451 const o = overtaken(async k => {
452 if (k !== n - 1) return;
453 assert.equal(refsOf(bare), `${p.tempRef} ${p.steps[n - 2].temp}`);
454 swept.push(...must(await sweepTransfers(clean)));
455 assert.equal(refsOf(bare), '', 'git: the sweep deleted the temp ref');
456 });
457 const run = await runTransfer(unwrap(createTransport(url, { fetchImpl: o.fetchImpl })), from, p, src.commit, { settle: FAST, ref: REF });
458 assert.deepEqual(swept, [p.tempRef]);
459 assert.equal(o.seen.reports.length, n);
460 assert.match(o.seen.reports[n - 1], NG, `atomic ${atomic}: git refused the delete of a ref that is gone`);
461 if (atomic) {
462 // one transaction: the create is refused with the delete, so git shows nothing and the run must not say done
463 assert.equal(refsOf(bare), '', 'git: an atomic push refused publishes nothing');
464 assert.equal(run.ok, false, `nothing is published; the run said ${say(run)}`);
465 assert.notEqual(run.error.code, 'ambiguous', 'git answered the push: a definite refusal');
466 } else {
467 // two updates: the create lands, the delete is refused; git shows the branch and no temp ref
468 assertPublished(bare, p);
469 assert.equal(run.ok, true, `the branch is published; the run said ${say(run)}`);
470 assert.deepEqual(run.value, { tag: 'done', head: p.head, pushes: n, tempLeft: false });
471 assert.deepEqual(must(await runTransfer(clean, from, p, src.commit, { ref: REF })), { tag: 'done', head: p.head, pushes: 0, tempLeft: false });
472 }
473 }
474});
476// A failure before any push request (REQ A): nothing was sent to receive-pack, so nothing can have landed and ply knows
477// it. It is reported as what it is, with no settle reads. Oracle: this wrapper's own counts of the runner's requests
478// (receive-pack POSTs, ls-refs reads, receive-pack advertisement GETs) and git's refs. `read(nth)` and `advert()` inject
479// the faults; `landed(nth)` runs once the nth read's whole answer is in.
480function counted({ read = () => 'pass', advert = () => 'pass', landed = async () => {} } = {}) {
481 const seen = { posts: 0, reads: 0, adverts: 0, postsAfterAdvert: 0, readsAfterAdvert: 0 };
482 let down = false;
483 const fetchImpl = async (input, init) => {
484 const url = typeof input === 'string' ? input : input.url;
485 const method = init?.method ?? (typeof input === 'string' ? 'GET' : input.method);
486 if (isPush(input, init)) { seen.posts++; if (seen.adverts > 0) seen.postsAfterAdvert++; }
487 if (method === 'GET' && url.includes('service=git-receive-pack')) { seen.adverts++; if (advert() === 'down') down = true; }
488 if (method === 'POST' && url.endsWith('/git-upload-pack')) {
489 const nth = seen.reads++;
490 if (seen.adverts > 0) seen.readsAfterAdvert++;
491 if (down || read(nth) === 'fail') throw new TypeError('injected: read failed');
492 const res = await fetch(input, init);
493 await res.clone().arrayBuffer();
494 await landed(nth);
495 return res;
496 }
497 if (down) throw new TypeError('injected: host down');
498 return fetch(input, init);
499 };
500 return { fetchImpl, seen };
502const codeOf = r => (r.ok ? `ok ${JSON.stringify(r.value)}` : r.error.code);
504test('a push refused here before any push request (the transport\'s maxPackBytes under the batch): limit_exceeded as it is, one read, no push request; never ambiguous, never a refused push', async () => {
505 const p = plan();
506 // every batch carries 40 KB blobs of random bytes, so 10 KB is under each: pushPack's plan refuses step 0 locally
507 const limits = { maxPackBytes: 10_000 };
508 // the refs readable
509 const a = counted();
510 const fine = repo({ fetchImpl: a.fetchImpl, limits });
511 const r1 = await runTransfer(fine.t, from, p, src.commit, { settle: FAST, ref: REF });
512 assert.equal(codeOf(r1), 'limit_exceeded', say(r1));
513 assert.deepEqual([a.seen.posts, a.seen.reads], [0, 1], 'no push request; the one status read before the step and no settle read');
514 assert.equal(refsOf(fine.bare), '');
515 // every read after the first failing: still the refusal, and no read was made to fail
516 const b = counted({ read: nth => (nth >= 1 ? 'fail' : 'pass') });
517 const unreadable = repo({ fetchImpl: b.fetchImpl, limits });
518 const r2 = await runTransfer(unreadable.t, from, p, src.commit, { settle: FAST, ref: REF });
519 assert.equal(codeOf(r2), 'limit_exceeded', say(r2));
520 assert.deepEqual([b.seen.posts, b.seen.reads], [0, 1]);
521 assert.equal(refsOf(unreadable.bare), '');
522 // another runner (a clean transport) lands step 0 right after this run's first read; maxSteps 1. This run made no
523 // push: it is not a pause counting one, and not `lastPush: 'refused'` (no server refused anything)
524 const other = { land: async () => {} };
525 const c = counted({ landed: async nth => { if (nth === 0) await other.land(); } });
526 const raced = repo({ fetchImpl: c.fetchImpl, limits });
527 const clean = unwrap(createTransport(raced.url, {}));
528 other.land = async () => { must(await transferStep(clean, from, p, 0, { commit: src.commit, ref: REF })); };
529 const r3 = await runTransfer(raced.t, from, p, src.commit, { maxSteps: 1, settle: FAST, ref: REF });
530 assert.equal(refsOf(raced.bare), `${p.tempRef} ${p.steps[0].temp}`, 'git shows the other runner landed step 0');
531 assert.equal(codeOf(r3), 'limit_exceeded', say(r3));
532 assert.deepEqual([c.seen.posts, c.seen.reads], [0, 1]);
533});
535test('the final push\'s advertisement GET failing (a real AbortSignal; the host down): aborted or network as it is, no push request for that step, no settle read', async () => {
536 const p = plan();
537 const n = p.steps.length;
538 assert.equal(p.atomic, true);
539 assert.ok(n >= 2 && p.steps[n - 2].kind === 'batch', 'the final step follows a batch: only it reads the advertisement');
540 for (const [fault, code] of [['abort', 'aborted'], ['down', 'network']]) {
541 const controller = new AbortController();
542 const c = counted({ advert: () => { if (fault === 'abort') controller.abort(); return fault === 'down' ? 'down' : 'pass'; } });
543 const { bare, t } = repo({ fetchImpl: c.fetchImpl, signal: controller.signal });
544 const r = await runTransfer(t, from, p, src.commit, { settle: FAST, ref: REF });
545 assert.equal(codeOf(r), code, `${fault}: ${say(r)}`);
546 assert.ok(c.seen.adverts >= 1, 'the fault was at the advertisement GET');
547 assert.equal(c.seen.posts, n - 1, `${fault}: the batch steps' pushes only`);
548 assert.deepEqual([c.seen.postsAfterAdvert, c.seen.readsAfterAdvert], [0, 0], `${fault}: no push request and no read after the failed GET`);
549 assert.equal(refsOf(bare), `${p.tempRef} ${p.steps[n - 2].temp}`, `${fault}: git shows the last batch and no published ref`);
550 }
551});
553// #272. The fetch rejects without reading the request body: the command list never left the process, so git never saw
554// a request. README (Resumable transfer): "A step that fails before any push request is made (… `aborted` or `network`
555// included …) is not a push: the run ends with that error as it is, with no settle reads, it is not counted in `pushes`,
556// and it is never `ambiguous` or a `lastPush`." So the expected values are: code 'network', exactly k settle spans
557// (one per earlier push, k pushes before step k), the run span's pushes k, and git's refs as k clean pushes left them
558// (or k + 1 steps when the other transport landed step k: that push was its, not ours).
559test('a push whose fetch rejects before pulling the body is not a push: the network error as it is, no settle, pushes unchanged (#272)', async () => {
560 const p = plan();
561 const final = p.steps.length - 1;
562 assert.ok(final >= 2);
563 for (const k of [0, 1, final]) {
564 for (const landedFirst of [false, true]) {
565 const { bare, url } = repo();
566 const clean = unwrap(createTransport(url));
567 let posts = 0, rejected = 0;
568 const fetchImpl = async (input, init) => {
569 if (!isPush(input, init) || posts++ !== k) return fetch(input, init);
570 if (landedFirst) must(await transferStep(clean, from, p, k, { commit: src.commit, ref: REF }));
571 rejected++;
572 throw new TypeError('injected: connect failed'); // the body is never read
573 };
574 const ex = arrayExporter();
575 const t = unwrap(createTransport(url, { fetchImpl, tracer: new Tracer({ exporter: ex }) }));
576 const run = await runTransfer(t, from, p, src.commit, { settle: FAST, ref: REF });
577 const at = `step ${k}, landed first ${landedFirst}`;
578 assert.equal(rejected, 1, at);
579 assert.equal(run.ok, false, `${at}: the run reported ${JSON.stringify(run.ok ? run.value : null)}`);
580 assert.equal(run.error.code, 'network', `${at}: ${run.error.code}: ${run.error.message}`);
581 const spans = ex.lines.flatMap(flattenLine);
582 assert.equal(spans.filter(s => s.name === 'git.transfer.settle').length, k, `${at}: settle reads only after the k pushes made`);
583 const [root] = spans.filter(s => s.name === 'git.transfer.run');
584 assert.equal(root.attrs['git.transfer.pushes'] ?? 0, k, `${at}: pushes counts only requests git received`);
585 const landed = landedFirst ? k + 1 : k;
586 const tempAt = landed === 0 ? '' : landed > final ? null : `${p.tempRef} ${p.steps[landed - 1].temp}`;
587 if (tempAt !== null) assert.equal(refsOf(bare), tempAt, `${at}: git's refs`);
588 else assertPublished(bare, p);
589 }
590 }
591});
Diagnostics4 git calls · 231 ms
CallStatusSizeTime0231 ms
discover200152 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
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 · hit3 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=HIT
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 · 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=55
Open pack map
fetch2009.9 KiB226 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=226
git.response.bytes=10098
git.response.cache=MISS
git.response.duration_ms=226
git.fetch.wants=432f5a54bf24204b3866eed4026d3500193fdae9
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=10098
git.pkt.payload.max=10077
git.sideband.band1.bytes=10076
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
a5e1e182592dd96e
Request
87e8056c…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format