Artifactspublic-v0

ply

main
ply4 git calls218 ms42 KiB2 cachedprotocol v2
278 lines12 KiBRaw
1// The version:refname space (version-refname.space.js, #124).
2//
3// Pair cells: two-name `git tag -l --sort=version:refname` is versioncmp, suffixes included. That is the contract,
4// because a suffix list with the release between prereleases makes versioncmp cyclic and git's qsort of a longer
5// list then parts from versioncmp.
6// Rank cells: the same command over the whole 11,880-name list. Where the order is total, the listing equals a sort
7// by compareVersionRefnames. Where it cycles, every inverted edge is checked again as two names.
8import { test } from 'vitest';
9import assert from 'node:assert/strict';
10import { spawn, spawnSync } from 'node:child_process';
11import { mkdtempSync, rmSync } from 'node:fs';
12import { tmpdir } from 'node:os';
13import { join } from 'node:path';
14import {
15 names, pairNames, unicodeNames, pairsOf, SUFFIX_CONFIGS, CYCLE, CYCLIC_SUFFIX,
16 NAME_COUNT, PAIR_NAME_COUNT, UNICODE_NAME_COUNT, PAIR_CELL_COUNT, RANK_CELL_COUNT, CELL_COUNT,
17 PREFIXES, LEFTS, MIDS, ENDS, PAIR_LEFTS, PAIR_MIDS, PAIR_ENDS,
18 UNICODE_PREFIXES, UNICODE_LEFTS, UNICODE_MIDS, UNICODE_ENDS,
19} from './version-refname.space.js';
20import { compareVersionRefnames } from '../../src/refs.js';
22// The product is byte strings. An Err is a test bug, not a cell.
23function cmp(a, b, suffixes) {
24 const r = compareVersionRefnames(a, b, { suffixes });
25 if (!r.ok) throw r.error;
26 return r.value;
29const ENV = {
30 ...Object.fromEntries(Object.entries(process.env).filter(([k]) => !k.startsWith('GIT_'))),
31 GIT_CONFIG_NOSYSTEM: '1', GIT_CONFIG_GLOBAL: '/dev/null', GIT_PAGER: 'cat', GIT_TERMINAL_PROMPT: '0',
32 LC_ALL: 'C.UTF-8',
33 GIT_AUTHOR_NAME: 'a', GIT_AUTHOR_EMAIL: 'a@x', GIT_COMMITTER_NAME: 'a', GIT_COMMITTER_EMAIL: 'a@x',
34 GIT_AUTHOR_DATE: '1700000000 +0000', GIT_COMMITTER_DATE: '1700000000 +0000',
35};
37const POOL = 16;
38const SHOW = 8;
40function git(args, opts = {}) {
41 const r = spawnSync('git', args, { env: ENV, encoding: 'utf8', maxBuffer: 1 << 26, ...opts });
42 assert.equal(r.status, 0, `git ${args.join(' ')}: ${r.stderr}`);
43 return r.stdout;
46/** @param {string[]} args */
47function gitText(args) {
48 return new Promise((resolve, reject) => {
49 const child = spawn('git', args, { env: ENV });
50 const out = [];
51 const err = [];
52 child.stdout.on('data', d => out.push(d));
53 child.stderr.on('data', d => err.push(d));
54 child.on('error', reject);
55 child.on('close', code => {
56 if (code !== 0) reject(new Error(`git ${args.join(' ')}: ${Buffer.concat(err).toString('utf8')}`));
57 else resolve(Buffer.concat(out).toString('utf8'));
58 });
59 });
62/** @param {number} workers @param {number} count @param {(i: number) => Promise<void>} fn */
63async function pool(workers, count, fn) {
64 let next = 0;
65 let failed = false;
66 const worker = async () => {
67 while (!failed) {
68 const i = next++;
69 if (i >= count) return;
70 try {
71 await fn(i);
72 } catch (e) {
73 failed = true;
74 throw e;
75 }
76 }
77 };
78 await Promise.all(Array.from({ length: workers }, worker));
81/** @param {string} dir @param {readonly string[]} tags */
82function initTags(dir, tags) {
83 git(['init', '-q', dir]);
84 git(['commit', '-q', '--allow-empty', '-m', 'c'], { cwd: dir });
85 const head = git(['rev-parse', 'HEAD'], { cwd: dir }).trim();
86 git(['update-ref', '--stdin'], { cwd: dir, input: tags.map(name => `create refs/tags/${name} ${head}\n`).join('') });
89/** @param {readonly string[]} suffixes */
90function suffixArgs(suffixes) {
91 const args = ['-c', 'core.quotepath=false'];
92 for (const suffix of suffixes) args.push('-c', `versionsort.suffix=${suffix}`);
93 return args;
96/** @param {string} dir @param {readonly string[]} suffixes */
97function gitList(dir, suffixes) {
98 const out = git([...suffixArgs(suffixes), '-C', dir, 'tag', '-l', '--sort=version:refname']);
99 return out.split('\n').filter(line => line.length > 0);
102// Pairs git's listing orders the other way from ply's listing, found while sorting ply's ranks (up to `limit`).
103// `a` is the name git lists first.
104/** @param {readonly string[]} gitNames @param {ReadonlyMap<string, number>} plyRank @param {number} limit */
105function listingInversions(gitNames, plyRank, limit) {
106 /** @type {{ name: string, rank: number }[]} */
107 const items = gitNames.map(name => ({ name, rank: plyRank.get(name) ?? -1 }));
108 /** @type {[string, string][]} */
109 const found = [];
110 /** @param {{ name: string, rank: number }[]} arr */
111 const sort = arr => {
112 if (arr.length <= 1) return arr;
113 const mid = arr.length >> 1;
114 const left = sort(arr.slice(0, mid));
115 const right = sort(arr.slice(mid));
116 /** @type {{ name: string, rank: number }[]} */
117 const merged = [];
118 let i = 0;
119 let j = 0;
120 while (i < left.length && j < right.length) {
121 if (left[i].rank <= right[j].rank) merged.push(left[i++]);
122 else {
123 if (found.length < limit) {
124 for (let k = i; k < left.length && found.length < limit; k++) found.push([left[k].name, right[j].name]);
125 }
126 merged.push(right[j++]);
127 }
128 }
129 for (; i < left.length; i++) merged.push(left[i]);
130 for (; j < right.length; j++) merged.push(right[j]);
131 return merged;
132 };
133 sort(items);
134 return found;
137/** @param {string} dir @param {readonly string[]} suffixes @param {string} a @param {string} b */
138async function gitPair(dir, suffixes, a, b) {
139 const out = await gitText([...suffixArgs(suffixes), '-C', dir, 'tag', '-l', '--sort=version:refname', '--', a, b]);
140 return out.split('\n').filter(line => line.length > 0);
143// One git process per pair, so the file is CPU-bound and slows with whatever shares the runner (#155): 168s on a CI
144// shard (durations.json, refreshed from CI run 41493029207); on a 4-core box 113s alone, 177s beside protocol.test.js,
145// 179s beside push-outcome, and no faster with the pool capped at 4. Two runs were stopped at an old 180s bound. A full
146// local run (every file at once) passed 360s three times, so the bound is 600s: the stall #155 was the shard balance
147// (durations.json), not this file.
148test('version:refname: two-name git tag -l is versioncmp, and a total listing matches it', { timeout: 600_000 }, async () => {
149 const ranked = names();
150 const paired = pairNames();
151 const unicode = unicodeNames();
152 assert.equal(PREFIXES.length * LEFTS.length * MIDS.length * ENDS.length, NAME_COUNT);
153 assert.equal(PAIR_LEFTS.length * PAIR_MIDS.length * PAIR_ENDS.length, PAIR_NAME_COUNT);
154 assert.equal(UNICODE_PREFIXES.length * UNICODE_LEFTS.length * UNICODE_MIDS.length * UNICODE_ENDS.length, UNICODE_NAME_COUNT);
155 assert.equal(ranked.length, NAME_COUNT);
156 assert.equal(new Set(ranked).size, ranked.length, 'two rank axes built one name');
157 assert.equal(paired.length, PAIR_NAME_COUNT);
158 assert.equal(new Set(paired).size, paired.length, 'two pair axes built one name');
159 assert.equal(new Set(unicode).size, unicode.length, 'two unicode axes built one name');
160 for (const [a, b] of CYCLE) assert.ok(paired.includes(a) && paired.includes(b), `${a} / ${b} missing from the pair product`);
161 const pairs = [...pairsOf(paired), ...pairsOf(unicode)];
162 assert.equal(pairs.length * SUFFIX_CONFIGS.length, PAIR_CELL_COUNT);
163 assert.equal(ranked.length * SUFFIX_CONFIGS.length, RANK_CELL_COUNT);
164 assert.equal(PAIR_CELL_COUNT + RANK_CELL_COUNT, CELL_COUNT);
166 const pairRoot = mkdtempSync(join(tmpdir(), 'ply-version-refname-pair-'));
167 const rankRoot = mkdtempSync(join(tmpdir(), 'ply-version-refname-rank-'));
168 try {
169 initTags(pairRoot, [...paired, ...unicode]);
170 initTags(rankRoot, ranked);
171 const t0 = performance.now();
172 const wrong = [];
174 for (const config of SUFFIX_CONFIGS) {
175 await pool(POOL, pairs.length, async i => {
176 if (wrong.length >= SHOW) return;
177 const [a, b] = pairs[i];
178 const ordered = cmp(a, b, config.suffixes);
179 const lines = await gitPair(pairRoot, config.suffixes, a, b);
180 const want = ordered < 0 ? [a, b] : ordered > 0 ? [b, a] : null;
181 if (!want || lines.length !== 2 || lines[0] !== want[0] || lines[1] !== want[1]) {
182 wrong.push(`${config.tag}: compare(${JSON.stringify(a)}, ${JSON.stringify(b)}) = ${ordered}, git tag -l [${lines.map(s => JSON.stringify(s)).join(', ')}]`);
183 }
184 });
185 if (wrong.length >= SHOW) break;
186 }
187 assert.deepEqual(wrong, []);
188 const pairMs = performance.now() - t0;
190 let rankCells = 0;
191 let inverted = 0;
192 /** @type {string[]} */
193 const listingNotes = [];
194 for (const config of SUFFIX_CONFIGS) {
195 const gitNames = gitList(rankRoot, config.suffixes);
196 assert.equal(gitNames.length, ranked.length, `${config.tag}: git listed every tag`);
197 assert.equal(new Set(gitNames).size, gitNames.length, `${config.tag}: git listed a tag twice`);
198 const plyNames = [...ranked].sort((a, b) => cmp(a, b, config.suffixes));
199 const at = plyNames.findIndex((name, i) => name !== gitNames[i]);
200 // Pairs the two listings order differently: git's qsort and a JS sort of a cyclic versioncmp.
201 // Each one is checked as two names. Two-name git is versioncmp, whichever listing placed it first.
202 /** @type {[string, string][]} */
203 const disagreed = [];
204 if (at !== -1) {
205 const plyRank = new Map(plyNames.map((name, i) => [name, i]));
206 disagreed.push(...listingInversions(gitNames, plyRank, 24));
207 assert.ok(disagreed.length >= 1, `${config.tag}: listings differ at ${at} and no inversion was found`);
208 }
209 if (config.tag === CYCLIC_SUFFIX) {
210 const pos = new Map(gitNames.map((name, i) => [name, i]));
211 let backwards = 0;
212 for (const [a, b] of CYCLE) {
213 const ia = pos.get(a);
214 const ib = pos.get(b);
215 assert.equal(typeof ia, 'number', `${config.tag}: ${a}`);
216 assert.equal(typeof ib, 'number', `${config.tag}: ${b}`);
217 if (ib < ia) {
218 backwards += 1;
219 disagreed.push([b, a]);
220 }
221 }
222 assert.ok(backwards >= 1, `${config.tag}: the cycle has an inverted edge in git's listing`);
223 }
224 const seen = new Set();
225 for (const [a, b] of disagreed) {
226 const key = `${a}\0${b}`;
227 if (seen.has(key)) continue;
228 seen.add(key);
229 const ordered = cmp(a, b, config.suffixes);
230 assert.notEqual(ordered, 0, `${config.tag}: ${a} and ${b}`);
231 const lines = await gitPair(rankRoot, config.suffixes, a, b);
232 const want = ordered < 0 ? [a, b] : [b, a];
233 assert.deepEqual(lines, want, `${config.tag}: listings disagree on ${JSON.stringify(a)} / ${JSON.stringify(b)}; two-name git [${lines.map(s => JSON.stringify(s)).join(', ')}], compare = ${ordered}`);
234 inverted += 1;
235 }
236 listingNotes.push(`${config.tag}: ${at === -1 ? 'listing matches' : `listing differs at ${at} (${disagreed.length} pairs checked)`}`);
237 rankCells += gitNames.length;
238 }
239 const ms = performance.now() - t0;
240 console.log(`version-refname: ${PAIR_CELL_COUNT} pair cells in ${pairMs.toFixed(0)} ms, ${rankCells} rank cells, ${inverted} listing disagreements confirmed by two-name git, ${ms.toFixed(0)} ms`);
241 console.log(`version-refname listings: ${listingNotes.join('; ')}`);
242 assert.equal(rankCells, RANK_CELL_COUNT);
243 assert.ok(inverted >= 1);
244 } finally {
245 rmSync(pairRoot, { recursive: true, force: true });
246 rmSync(rankRoot, { recursive: true, force: true });
247 }
248});
250// Each refusal kind. git versioncmp is only defined on bytes: the byte strings those refusals stand next to
251// (`1`, `é`) sort the same as `git tag -l --sort=version:refname`.
252test('version:refname refusals are typed, and the byte string git sorts still matches', () => {
253 const dir = mkdtempSync(join(tmpdir(), 'ply-version-refname-refuse-'));
254 try {
255 initTags(dir, ['1', 'a', 'é']);
256 const gitNames = gitList(dir, []);
257 const plyNames = ['1', 'a', 'é'].sort((a, b) => cmp(a, b, []));
258 assert.deepEqual(plyNames, gitNames);
259 } finally {
260 rmSync(dir, { recursive: true, force: true });
261 }
262 const cases = [
263 [1, 'a', undefined, 'not_string', 'left'],
264 ['a', 1, undefined, 'not_string', 'right'],
265 ['a', 'b', [1], 'not_string', 'suffix'],
266 ['\uD800', 'a', undefined, 'not_bytes', 'left'],
267 ['a', '\uD800', undefined, 'not_bytes', 'right'],
268 ['a', 'b', ['\uD800'], 'not_bytes', 'suffix'],
269 ['\udcc3\udca9', 'a', undefined, 'not_bytes', 'left'],
270 ];
271 for (const [a, b, suffixes, kind, which] of cases) {
272 const r = compareVersionRefnames(a, b, suffixes === undefined ? undefined : { suffixes });
273 assert.equal(r.ok, false, `${kind} ${which}`);
274 assert.equal(r.error.kind, kind);
275 assert.equal(r.error.which, which);
276 assert.equal(r.error.code, 'bad_request');
277 }
278});
Diagnostics4 git calls · 218 ms
CallStatusSizeTime0218 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
fetch2004.3 KiB213 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=213
git.response.bytes=4406
git.response.cache=MISS
git.response.duration_ms=213
git.fetch.wants=c794e1e059cd09b05b9959550570a76d4dec3266
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=4406
git.pkt.payload.max=4385
git.sideband.band1.bytes=4384
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
a5e2c75a49244afc
Request
ebc452b3…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format