main
1// readFile / listTree against git as the oracle: `git show <ref>:<path>` for bytes, `git ls-tree <ref>:<path>` for
2// listings, over git http-backend. Covers nested paths, every entry kind (regular, executable, symlink, gitlink, tree),
3// empty and multi-MiB blobs, every ref form (HEAD, full, short, annotated/lightweight tag, oid), the not_found shapes,
4// a lying server (wrong blob served → the stream errors), and credential hygiene (spans, echoed error bodies).
5import { test, beforeAll as before, afterAll as after } from 'vitest';
6import assert from 'node:assert/strict';
7import http from 'node:http';
8import { join } from 'node:path';
9import { execFileSync, spawnSync } from 'node:child_process';
10import { mkdirSync, readdirSync, symlinkSync, writeFileSync } from 'node:fs';
11import { git, gitBuf, gitAsync, tmpRoot, makeBareRepo, startGitServer, commitFiles, initRepo } from './server.js';
12import { readFile, listTree, readCommit, refCandidates, walkPath, fetchCommit, openBlob, readBlob, resolveCommit, resolveAbbrev } from '../src/read.js';
13import { lsRefs as lsRefsR } from '../src/lsrefs.js';
14import { createTransport as createTransportR } from '../src/http.js';
15import { fetchObject as fetchObjectR } from '../src/fetch.js';
16import { parseIdent, TYPE } from '../src/objects.js';
17import { Tracer, arrayExporter, flattenLine } from '../src/otel.js';
18import { unwrap, unwrapAsync } from './unwrap.js';
20// Throwing adapters over the Result API (0.2.0): the assertions below are about the throw.
21const lsRefs = (...a) => unwrapAsync(lsRefsR(...a));
22const createTransport = (...a) => unwrap(createTransportR(...a));
23const fetchObject = (...a) => unwrapAsync(fetchObjectR(...a));
24let root, cleanup, srv, R;
25const tracer = () => new Tracer();
26const drain = async stream => { const parts = []; for await (const c of stream) parts.push(Buffer.from(c)); return Buffer.concat(parts); };
27const lsTree = (bare, spec) => git(bare, ['ls-tree', '-z', spec]).split('\0').filter(Boolean).map(l => { const [meta, name] = l.split('\t'); const [mode, type, oid] = meta.split(' '); return { name, mode, oid, type }; });
29before(async () => {
30 ({ root, cleanup } = tmpRoot()); srv = await startGitServer(root);
31 const bare = makeBareRepo(root, 'r'); const work = join(root, 'wr'); mkdirSync(work); initRepo(work);
32 const big = Buffer.alloc(3 << 20); for (let i = 0; i < big.length; i += 7) big[i] = (i * 2654435761) >>> 24;
33 commitFiles(work, bare, { 'README.md': '# v1\n', 'a/b/c.txt': 'nested\n', 'empty.txt': '', 'run.sh': { mode: '100755', data: '#!/bin/sh\necho hi\n' }, 'big.bin': big, 'dir with space/ünï.txt': 'unicode\n' }, 'one');
34 git(work, ['tag', '-a', '-m', 'annotated', 'v1']); git(work, ['tag', 'light']); git(work, ['push', '-q', bare, 'refs/tags/v1', 'refs/tags/light']);
35 symlinkSync('a/b/c.txt', join(work, 'link'));
36 commitFiles(work, bare, { 'README.md': '# v2\n' }, 'two');
37 git(work, ['push', '-q', bare, 'HEAD:refs/heads/feature']);
38 commitFiles(work, bare, { 'README.md': '# v3 main\n' }, 'three');
39 // a branch and a tag with the same short name: git rev-parse picks the tag
40 git(work, ['tag', 'dup', 'HEAD~2']); git(work, ['push', '-q', bare, 'refs/tags/dup', 'HEAD~1:refs/heads/dup']);
41 // last, because commitFiles' `add -A` would drop it: a gitlink (submodule entry) with no .gitmodules, as git allows
42 git(work, ['update-index', '--add', '--cacheinfo', `160000,${git(work, ['rev-parse', 'HEAD~2']).trim()},vendor/sub`]);
43 git(work, ['-c', 'user.name=seed', '-c', 'user.email=seed@x', 'commit', '-q', '-m', 'four']); git(work, ['push', '-q', bare, 'HEAD:refs/heads/main']);
44 R = { bare, work, url: `${srv.url}/r.git` };
45});
46after(async () => { await srv.close(); cleanup(); });
48test('refCandidates is git rev-parse order (tags before heads); oids skip ls-refs; bad refs refused', () => {
49 assert.deepEqual(unwrap(refCandidates(null)), ['HEAD']);
50 assert.deepEqual(unwrap(refCandidates('main')), ['refs/main', 'refs/tags/main', 'refs/heads/main']);
51 assert.deepEqual(unwrap(refCandidates('refs/heads/x')), ['refs/heads/x']);
52 assert.deepEqual(unwrap(refCandidates('a'.repeat(40))), []);
53 for (const bad of ['ma..in', 'x y', 'refs/heads/a\nb', 'a:b']) assert.throws(() => unwrap(refCandidates(bad)), e => e.code === 'bad_request', bad);
54});
56test('readFile bytes == git show <ref>:<path> for every blob path and every ref form', async () => {
57 const oid = git(R.bare, ['rev-parse', 'main~1']).trim();
58 const refs = [null, 'HEAD', 'main', 'refs/heads/main', 'feature', 'v1', 'refs/tags/v1', 'light', 'dup', oid];
59 const paths = ['README.md', 'a/b/c.txt', 'empty.txt', 'run.sh', 'big.bin', 'dir with space/ünï.txt', 'link'];
60 let n = 0;
61 for (const ref of refs) for (const path of paths) {
62 const spec = `${ref ?? 'HEAD'}:${path}`;
63 let want; try { want = gitBuf(R.bare, ['show', spec]); } catch { continue; } // link does not exist at v1
64 const r = await unwrapAsync(readFile(R.url, { ref, path }, { tracer: tracer() }));
65 const got = await drain(r.stream);
66 assert.equal(r.size, want.length, spec);
67 assert.ok(got.equals(want), `${spec}: bytes differ`);
68 assert.equal(r.blob, git(R.bare, ['rev-parse', spec]).trim(), spec);
69 assert.equal(r.commit, git(R.bare, ['rev-parse', `${ref ?? 'HEAD'}^{commit}`]).trim(), spec);
70 n++;
71 }
72 assert.ok(n >= 60, `cases ${n}`);
73 const dup = await unwrapAsync(readFile(R.url, { ref: 'dup', path: 'README.md' }, { tracer: tracer() }));
74 assert.equal(dup.ref, 'refs/tags/dup'); assert.equal((await drain(dup.stream)).toString(), '# v1\n');
75 const head = await unwrapAsync(readFile(R.url, { path: 'README.md' }, { tracer: tracer() }));
76 assert.equal(head.ref, 'refs/heads/main'); assert.equal((await drain(head.stream)).toString(), '# v3 main\n');
77 console.log(` ${n} (ref, path) cases equal to git show`);
78});
80test('modes: symlink reads as its target (120000), executable 100755; gitlink and tree are not files', async () => {
81 const link = await unwrapAsync(readFile(R.url, { path: 'link' }, { tracer: tracer() }));
82 assert.equal(link.mode, '120000'); assert.equal((await drain(link.stream)).toString(), 'a/b/c.txt');
83 const sh = await unwrapAsync(readFile(R.url, { path: 'run.sh' }, { tracer: tracer() })); await drain(sh.stream);
84 assert.equal(sh.mode, '100755');
85 await assert.rejects(unwrapAsync(readFile(R.url, { path: 'vendor/sub' }, { tracer: tracer() })), e => e.code === 'not_found' && /submodule/.test(e.message));
86 await assert.rejects(unwrapAsync(readFile(R.url, { path: 'a/b' }, { tracer: tracer() })), e => e.code === 'not_found' && /directory/.test(e.message));
87});
89test('typed misses match git: ref, unborn HEAD, path, directory, submodule', async () => {
90 const ref = 'no-such-branch';
91 assert.throws(() => git(R.bare, ['rev-parse', '--verify', '--quiet', ref]));
92 await assert.rejects(unwrapAsync(readFile(R.url, { ref, path: 'README.md' }, { tracer: tracer() })), e => {
93 assert.equal(e.code, 'not_found');
94 assert.equal(e.miss.kind, 'ref');
95 assert.equal(e.miss.ref, ref);
96 assert.deepEqual([...e.miss.candidates], unwrap(refCandidates(ref)));
97 return true;
98 });
100 const empty = makeBareRepo(root, 'empty');
101 // git 2.43's ls-remote receives `unborn HEAD symref-target:refs/heads/main` and prints no line (no oid to list).
102 // symbolic-ref is the target that line names; an empty listing is the unborn repository.
103 assert.equal(git(empty, ['ls-remote', '--symref', '.']).trim(), '');
104 const target = git(empty, ['symbolic-ref', 'HEAD']).trim();
105 assert.equal(target, 'refs/heads/main');
106 for (const asked of [null, 'HEAD', 'main', 'refs/heads/main']) {
107 const got = await unwrapAsync(readCommit(`${srv.url}/empty.git`, { ref: asked }, { tracer: tracer() }));
108 assert.deepEqual(got, { empty: true, ref: target }, String(asked));
109 }
110 await assert.rejects(unwrapAsync(readFile(`${srv.url}/empty.git`, { path: 'README.md' }, { tracer: tracer() })), e => e.code === 'not_found' && e.miss.kind === 'head_unborn' && e.miss.target === target);
111 await assert.rejects(unwrapAsync(readFile(`${srv.url}/empty.git`, { ref: 'nope', path: 'README.md' }, { tracer: tracer() })), e => e.code === 'not_found' && e.miss.kind === 'ref' && e.miss.ref === 'nope');
113 const tree = git(R.bare, ['rev-parse', 'HEAD^{tree}']).trim();
114 const t = createTransport(R.url, { tracer: tracer() });
115 const missing = await walkPath(t, tree, ['a', 'nope']);
116 assert.equal(missing.ok, false);
117 if (!missing.ok) assert.deepEqual(missing.error.miss, { kind: 'path', missing: 'nope', existingPath: 'a' });
118 assert.throws(() => git(R.bare, ['cat-file', '-e', 'HEAD:a/nope']));
119 const throughFile = await walkPath(t, tree, ['README.md', 'x']);
120 assert.equal(throughFile.ok, false);
121 if (!throughFile.ok) {
122 const mode = git(R.bare, ['ls-tree', 'HEAD', 'README.md']).slice(0, 6);
123 assert.deepEqual(throughFile.error.miss, { kind: 'not_directory', path: 'README.md', mode });
124 }
125 const dir = await unwrapAsync(readFile(R.url, { path: 'a/b' }, { tracer: tracer() })).then(() => { throw new Error('a directory was a file'); }, e => e);
126 assert.equal(dir.code, 'not_found');
127 assert.deepEqual(dir.miss, { kind: 'is_directory', path: 'a/b', tree: git(R.bare, ['rev-parse', 'HEAD:a/b']).trim() });
128 const subLine = git(R.bare, ['ls-tree', 'HEAD', 'vendor/sub']).trim();
129 const subOid = subLine.split(/\s+/)[2];
130 assert.equal(subLine.slice(0, 6), '160000');
131 const sub = await unwrapAsync(readFile(R.url, { path: 'vendor/sub' }, { tracer: tracer() })).then(() => { throw new Error('a submodule was a file'); }, e => e);
132 assert.equal(sub.code, 'not_found');
133 assert.deepEqual(sub.miss, { kind: 'submodule', path: 'vendor/sub', commit: subOid });
134});
136// A commit whose object is a tree, blob, or tag is not_found (refuseNonCommit). The pack_corrupt line that remains is a
137// want whose expected type is not a commit: walking a path from a blob asks for a tree and the object is a blob.
138test('a path walk that starts at a blob is pack_corrupt (expected tree, got blob)', async () => {
139 const blob = git(R.bare, ['rev-parse', 'HEAD:README.md']).trim();
140 const walked = await walkPath(createTransport(R.url, { tracer: tracer() }), blob, ['x']);
141 assert.equal(walked.ok, false);
142 if (!walked.ok) {
143 assert.equal(walked.error.code, 'pack_corrupt');
144 assert.match(walked.error.message, /expected tree, got blob/);
145 }
146});
148test('ply does not classify a not_found by matching its own message', async () => {
149 const { readdirSync, readFileSync } = await import('node:fs');
150 // The classifiers ply-web used, and the ones this tree used to rethrow not_found. The messages themselves stay;
151 // nothing in src tests a message to decide a code.
152 const banned = ['thrownMessageMatches(', '/not our ref/', '/expected commit', '/is a directory', '/is not a directory', '/path component', '/remote HEAD', '/^ref ', 'expected commit|not our ref'];
153 for (const f of readdirSync('src').filter(name => /\.[jt]s$/.test(name))) {
154 const text = readFileSync(`src/${f}`, 'utf8');
155 for (const needle of banned) assert.equal(text.includes(needle), false, `${f} contains ${needle}`);
156 }
157});
159test('fetchCommit matches git fetch and cat-file; a tree want of a blob stays pack_corrupt', async () => {
160 const tip = git(R.bare, ['rev-parse', 'main']).trim();
161 const tree = git(R.bare, ['rev-parse', `${tip}^{tree}`]).trim();
162 const blob = git(R.bare, ['rev-parse', `${tip}:README.md`]).trim();
163 const tag = git(R.bare, ['rev-parse', 'v1']).trim();
164 const dangling = git(R.bare, ['-c', 'user.name=seed', '-c', 'user.email=seed@x', 'commit-tree', tree, '-m', 'dangling']).trim();
165 const absent = 'ab'.repeat(20);
166 // gitAsync, not git(): execFileSync would block the event loop and deadlock the http-backend in this process.
167 const classify = async (oid) => {
168 const dir = join(root, `oracle-${oid.slice(0, 12)}`);
169 mkdirSync(dir);
170 git(dir, ['init', '-q']);
171 git(dir, ['remote', 'add', 'origin', R.url]);
172 const fetched = await gitAsync(dir, ['fetch', '--depth=1', 'origin', oid]);
173 const text = `${fetched.stdout}\n${fetched.stderr}`;
174 let typ = null;
175 try { typ = git(R.bare, ['cat-file', '-t', oid]).trim(); } catch { typ = null; }
176 if (fetched.status !== 0 && /not our ref|couldn't find remote ref/.test(text)) return { class: 'absent', typ };
177 if (fetched.status === 0 && typ !== 'commit') return { class: 'not_commit', typ };
178 if (fetched.status === 0 && typ === 'commit') return { class: 'ok', typ };
179 throw new Error(`unclassified ${oid} status ${fetched.status} type ${typ}: ${text}`);
180 };
181 const cases = [
182 ['tip', tip, 'ok'],
183 ['dangling', dangling, 'ok'],
184 ['tree', tree, 'not_commit'],
185 ['blob', blob, 'not_commit'],
186 ['tag', tag, 'not_commit'],
187 ['absent', absent, 'absent'],
188 ];
189 for (const [name, oid, want] of cases) {
190 const gitClass = await classify(oid);
191 assert.equal(gitClass.class, want, `${name}: git classified ${gitClass.class} (${gitClass.typ})`);
192 const got = await fetchCommit(createTransport(R.url, { tracer: tracer() }), oid);
193 if (want === 'ok') {
194 assert.equal(got.ok, true, name);
195 if (!got.ok) continue;
196 assert.ok(Buffer.from(got.value.data).equals(gitBuf(R.bare, ['cat-file', 'commit', oid])), name);
197 assert.equal(got.value.oid, oid, name);
198 assert.equal(got.value.commit.tree, git(R.bare, ['rev-parse', `${oid}^{tree}`]).trim(), name);
199 } else {
200 assert.equal(got.ok, false, name);
201 if (got.ok) continue;
202 assert.equal(got.error.code, 'not_found', `${name}: ${got.error.code} ${got.error.message}`);
203 assert.equal(got.error.miss.kind, 'commit', name);
204 assert.equal(got.error.miss.miss.kind, want, name);
205 assert.equal(got.error.miss.miss.oid, oid, name);
206 if (want === 'not_commit') assert.equal(got.error.miss.miss.type, gitClass.typ, name);
207 }
208 }
209 await assert.rejects(fetchObject(createTransport(R.url, { tracer: tracer() }), { oid: blob, expectType: TYPE.TREE }), e => e.code === 'pack_corrupt' && e.message === 'expected tree, got blob' && e.miss === undefined);
210});
212test('not_found shapes: missing path, path through a file, missing ref, unknown oid; bad path is bad_request before I/O', async () => {
213 for (const path of ['nope', 'a/nope', 'a/b/c.txt/x', 'README.md/x', 'empty.txt/x/y']) await assert.rejects(unwrapAsync(readFile(R.url, { path }, { tracer: tracer() })), e => e.code === 'not_found', path);
214 for (const path of ['nope', 'README.md', 'a/b/c.txt', 'vendor/sub']) await assert.rejects(unwrapAsync(listTree(R.url, { path }, { tracer: tracer() })), e => e.code === 'not_found', path);
215 await assert.rejects(unwrapAsync(readFile(R.url, { ref: 'no-such-branch', path: 'README.md' }, { tracer: tracer() })), e => e.code === 'not_found');
216 await assert.rejects(unwrapAsync(readFile(R.url, { ref: 'f'.repeat(40), path: 'README.md' }, { tracer: tracer() })), e => e.code === 'not_found', 'unknown oid');
217 const blobOid = git(R.bare, ['rev-parse', 'main:README.md']).trim();
218 await assert.rejects(unwrapAsync(readFile(R.url, { ref: blobOid, path: 'README.md' }, { tracer: tracer() })), e => e.code === 'not_found', 'oid of a blob is not a commit');
219 let calls = 0; const counting = (u, i) => { calls++; return fetch(u, i); };
220 for (const path of ['', '../x', 'a//b', '.git/config', 'a/.git']) await assert.rejects(unwrapAsync(readFile(R.url, { path }, { tracer: tracer(), fetchImpl: counting })), e => e.code === 'bad_request', JSON.stringify(path));
221 assert.equal(calls, 0);
222});
224test('listTree == git ls-tree <ref>:<path> (root, nested, gitlink, symlink, exec, at a tag)', async () => {
225 for (const [ref, path] of [[null, ''], ['main', 'a'], ['main', 'a/b'], ['main', 'a/b/'], ['main', 'vendor'], ['main', 'dir with space'], ['v1', ''], ['feature', '']]) {
226 const r = await unwrapAsync(listTree(R.url, { ref, path }, { tracer: tracer() }));
227 const spec = `${ref ?? 'HEAD'}:${path.replace(/\/$/, '')}`;
228 assert.deepEqual(r.entries, lsTree(R.bare, spec), spec);
229 assert.equal(r.tree, git(R.bare, ['rev-parse', spec]).trim());
230 }
231 const top = await unwrapAsync(listTree(R.url, {}, { tracer: tracer() }));
232 assert.deepEqual(Object.fromEntries(top.entries.map(e => [e.name, `${e.mode} ${e.type}`])), { 'README.md': '100644 blob', a: '040000 tree', 'big.bin': '100644 blob', 'dir with space': '040000 tree', 'empty.txt': '100644 blob', link: '120000 blob', 'run.sh': '100755 blob', vendor: '040000 tree' });
233 const vendor = await unwrapAsync(listTree(R.url, { path: 'vendor' }, { tracer: tracer() }));
234 assert.deepEqual(vendor.entries.map(e => [e.mode, e.type]), [['160000', 'commit']]);
235});
237test('streamed: a 3 MiB blob arrives in many chunks; tree cache is reused across reads', async () => {
238 const cache = new Map(); const ex = arrayExporter();
239 const r = await unwrapAsync(readFile(R.url, { path: 'big.bin' }, { tracer: new Tracer({ exporter: ex }), cache }));
240 let chunks = 0, max = 0, len = 0; for await (const c of r.stream) { chunks++; max = Math.max(max, c.length); len += c.length; }
241 assert.equal(len, 3 << 20); assert.ok(chunks > 10, `chunks ${chunks}`); assert.ok(max <= 1 << 16, `max chunk ${max}`);
242 const blob = ex.lines.flatMap(flattenLine).find(s => s.name === 'git.read.blob');
243 assert.equal(blob.attrs['git.read.blob.verified'], true); assert.equal(blob.attrs['git.read.blob.bytes'], 3 << 20);
244 const ex2 = arrayExporter();
245 const r2 = await unwrapAsync(readFile(R.url, { path: 'a/b/c.txt' }, { tracer: new Tracer({ exporter: ex2 }), cache })); await drain(r2.stream);
246 const trees = ex2.lines.flatMap(flattenLine).filter(s => s.name === 'git.read.tree');
247 assert.deepEqual(trees.map(s => s.attrs['git.read.tree.cache_hit']), [true, false, false]);
248});
250test('a server that serves a different blob than asked: the stream errors (pack_corrupt), never ends cleanly', async () => {
251 const want = git(R.bare, ['rev-parse', 'main:README.md']).trim(), other = git(R.bare, ['rev-parse', 'main:a/b/c.txt']).trim();
252 const liar = (u, init) => {
253 const body = init?.body instanceof Uint8Array ? Buffer.from(init.body).toString('latin1') : null;
254 if (body && body.includes(`want ${want}\n`) && !body.includes('filter ')) return fetch(u, { ...init, body: Buffer.from(body.replace(want, other), 'latin1') });
255 return fetch(u, init);
256 };
257 const r = await unwrapAsync(readFile(R.url, { path: 'README.md' }, { tracer: tracer(), fetchImpl: liar }));
258 await assert.rejects(drain(r.stream), e => e.code === 'pack_corrupt' && /hashes to/.test(e.message));
259});
261test('credential hygiene: never in spans; an upstream error body echoing Authorization is scrubbed', async () => {
262 const token = 'ghp_SECRETTOKEN1234567890abcdef';
263 const ex = arrayExporter();
264 const r = await unwrapAsync(readFile(R.url, { path: 'README.md' }, { tracer: new Tracer({ exporter: ex }), auth: { token } })); await drain(r.stream);
265 await unwrapAsync(listTree(R.url, {}, { tracer: new Tracer({ exporter: ex }), auth: { header: `Bearer ${token}` } }));
266 const dump = JSON.stringify(ex.lines);
267 for (const s of [token, btoa(`x-access-token:${token}`)]) assert.ok(!dump.includes(s), 'credential in spans');
268 const echo = http.createServer((req, res) => { res.writeHead(500, { 'content-type': 'text/plain' }); res.end(`you sent: ${req.headers.authorization} / decoded: ${Buffer.from(String(req.headers.authorization).split(' ')[1] ?? '', 'base64').toString()}`); });
269 await new Promise(ok => echo.listen(0, '127.0.0.1', ok));
270 try {
271 const url = `http://127.0.0.1:${echo.address().port}/x.git`;
272 for (const auth of [{ token }, { bearer: token }, { header: `Bearer ${token}` }, { header: `Basic ${btoa(`oauth2:${token}`)}` }]) {
273 const ex2 = arrayExporter();
274 const err = await unwrapAsync(readFile(url, { path: 'README.md' }, { tracer: new Tracer({ exporter: ex2 }), auth })).catch(e => e);
275 assert.equal(err.code, 'upstream_5xx'); assert.equal(err.status, 500);
276 assert.match(err.message, /\[redacted\]/);
277 const all = err.message + JSON.stringify(ex2.lines);
278 for (const s of [token, btoa(`x-access-token:${token}`), btoa(`oauth2:${token}`)]) assert.ok(!all.includes(s), `credential leaked via ${JSON.stringify(Object.keys(auth))}: ${err.message}`);
279 }
280 } finally { await new Promise(ok => echo.close(ok)); }
281});
283// ---- readCommit vs git log / git cat-file ----
285const SEP = '\x1f';
286const FMT = ['%H', '%T', '%P', '%an', '%ae', '%at', '%ad', '%cn', '%ce', '%ct', '%cd'].join(SEP);
287// what git says about `spec`: idents from `git log` (raw dates for epoch + tz, iso-strict in UTC for time), the message
288// and the signature header from `git cat-file commit`
289function gitCommit(bare, spec) {
290 const env = { ...process.env, TZ: 'UTC' };
291 const [oid, tree, parents, an, ae, at, ad, cn, ce, ct, cd] = git(bare, ['log', '-1', '--date=raw', `--format=${FMT}`, spec], { env }).replace(/\n$/, '').split(SEP);
292 const [aiso, ciso] = git(bare, ['log', '-1', '--date=iso-strict-local', `--format=%ad${SEP}%cd`, spec], { env }).trim().split(SEP).map(d => d.replace(/\+00:00$/, 'Z'));
293 const raw = gitBuf(bare, ['cat-file', 'commit', oid]).toString('utf8');
294 const cut = raw.indexOf('\n\n');
295 const header = raw.slice(0, cut);
296 return {
297 oid, tree, parents: parents ? parents.split(' ') : [],
298 author: { name: an, email: ae, time: aiso, epoch: Number(at), tz: ad.split(' ')[1] },
299 committer: { name: cn, email: ce, time: ciso, epoch: Number(ct), tz: cd.split(' ')[1] },
300 message: raw.slice(cut + 2), signed: /^gpgsig(-sha256)? /m.test(header),
301 };
302}
304test('parseIdent is git split_ident_line: name trimmed, email to the first >, date after the last >', () => {
305 assert.deepEqual(parseIdent('A U Thor <a@x> 1700000000 +0530'), { name: 'A U Thor', email: 'a@x', time: '1700000000', tz: '+0530' });
306 assert.deepEqual(parseIdent('Zoë <z@ë> 0 -1200'), { name: 'Zoë', email: 'z@ë', time: '0', tz: '-1200' });
307 assert.deepEqual(parseIdent('weird <a@x> trail> 5 +0000'), { name: 'weird', email: 'a@x', time: '5', tz: '+0000' });
308 assert.deepEqual(parseIdent('no email'), { name: 'no email', email: '', time: null, tz: '' });
309});
311test('readCommit == git log -1 / git cat-file commit: root, merge (2 parents), signed, non-ASCII author, verbatim multi-line message, tags, oid', async () => {
312 const bare = makeBareRepo(root, 'rc'); const work = join(root, 'wrc'); mkdirSync(work); initRepo(work);
313 const at = (author, committer, extra = {}) => ({ env: { ...process.env, GIT_AUTHOR_NAME: author[0], GIT_AUTHOR_EMAIL: author[1], GIT_AUTHOR_DATE: author[2], GIT_COMMITTER_NAME: committer[0], GIT_COMMITTER_EMAIL: committer[1], GIT_COMMITTER_DATE: committer[2], ...extra } });
314 const commit = (args, who) => git(work, ['commit', '-q', ...args], who);
315 writeFileSync(join(work, 'f.txt'), 'one\n'); git(work, ['add', '-A']);
316 commit(['-m', 'root'], at(['Root', 'root@x', '1000000000 +0000'], ['Root', 'root@x', '1000000000 +0000']));
317 const rootOid = git(work, ['rev-parse', 'HEAD']).trim();
318 git(work, ['tag', '-a', '-m', 'annotated', 'v1']); git(work, ['tag', 'light']);
319 // side: non-ASCII author and committer, odd offsets, a message kept verbatim (indentation, blank runs, no cleanup)
320 git(work, ['checkout', '-q', '-b', 'side']);
321 writeFileSync(join(work, 's.txt'), 'side\n'); git(work, ['add', '-A']);
322 const msg = 'Ünïcödé subject — 李雷\n\nbody line one\n indented line\n\n\n# not a comment here\ntrailing spaces \n\n';
323 writeFileSync(join(root, 'msg.txt'), msg);
324 commit(['--cleanup=verbatim', '-F', join(root, 'msg.txt')], at(['Zoë Ångström 李雷', 'zoë@exämple.org', '1700000000 +0530'], ['Ørjan Nüñez', 'ørjan@exämple.org', '1700003600 -0800']));
325 // main moves, then a --no-ff merge of side: two parents
326 git(work, ['checkout', '-q', 'main']);
327 writeFileSync(join(work, 'm.txt'), 'main\n'); git(work, ['add', '-A']);
328 commit(['-m', 'main moves'], at(['M', 'm@x', '1700007200 +1345'], ['M', 'm@x', '1700007200 +1345']));
329 git(work, ['merge', '-q', '--no-ff', '-m', "Merge branch 'side'", 'side'], at(['Merger', 'merge@x', '1700010800 -0330'], ['Merger', 'merge@x', '1700010800 -0330']));
330 // signed: a real SSH signature when ssh-keygen exists, else an OpenPGP-shaped gpgsig header written by hand
331 git(work, ['checkout', '-q', '-b', 'signed']);
332 writeFileSync(join(work, 'g.txt'), 'signed\n'); git(work, ['add', '-A']);
333 let how = 'ssh';
334 try {
335 const key = join(root, 'sign_key'); execFileSync('ssh-keygen', ['-q', '-t', 'ed25519', '-N', '', '-C', 'ply-test', '-f', key], { stdio: 'ignore' });
336 git(work, ['-c', 'gpg.format=ssh', '-c', `user.signingkey=${key}`, 'commit', '-q', '-S', '-m', 'signed commit\n\nwith a body'], at(['S', 's@x', '1700014400 +0100'], ['S', 's@x', '1700014400 +0100']));
337 } catch {
338 how = 'handmade';
339 const tree = git(work, ['write-tree']).trim(), parent = git(work, ['rev-parse', 'HEAD']).trim();
340 const obj = `tree ${tree}\nparent ${parent}\nauthor S <s@x> 1700014400 +0100\ncommitter S <s@x> 1700014400 +0100\ngpgsig -----BEGIN PGP SIGNATURE-----\n \n iQEzBAABCAAdFiEE\n =abcd\n -----END PGP SIGNATURE-----\n\nsigned commit\n\nwith a body\n`;
341 git(work, ['update-ref', 'refs/heads/signed', gitBuf(work, ['hash-object', '-t', 'commit', '-w', '--stdin'], obj).toString().trim()]);
342 }
343 git(work, ['push', '-q', bare, 'main', 'side', 'signed', 'refs/tags/v1', 'refs/tags/light']);
344 git(bare, ['symbolic-ref', 'HEAD', 'refs/heads/main']);
345 const url = `${srv.url}/rc.git`;
346 const sideOid = git(bare, ['rev-parse', 'side']).trim();
347 const cases = [[null, 'HEAD', 'refs/heads/main'], ['main', 'main', 'refs/heads/main'], ['refs/heads/main', 'main', 'refs/heads/main'], ['side', 'side', 'refs/heads/side'], ['signed', 'signed', 'refs/heads/signed'],
348 ['v1', 'v1^{commit}', 'refs/tags/v1'], ['refs/tags/light', 'light', 'refs/tags/light'], [rootOid, rootOid, rootOid], [sideOid, sideOid, sideOid]];
349 for (const [ref, spec, name] of cases) {
350 const got = await unwrapAsync(readCommit(url, { ref }, { tracer: tracer() }));
351 const { ref: gotRef, ...rest } = got;
352 assert.equal(gotRef, name, String(ref));
353 assert.deepEqual(rest, gitCommit(bare, spec), `${ref}: readCommit != git`);
354 }
355 // the properties each case was built for, stated independently of the oracle
356 const merge = await unwrapAsync(readCommit(url, {}, { tracer: tracer() }));
357 assert.equal(merge.parents.length, 2); assert.deepEqual(merge.parents, [git(bare, ['rev-parse', 'main^1']).trim(), sideOid]); assert.equal(merge.signed, false);
358 assert.equal(merge.author.tz, '-0330'); assert.equal(merge.author.time, '2023-11-15T01:13:20Z'); assert.equal(merge.author.epoch, 1700010800);
359 const side = await unwrapAsync(readCommit(url, { ref: 'side' }, { tracer: tracer() }));
360 assert.equal(side.message, msg); assert.deepEqual(side.author, { name: 'Zoë Ångström 李雷', email: 'zoë@exämple.org', time: '2023-11-14T22:13:20Z', epoch: 1700000000, tz: '+0530' });
361 assert.equal(side.committer.name, 'Ørjan Nüñez'); assert.equal(side.committer.tz, '-0800');
362 const signed = await unwrapAsync(readCommit(url, { ref: 'signed' }, { tracer: tracer() }));
363 assert.equal(signed.signed, true, how); assert.equal(signed.message, 'signed commit\n\nwith a body\n');
364 const r0 = await unwrapAsync(readCommit(url, { ref: 'v1' }, { tracer: tracer() }));
365 assert.deepEqual(r0.parents, []); assert.equal(r0.oid, rootOid);
366 // not_found: missing ref, unknown oid, the oid of a tree; bad ref is bad_request
367 await assert.rejects(unwrapAsync(readCommit(url, { ref: 'nope' }, { tracer: tracer() })), e => e.code === 'not_found');
368 await assert.rejects(unwrapAsync(readCommit(url, { ref: 'f'.repeat(40) }, { tracer: tracer() })), e => e.code === 'not_found');
369 await assert.rejects(unwrapAsync(readCommit(url, { ref: git(bare, ['rev-parse', 'main^{tree}']).trim() }, { tracer: tracer() })), e => e.code === 'not_found');
370 await assert.rejects(unwrapAsync(readCommit(url, { ref: 'a..b' }, { tracer: tracer() })), e => e.code === 'bad_request');
371 console.log(` ${cases.length} refs equal to git (signature: ${how})`);
372});
374test('readCommit fetches the commit object only: one tree:0 deepen-1 fetch, no tree fetches', async () => {
375 const ex = arrayExporter();
376 await unwrapAsync(readCommit(R.url, { ref: 'main' }, { tracer: new Tracer({ exporter: ex }) }));
377 const spans = ex.lines.flatMap(flattenLine);
378 const fetches = spans.filter(s => s.name === 'git.upload-pack fetch');
379 assert.equal(fetches.length, 1);
380 assert.equal(fetches[0].attrs['git.object.type'], 'commit');
381 assert.equal(fetches[0].attrs['git.fetch.filter'], 'tree:0'); assert.equal(fetches[0].attrs['git.fetch.deepen'], 1);
382 assert.ok(!spans.some(s => s.name === 'git.read.tree'));
383});
385test('abbreviated oids: unique among advertised tips agrees with git, and a ref name wins', async () => {
386 const bare = makeBareRepo(root, 'abbrev');
387 const work = join(root, 'abbrev-work');
388 mkdirSync(work);
389 initRepo(work);
390 const empty = '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
391 const oids = [];
392 const bucket = (n) => {
393 const m = new Map();
394 for (const oid of oids) {
395 const k = oid.slice(0, n);
396 const g = m.get(k);
397 if (g) g.push(oid); else m.set(k, [oid]);
398 }
399 return m;
400 };
401 let parent = null;
402 for (let i = 0; i < 4000; i++) {
403 const args = ['-c', 'user.name=seed', '-c', 'user.email=seed@x', '-c', 'commit.gpgsign=false', 'commit-tree', empty, '-m', String(i)];
404 if (parent) args.push('-p', parent);
405 parent = git(work, args).trim();
406 oids.push(parent);
407 if ([...bucket(4).values()].some(v => v.length >= 2) && [...bucket(5).values()].some(v => v.length >= 2) && oids.length > 64) break;
408 }
409 const g4 = bucket(4);
410 const g5 = bucket(5);
411 const pair4 = [...g4.entries()].find(([, v]) => v.length >= 2);
412 const pair5 = [...g5.entries()].find(([, v]) => v.length >= 2);
413 const singles = [...g4.entries()].filter(([, v]) => v.length === 1);
414 assert.ok(pair4 && pair5 && singles.length >= 4, `no shared prefix in ${oids.length} commits`);
415 const used = new Set([...pair4[1], ...pair5[1]]);
416 const free = singles.filter(([, v]) => !used.has(v[0]));
417 assert.ok(free.length >= 4, `free singles ${free.length}`);
418 const [uniquePrefix, [uniqueOid]] = free[0];
419 const [namePrefix, [nameOid]] = free[1];
420 const nameTarget = free[2][1][0];
421 const [interiorPrefix, [interiorOid]] = free[3];
422 git(work, ['update-ref', 'refs/heads/main', parent]);
423 git(work, ['push', '-q', bare, 'refs/heads/main']);
424 const point = (ref, oid) => git(bare, ['update-ref', ref, oid]);
425 point('refs/heads/main', uniqueOid);
426 point('refs/heads/a', pair4[1][0]);
427 point('refs/heads/b', pair4[1][1]);
428 point('refs/heads/c', pair5[1][0]);
429 point('refs/heads/d', pair5[1][1]);
430 point('refs/heads/hold', nameOid);
431 point(`refs/heads/${namePrefix}`, nameTarget);
432 git(bare, ['-c', 'user.name=seed', '-c', 'user.email=seed@x', '-c', 'commit.gpgsign=false', '-c', 'tag.gpgsign=false', 'tag', '-a', '-m', 'ann', 'v1', uniqueOid]);
433 const tagOid = git(bare, ['rev-parse', 'v1']).trim();
434 let up = uniquePrefix;
435 if (tagOid.startsWith(up)) {
436 for (let n = 5; n < 40 && tagOid.startsWith(uniqueOid.slice(0, n)); n++) up = uniqueOid.slice(0, n + 1);
437 }
438 const url = `${srv.url}/abbrev.git`;
439 const t = () => createTransport(url, { tracer: tracer() });
440 const disambiguate = prefix => git(bare, ['rev-parse', `--disambiguate=${prefix}`]).trim().split('\n').filter(Boolean);
441 const advertised = git(bare, ['for-each-ref', '--format=%(objectname) %(objecttype) %(*objectname)']).trim().split('\n').flatMap(l => l.split(' ').filter(x => /^[0-9a-f]{40}$/.test(x)));
442 const expectOids = prefix => [...new Set(advertised.filter(o => o.startsWith(prefix)))].sort();
443 // a ref named like the prefix wins (git warns that the name is ambiguous, and still answers the ref)
444 const named = await unwrapAsync(resolveCommit(t(), namePrefix, { abbrev: 'advertised' }));
445 assert.deepEqual(named, { name: `refs/heads/${namePrefix}`, commit: nameTarget });
446 const rev = spawnSync('git', ['rev-parse', namePrefix], { cwd: bare, encoding: 'utf8' });
447 assert.equal(rev.status, 0);
448 assert.match(`${rev.stderr}`, /ambiguous/);
449 assert.equal(rev.stdout.trim(), nameTarget);
450 assert.equal(git(bare, ['rev-parse', `${namePrefix}^{commit}`]).trim(), nameTarget);
451 // unique among advertised tips: git's candidates include that commit
452 const unique = await unwrapAsync(resolveCommit(t(), up, { abbrev: 'advertised' }));
453 assert.deepEqual(unique, { name: uniqueOid, commit: uniqueOid });
454 const uniqueObjs = disambiguate(up);
455 assert.ok(uniqueObjs.includes(uniqueOid), uniqueObjs.join(' '));
456 assert.equal(git(bare, ['cat-file', '-t', uniqueOid]).trim(), 'commit');
457 const listed = await lsRefs(t(), ['HEAD', 'refs/heads/', 'refs/tags/']);
458 const peeled = resolveAbbrev(up, listed);
459 assert.equal(peeled.ok && peeled.value.kind, 'unique_among_advertised');
460 if (peeled.ok && peeled.value.kind === 'unique_among_advertised') {
461 assert.equal(peeled.value.oid, uniqueOid);
462 assert.ok(peeled.value.refs.includes('refs/tags/v1'));
463 }
464 // two advertised tips: ply is ambiguous, and each oid is one of git's
465 for (const prefix of [pair4[0], pair5[0]]) {
466 const want = expectOids(prefix);
467 assert.ok(want.length >= 2, prefix);
468 await assert.rejects(unwrapAsync(resolveCommit(t(), prefix, { abbrev: 'advertised' })), e => {
469 assert.equal(e.code, 'bad_request');
470 assert.deepEqual([...e.got].sort(), want);
471 return true;
472 });
473 const objs = disambiguate(prefix);
474 for (const oid of want) assert.ok(objs.includes(oid), `${prefix} ${oid}`);
475 }
476 // git's only object for this prefix is an interior commit: ply does not call it ambiguous
477 assert.deepEqual(disambiguate(interiorPrefix), [interiorOid]);
478 await assert.rejects(unwrapAsync(resolveCommit(t(), interiorPrefix, { abbrev: 'advertised' })), e => e.code === 'not_found');
479 // default off: the same prefix is a ref name that does not exist, not an object id
480 await assert.rejects(unwrapAsync(resolveCommit(t(), up)), e => e.code === 'not_found');
481 console.log(` abbrev oracle over ${oids.length} commits`);
482});
484test('openBlob and readBlob match git cat-file; too_big is the object header', async () => {
485 // two near-copies, so git stores one as a delta. openBlob asks for one oid and sends no have, so upload-pack has no
486 // in-pack base and sends the object whole. The bytes are still cat-file's.
487 git(R.work, ['checkout', '-q', '-B', 'deltas', 'main']);
488 const base = Buffer.alloc(4000, 0x61);
489 const near = Buffer.from(base); near[20] = 0x62;
490 writeFileSync(join(R.work, 'delta-a.bin'), base);
491 writeFileSync(join(R.work, 'delta-b.bin'), near);
492 git(R.work, ['add', 'delta-a.bin', 'delta-b.bin']);
493 git(R.work, ['-c', 'user.name=seed', '-c', 'user.email=seed@x', 'commit', '-q', '-m', 'deltas']);
494 git(R.work, ['push', '-q', R.bare, 'HEAD:refs/heads/deltas']);
495 git(R.work, ['checkout', '-q', 'main']);
496 git(R.bare, ['repack', '-a', '-d', '-f']);
497 const packs = readdirSync(join(R.bare, 'objects', 'pack')).filter(n => n.endsWith('.pack'));
498 const listed = packs.map(n => git(R.bare, ['verify-pack', '-v', join(R.bare, 'objects', 'pack', n)])).join('\n');
499 const deltified = listed.split('\n').filter(l => /\sblob\s.*\s[0-9a-f]{40}$/.test(l)).map(l => l.slice(0, 40));
500 assert.ok(deltified.length >= 1, 'the fixture pack stores a blob as a delta');
501 const specs = ['empty.txt', 'README.md', 'run.sh', 'big.bin', 'deltas:delta-a.bin', 'deltas:delta-b.bin'];
502 for (const spec of specs) {
503 const oid = git(R.work, ['rev-parse', `${spec.includes(':') ? spec : `main:${spec}`}`]).trim();
504 const size = Number(git(R.work, ['cat-file', '-s', oid]));
505 const bytes = gitBuf(R.work, ['cat-file', 'blob', oid]);
506 assert.equal(bytes.length, size);
507 const ex = arrayExporter(); const tr = new Tracer({ exporter: ex });
508 const capped = await openBlob(createTransport(R.url, { tracer: tr }), oid, { maxBytes: Math.max(0, size - 1), follows: tr.start('cap') });
509 if (size === 0) {
510 assert.equal(capped.ok && capped.value.kind, 'stream', spec);
511 if (capped.ok && capped.value.kind === 'stream') assert.equal(capped.value.size, 0);
512 } else {
513 assert.equal(capped.ok, true, spec);
514 if (capped.ok) assert.deepEqual(capped.value, { kind: 'too_big', size });
515 const span = ex.lines.flatMap(flattenLine).find(s => s.name === 'git.read.blob');
516 assert.equal(span.attrs['git.read.blob.header_only'], true, spec);
517 assert.equal(span.attrs['git.read.blob.bytes'], undefined, spec);
518 }
519 const got = await readBlob(createTransport(R.url, { tracer: tracer() }), oid, { maxBytes: size });
520 assert.equal(got.ok, true, spec);
521 if (got.ok && got.value.kind === 'bytes') assert.deepEqual(Buffer.from(got.value.data), bytes);
522 else assert.fail(`${spec} was ${got.ok ? got.value.kind : got.error.code}`);
523 }
524 assert.ok(specs.map(spec => git(R.work, ['rev-parse', `${spec.includes(':') ? spec : `main:${spec}`}`]).trim()).some(oid => deltified.includes(oid)));
525});
Diagnostics4 git calls · 525 ms
CallStatusSizeTime0525 ms
discover200152 B4 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=4 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 · hit6 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=6 git.response.bytes=145 git.response.cache=HIT git.response.duration_ms=6 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=55Open pack map
fetch20011 KiB517 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=517 git.response.bytes=11024 git.response.cache=MISS git.response.duration_ms=517 git.fetch.wants=791c5625bbb73245d5acfdd2860d3cfaa5aabc45 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=11024 git.pkt.payload.max=11003 git.sideband.band1.bytes=11002 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
- a5e2757deb16d806
- Request
- dcaa2e13…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format