GitHubivarvong

ply

main
ply6 git calls33 ms23 KiB4 cachedprotocol v2
236 lines13 KiBRaw
1// git-source manifest: the checks and messages git itself has, extracted mechanically from git's source at a pinned tag.
2// Every entry is something git decides; test/space/registry.js must say which ply space covers it or why it does not
3// apply (test/space/gates.test.js, gate b). The output is committed as test/space/git-manifest.json.
4//
5// node scripts/git-manifest.mjs summary from a cached shallow clone (cloned on first use)
6// node scripts/git-manifest.mjs --write rewrite test/space/git-manifest.json
7// node scripts/git-manifest.mjs --src <dir> use an existing checkout of git at GIT_TAG instead of cloning
8//
9// Kinds (each entry: id, kind, file, line, fn, text):
10// fsck-msg fsck.h FOREACH_FSCK_MSG_ID: the msg id and its default severity, plus the fsck.c functions reporting it
11// die / error every die(), die_errno(), error(), error_errno() in the protocol files below, with its format string
12// reject a reason receive-pack writes on an "ng" line: `ret = "..."`, `->error_string = "..."`, the unpack
13// status strings unpack() returns, and every `return "..."` of receive-pack
14// ng / err-pkt a string literal starting "ng " or "ERR " (what a client reads as a refusal)
15// capability a capability a server advertises (receive-pack show_ref, upload-pack write_v0_ref and
16// upload_pack_advertise, ls-refs ls_refs_advertise, serve.c capabilities[])
17// request-arg an argument upload-pack's v2 fetch and ls-refs accept (process_args, ls_refs)
18// refname-rule refs.c check_refname_component / check_or_sanitize_refname conditions (the comment bullets and the
19// `/* Refname ... */` branch comments, each the condition git returns -1 for)
20// tree-mode fsck_tree: every `case S_IF...` mode it accepts, and every FSCK_MSG it reports
21import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'node:fs';
22import { join, dirname } from 'node:path';
23import { tmpdir } from 'node:os';
24import { execFileSync } from 'node:child_process';
25import { fileURLToPath } from 'node:url';
27export const GIT_TAG = 'v2.50.0';
28const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
29export const MANIFEST = join(ROOT, 'test/space/git-manifest.json');
30export const MESSAGE_FILES = ['builtin/receive-pack.c', 'upload-pack.c', 'connect.c', 'pkt-line.c', 'sideband.c', 'send-pack.c', 'transport.c', 'http.c'];
32function gitSource(argv) {
33 const i = argv.indexOf('--src');
34 if (i >= 0) return argv[i + 1];
35 const dir = join(tmpdir(), `ply-git-src-${GIT_TAG}`);
36 if (!existsSync(join(dir, 'fsck.h'))) {
37 mkdirSync(dir, { recursive: true });
38 execFileSync('git', ['clone', '-q', '--depth', '1', '--branch', GIT_TAG, 'https://github.com/git/git.git', dir], { stdio: 'inherit' });
39 }
40 return dir;
43// ---- a small C reader: comments blanked (newlines kept), string literals decoded, functions located ----
44function blankComments(s) {
45 let out = '', i = 0;
46 while (i < s.length) {
47 const c = s[i], d = s[i + 1];
48 if (c === '"' || c === "'") { const j = endOfLiteral(s, i); out += s.slice(i, j); i = j; continue; }
49 if (c === '/' && d === '*') { const j = s.indexOf('*/', i + 2); const k = j < 0 ? s.length : j + 2; out += s.slice(i, k).replace(/[^\n]/g, ' '); i = k; continue; }
50 if (c === '/' && d === '/') { const j = s.indexOf('\n', i); const k = j < 0 ? s.length : j; out += ' '.repeat(k - i); i = k; continue; }
51 out += c; i++;
52 }
53 return out;
55function endOfLiteral(s, i) {
56 const q = s[i];
57 let j = i + 1;
58 while (j < s.length && s[j] !== q) { if (s[j] === '\\') j++; j++; }
59 return j + 1;
61const unescape = body => body.replace(/\\(n|t|r|"|'|\\|0|x[0-9a-fA-F]{2}|[0-7]{1,3})/g, (_, e) => ({ n: '\n', t: '\t', r: '\r', '"': '"', "'": "'", '\\': '\\', 0: '\0' }[e] ?? (e[0] === 'x' ? String.fromCharCode(parseInt(e.slice(1), 16)) : String.fromCharCode(parseInt(e, 8)))));
62// Adjacent string literals from position i (after whitespace, through `_(`, `N_(`, `Q_(`): the decoded text, or null.
63function stringAt(s, i) {
64 const ws = () => { while (i < s.length && /\s/.test(s[i])) i++; };
65 ws();
66 const m = /^(?:N_|Q_|_)\s*\(/.exec(s.slice(i, i + 8));
67 if (m) { i += m[0].length; ws(); }
68 if (s[i] !== '"') return null;
69 let text = '';
70 while (s[i] === '"') { const j = endOfLiteral(s, i); text += unescape(s.slice(i + 1, j - 1)); i = j; ws(); }
71 return text;
73const lineAt = (s, i) => { let n = 1; for (let k = 0; k < i; k++) if (s.charCodeAt(k) === 10) n++; return n; };
74// Function bodies: a `{` at column 0 opens one; its name is the last `name(` at the start of a column-0 line before it.
75function functions(s) {
76 const fns = [];
77 const re = /^\{/gm;
78 let m;
79 while ((m = re.exec(s))) {
80 const head = s.slice(Math.max(0, m.index - 600), m.index);
81 const names = [...head.matchAll(/^[A-Za-z_][\w \t*]*?\b(\w+)\s*\(/gm)];
82 const name = names.length ? names[names.length - 1][1] : '?';
83 let depth = 0, j = m.index;
84 for (; j < s.length; j++) {
85 const c = s[j];
86 if (c === '"' || c === "'") { j = endOfLiteral(s, j) - 1; continue; }
87 if (c === '{') depth++;
88 else if (c === '}' && --depth === 0) break;
89 }
90 fns.push({ name, start: m.index, end: j });
91 }
92 return fns;
94const fnAt = (fns, i) => fns.find(f => f.start <= i && i <= f.end)?.name ?? '(top)';
95function literalsIn(s, from, to) {
96 const out = [];
97 for (let i = from; i < to; i++) {
98 if (s[i] === "'") { i = endOfLiteral(s, i) - 1; continue; }
99 if (s[i] !== '"') continue;
100 const j = endOfLiteral(s, i);
101 out.push({ text: unescape(s.slice(i + 1, j - 1)), at: i });
102 i = j - 1;
103 }
104 return out;
107function load(dir, file) {
108 const raw = readFileSync(join(dir, file), 'utf8');
109 const s = blankComments(raw);
110 return { file, raw, s, fns: functions(s) };
113export function extract(dir) {
114 const entries = [];
115 const add = e => entries.push(e);
117 // ---- fsck message ids ----
118 const fsckH = readFileSync(join(dir, 'fsck.h'), 'utf8');
119 const fsckC = load(dir, 'fsck.c');
120 const block = fsckH.slice(fsckH.indexOf('#define FOREACH_FSCK_MSG_ID'));
121 const hLine = fsckH.slice(0, fsckH.indexOf('#define FOREACH_FSCK_MSG_ID')).split('\n').length;
122 for (const m of block.matchAll(/FUNC\((\w+), (\w+)\)/g)) {
123 const [, id, severity] = m;
124 const camel = id.toLowerCase().replace(/_([a-z0-9])/g, (_, c) => c.toUpperCase());
125 const reportedIn = [...new Set([...fsckC.s.matchAll(new RegExp(`FSCK_MSG_${id}\\b`, 'g'))].map(x => fnAt(fsckC.fns, x.index)))];
126 add({ id: `fsck:${camel}`, kind: 'fsck-msg', file: 'fsck.h', line: hLine + block.slice(0, m.index).split('\n').length - 1, fn: 'FOREACH_FSCK_MSG_ID', text: `${id} ${severity}`, severity, reportedIn });
127 }
129 // ---- messages, reject reasons, refusal strings ----
130 for (const file of MESSAGE_FILES) {
131 const f = load(dir, file);
132 const short = file.replace('builtin/', '');
133 for (const m of f.s.matchAll(/(?<![\w.>])(die_errno|die|error_errno|error)\s*\(/g)) {
134 const text = stringAt(f.s, m.index + m[0].length) ?? '<expr>';
135 add({ kind: m[1].startsWith('die') ? 'die' : 'error', file, line: lineAt(f.s, m.index), fn: fnAt(f.fns, m.index), text, base: `${short}:${m[1].startsWith('die') ? 'die' : 'error'}:${fnAt(f.fns, m.index)}:${text}` });
136 }
137 if (file === 'builtin/receive-pack.c') {
138 for (const m of f.s.matchAll(/(?:\bret|error_string|reported_error)\s*=\s*(?=(?:N_|_)?\s*\(?\s*")/g)) {
139 const text = stringAt(f.s, m.index + m[0].length);
140 if (text !== null) add({ kind: 'reject', file, line: lineAt(f.s, m.index), fn: fnAt(f.fns, m.index), text, base: `${short}:reject:${text}` });
141 }
142 for (const m of f.s.matchAll(/\breturn\s+(?=(?:N_|_)?\s*\(?\s*")/g)) {
143 const text = stringAt(f.s, m.index + m[0].length);
144 if (text !== null) add({ kind: 'reject', file, line: lineAt(f.s, m.index), fn: fnAt(f.fns, m.index), text, base: `${short}:reject:${text}` });
145 }
146 }
147 for (const lit of literalsIn(f.s, 0, f.s.length)) {
148 if (lit.text.startsWith('ng ')) add({ kind: 'ng', file, line: lineAt(f.s, lit.at), fn: fnAt(f.fns, lit.at), text: lit.text, base: `${short}:ng:${fnAt(f.fns, lit.at)}:${lit.text}` });
149 if (lit.text.startsWith('ERR ')) add({ kind: 'err-pkt', file, line: lineAt(f.s, lit.at), fn: fnAt(f.fns, lit.at), text: lit.text, base: `${short}:err-pkt:${fnAt(f.fns, lit.at)}:${lit.text}` });
150 }
151 }
153 // ---- capabilities and request arguments ----
154 const capFns = [['builtin/receive-pack.c', 'show_ref', 'receive-pack v0'], ['upload-pack.c', 'write_v0_ref', 'upload-pack v0'], ['upload-pack.c', 'upload_pack_advertise', 'v2 fetch'], ['ls-refs.c', 'ls_refs_advertise', 'v2 ls-refs']];
155 for (const [file, fn, where] of capFns) {
156 const f = load(dir, file);
157 const body = f.fns.find(x => x.name === fn);
158 if (!body) throw new Error(`git-manifest: ${file} has no ${fn}()`);
159 for (const lit of literalsIn(f.s, body.start, body.end)) {
160 for (const tok of lit.text.split(/\s+/)) {
161 const name = tok.replace(/=.*$/, '');
162 if (!/^[a-z][a-z0-9_-]+$/.test(name)) continue;
163 add({ kind: 'capability', file, line: lineAt(f.s, lit.at), fn, text: name, where, base: `capability:${where}:${name}` });
164 }
165 }
166 }
167 const serve = load(dir, 'serve.c');
168 for (const m of serve.s.matchAll(/\.name\s*=\s*"([^"]+)"/g)) add({ kind: 'capability', file: 'serve.c', line: lineAt(serve.s, m.index), fn: 'capabilities[]', text: m[1], where: 'v2 serve', base: `capability:v2 serve:${m[1]}` });
169 const argFns = [['upload-pack.c', ['process_args', 'parse_want', 'parse_want_ref', 'parse_have', 'process_shallow', 'process_deepen', 'process_deepen_since', 'process_deepen_not'], 'v2 fetch'], ['ls-refs.c', ['ls_refs'], 'v2 ls-refs']];
170 for (const [file, names, where] of argFns) {
171 const f = load(dir, file);
172 const seen = new Set();
173 for (const fn of names) {
174 const body = f.fns.find(x => x.name === fn);
175 if (!body) throw new Error(`git-manifest: ${file} has no ${fn}()`);
176 for (const m of f.s.slice(body.start, body.end).matchAll(/(?:skip_prefix|strcmp|starts_with|parse_feature_value)\s*\(\s*(?:\w+\s*,\s*"([^"]+)"|"([^"]+)"\s*,)/g)) {
177 const name = (m[1] ?? m[2]).trim();
178 if (seen.has(name) || !/^[a-z][a-z0-9-]*$/.test(name)) continue;
179 seen.add(name);
180 add({ kind: 'request-arg', file, line: lineAt(f.s, body.start + m.index), fn, text: name, where, base: `request-arg:${where}:${name}` });
181 }
182 }
183 }
185 // ---- ref-name rules ----
186 const refs = load(dir, 'refs.c');
187 const comp = refs.raw.indexOf('static int check_refname_component(');
188 const doc = refs.raw.lastIndexOf('/*', comp);
189 const docText = refs.raw.slice(doc, comp);
190 const docLine = lineAt(refs.raw, doc);
191 docText.split('\n').forEach((l, k) => {
192 const m = /^\s*\*\s+- (it .+?)(?:, or|\.)?$/.exec(l);
193 if (m) add({ kind: 'refname-rule', file: 'refs.c', line: docLine + k, fn: 'check_refname_component', text: m[1], base: `refname:${m[1]}` });
194 });
195 const rf = functions(blankComments(refs.raw));
196 for (const fn of ['check_refname_component', 'check_or_sanitize_refname']) {
197 const body = rf.find(x => x.name === fn);
198 for (const m of refs.raw.slice(body.start, body.end).matchAll(/\/\*\s*((?:Refname|Component)[^*]*?)\s*\*\//g)) add({ kind: 'refname-rule', file: 'refs.c', line: lineAt(refs.raw, body.start + m.index), fn, text: m[1].replace(/\s+/g, ' '), base: `refname:${m[1].replace(/\s+/g, ' ')}` });
199 for (const m of refs.raw.slice(body.start, body.end).matchAll(/if \(!\(\*?flags & (REFNAME_\w+)\)[^\n]*/g)) add({ kind: 'refname-rule', file: 'refs.c', line: lineAt(refs.raw, body.start + m.index), fn, text: `unless ${m[1]}: ${m[0].trim()}`, base: `refname:unless ${m[1]}` });
200 }
202 // ---- fsck_tree ----
203 const tree = fsckC.fns.find(x => x.name === 'fsck_tree');
204 if (!tree) throw new Error('git-manifest: fsck.c has no fsck_tree()');
205 const tb = fsckC.s.slice(tree.start, tree.end);
206 for (const m of tb.matchAll(/case (S_IF\w+(?:\s*\|\s*0\d+)?):/g)) add({ kind: 'tree-mode', file: 'fsck.c', line: lineAt(fsckC.s, tree.start + m.index), fn: 'fsck_tree', text: `mode ${m[1].replace(/\s+/g, '')}`, base: `tree-mode:case ${m[1].replace(/\s+/g, '')}` });
207 for (const m of tb.matchAll(/FSCK_MSG_(\w+)/g)) add({ kind: 'tree-mode', file: 'fsck.c', line: lineAt(fsckC.s, tree.start + m.index), fn: 'fsck_tree', text: `reports ${m[1]}`, base: `tree-mode:reports ${m[1]}` });
209 // stable ids: kind-specific base, numbered when git repeats it
210 const seen = new Map();
211 for (const e of entries) {
212 if (e.id) continue;
213 const base = e.base.replace(/\s+/g, ' ').slice(0, 140);
214 const n = (seen.get(base) ?? 0) + 1;
215 seen.set(base, n);
216 e.id = n === 1 ? base : `${base}#${n}`;
217 delete e.base;
218 }
219 return entries;
222export function build(dir) {
223 const version = execFileSync('git', ['-C', dir, 'describe', '--tags', '--exact-match'], { encoding: 'utf8' }).trim();
224 if (version !== GIT_TAG) throw new Error(`git-manifest: ${dir} is at ${version}, not ${GIT_TAG}`);
225 const commit = execFileSync('git', ['-C', dir, 'rev-parse', 'HEAD'], { encoding: 'utf8' }).trim();
226 const entries = extract(dir);
227 const counts = {};
228 for (const e of entries) counts[e.kind] = (counts[e.kind] ?? 0) + 1;
229 return { git: { tag: GIT_TAG, commit, source: 'https://github.com/git/git' }, generator: 'scripts/git-manifest.mjs', counts, entries };
232if (process.argv[1] === fileURLToPath(import.meta.url)) {
233 const m = build(gitSource(process.argv));
234 if (process.argv.includes('--write')) writeFileSync(MANIFEST, `${JSON.stringify(m, null, 1)}\n`);
235 console.log(`git ${m.git.tag} (${m.git.commit.slice(0, 12)}): ${m.entries.length} entries`, m.counts);
Diagnostics6 git calls · 33 ms
CallStatusSizeTime033 ms
discover200191 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=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=10
Open 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=5323b9266eea5ded637285d1c5320699f12be76e
Open pack map
fetchtree:0200831 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=831
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=b9290e370bd091fd3272931655a175dd1a7af82d
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=831
git.pkt.payload.max=804
git.sideband.band1.bytes=804
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=804
git.pack.trailer=81ddc1f73214935b4ee953ad84f959db955ce0d1
Open pack map
fetch2004.7 KiB · hit21 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=19
git.response.bytes=4819
git.response.cache=HIT
git.response.duration_ms=21
git.fetch.wants=1234acbf39a974d7a11908216c72aa4551aa9b06
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=4819
git.pkt.payload.max=4792
git.sideband.band1.bytes=4792
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
a5e1559acc9b90ce
Request
bd0ca684…
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