GitHubivarvong
ply
main
1// Mutation tests for pushTree (tree.js), checkoutTree (checkout.js) and readFile/listTree/readCommit (read.js), without a
2// git server. The pure steps are asserted directly; the operations run against `remote()`, an in-process fake that
3// speaks the transport contract (transport.js: `frames(service, body)`), answers ls-refs, v2 fetch (tree:0, blob:none,
4// plain wants) and receive-pack (report-status over side-band-64k), and builds every response with ply's own codecs.
5// Tree and commit oids that matter are git's (`git write-tree` / `git commit-tree` over the same files), not ply's.
6// The integration suites (push-tree, checkout-tree, read) judge the same operations against git http-backend.
7import { test } from 'vitest';
8import assert from 'node:assert/strict';
9import { fileEntries, buildTree, selectObjects, validateTreeSpec, pushTree } from '../src/tree.js';
10import { editFile, lookupEntry } from '../src/edit.js';
11import { canonMode, treeFiles, checkoutTree } from '../src/checkout.js';
12import { refCandidates, entryType, lsMode, resolveCommit, resolveAbbrev, commitView, readCommit, listTree, readFile, openBlob, readBlob, fetchCommit, walkPath } from '../src/read.js';
13import { hashObject, encodeTree, encodeCommit, encodeTag, parseTree, parseCommit, TYPE, TYPE_ID } from '../src/objects.js';
14import { buildPack, encodeObjHeader, zlibDeflate } from '../src/pack.js';
15import { parsePack, resolvePack, makeDelta } from '../src/packresolve.js';
16import { pkts } from '../src/pktline.js';
17import { te, td, concat, bytesOf } from '../src/bytes.js';
18import { Sha1 } from '../src/sha1.js';
19import { ZERO_OID } from '../src/push.js';
20import { DEFAULT_LIMITS, LIMIT_OBJECT_OVERHEAD as OV, withLimits } from '../src/limits.js';
21import { plyError as fail } from '../src/errors.js';
22import { Tracer, arrayExporter, flattenLine } from '../src/otel.js';
23import { unwrap, unwrapAsync } from './unwrap.js';
24import { ok } from '../src/result.js';
26const GITLINK = 'c0ffee'.padEnd(40, '0');
27const A = { name: 'A', email: 'a@x' };
28const T0 = 1700000000;
29// git write-tree / commit-tree over FILES (author and committer "A <a@x> 1700000000 +0000", message "deploy")
30const FILES = () => ({
31 README: 'hello\n', 'src/a.js': 'a\n', 'src/lib/b.js': 'b\n',
32 'run.sh': { data: '#!/bin/sh\n', mode: '100755' }, link: { mode: '120000', data: 'README' }, sub: { mode: '160000', oid: GITLINK },
33});
34const GIT = {
35 root: '516560e7fe0766acc830fce50b4f6bcf3b35bb83', src: 'e094828ddb490f7c7fc78031e31089f3156da7cd', lib: '365e3dfff5383dbc31ea16336fa73ff0dc2bb025',
36 readme: 'ce013625030ba8dba906f756967f9e9ca394464a', link: '100b93820ade4c16225673b4ca62bb3ade63c313', run: '1a2485251c33a70432394c93fb89330ef214bfc9',
37 a: '78981922613b2afb6025042ff6bd878ac1994e85', b: '61780798228d17af2d34fce4cfbdf35556832472', commit: '217d4d2b52dc2f2156e5e88d5340e9e9b97f83fb',
38 // src/a.js → 'A\n': the new root and src trees, and the child commit of `commit`
39 root2: 'b6a71e2069a3b261745147e78ccd34bde142c998', src2: 'baa4b47b50ca112598753de9675c52e757514321', commit2: '08b2316ec8417327ce57e304a333cf880b8ceac5',
40};
41const EMPTY_TREE = '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
42const EMPTY_BLOB = 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391';
44const rejects = (p, code, message, extra = {}) => assert.rejects(p, e => {
45 assert.equal(e.code, code, e.message);
46 if (message instanceof RegExp) assert.match(e.message, message); else if (message !== undefined) assert.equal(e.message, message);
47 for (const [k, v] of Object.entries(extra)) assert.deepEqual(e[k], v, k);
48 return true;
49});
50const throws = (fn, code, message, extra = {}) => assert.throws(fn, e => {
51 assert.equal(e.code, code, e.message);
52 if (message !== undefined) assert.equal(e.message, message);
53 for (const [k, v] of Object.entries(extra)) assert.deepEqual(e[k], v, k);
54 return true;
55});
56const readAll = async stream => { const parts = []; for await (const c of stream) parts.push(c); return td.decode(concat(parts)); };
57const streamOf = r => { assert.equal(r.ok, true, r.ok ? '' : r.error?.message); if (!r.ok || r.value.kind !== 'stream') throw new Error('expected a stream'); return r.value; };
58const errOf = r => { assert.equal(r.ok, false); if (r.ok) throw new Error('expected an error'); return r.error; };
60// ---- the fake remote ----
62// pkt-lines of a request body; receive-pack stops at the first flush and the rest is the pack
63function requestLines(body, { stopAtFlush = false } = {}) {
64 const lines = []; let p = 0;
65 while (p < body.length) {
66 const n = parseInt(td.decode(body.subarray(p, p + 4)), 16);
67 if (n === 0) { p += 4; if (stopAtFlush) break; continue; }
68 if (n === 1) { p += 4; continue; }
69 lines.push(td.decode(body.subarray(p + 4, p + n)).replace(/\n$/, '')); p += n;
70 }
71 return { lines, rest: body.subarray(p) };
72}
73const dataFrame = s => ({ kind: 'data', payload: bytesOf(s) });
74const band = (n, bytes) => ({ kind: 'data', payload: concat([Uint8Array.of(n), bytes]) });
75const packFrames = (pack, chunk = 1 << 16) => {
76 const out = [dataFrame('packfile\n')];
77 for (let i = 0; i < pack.length; i += chunk) out.push(band(1, pack.subarray(i, i + chunk)));
78 return [...out, { kind: 'flush' }];
79};
81function remote({ head = 'refs/heads/main', chunk = 1 << 16, hook = null } = {}) {
82 const objects = new Map(), refs = new Map(), log = [];
83 const put = (typeName, data) => { data = bytesOf(data); const oid = hashObject(typeName, data); objects.set(oid, { type: TYPE_ID[typeName], data }); return oid; };
84 const commit = (files, { ref = 'refs/heads/main', parents = [], time = T0, message = 'deploy\n' } = {}) => {
85 const built = unwrap(buildTree(unwrap(fileEntries(files))));
86 for (const tr of built.trees.values()) objects.set(tr.oid, { type: TYPE.TREE, data: tr.data });
87 for (const [oid, data] of built.blobs) objects.set(oid, { type: TYPE.BLOB, data });
88 const who = `A <a@x> ${time} +0000`;
89 const oid = put('commit', unwrap(encodeCommit({ tree: built.root, parents, author: who, committer: who, message })));
90 if (ref) refs.set(ref, oid);
91 return oid;
92 };
93 const trees = (oid, out) => {
94 if (out.has(oid)) return out; out.add(oid);
95 for (const e of unwrap(parseTree(objects.get(oid).data))) if (e.mode === '40000') trees(e.oid, out);
96 return out;
97 };
98 const select = (want, filter) => {
99 const o = objects.get(want);
100 if (filter !== 'blob:none' || o.type === TYPE.BLOB) return [want];
101 return [...trees(o.type === TYPE.COMMIT ? unwrap(parseCommit(o.data)).tree : want, new Set(o.type === TYPE.COMMIT ? [want] : []))];
102 };
103 async function* serve(service, body) {
104 if (service === 'git-receive-pack') {
105 const { lines, rest } = requestLines(body, { stopAtFlush: true });
106 const commands = lines.map(l => { const [old, nu, ref] = l.split('\0')[0].split(' '); return { old, new: nu, ref }; });
107 const req = { service, commands, pack: rest.length ? unwrap(parsePack(rest)) : null };
108 log.push(req);
109 if (hook) { const r = await hook(req); if (r) { yield* r; return; } }
110 if (req.pack) for (const [oid, o] of unwrap(resolvePack(req.pack, b => objects.get(b))).objects) objects.set(oid, { type: o.type, data: o.data });
111 const report = ['unpack ok'];
112 for (const c of commands) {
113 if ((refs.get(c.ref) ?? ZERO_OID) !== c.old) { report.push(`ng ${c.ref} fetch first`); continue; }
114 if (c.new === ZERO_OID) refs.delete(c.ref); else refs.set(c.ref, c.new);
115 report.push(`ok ${c.ref}`);
116 }
117 yield band(1, unwrap(pkts(report.map(l => l + '\n'))));
118 yield { kind: 'flush' };
119 return;
120 }
121 const { lines } = requestLines(body);
122 const arg = k => lines.filter(l => l.startsWith(`${k} `)).map(l => l.slice(k.length + 1));
123 if (lines[0] === 'command=ls-refs') {
124 const req = { service, command: 'ls-refs', prefixes: arg('ref-prefix'), unborn: lines.includes('unborn') };
125 log.push(req);
126 if (hook) { const r = await hook(req); if (r) { yield* r; return; } }
127 const match = name => !req.prefixes.length || req.prefixes.some(p => name.startsWith(p));
128 const line = (name, oid, extra = '') => { const o = objects.get(oid); const peeled = o?.type === TYPE.TAG ? ` peeled:${td.decode(o.data).slice(7, 47)}` : ''; return dataFrame(`${oid} ${name}${extra}${peeled}\n`); };
129 if (match('HEAD')) {
130 if (typeof head === 'string' && head.startsWith('refs/') && refs.has(head)) yield line('HEAD', refs.get(head), ` symref-target:${head}`);
131 else if (head && !head.startsWith('refs/')) yield line('HEAD', head);
132 }
133 for (const [name, oid] of refs) if (match(name)) yield line(name, oid);
134 yield { kind: 'flush' };
135 return;
136 }
137 const req = { service, command: 'fetch', wants: arg('want'), filter: arg('filter')[0] ?? null, deepen: Number(arg('deepen')[0] ?? 0) };
138 log.push(req);
139 if (hook) { const r = await hook(req); if (r) { yield* r; return; } }
140 const missing = req.wants.find(w => !objects.has(w));
141 if (missing) { yield dataFrame(`ERR upload-pack: not our ref ${missing}`); return; }
142 const send = [...new Set(req.wants.flatMap(w => select(w, req.filter)))];
143 const { pack } = await buildPack(send.map(oid => objects.get(oid)));
144 yield* packFrames(pack, chunk);
145 }
146 const transport = ({ tracer, limits = DEFAULT_LIMITS, quirks = {} }) => ({
147 kind: 'http', url: 'http://fake/r.git', tracer, parent: null, limits, signal: null, quirks, objects: new Map(), scrub: null,
148 frames: (service, body) => serve(service, body),
149 // A ref miss asks discover() before it classifies the miss. No `unborn` feature: no second ls-refs, HEAD stays head_absent.
150 advertise: async () => pkts(['version 2\n', 'ls-refs\n', 'fetch\n', 'object-format=sha1\n']),
151 });
152 return { objects, refs, log, put, commit, transport, fetches: () => log.filter(r => r.command === 'fetch'), pushes: () => log.filter(r => r.service === 'git-receive-pack') };
153}
155// an operation's options over the fake: { opts, spans(), span(name) }
156function env(r, { limits = DEFAULT_LIMITS, quirks = {} } = {}) {
157 const ex = arrayExporter(); const tracer = new Tracer({ exporter: ex });
158 const transport = r.transport({ tracer, limits, quirks });
159 const spans = () => ex.lines.flatMap(flattenLine);
160 return { opts: { tracer, transport, limits }, transport, spans, span: name => spans().find(s => s.name === name), all: name => spans().filter(s => s.name === name) };
161}
163// ======================================================================================================================
164// tree.js — pure steps
165// ======================================================================================================================
167test('fileEntries: the three input shapes, defaults and git blob oids', () => {
168 const fromObject = unwrap(fileEntries({ 'a/b.txt': 'hello\n', bin: Uint8Array.of(0, 1), x: { data: 'ex', mode: '100755' } }));
169 assert.deepEqual(fromObject.map(e => [e.path, e.comps, e.mode, e.oid]), [
170 ['a/b.txt', ['a', 'b.txt'], '100644', GIT.readme],
171 ['bin', ['bin'], '100644', hashObject('blob', Uint8Array.of(0, 1))],
172 ['x', ['x'], '100755', hashObject('blob', te.encode('ex'))],
173 ]);
174 assert.deepEqual([...fromObject[0].data], [...te.encode('hello\n')]);
175 const map = unwrap(fileEntries(new Map([['m', 'hello\n']])));
176 assert.deepEqual(map.map(e => [e.path, e.mode, e.oid]), [['m', '100644', GIT.readme]]);
177 const list = unwrap(fileEntries([{ path: 'l', data: '' }, { path: 'g', mode: '160000', oid: GITLINK }]));
178 assert.deepEqual(list[0].oid, EMPTY_BLOB);
179 assert.deepEqual(list[1], { path: 'g', comps: ['g'], mode: '160000', oid: GITLINK });
180 assert.equal('data' in list[1], false);
181 assert.deepEqual(unwrap(fileEntries({})), []);
182 assert.deepEqual(unwrap(fileEntries([])), []);
183 assert.deepEqual(unwrap(fileEntries({ s: { mode: '120000', data: 'target' } }))[0].oid, hashObject('blob', te.encode('target')));
184});
186test('fileEntries: every refusal, with its code, message and what', () => {
187 for (const bad of [null, undefined, 'files', 7]) throws(() => unwrap(fileEntries(bad)), 'bad_request', 'files must be an object or an iterable of { path, mode, data }', { what: 'files' });
188 throws(() => unwrap(fileEntries({ g: { mode: '160000', oid: 'xyz' } })), 'bad_request', 'g: a gitlink (160000) takes a 40-hex oid and no data', { what: 'gitlink', path: 'g' });
189 throws(() => unwrap(fileEntries({ g: { mode: '160000' } })), 'bad_request', 'g: a gitlink (160000) takes a 40-hex oid and no data', { what: 'gitlink' });
190 throws(() => unwrap(fileEntries({ g: { mode: '160000', oid: GITLINK.toUpperCase() } })), 'bad_request', undefined, { what: 'gitlink' });
191 throws(() => unwrap(fileEntries({ g: { mode: '160000', oid: `${GITLINK}0` } })), 'bad_request', undefined, { what: 'gitlink' });
192 throws(() => unwrap(fileEntries({ g: { mode: '160000', oid: `x${GITLINK}` } })), 'bad_request', undefined, { what: 'gitlink' });
193 throws(() => unwrap(fileEntries({ g: { mode: '160000', oid: GITLINK, data: '' } })), 'bad_request', undefined, { what: 'gitlink' });
194 for (const mode of ['100664', '40000', '644', 100644, '']) throws(() => unwrap(fileEntries({ f: { mode, data: 'x' } })), 'bad_request', `f: mode ${JSON.stringify(mode)} is not 100644, 100755, 120000 or 160000`, { what: 'mode', path: 'f' });
195 for (const data of [undefined, 5, null, [1]]) throws(() => unwrap(fileEntries([{ path: 'f', data }])), 'bad_request', 'f: data must be a string or Uint8Array', { what: 'data', path: 'f' });
196 throws(() => unwrap(fileEntries({ l: { mode: '120000', data: '' } })), 'bad_request', 'l: a symlink target must be non-empty and contain no NUL', { what: 'symlink', path: 'l' });
197 throws(() => unwrap(fileEntries({ l: { mode: '120000', data: 'a\0b' } })), 'bad_request', undefined, { what: 'symlink' });
198 throws(() => unwrap(fileEntries({ l: { mode: '120000', data: Uint8Array.of(0) } })), 'bad_request', undefined, { what: 'symlink' });
199 // a NUL is fine in a regular file, and an empty file is git's empty blob
200 assert.equal(unwrap(fileEntries({ f: 'a\0b', e: '' })).length, 2);
201 throws(() => unwrap(fileEntries({ '../x': 'y' })), 'bad_request', undefined, { what: 'path' });
202 throws(() => unwrap(fileEntries([null])), 'bad_request', undefined, { what: 'path' });
203 throws(() => unwrap(fileEntries([{ data: 'x' }])), 'bad_request', undefined, { what: 'path' });
204});
206test('fileEntries: maxPackObjects and maxObjectSize bound exactly', () => {
207 const two = withLimits({ maxPackObjects: 2 });
208 assert.equal(unwrap(fileEntries({ a: '1', b: '2' }, two)).length, 2);
209 throws(() => unwrap(fileEntries({ a: '1', b: '2', c: '3' }, two)), 'limit_exceeded', 'files 3 exceeds limit 2', { what: 'files', got: 3, max: 2 });
210 const four = withLimits({ maxObjectSize: 4 });
211 assert.equal(unwrap(fileEntries({ a: 'abcd' }, four))[0].data.length, 4);
212 throws(() => unwrap(fileEntries({ a: 'abcde' }, four)), 'limit_exceeded', 'object size 5 exceeds limit 4', { what: 'object size', got: 5, max: 4 });
213 // a gitlink has no data, so the size cap never applies to it
214 assert.equal(unwrap(fileEntries({ g: { mode: '160000', oid: GITLINK } }, withLimits({ maxObjectSize: 0 }))).length, 1);
215 throws(() => unwrap(fileEntries({ a: 'x' }, { ...DEFAULT_LIMITS, maxPackObjects: null })), 'bad_request', 'limits.maxPackObjects must be a finite non-negative number');
216 throws(() => unwrap(fileEntries({ a: 'x' }, { ...DEFAULT_LIMITS, maxObjectSize: null })), 'bad_request', 'limits.maxObjectSize must be a finite non-negative number');
217 // a key left out is the default (#156 parseLimits)
218 assert.equal(unwrap(fileEntries({ a: 'x' }, { ...DEFAULT_LIMITS, maxPackObjects: undefined })).length, 1);
219 assert.equal(unwrap(fileEntries({ a: 'x' }, {})).length, 1);
220});
222test('buildTree: git\'s tree oids, every directory, blobs by oid', () => {
223 const built = unwrap(buildTree(unwrap(fileEntries(FILES()))));
224 assert.equal(built.root, GIT.root);
225 assert.deepEqual([...built.trees.keys()].sort(), ['', 'src', 'src/lib']);
226 assert.equal(built.trees.get('src').oid, GIT.src);
227 assert.equal(built.trees.get('src/lib').oid, GIT.lib);
228 assert.equal(built.trees.get('').oid, GIT.root);
229 assert.equal(hashObject('tree', built.trees.get('src').data), GIT.src);
230 assert.deepEqual(built.trees.get('src').entries.map(e => [e.mode, e.name, e.oid]), [['100644', 'a.js', GIT.a], ['40000', 'lib', GIT.lib]]);
231 assert.deepEqual(built.trees.get('').entries.map(e => e.name).sort(), ['README', 'link', 'run.sh', 'src', 'sub']);
232 // blobs: every file's content once by oid; the gitlink names a commit that is not here
233 assert.deepEqual([...built.blobs.keys()].sort(), [GIT.readme, GIT.link, GIT.run, GIT.a, GIT.b].sort());
234 assert.equal(td.decode(built.blobs.get(GIT.b)), 'b\n');
235 const none = unwrap(buildTree([]));
236 assert.equal(none.root, EMPTY_TREE);
237 assert.equal(none.trees.size, 1);
238 assert.equal(none.blobs.size, 0);
239 // an empty file is a blob too (an empty Uint8Array is truthy)
240 assert.deepEqual([...unwrap(buildTree(unwrap(fileEntries({ e: '' })))).blobs.keys()], [EMPTY_BLOB]);
241 // deep before shallow: a four-level chain still encodes
242 const deep = unwrap(buildTree(unwrap(fileEntries({ 'a/b/c/d': 'x', 'a/y': 'z' }))));
243 assert.equal(deep.trees.size, 4);
244 assert.equal(unwrap(parseTree(deep.trees.get('a/b').data))[0].oid, deep.trees.get('a/b/c').oid);
245});
247test('buildTree: file/directory clashes, duplicates and maxTreeEntries', () => {
248 const clash = files => () => unwrap(buildTree(unwrap(fileEntries(files))));
249 throws(clash([{ path: 'x/a', data: '1' }, { path: 'x/a/b', data: '2' }]), 'bad_request', 'x/a is both a file and a directory', { what: 'path', path: 'x/a/b' });
250 throws(clash([{ path: 'x/a/b', data: '1' }, { path: 'x/a', data: '2' }]), 'bad_request', 'x/a is both a file and a directory', { what: 'path', path: 'x/a' });
251 throws(clash([{ path: 'x/a', data: '1' }, { path: 'x/a', data: '2' }]), 'bad_request', 'x/a is given twice', { what: 'path', path: 'x/a' });
252 throws(clash([{ path: 'a', data: '1' }, { path: 'a/b/c', data: '2' }]), 'bad_request', 'a is both a file and a directory', { path: 'a/b/c' });
253 // two files under one new directory share it
254 assert.equal(unwrap(buildTree(unwrap(fileEntries({ 'd/a': '1', 'd/b': '2' })))).trees.get('d').entries.length, 2);
255 const two = withLimits({ maxTreeEntries: 2 });
256 assert.equal(unwrap(buildTree(unwrap(fileEntries({ a: '1', b: '2', 'c/d': '3', 'c/e': '4' })), withLimits({ maxTreeEntries: 3 }))).trees.size, 2);
257 assert.equal(unwrap(buildTree(unwrap(fileEntries({ a: '1', b: '2' })), two)).trees.get('').entries.length, 2);
258 throws(() => unwrap(buildTree(unwrap(fileEntries({ a: '1', b: '2', c: '3' })), two)), 'limit_exceeded', 'tree entries 3 exceeds limit 2', { what: 'tree entries', got: 3, max: 2 });
259 throws(() => unwrap(buildTree([], { maxTreeEntries: -1 })), 'bad_request', 'limits.maxTreeEntries must be a finite non-negative number');
260 assert.equal(unwrap(buildTree([], {})).root, '4b825dc642cb6eb9a060e54bf8d69288fbee4904');
261});
263test('selectObjects: walk order, dedupe, what the server has, gitlinks', () => {
264 const built = unwrap(buildTree(unwrap(fileEntries(FILES()))));
265 const fresh = selectObjects(built);
266 assert.deepEqual(fresh.trees.map(t => hashObject('tree', t.data)), [GIT.root, GIT.src, GIT.lib]);
267 assert.ok(fresh.trees.every(t => t.type === TYPE.TREE && t.base === undefined));
268 // blobs follow the walk over each directory's entries as given (README, src/a.js, src/lib/b.js, run.sh, link, sub)
269 assert.deepEqual(fresh.blobs.map(b => hashObject('blob', b.data)), [GIT.readme, GIT.a, GIT.b, GIT.run, GIT.link]);
270 assert.ok(fresh.blobs.every(b => b.type === TYPE.BLOB));
271 assert.equal(fresh.deltas, 0);
272 // the server has src/lib (so its blob too) and README's blob
273 const known = selectObjects(built, { have: new Set([GIT.lib, GIT.readme]) });
274 assert.deepEqual(known.trees.map(t => hashObject('tree', t.data)), [GIT.root, GIT.src]);
275 assert.deepEqual(known.blobs.map(b => hashObject('blob', b.data)), [GIT.a, GIT.run, GIT.link]);
276 assert.deepEqual(selectObjects(built, { have: new Set([GIT.root]) }), { trees: [], blobs: [], deltas: 0 });
277 // one blob at two paths, one tree at two paths: each sent once
278 const twins = unwrap(buildTree(unwrap(fileEntries({ 'p/x': 'same', 'q/x': 'same', r: 'same' }))));
279 const sel = selectObjects(twins);
280 assert.equal(sel.trees.length, 2);
281 assert.equal(sel.blobs.length, 1);
282 // a gitlink alone sends nothing but its tree
283 const g = selectObjects(unwrap(buildTree(unwrap(fileEntries({ g: { mode: '160000', oid: GITLINK } })))));
284 assert.deepEqual([g.trees.length, g.blobs.length], [1, 0]);
285});
287test('selectObjects: a tree goes as a REF_DELTA only when that is smaller and at least DELTA_SIZE_MIN', () => {
288 const before = unwrap(buildTree(unwrap(fileEntries(FILES()))));
289 const after = unwrap(buildTree(unwrap(fileEntries({ ...FILES(), 'src/a.js': 'A\n' }))));
290 assert.equal(after.root, GIT.root2);
291 const old = new Map([['', { oid: GIT.root, data: before.trees.get('').data }], ['src', { oid: GIT.src, data: before.trees.get('src').data }]]);
292 const sel = selectObjects(after, { have: new Set([GIT.lib, GIT.readme, GIT.link, GIT.run, GIT.b]), old });
293 assert.equal(sel.deltas, 2);
294 assert.deepEqual(sel.trees.map(t => t.base), [{ tag: 'ref', oid: GIT.root }, { tag: 'ref', oid: GIT.src }]);
295 assert.deepEqual([...sel.trees[0].data], [...makeDelta(before.trees.get('').data, after.trees.get('').data).delta]);
296 assert.deepEqual([...sel.trees[1].data], [...makeDelta(before.trees.get('src').data, after.trees.get('src').data).delta]);
297 assert.deepEqual(sel.blobs.map(b => td.decode(b.data)), ['A\n']);
298 // an unrelated base gives a delta no smaller than the tree: the tree goes whole
299 const other = unwrap(buildTree(unwrap(fileEntries({ zzz: 'q' }))));
300 const whole = selectObjects(other, { old: new Map([['', { oid: GIT.lib, data: te.encode('#'.repeat(64)) }]]) });
301 assert.equal(whole.deltas, 0);
302 assert.deepEqual([...whole.trees[0].data], [...other.trees.get('').data]);
303 // identical bytes: header (2) + one copy op (2) is exactly DELTA_SIZE_MIN (4), and smaller than the tree
304 const same = selectObjects(before, { old: new Map([['src/lib', { oid: 'f'.repeat(40), data: before.trees.get('src/lib').data }]]) });
305 const lib = same.trees.find(t => t.base);
306 assert.equal(lib.data.length, 4);
307 assert.deepEqual(lib.base, { tag: 'ref', oid: 'f'.repeat(40) });
308 // the empty tree against itself: a 2-byte delta is under DELTA_SIZE_MIN, so whole (0 bytes)
309 const empty = unwrap(buildTree([]));
310 const e = selectObjects(empty, { old: new Map([['', { oid: EMPTY_TREE, data: new Uint8Array(0) }]]) });
311 assert.deepEqual([e.deltas, e.trees[0].data.length, e.trees[0].base], [0, 0, undefined]);
312});
314test('validateTreeSpec: defaults, create, head, and each field checked before I/O', () => {
315 const s = unwrap(validateTreeSpec({ files: { a: 'x' }, author: A, message: 'm', time: T0 }, DEFAULT_LIMITS));
316 assert.equal(Object.isFrozen(s), true);
317 assert.deepEqual([s.ref, s.base, s.create, s.files, s.message, s.head], ['refs/heads/main', 'refs/heads/main', false, 1, 'm', null]);
318 assert.deepEqual({ ...s.author }, { name: 'A', email: 'a@x', time: T0, tz: '+0000' });
319 assert.equal(s.built.root, unwrap(buildTree(unwrap(fileEntries({ a: 'x' })))).root);
320 const c = unwrap(validateTreeSpec({ ref: 'refs/heads/new', base: 'refs/heads/main', files: {}, author: { ...A, tz: '+0530' }, message: '', time: 0 }, DEFAULT_LIMITS, 'a'.repeat(40)));
321 assert.deepEqual([c.ref, c.base, c.create, c.head, c.author.tz, c.files], ['refs/heads/new', 'refs/heads/main', true, 'a'.repeat(40), '+0530', 0]);
322 assert.equal(unwrap(validateTreeSpec({ ref: 'refs/heads/x', files: {}, author: A, message: '' }, DEFAULT_LIMITS)).base, 'refs/heads/x');
323 const t0 = Math.floor(Date.now() / 1000);
324 const now = unwrap(validateTreeSpec({ files: {}, author: A, message: '' }, DEFAULT_LIMITS)).author.time;
325 assert.ok(now >= t0 && now <= t0 + 2, `${now} vs ${t0}`);
326 const ok = { files: {}, author: A, message: 'm', time: T0 };
327 throws(() => unwrap(validateTreeSpec(ok, DEFAULT_LIMITS, 'abc')), 'bad_request', 'head must be a 40-hex oid');
328 throws(() => unwrap(validateTreeSpec(ok, DEFAULT_LIMITS, 'A'.repeat(40))), 'bad_request', 'head must be a 40-hex oid');
329 throws(() => unwrap(validateTreeSpec(ok, DEFAULT_LIMITS, `${'a'.repeat(40)}\n`)), 'bad_request', 'head must be a 40-hex oid');
330 throws(() => unwrap(validateTreeSpec({ ...ok, ref: 'main' }, DEFAULT_LIMITS)), 'bad_request', undefined, { what: 'ref' });
331 throws(() => unwrap(validateTreeSpec({ ...ok, base: 'main' }, DEFAULT_LIMITS)), 'bad_request', undefined, { what: 'ref' });
332 throws(() => unwrap(validateTreeSpec({ ...ok, author: undefined }, DEFAULT_LIMITS)), 'bad_request', undefined, { what: 'author name' });
333 throws(() => unwrap(validateTreeSpec({ ...ok, author: { name: 'A' } }, DEFAULT_LIMITS)), 'bad_request', undefined, { what: 'author email' });
334 throws(() => unwrap(validateTreeSpec({ ...ok, message: 'a\0' }, DEFAULT_LIMITS)), 'bad_request', undefined, { what: 'message' });
335 throws(() => unwrap(validateTreeSpec({ ...ok, files: null }, DEFAULT_LIMITS)), 'bad_request', undefined, { what: 'files' });
336 throws(() => unwrap(validateTreeSpec({ ...ok, files: { a: '1', 'a/b': '2' } }, DEFAULT_LIMITS)), 'bad_request', 'a is both a file and a directory');
337 // the total is the sum of blob bytes plus LIMIT_OBJECT_OVERHEAD per entry (#142). Exactly the cap is kept; the next
338 // byte is limit_exceeded, in fileEntries, before buildTree.
339 const cap = withLimits({ maxPushBytes: 4 + 2 * OV });
340 assert.equal(unwrap(validateTreeSpec({ ...ok, files: { a: 'ab', b: 'cd' } }, cap)).built.root, unwrap(buildTree(unwrap(fileEntries({ a: 'ab', b: 'cd' }, cap)))).root);
341 throws(() => unwrap(validateTreeSpec({ ...ok, files: { a: 'abc', b: 'de' } }, cap)), 'limit_exceeded', `push bytes ${5 + 2 * OV} exceeds limit ${4 + 2 * OV}`, { what: 'push bytes', got: 5 + 2 * OV, max: 4 + 2 * OV });
342 throws(() => unwrap(fileEntries({ a: 'abc', b: 'de' }, cap)), 'limit_exceeded', undefined, { what: 'push bytes', got: 5 + 2 * OV });
343 // a gitlink has no bytes but is an entry: it costs the overhead
344 assert.equal(unwrap(validateTreeSpec({ ...ok, files: { g: { mode: '160000', oid: GITLINK } } }, withLimits({ maxPushBytes: OV }))).files, 1);
345 throws(() => unwrap(validateTreeSpec({ ...ok, files: { g: { mode: '160000', oid: GITLINK } } }, withLimits({ maxPushBytes: OV - 1 }))), 'limit_exceeded', undefined, { what: 'push bytes', got: OV, max: OV - 1 });
346 throws(() => unwrap(validateTreeSpec({ ...ok, files: { a: 'x' } }, withLimits({ maxPushBytes: OV }))), 'limit_exceeded', `push bytes ${OV + 1} exceeds limit ${OV}`, { what: 'push bytes', got: OV + 1, max: OV });
347 // a file over maxObjectSize is still that error, from fileEntries, not the total
348 throws(() => unwrap(validateTreeSpec({ ...ok, files: { a: 'abcd' } }, withLimits({ maxObjectSize: 2, maxPushBytes: 1 }))), 'limit_exceeded', 'object size 4 exceeds limit 2');
349 throws(() => unwrap(validateTreeSpec(ok, { ...DEFAULT_LIMITS, maxPushBytes: null })), 'bad_request', 'limits.maxPushBytes must be a finite non-negative number');
350 assert.equal(unwrap(validateTreeSpec(ok, { ...DEFAULT_LIMITS, maxPushBytes: undefined })).files, unwrap(validateTreeSpec(ok, DEFAULT_LIMITS)).files);
351});
353// ======================================================================================================================
354// tree.js — pushTree over the fake remote
355// ======================================================================================================================
357test('pushTree: the first commit of a new ref is git\'s commit, every object sent, the CAS old oid zero', async () => {
358 const r = remote(); const e = env(r);
359 const res = await unwrapAsync(pushTree('http://fake/r.git', { files: FILES(), author: A, message: 'deploy', time: T0 }, e.opts));
360 assert.deepEqual(res, {
361 noop: false, ref: 'refs/heads/main', oldCommit: null, newCommit: GIT.commit, oldTree: null, newTree: GIT.root, hint: null, attempt: 0,
362 pushBytes: res.pushBytes, treesRead: 0, objects: { files: 6, trees: 3, sent: 9, sentTrees: 3, sentBlobs: 5, treeDeltas: 0 },
363 });
364 assert.ok(res.pushBytes > 0);
365 assert.equal(r.refs.get('refs/heads/main'), GIT.commit);
366 const [p] = r.pushes();
367 assert.deepEqual(p.commands, [{ old: ZERO_OID, new: GIT.commit, ref: 'refs/heads/main' }]);
368 assert.equal(p.pack.objects.length, 9);
369 assert.equal(p.pack.objects[0].type, TYPE.COMMIT);
370 assert.equal(r.fetches().length, 0, 'an absent ref needs no parent fetch');
371 const root = e.span('git.push_tree');
372 assert.deepEqual(Object.fromEntries(Object.entries(root.attrs).filter(([k]) => k.startsWith('git.push_tree.'))), {
373 'git.push_tree.url': 'http://fake/r.git', 'git.push_tree.ref': 'refs/heads/main', 'git.push_tree.base': 'refs/heads/main', 'git.push_tree.create': false,
374 'git.push_tree.files': 6, 'git.push_tree.trees': 3, 'git.push_tree.tree.new': GIT.root, 'git.push_tree.head.hint': '', 'git.push_tree.attempt': 0,
375 'git.push_tree.commit.old': '', 'git.push_tree.tree.old': '', 'git.push_tree.commit.new': GIT.commit, 'git.push_tree.send.trees': 3,
376 'git.push_tree.send.tree_deltas': 0, 'git.push_tree.send.blobs': 5, 'git.push_tree.push.bytes': res.pushBytes, 'git.push_tree.push.objects': 9,
377 'git.push_tree.head.outcome': '', 'git.push_tree.outcome': 'ok',
378 });
379 const diff = e.span('git.push_tree.diff');
380 assert.deepEqual([diff.attrs['git.push_tree.diff.trees_read'], diff.attrs['git.push_tree.diff.cache_hits'], diff.attrs['git.push_tree.diff.have']], [0, 0, 0]);
381 assert.equal(diff.parentSpanId, root.spanId);
382 // a message that already ends in a newline is not given a second one
383 const r2 = remote();
384 assert.equal((await unwrapAsync(pushTree('http://fake/r.git', { files: FILES(), author: A, message: 'deploy\n', time: T0 }, env(r2).opts))).newCommit, GIT.commit);
385});
387test('pushTree: a repeat push reads only the changed trees, sends deltas and only the new blob; the same tree is a noop', async () => {
388 const r = remote(); r.commit(FILES());
389 assert.equal(r.refs.get('refs/heads/main'), GIT.commit);
390 const cache = new Map();
391 const e = env(r);
392 const res = await unwrapAsync(pushTree('http://fake/r.git', { files: { ...FILES(), 'src/a.js': 'A\n' }, author: A, message: 'deploy', time: T0 }, { ...e.opts, cache }));
393 assert.equal(res.newCommit, GIT.commit2);
394 assert.deepEqual([res.oldCommit, res.oldTree, res.newTree, res.treesRead], [GIT.commit, GIT.root, GIT.root2, 2]);
395 assert.deepEqual(res.objects, { files: 6, trees: 3, sent: 4, sentTrees: 2, sentBlobs: 1, treeDeltas: 2 });
396 assert.equal(r.refs.get('refs/heads/main'), GIT.commit2);
397 // the commit (tree:0 deepen 1), then the root and src trees (tree:0) — never src/lib, which did not change
398 assert.deepEqual(r.fetches().map(f => [f.wants[0], f.filter, f.deepen]), [[GIT.commit, 'tree:0', 1], [GIT.root, 'tree:0', 0], [GIT.src, 'tree:0', 0]]);
399 const [p] = r.pushes();
400 assert.deepEqual(p.commands, [{ old: GIT.commit, new: GIT.commit2, ref: 'refs/heads/main' }]);
401 assert.deepEqual(p.pack.objects.map(o => [o.type, o.base.tag === 'ref' ? o.base.oid : null]), [[TYPE.COMMIT, null], [TYPE.REF_DELTA, GIT.root], [TYPE.REF_DELTA, GIT.src], [TYPE.BLOB, null]]);
402 const diff = e.span('git.push_tree.diff');
403 // have: the old root, its five entries, and src's two
404 assert.deepEqual([diff.attrs['git.push_tree.diff.trees_read'], diff.attrs['git.push_tree.diff.cache_hits'], diff.attrs['git.push_tree.diff.have']], [2, 0, 8]);
405 const root = e.span('git.push_tree');
406 assert.deepEqual([root.attrs['git.push_tree.commit.old'], root.attrs['git.push_tree.tree.old'], root.attrs['git.push_tree.send.tree_deltas']], [GIT.commit, GIT.root, 2]);
407 // every new tree is cached by oid after the push; a later push from it reads nothing
408 assert.deepEqual([...cache.keys()].sort(), [GIT.root, GIT.src, GIT.lib, GIT.root2, GIT.src2].sort());
409 const e2 = env(r);
410 const back = await unwrapAsync(pushTree('http://fake/r.git', { files: FILES(), author: A, message: 'deploy', time: T0 }, { ...e2.opts, cache }));
411 assert.deepEqual([back.treesRead, back.oldCommit, back.newTree], [0, GIT.commit2, GIT.root]);
412 assert.deepEqual([e2.span('git.push_tree.diff').attrs['git.push_tree.diff.cache_hits'], e2.span('git.push_tree.diff').attrs['git.push_tree.diff.trees_read']], [2, 0]);
413 // the same files again: noop, no receive-pack
414 const e3 = env(r); const before = r.pushes().length;
415 const noop = await unwrapAsync(pushTree('http://fake/r.git', { files: FILES(), author: A, message: 'other', time: T0 + 5 }, e3.opts));
416 assert.deepEqual(noop, {
417 noop: true, ref: 'refs/heads/main', oldCommit: back.newCommit, newCommit: back.newCommit, oldTree: GIT.root, newTree: GIT.root, hint: null, attempt: 0,
418 pushBytes: 0, treesRead: 0, objects: { files: 6, trees: 3, sent: 0, sentTrees: 0, sentBlobs: 0, treeDeltas: 0 },
419 });
420 assert.equal(r.pushes().length, before);
421 assert.equal(e3.span('git.push_tree').attrs['git.push_tree.outcome'], 'noop');
422});
424test('pushTree: parent trees are read only where both sides are directories with different oids', async () => {
425 const r = remote(); r.commit({ d: { mode: '120000', data: 'x' }, 'e/f': '1', 'g/h': '1', 'same/x': 's' });
426 // d becomes a directory, e becomes a file, g changes, same is unchanged
427 const res = await unwrapAsync(pushTree('http://fake/r.git', { files: { 'd/y': '2', e: '3', 'g/h': '2', 'same/x': 's' }, author: A, message: 'm', time: T0 }, env(r).opts));
428 assert.equal(res.treesRead, 2, 'the root and g');
429 assert.deepEqual(r.fetches().slice(1).map(f => f.wants[0]).length, 2);
430 const parsed = unwrap(parseTree(r.objects.get(unwrap(parseCommit(r.objects.get(res.newCommit).data)).tree).data));
431 assert.deepEqual(parsed.map(x => [x.name, x.mode]), [['d', '40000'], ['e', '100644'], ['g', '40000'], ['same', '40000']]);
432 // the pushed tree resolves on the server (every delta base and blob it needs is there)
433 assert.equal(r.refs.get('refs/heads/main'), res.newCommit);
434});
436test('pushTree: create a ref from base, refuse an absent base', async () => {
437 const r = remote(); r.commit(FILES());
438 const res = await unwrapAsync(pushTree('http://fake/r.git', { ref: 'refs/heads/topic', base: 'refs/heads/main', files: { ...FILES(), 'src/a.js': 'A\n' }, author: A, message: 'deploy', time: T0 }, env(r).opts));
439 assert.equal(res.newCommit, GIT.commit2);
440 assert.equal(res.oldCommit, GIT.commit);
441 assert.deepEqual(r.pushes()[0].commands, [{ old: ZERO_OID, new: GIT.commit2, ref: 'refs/heads/topic' }]);
442 assert.equal(r.refs.get('refs/heads/main'), GIT.commit);
443 // base = the new ref's own tree: still a commit, never a noop, when creating
444 const same = await unwrapAsync(pushTree('http://fake/r.git', { ref: 'refs/heads/copy', base: 'refs/heads/main', files: FILES(), author: A, message: 'deploy', time: T0 }, env(r).opts));
445 assert.equal(same.noop, false);
446 assert.equal(r.refs.get('refs/heads/copy'), same.newCommit);
447 const e = env(r);
448 await rejects(unwrapAsync(pushTree('http://fake/r.git', { ref: 'refs/heads/x', base: 'refs/heads/gone', files: {}, author: A, message: 'm', time: T0 }, e.opts)), 'not_found', 'ref refs/heads/gone not found', { attempt: 0, hint: null, miss: { kind: 'ref', ref: 'refs/heads/gone', candidates: ['refs/heads/gone'] } });
449 assert.equal(e.span('git.push_tree').attrs['git.push_tree.outcome'], 'not_found');
450});
452test('pushTree: a lost race is retried once from a fresh ls-refs, linked to the first attempt; a second loss is thrown', async () => {
453 const r = remote({ hook: async req => { if (req.service === 'git-receive-pack' && moves-- > 0) r.commit({ other: String(moves) }, { parents: [r.refs.get('refs/heads/main')] }); } });
454 let moves = 1;
455 r.commit(FILES());
456 const e = env(r);
457 const res = await unwrapAsync(pushTree('http://fake/r.git', { files: { ...FILES(), 'src/a.js': 'A\n' }, author: A, message: 'deploy', time: T0 }, e.opts));
458 assert.equal(res.attempt, 1);
459 const raced = unwrap(parseCommit(r.objects.get(r.refs.get('refs/heads/main')).data));
460 assert.equal(raced.tree, GIT.root2);
461 assert.deepEqual(raced.parents, [res.oldCommit]);
462 assert.notEqual(res.oldCommit, GIT.commit);
463 const roots = e.all('git.push_tree');
464 assert.deepEqual(roots.map(s => [s.attrs['git.push_tree.attempt'], s.attrs['git.push_tree.outcome']]), [[0, 'cas_conflict'], [1, 'ok']]);
465 assert.deepEqual(roots[1].links.map(l => [l.spanId, l.attrs['ply.link']]), [[roots[0].spanId, 'retry_of']]);
466 assert.equal(roots[0].links.length, 0);
467 moves = 2;
468 const e2 = env(r);
469 await rejects(unwrapAsync(pushTree('http://fake/r.git', { files: { ...FILES(), 'src/a.js': 'B\n' }, author: A, message: 'deploy', time: T0 }, e2.opts)), 'cas_conflict', 'push rejected: fetch first', { attempt: 1 });
470 assert.equal(e2.all('git.push_tree').length, 2);
471});
473const settled = p => p.then(value => ({ settled: 'resolved', value }), reason => ({ settled: 'rejected', reason }));
475test('editFile: a lost race is retried once from a fresh ls-refs', async () => {
476 let moves = 1;
477 // the raced commit keeps the current tree, so the retry still finds the file the edit names
478 const r = remote({ hook: async req => {
479 if (req.service !== 'git-receive-pack' || moves-- <= 0) return;
480 const cur = r.refs.get('refs/heads/main');
481 const tree = unwrap(parseCommit(r.objects.get(cur).data)).tree;
482 const who = `A <a@x> ${T0} +0000`;
483 r.refs.set('refs/heads/main', r.put('commit', unwrap(encodeCommit({ tree, parents: [cur], author: who, committer: who, message: 'raced\n' }))));
484 } });
485 r.commit(FILES());
486 const e = env(r);
487 const res = await unwrapAsync(editFile('http://fake/r.git', { path: 'README', find: 'hello', replace: 'HELLO', author: A, message: 'm', time: T0 }, e.opts));
488 assert.equal(res.attempt, 1);
489 assert.equal(res.noop, false);
490 assert.deepEqual(e.all('git.edit').map(s => [s.attrs['git.edit.attempt'], s.attrs['git.edit.outcome']]), [[0, 'cas_conflict'], [1, 'ok']]);
491 // a hint on the lost attempt is what the retry reports, and the retry's span links to the first
492 moves = 1;
493 const hinted = env(r);
494 const again = await unwrapAsync(editFile('http://fake/r.git', { path: 'README', find: 'HELLO', replace: 'AGAIN', author: A, message: 'm', time: T0 }, { ...hinted.opts, head: r.refs.get('refs/heads/main') }));
495 assert.deepEqual([again.attempt, again.hint], [1, 'used']);
496 const roots = hinted.all('git.edit');
497 assert.deepEqual(roots[1].links.map(l => [l.spanId, l.attrs['ply.link']]), [[roots[0].spanId, 'retry_of']]);
498 // a non-conflict error is the one attempt, not a retry
499 const missing = env(r);
500 await rejects(unwrapAsync(editFile('http://fake/r.git', { ref: 'refs/heads/missing', path: 'README', find: 'a', replace: 'b', author: A, message: 'm', time: T0 }, missing.opts)), 'not_found', 'ref refs/heads/missing not found', { miss: { kind: 'ref', ref: 'refs/heads/missing', candidates: ['refs/heads/missing'] } });
501 assert.equal(missing.all('git.edit').length, 1);
502});
504test('lookupEntry: a missing name, a file along the way, and a last component that is not a regular file', () => {
505 const tree = unwrap(encodeTree([
506 { name: 'README', mode: '100644', oid: GIT.readme },
507 { name: 'src', mode: '40000', oid: GIT.src },
508 { name: 'sub', mode: '160000', oid: GITLINK },
509 { name: 'link', mode: '120000', oid: GIT.link },
510 { name: 'run.sh', mode: '100755', oid: GIT.run },
511 ]));
512 throws(() => unwrap(lookupEntry(tree, 'nope', true, DEFAULT_LIMITS)), 'not_found', 'path component nope not found', { miss: { kind: 'path', missing: 'nope', existingPath: '' } });
513 throws(() => unwrap(lookupEntry(tree, 'README', false, DEFAULT_LIMITS)), 'not_found', 'README is not a directory', { miss: { kind: 'not_directory', path: 'README', mode: '100644' } });
514 assert.equal(unwrap(lookupEntry(tree, 'src', false, DEFAULT_LIMITS)).entry.oid, GIT.src);
515 throws(() => unwrap(lookupEntry(tree, 'src', true, DEFAULT_LIMITS)), 'not_found', 'src is not a regular file (mode 40000)', { miss: { kind: 'is_directory', path: 'src', tree: GIT.src } });
516 throws(() => unwrap(lookupEntry(tree, 'sub', true, DEFAULT_LIMITS)), 'not_found', 'sub is not a regular file (mode 160000)', { miss: { kind: 'submodule', path: 'sub', commit: GITLINK } });
517 throws(() => unwrap(lookupEntry(tree, 'link', true, DEFAULT_LIMITS)), 'not_found', 'link is not a regular file (mode 120000)', { miss: { kind: 'not_directory', path: 'link', mode: '120000' } });
518 assert.equal(unwrap(lookupEntry(tree, 'README', true, DEFAULT_LIMITS)).entry.oid, GIT.readme);
519 assert.equal(unwrap(lookupEntry(tree, 'run.sh', true, DEFAULT_LIMITS)).entry.mode, '100755');
520});
522test('editFile and pushTree: a non-Error from the first attempt is a rejection, and is not retried', async () => {
523 // a plain object whose code is cas_conflict used to come back as the resolved value; an Error with that code is retried once
524 const plain = { code: 'cas_conflict', message: 'not an Error' };
525 const r = remote();
526 r.commit(FILES());
527 let calls = 0;
528 const e = env(r);
529 const real = e.transport.frames;
530 e.transport.frames = (...args) => { calls += 1; if (calls === 1) throw plain; return real(...args); };
531 const edit = await settled(unwrapAsync(editFile('http://fake/r.git', { path: 'README', find: 'hello', replace: 'HELLO', author: A, message: 'm', time: T0 }, e.opts)));
532 assert.deepEqual(edit, { settled: 'rejected', reason: plain });
533 assert.equal(calls, 1, 'a non-Error is not a cas_conflict retry');
534 calls = 0;
535 const tree = await settled(unwrapAsync(pushTree('http://fake/r.git', { files: { ...FILES(), 'src/a.js': 'A\n' }, author: A, message: 'm', time: T0 }, e.opts)));
536 assert.deepEqual(tree, { settled: 'rejected', reason: plain });
537 assert.equal(calls, 1);
538 // a string thrown after the protocol (the caller's signer) is not the return value: it is the caller's own code
539 // failing, the Err that names the signer and carries the string untouched (README "Your own code failing")
540 const e2 = env(r);
541 const before = r.pushes().length;
542 const signer = { kind: 'ssh-ed25519', publicKeyLine: 'ssh-ed25519 AAAA', sign() { throw 'x'; } };
543 const signed = await settled(unwrapAsync(pushTree('http://fake/r.git', { files: { ...FILES(), 'src/a.js': 'B\n' }, author: A, message: 'm', time: T0 }, { ...e2.opts, signer })));
544 assert.deepEqual([signed.settled, signed.reason.code, signed.reason.what, signed.reason.cause], ['rejected', 'bad_request', 'signer', 'x']);
545 assert.equal(r.pushes().length, before);
546 const editSigned = await settled(unwrapAsync(editFile('http://fake/r.git', { path: 'README', find: 'hello', replace: 'HELLO', author: A, message: 'm', time: T0 }, { ...env(r).opts, signer: { ...signer, sign() { throw 'y'; } } })));
547 assert.deepEqual([editSigned.settled, editSigned.reason.code, editSigned.reason.what, editSigned.reason.cause], ['rejected', 'bad_request', 'signer', 'y']);
548});
550test('pushTree: git.push_tree.diff belongs to the operation tracer, in the root span\'s trace', async () => {
551 const r = remote(); r.commit(FILES());
552 const opEx = arrayExporter(); const opTracer = new Tracer({ exporter: opEx });
553 const wireEx = arrayExporter(); const wireTracer = new Tracer({ exporter: wireEx });
554 await unwrapAsync(pushTree('http://fake/r.git', { files: { ...FILES(), 'src/a.js': 'A\n' }, author: A, message: 'deploy', time: T0 }, { tracer: opTracer, transport: r.transport({ tracer: wireTracer }) }));
555 const op = opEx.lines.flatMap(flattenLine);
556 const root = op.find(s => s.name === 'git.push_tree');
557 const diff = op.find(s => s.name === 'git.push_tree.diff');
558 assert.ok(root && diff, 'the diff span is exported with the operation');
559 assert.equal(diff.traceId, root.traceId);
560 assert.equal(diff.parentSpanId, root.spanId);
561 const wire = wireEx.lines.flatMap(flattenLine);
562 assert.equal(wire.some(s => s.name === 'git.push_tree.diff'), false);
563 assert.equal(wire.some(s => s.name === 'git.upload-pack ls-refs'), true);
564});
566test('otel: end() scrubs an empty status message and leaves an unset message unset', () => {
567 const ex = arrayExporter(); const t = new Tracer({ exporter: ex });
568 const empty = t.start('git.op', { scrub: s => (s === '' ? 'EMPTY' : s.replaceAll('SECRET', '[redacted]')) });
569 empty.status = { code: 2, message: '' };
570 empty.end();
571 const secret = t.start('git.op', { scrub: s => s.replaceAll('SECRET', '[redacted]') });
572 secret.status = { code: 2, message: 'SECRET' };
573 secret.end();
574 const ok = t.start('git.op', { scrub: () => 'CALLED' });
575 ok.end();
576 const [a, b, c] = ex.lines.flatMap(flattenLine);
577 assert.equal(a.status.message, 'EMPTY');
578 assert.equal(b.status.message, '[redacted]');
579 assert.equal(c.status.message, undefined);
580});
582test('pushTree: the head hint — used, stale, a failed speculative fetch, and strictHead', async () => {
583 const r = remote(); r.commit(FILES());
584 const files = { ...FILES(), 'src/a.js': 'A\n' };
585 const spec = { files, author: A, message: 'deploy', time: T0 };
586 await rejects(unwrapAsync(pushTree('http://fake/r.git', spec, { ...env(r).opts, strictHead: true })), 'bad_request', 'strictHead needs head');
587 assert.equal(r.log.length, 0);
588 // used: the speculative commit is the parent, fetched once
589 const e = env(r);
590 const used = await unwrapAsync(pushTree('http://fake/r.git', spec, { ...e.opts, head: GIT.commit }));
591 assert.deepEqual([used.hint, used.newCommit], ['used', GIT.commit2]);
592 assert.equal(r.fetches().filter(f => f.wants[0] === GIT.commit).length, 1);
593 assert.deepEqual([e.span('git.push_tree').attrs['git.push_tree.head.hint'], e.span('git.push_tree').attrs['git.push_tree.head.outcome']], [GIT.commit, 'used']);
594 // stale: ls-refs wins, the real parent is fetched as well
595 r.log.length = 0;
596 const stale = await unwrapAsync(pushTree('http://fake/r.git', { ...spec, files: FILES() }, { ...env(r).opts, head: GIT.commit }));
597 assert.deepEqual([stale.hint, stale.oldCommit], ['stale', GIT.commit2]);
598 assert.deepEqual(r.fetches().filter(f => f.deepen === 1).map(f => f.wants[0]).sort(), [GIT.commit, GIT.commit2].sort());
599 // a hint the server does not have: recorded, and the push goes on from ls-refs
600 const e3 = env(r);
601 const ghost = 'd'.repeat(40);
602 const res = await unwrapAsync(pushTree('http://fake/r.git', { ...spec, files: { x: '1' } }, { ...e3.opts, head: ghost }));
603 assert.equal(res.hint, 'stale');
604 assert.equal(e3.span('git.push_tree').attrs['git.push_tree.head.speculative_error'], 'remote_error');
605 assert.equal(e.span('git.push_tree').attrs['git.push_tree.head.speculative_error'], undefined);
606 // strictHead with a stale hint: cas_conflict, never retried
607 const e4 = env(r); r.log.length = 0;
608 await rejects(unwrapAsync(pushTree('http://fake/r.git', { ...spec, files: { y: '1' } }, { ...e4.opts, head: GIT.commit, strictHead: true })), 'cas_conflict',
609 `refs/heads/main is at ${res.newCommit}, not the expected head ${GIT.commit}`, { expected: GIT.commit, actual: res.newCommit, attempt: 0, hint: 'stale' });
610 assert.equal(e4.all('git.push_tree').length, 1);
611 assert.equal(r.pushes().length, 0);
612 // strictHead with a matching hint pushes; with an absent ref, the hint is stale against nothing
613 const ok = await unwrapAsync(pushTree('http://fake/r.git', { ...spec, files: { y: '1' } }, { ...env(r).opts, head: res.newCommit, strictHead: true }));
614 assert.equal(ok.hint, 'used');
615 await rejects(unwrapAsync(pushTree('http://fake/r.git', { ...spec, ref: 'refs/heads/none' }, { ...env(r).opts, head: GIT.commit, strictHead: true })), 'cas_conflict',
616 `refs/heads/none is at nothing, not the expected head ${GIT.commit}`);
617});
619test('pushTree: a lost race with a hint retries without it and reports the first hint', async () => {
620 let moves = 1;
621 const r = remote({ hook: async req => { if (req.service === 'git-receive-pack' && moves-- > 0) r.commit({ other: 'x' }, { parents: [r.refs.get('refs/heads/main')] }); } });
622 r.commit(FILES());
623 const e = env(r);
624 const res = await unwrapAsync(pushTree('http://fake/r.git', { files: { z: '1' }, author: A, message: 'm', time: T0 }, { ...e.opts, head: GIT.commit }));
625 assert.deepEqual([res.attempt, res.hint], [1, 'used']);
626 const roots = e.all('git.push_tree');
627 assert.deepEqual(roots.map(s => [s.attrs['git.push_tree.head.hint'], s.attrs['git.push_tree.head.outcome']]), [[GIT.commit, 'used'], ['', 'used']]);
628});
630test('pushTree: verify reads the ref back; a different value after an acknowledged push is ambiguous', async () => {
631 const r = remote();
632 const e = env(r);
633 const res = await unwrapAsync(pushTree('http://fake/r.git', { files: FILES(), author: A, message: 'deploy', time: T0 }, { ...e.opts, verify: true }));
634 assert.equal(res.newCommit, GIT.commit);
635 assert.equal(e.span('git.push_tree').attrs['git.push_tree.verify.ref'], GIT.commit);
636 let pushed = false;
637 const liar = remote({ hook: async req => { if (req.service === 'git-receive-pack') pushed = true; else if (pushed && req.command === 'ls-refs') return [{ kind: 'flush' }]; } });
638 await rejects(unwrapAsync(pushTree('http://fake/r.git', { files: FILES(), author: A, message: 'deploy', time: T0 }, { ...env(liar).opts, verify: true })), 'ambiguous',
639 `push reported ok but refs/heads/main is absent, not ${GIT.commit}`);
640 // without verify, the same server is believed
641 pushed = false;
642 assert.equal((await unwrapAsync(pushTree('http://fake/r.git', { ref: 'refs/heads/b', files: FILES(), author: A, message: 'deploy', time: T0 }, env(liar).opts))).newCommit, GIT.commit);
643});
645test('pushTree: a signer\'s signature is in the pushed commit and on the span', async () => {
646 const r = remote(); const e = env(r);
647 const sig = '-----BEGIN SSH SIGNATURE-----\nU0lH\n-----END SSH SIGNATURE-----\n';
648 const signer = { kind: 'ssh', publicKeyLine: 'ssh-ed25519 AAAA', sign: async () => sig };
649 const res = await unwrapAsync(pushTree('http://fake/r.git', { files: FILES(), author: A, message: 'deploy', time: T0 }, { ...e.opts, signer }));
650 const c = unwrap(parseCommit(r.objects.get(res.newCommit).data));
651 assert.equal(c.tree, GIT.root);
652 assert.ok(c.extra.some(l => l.startsWith('gpgsig -----BEGIN SSH SIGNATURE-----')));
653 assert.notEqual(res.newCommit, GIT.commit);
654 const root = e.span('git.push_tree');
655 assert.deepEqual([root.attrs['git.push_tree.commit.signed'], root.attrs['git.push_tree.commit.signer']], ['ssh', 'ssh-ed25519 AAAA']);
656 const r2 = remote(); const e2 = env(r2);
657 await unwrapAsync(pushTree('http://fake/r.git', { files: FILES(), author: A, message: 'deploy', time: T0 }, { ...e2.opts, signer: { kind: 'x', sign: async () => sig } }));
658 assert.equal(e2.span('git.push_tree').attrs['git.push_tree.commit.signer'], '');
659 assert.equal(env(remote()).span('git.push_tree'), undefined);
660 assert.equal(e2.spans().find(s => s.name === 'git.push_tree').attrs['git.push_tree.commit.signed'], 'x');
661 const plain = env(remote());
662 await unwrapAsync(pushTree('http://fake/r.git', { files: FILES(), author: A, message: 'deploy', time: T0 }, plain.opts));
663 assert.equal(plain.span('git.push_tree').attrs['git.push_tree.commit.signed'], undefined);
664});
666// ======================================================================================================================
667// checkout.js
668// ======================================================================================================================
670test('canonMode is git\'s canon_mode', () => {
671 for (const m of ['120000', '160000', '40000']) assert.equal(canonMode(m), m);
672 const cases = { 100644: '100644', 100755: '100755', 100664: '100644', 100600: '100644', 100000: '100644', 100700: '100755', 100100: '100755', 100744: '100755', 100777: '100755', 100677: '100644' };
673 for (const [m, want] of Object.entries(cases)) assert.equal(canonMode(m), want, m);
674 // the low 16 bits, as tree-walk keeps them (observed with git ls-tree 2.50): a type git does not know is a gitlink,
675 // and the digits above the sixth from the right are gone
676 const odd = { 100: '160000', 1006440: '160000', 200644: '160000', 120001: '120000', 160001: '160000', 4000: '160000', 140000: '160000', 40755: '40000', 7100644: '100644', 300644: '100644', 107777: '100755', 1: '160000', 777777: '160000' };
677 for (const [m, want] of Object.entries(odd)) assert.equal(canonMode(m), want, m);
678 // what parse_mode refuses (empty, a non-octal byte) or fsck --strict errors on (zero-padded), and non-strings
679 for (const m of ['0100644', '040000', '0', '100648', '100x44', '', 'x100644', '100644x', ' 100644', '8']) assert.equal(canonMode(m), null, m);
680 assert.equal(canonMode(100644), null);
681});
683test('treeFiles: depth-first in tree order, canonical modes, every refusal', () => {
684 const trees = new Map([
685 ['r', [{ mode: '100664', name: 'a', oid: '1' }, { mode: '40000', name: 'd', oid: 'd' }, { mode: '160000', name: 'g', oid: '2' }, { mode: '100755', name: 'z', oid: '3' }]],
686 ['d', [{ mode: '120000', name: 'l', oid: '4' }, { mode: '40000', name: 'e', oid: 'e' }]],
687 ['e', [{ mode: '100644', name: 'f', oid: '5' }]],
688 ]);
689 const tree = oid => ok(trees.get(oid));
690 assert.deepEqual(unwrap(treeFiles(tree, 'r')), [
691 { path: 'a', mode: '100644', oid: '1' }, { path: 'd/l', mode: '120000', oid: '4' }, { path: 'd/e/f', mode: '100644', oid: '5' },
692 { path: 'g', mode: '160000', oid: '2' }, { path: 'z', mode: '100755', oid: '3' },
693 ]);
694 assert.deepEqual(unwrap(treeFiles(tree, 'e')), [{ path: 'f', mode: '100644', oid: '5' }]);
695 // 3 + 2 + 1 = 6 entries visited, trees included; 9 at the root level would be 9
696 assert.equal(unwrap(treeFiles(tree, 'r', { limits: withLimits({ maxPackObjects: 7 }) })).length, 5);
697 throws(() => unwrap(treeFiles(tree, 'r', { limits: withLimits({ maxPackObjects: 6 }) })), 'limit_exceeded', 'checkout entries 7 exceeds limit 6', { what: 'checkout entries', got: 7, max: 6 });
698 // depth: d/e/f is level 3
699 assert.equal(unwrap(treeFiles(tree, 'r', { limits: withLimits({ maxPathDepth: 3 }) })).length, 5);
700 throws(() => unwrap(treeFiles(tree, 'r', { limits: withLimits({ maxPathDepth: 2 }) })), 'limit_exceeded', 'path depth 3 exceeds limit 2', { what: 'path depth' });
701 throws(() => unwrap(treeFiles(tree, 'e', { depth: 2, limits: withLimits({ maxPathDepth: 2 }) })), 'limit_exceeded', 'path depth 3 exceeds limit 2');
702 assert.equal(unwrap(treeFiles(tree, 'e', { depth: 1, limits: withLimits({ maxPathDepth: 2 }) })).length, 1);
703 const dup = new Map([['r', [{ mode: '100644', name: 'a', oid: '1' }, { mode: '40000', name: 'a', oid: 'x' }]], ['x', []]]);
704 throws(() => unwrap(treeFiles(o => ok(dup.get(o)), 'r')), 'pack_corrupt', 'tree r: duplicate entry "a" (git fsck duplicateEntries)');
705 // the same name in two different trees is not a duplicate
706 const two = new Map([['r', [{ mode: '40000', name: 'a', oid: 'x' }, { mode: '40000', name: 'b', oid: 'x' }]], ['x', [{ mode: '100644', name: 'a', oid: '1' }]]]);
707 assert.deepEqual(unwrap(treeFiles(o => ok(two.get(o)), 'r')).map(f => f.path), ['a/a', 'b/a']);
708 const bad = new Map([['r', [{ mode: '40755', name: 'd', oid: 'd' }]], ['d', [{ mode: '100648', name: 'x', oid: '1' }]]]);
709 throws(() => unwrap(treeFiles(o => ok(bad.get(o)), 'r')), 'pack_corrupt', 'tree d: d/x has mode 100648, which git cannot check out');
710 throws(() => unwrap(treeFiles(tree, 'r', { limits: { maxPackObjects: NaN } })), 'bad_request', 'limits.maxPackObjects must be a finite non-negative number');
711 throws(() => unwrap(treeFiles(tree, 'r', { limits: { maxPackObjects: 9, maxPathDepth: -1 } })), 'bad_request', 'limits.maxPathDepth must be a finite non-negative number');
712 // a key left out is the default (#156 parseLimits)
713 assert.deepEqual(unwrap(treeFiles(tree, 'r', { limits: {} })), unwrap(treeFiles(tree, 'r')));
714});
716const drain = async files => { const out = []; for await (const f of files) out.push({ path: f.path, mode: f.mode, oid: f.oid, size: f.size, text: f.stream ? await readAll(f.stream) : null }); return out; };
718test('checkoutTree: one blob:none fetch for the trees, blobs in batches, gitlinks first', async () => {
719 const r = remote(); r.commit(FILES());
720 const e = env(r);
721 const res = await unwrapAsync(checkoutTree('http://fake/r.git', {}, e.opts));
722 assert.deepEqual([res.ref, res.commit, res.tree, res.path], ['refs/heads/main', GIT.commit, GIT.root, '']);
723 assert.deepEqual(res.entries, [
724 { path: 'README', mode: '100644', oid: GIT.readme }, { path: 'link', mode: '120000', oid: GIT.link }, { path: 'run.sh', mode: '100755', oid: GIT.run },
725 { path: 'src/a.js', mode: '100644', oid: GIT.a }, { path: 'src/lib/b.js', mode: '100644', oid: GIT.b }, { path: 'sub', mode: '160000', oid: GITLINK },
726 ]);
727 const [trees] = r.fetches();
728 assert.deepEqual([trees.wants, trees.filter, trees.deepen], [[GIT.commit], 'blob:none', 1]);
729 const root = e.span('git.checkout');
730 assert.deepEqual(Object.fromEntries(Object.entries(root.attrs).filter(([k]) => k.startsWith('git.checkout.'))), {
731 'git.checkout.url': 'http://fake/r.git', 'git.checkout.ref': 'HEAD', 'git.checkout.path': '', 'git.checkout.batch': 1000,
732 'git.checkout.commit': GIT.commit, 'git.checkout.tree': GIT.root, 'git.checkout.entries': 6, 'git.checkout.pack.objects': 4, 'git.checkout.pack.trees': 3,
733 'git.checkout.outcome': 'ok',
734 });
735 const files = await drain(res.files);
736 assert.deepEqual(files.map(f => [f.path, f.size, f.text]), [
737 ['sub', 0, null], ['README', 6, 'hello\n'], ['link', 6, 'README'], ['run.sh', 10, '#!/bin/sh\n'], ['src/a.js', 2, 'a\n'], ['src/lib/b.js', 2, 'b\n'],
738 ]);
739 assert.deepEqual(r.fetches().slice(1).map(f => [f.wants.length, f.filter]), [[5, null]]);
740 const blobs = e.span('git.checkout.blobs');
741 // consumed after checkoutTree returns: a root of its own trace that follows the checkout root (README "Span lifetimes")
742 assert.deepEqual([blobs.parentSpanId, blobs.links.map(l => [l.traceId, l.spanId, l.attrs['ply.link']])], [null, [[root.traceId, root.spanId, 'follows_from']]]);
743 assert.notEqual(blobs.traceId, root.traceId);
744 assert.deepEqual(Object.fromEntries(Object.entries(blobs.attrs).filter(([k]) => k.startsWith('git.checkout.'))), {
745 'git.checkout.blobs.unique': 5, 'git.checkout.blobs.paths': 6, 'git.checkout.blobs.fetches': 1, 'git.checkout.blobs.buffered': 5, 'git.checkout.blobs.streamed': 0,
746 'git.checkout.blobs.splits': 0, 'git.checkout.blobs.bytes': 26, 'git.checkout.blobs.single': false, 'git.checkout.blobs.outcome': 'ok',
747 });
748});
750test('checkoutTree: a subtree by path, a ref by short name, batch bounds', async () => {
751 const r = remote(); r.commit(FILES()); r.commit({ x: '1' }, { ref: 'refs/tags/v1' });
752 for (const path of ['src', 'src/']) {
753 const res = await unwrapAsync(checkoutTree('http://fake/r.git', { ref: 'main', path }, env(r).opts));
754 assert.deepEqual([res.ref, res.tree, res.path], ['refs/heads/main', GIT.src, 'src']);
755 assert.deepEqual(res.entries.map(x => x.path), ['a.js', 'lib/b.js']);
756 }
757 for (const path of [undefined, null, '', '/']) assert.equal((await unwrapAsync(checkoutTree('http://fake/r.git', { path }, env(r).opts))).tree, GIT.root);
758 await rejects(unwrapAsync(checkoutTree('http://fake/r.git', { path: ['src'] }, env(r).opts)), 'bad_request', 'invalid path: must be a non-empty string');
759 const deep = await unwrapAsync(checkoutTree('http://fake/r.git', { path: 'src/lib' }, env(r).opts));
760 assert.deepEqual([deep.tree, deep.entries.map(x => x.path)], [GIT.lib, ['b.js']]);
761 assert.equal((await unwrapAsync(checkoutTree('http://fake/r.git', { ref: 'v1' }, env(r).opts))).ref, 'refs/tags/v1');
762 await rejects(unwrapAsync(checkoutTree('http://fake/r.git', { path: 'nope' }, env(r).opts)), 'not_found', 'path component nope not found', { miss: { kind: 'path', missing: 'nope', existingPath: '' } });
763 await rejects(unwrapAsync(checkoutTree('http://fake/r.git', { path: 'src/nope' }, env(r).opts)), 'not_found', 'path component nope not found', { miss: { kind: 'path', missing: 'nope', existingPath: 'src' } });
764 await rejects(unwrapAsync(checkoutTree('http://fake/r.git', { path: 'src/lib/nope' }, env(r).opts)), 'not_found', 'path component nope not found', { miss: { kind: 'path', missing: 'nope', existingPath: 'src/lib' } });
765 await rejects(unwrapAsync(checkoutTree('http://fake/r.git', { path: 'src/a.js' }, env(r).opts)), 'not_found', 'src/a.js is not a directory (mode 100644)', { miss: { kind: 'not_directory', path: 'src/a.js', mode: '100644' } });
766 await rejects(unwrapAsync(checkoutTree('http://fake/r.git', { path: 'README/x' }, env(r).opts)), 'not_found', 'README is not a directory (mode 100644)', { miss: { kind: 'not_directory', path: 'README', mode: '100644' } });
767 await rejects(unwrapAsync(checkoutTree('http://fake/r.git', { path: '../x' }, env(r).opts)), 'bad_request');
768 for (const batch of [0, -1, 1.5, NaN, '2', 3]) {
769 await rejects(unwrapAsync(checkoutTree('http://fake/r.git', {}, { ...env(r).opts, batch, limits: withLimits({ maxPackObjects: 2 }) })), 'bad_request', 'batch must be an integer from 1 to limits.maxPackObjects', { what: 'batch' });
770 }
771 const n = r.log.length;
772 const two = await unwrapAsync(checkoutTree('http://fake/r.git', {}, { ...env(r).opts, batch: 2, limits: withLimits({ maxPackObjects: 100 }) }));
773 assert.equal(two.entries.length, 6);
774 await drain(two.files);
775 assert.deepEqual(r.fetches().slice(-3).map(f => f.wants.length), [2, 2, 1], 'five blobs in wants lists of two; the last alone');
776 assert.ok(r.log.length > n);
777 const one = await unwrapAsync(checkoutTree('http://fake/r.git', {}, { ...env(r).opts, batch: 1 }));
778 assert.deepEqual((await drain(one.files)).map(f => f.text), [null, 'hello\n', 'README', '#!/bin/sh\n', 'a\n', 'b\n']);
779});
781test('checkoutTree: a bare commit oid, a non-commit, missing trees and a short pack', async () => {
782 const r = remote(); const c = r.commit(FILES());
783 const e = env(r);
784 const res = await unwrapAsync(checkoutTree('http://fake/r.git', { ref: c }, e.opts));
785 assert.deepEqual([res.ref, res.commit], [c, c]);
786 assert.equal(r.log.filter(x => x.command === 'ls-refs').length, 0, 'a bare oid needs no ls-refs');
787 assert.equal(e.span('git.checkout').attrs['git.checkout.ref'], c);
788 const missing = 'e'.repeat(40);
789 await rejects(unwrapAsync(checkoutTree('http://fake/r.git', { ref: missing }, env(r).opts)), 'not_found', `commit ${missing} not found on the remote`, { miss: { kind: 'commit', miss: { kind: 'absent', oid: missing } } });
790 await rejects(unwrapAsync(checkoutTree('http://fake/r.git', { ref: GIT.src }, env(r).opts)), 'not_found', `${GIT.src} is not a commit`, { miss: { kind: 'commit', miss: { kind: 'not_commit', oid: GIT.src, type: 'tree' } } });
791 await rejects(unwrapAsync(checkoutTree('http://fake/r.git', { ref: GIT.readme }, env(r).opts)), 'not_found', `${GIT.readme} is not a commit`, { miss: { kind: 'commit', miss: { kind: 'not_commit', oid: GIT.readme, type: 'blob' } } });
792 // a pack without the commit, or without a tree under it
793 const only = async oids => (await buildPack(oids.map(o => r.objects.get(o)))).pack;
794 const lying = pack => remote({ hook: async req => (req.command === 'fetch' ? packFrames(await pack()) : undefined) });
795 const noCommit = lying(() => only([GIT.root])); noCommit.refs.set('refs/heads/main', c); noCommit.objects.set(c, r.objects.get(c));
796 await rejects(unwrapAsync(checkoutTree('http://fake/r.git', {}, env(noCommit).opts)), 'pack_corrupt', `commit ${c} not in the blob:none pack (1 objects)`);
797 const noTree = lying(() => only([c, GIT.root])); noTree.refs.set('refs/heads/main', c); noTree.objects.set(c, r.objects.get(c));
798 await rejects(unwrapAsync(checkoutTree('http://fake/r.git', {}, env(noTree).opts)), 'pack_corrupt', `tree ${GIT.src} not in the blob:none pack`);
799 // a blob where a tree should be is still a missing tree
800 const blobTree = lying(() => only([c, GIT.readme])); blobTree.refs.set('refs/heads/main', c); blobTree.objects.set(c, r.objects.get(c));
801 await rejects(unwrapAsync(checkoutTree('http://fake/r.git', {}, env(blobTree).opts)), 'pack_corrupt', `tree ${GIT.root} not in the blob:none pack`);
802 const other = remote({ hook: async req => (req.command === 'fetch' ? [dataFrame('ERR upload-pack: something else')] : undefined) }); other.refs.set('refs/heads/main', c);
803 await rejects(unwrapAsync(checkoutTree('http://fake/r.git', {}, env(other).opts)), 'remote_error', 'remote: upload-pack: something else');
804 const absent = remote(); absent.refs.set('refs/heads/main', c);
805 await rejects(unwrapAsync(checkoutTree('http://fake/r.git', {}, env(absent).opts)), 'not_found', `commit ${c} not found on the remote`, { miss: { kind: 'commit', miss: { kind: 'absent', oid: c } } });
806 // a pack_corrupt that happens to carry not_our_ref is still pack_corrupt: only remote_error is reclassified
807 const odd = remote({ hook: async req => { if (req.command !== 'fetch') return; const e = new Error('odd'); e.code = 'pack_corrupt'; e.reason = 'not_our_ref'; throw e; } });
808 odd.refs.set('refs/heads/main', c);
809 await rejects(unwrapAsync(checkoutTree('http://fake/r.git', {}, env(odd).opts)), 'pack_corrupt', 'odd');
810});
812test('checkoutTree: one blob at two paths is fetched once and yielded twice, each with its own stream', async () => {
813 const r = remote(); r.commit({ 'a/x': 'same\n', 'b/x': 'same\n', c: 'other\n' });
814 const e = env(r);
815 const res = await unwrapAsync(checkoutTree('http://fake/r.git', {}, e.opts));
816 const files = await drain(res.files);
817 assert.deepEqual(files.map(f => [f.path, f.text]), [['a/x', 'same\n'], ['b/x', 'same\n'], ['c', 'other\n']]);
818 assert.deepEqual(r.fetches().slice(1).map(f => f.wants.length), [2]);
819 const s = e.span('git.checkout.blobs').attrs;
820 assert.deepEqual([s['git.checkout.blobs.unique'], s['git.checkout.blobs.paths'], s['git.checkout.blobs.buffered'], s['git.checkout.blobs.bytes']], [2, 3, 3, 16]);
821 // an empty blob streams nothing and ends
822 const z = remote(); z.commit({ e: '', f: 'x' });
823 assert.deepEqual((await drain((await unwrapAsync(checkoutTree('http://fake/r.git', {}, env(z).opts))).files)).map(f => [f.path, f.size, f.text]), [['e', 0, ''], ['f', 1, 'x']]);
824});
826test('checkoutTree: a batch over a cap splits in half; one left alone streams on its own', async () => {
827 const r = remote({ hook: async req => { if (req.command === 'fetch' && req.filter === null && req.wants.length > 2) throw fail('limit_exceeded', 'pack bytes 9 exceeds limit 1', { what: 'pack bytes' }); } });
828 r.commit({ a: '1', b: '2', c: '3', d: '4', e: '5' });
829 const e = env(r);
830 const files = await drain((await unwrapAsync(checkoutTree('http://fake/r.git', {}, { ...e.opts, batch: 5 }))).files);
831 assert.deepEqual(files.map(f => f.text), ['1', '2', '3', '4', '5']);
832 // 5 → 3 + 2 → (2 + 1) + 2: the singles stream
833 assert.deepEqual(r.fetches().slice(1).map(f => f.wants.length), [5, 3, 2, 1, 2]);
834 const s = e.span('git.checkout.blobs').attrs;
835 assert.deepEqual([s['git.checkout.blobs.splits'], s['git.checkout.blobs.fetches'], s['git.checkout.blobs.buffered'], s['git.checkout.blobs.streamed'], s['git.checkout.blobs.bytes']], [2, 2, 4, 1, 5]);
836 assert.equal(e.all('git.read.blob').length, 1);
837 const [rb, cb] = [e.span('git.read.blob'), e.span('git.checkout.blobs')];
838 assert.deepEqual([rb.parentSpanId, rb.links.map(l => [l.traceId, l.spanId, l.attrs['ply.link']])], [null, [[cb.traceId, cb.spanId, 'follows_from']]]);
839 assert.deepEqual(cb.links.filter(l => l.attrs['ply.link'] === 'followed_by').map(l => l.spanId), [rb.spanId], 'the blobs span, open when the stream starts, links back');
840});
842test('checkoutTree: a host refusing many wants is asked one at a time from then on; a lie is never a reason to fall back', async () => {
843 const refusing = code => remote({ hook: async req => { if (req.command === 'fetch' && req.filter === null && req.wants.length > 1) throw fail(code, `${code} refused`); } });
844 for (const code of ['http', 'remote_error', 'protocol']) {
845 const r = refusing(code); r.commit({ a: '1', b: '2', c: '3' });
846 const e = env(r);
847 const files = await drain((await unwrapAsync(checkoutTree('http://fake/r.git', {}, { ...e.opts, batch: 2 }))).files);
848 assert.deepEqual(files.map(f => f.text), ['1', '2', '3']);
849 assert.deepEqual(r.fetches().slice(1).map(f => f.wants.length), [2, 1, 1, 1], code);
850 assert.equal(e.transport.quirks.blobWants, 'single');
851 const s = e.span('git.checkout.blobs').attrs;
852 assert.deepEqual([s['git.checkout.blobs.refused'], s['git.checkout.blobs.single'], s['git.checkout.blobs.streamed'], s['git.checkout.blobs.fetches']], [code, true, 3, 0]);
853 }
854 // remembered: a transport that already knows asks for singles from the start
855 const r = remote(); r.commit({ a: '1', b: '2' });
856 const e = env(r, { quirks: { blobWants: 'single' } });
857 await drain((await unwrapAsync(checkoutTree('http://fake/r.git', {}, e.opts))).files);
858 assert.deepEqual(r.fetches().slice(1).map(f => f.wants.length), [1, 1]);
859 assert.equal(e.span('git.checkout.blobs').attrs['git.checkout.blobs.refused'], undefined);
860 // a transport without quirks still falls back for the rest of this checkout
861 const nq = refusing('http'); nq.commit({ a: '1', b: '2', c: '3' });
862 const ne = env(nq, { quirks: null });
863 assert.deepEqual((await drain((await unwrapAsync(checkoutTree('http://fake/r.git', {}, { ...ne.opts, batch: 2 }))).files)).map(f => f.text), ['1', '2', '3']);
864 assert.deepEqual(nq.fetches().slice(1).map(f => f.wants.length), [2, 1, 1, 1]);
865 // other failures propagate and are the span's outcome
866 for (const code of ['pack_corrupt', 'network']) {
867 const bad = refusing(code); bad.commit({ a: '1', b: '2' });
868 const be = env(bad);
869 await rejects(drain((await unwrapAsync(checkoutTree('http://fake/r.git', {}, be.opts))).files), code, `${code} refused`);
870 assert.equal(be.transport.quirks.blobWants, 'many'); // nothing learned: the filled default
871 const s = be.span('git.checkout.blobs');
872 assert.equal(s.attrs['git.checkout.blobs.outcome'], code);
873 assert.equal(s.status.code, 2);
874 }
875});
877test('checkoutTree: a batch pack with an unwanted object or a non-blob is pack_corrupt before any file', async () => {
878 const r = remote(); const c = r.commit({ a: '1', b: '2' });
879 const [a, b] = [hashObject('blob', te.encode('1')), hashObject('blob', te.encode('2'))];
880 const serve = list => remote({ hook: async req => (req.command === 'fetch' && req.wants.length > 1 ? packFrames((await buildPack(list)).pack) : undefined) });
881 const copy = s => { for (const [k, v] of r.objects) s.objects.set(k, v); s.refs.set('refs/heads/main', c); return s; };
882 const extra = copy(serve([r.objects.get(a), r.objects.get(b), { type: TYPE.BLOB, data: te.encode('3') }]));
883 const seen = [];
884 await rejects((async () => { for await (const f of (await unwrapAsync(checkoutTree('http://fake/r.git', {}, env(extra).opts))).files) seen.push(f.path); })(), 'pack_corrupt',
885 `object ${hashObject('blob', te.encode('3'))} served but not wanted (2 blobs asked for)`);
886 assert.deepEqual(seen, []);
887 const tree = copy(serve([r.objects.get(a), { type: TYPE.TREE, data: new Uint8Array(0) }]));
888 await rejects(drain((await unwrapAsync(checkoutTree('http://fake/r.git', {}, env(tree).opts))).files), 'pack_corrupt', `object ${EMPTY_TREE} served but not wanted (2 blobs asked for)`);
889 // a wanted oid whose object is a tree (the server lies about a blob entry)
890 const t2 = remote(); const treeOid = t2.put('tree', new Uint8Array(0));
891 const blobOid = t2.put('blob', 'x');
892 t2.refs.set('refs/heads/main', t2.put('commit', unwrap(encodeCommit({ tree: t2.put('tree', unwrap(encodeTree([{ mode: '100644', name: 'f', oid: treeOid }, { mode: '100644', name: 'g', oid: blobOid }]))), author: 'A <a@x> 1 +0000', message: 'm\n' }))));
893 await rejects(drain((await unwrapAsync(checkoutTree('http://fake/r.git', {}, env(t2).opts))).files), 'pack_corrupt', `expected blob ${treeOid}, got tree`);
894 // a wanted blob the pack leaves out is streamed on its own
895 const short = copy(serve([r.objects.get(a)]));
896 const se = env(short);
897 assert.deepEqual((await drain((await unwrapAsync(checkoutTree('http://fake/r.git', {}, se.opts))).files)).map(f => f.text), ['1', '2']);
898 const s = se.span('git.checkout.blobs').attrs;
899 assert.deepEqual([s['git.checkout.blobs.buffered'], s['git.checkout.blobs.streamed']], [1, 1]);
900});
902test('checkoutTree: stopping early ends the blobs span as cancelled', async () => {
903 const r = remote(); r.commit({ a: '1', b: '2' });
904 const e = env(r);
905 const res = await unwrapAsync(checkoutTree('http://fake/r.git', {}, e.opts));
906 for await (const f of res.files) { assert.equal(f.path, 'a'); break; }
907 const s = e.span('git.checkout.blobs');
908 assert.equal(s.attrs['git.checkout.blobs.outcome'], 'cancelled');
909 assert.equal(s.status.code, 1);
910 // gitlinks only: no fetch at all
911 const g = remote(); g.commit({ s: { mode: '160000', oid: GITLINK } });
912 const ge = env(g);
913 assert.deepEqual(await drain((await unwrapAsync(checkoutTree('http://fake/r.git', {}, ge.opts))).files), [{ path: 's', mode: '160000', oid: GITLINK, size: 0, text: null }]);
914 assert.equal(g.fetches().length, 1);
915 assert.deepEqual([ge.span('git.checkout.blobs').attrs['git.checkout.blobs.unique'], ge.span('git.checkout.blobs').attrs['git.checkout.blobs.outcome']], [0, 'ok']);
916});
918test('checkoutTree: walk limits come from opts.limits', async () => {
919 const r = remote(); r.commit(FILES());
920 await rejects(unwrapAsync(checkoutTree('http://fake/r.git', {}, { ...env(r).opts, batch: 7, limits: withLimits({ maxPackObjects: 7 }) })), 'limit_exceeded', 'checkout entries 8 exceeds limit 7');
921 assert.equal((await unwrapAsync(checkoutTree('http://fake/r.git', {}, { ...env(r).opts, batch: 8, limits: withLimits({ maxPackObjects: 8 }) }))).entries.length, 6);
922 await rejects(unwrapAsync(checkoutTree('http://fake/r.git', {}, { ...env(r).opts, limits: withLimits({ maxPathDepth: 2 }) })), 'limit_exceeded', 'path depth 3 exceeds limit 2');
923 await rejects(unwrapAsync(checkoutTree('http://fake/r.git', { path: 'src' }, { ...env(r).opts, limits: withLimits({ maxPathDepth: 2 }) })), 'limit_exceeded', 'path depth 3 exceeds limit 2');
924 assert.equal((await unwrapAsync(checkoutTree('http://fake/r.git', { path: 'src/lib' }, { ...env(r).opts, limits: withLimits({ maxPathDepth: 3 }) }))).entries.length, 1);
925});
927// ======================================================================================================================
928// read.js
929// ======================================================================================================================
931test('refCandidates, entryType and lsMode', () => {
932 for (const ref of [null, undefined, '', 'HEAD']) assert.deepEqual(unwrap(refCandidates(ref)), ['HEAD']);
933 assert.deepEqual(unwrap(refCandidates('a'.repeat(40))), []);
934 assert.deepEqual(unwrap(refCandidates('A'.repeat(40))), ['refs/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', 'refs/tags/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', 'refs/heads/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA']);
935 assert.deepEqual(unwrap(refCandidates('a'.repeat(39))), [`refs/${'a'.repeat(39)}`, `refs/tags/${'a'.repeat(39)}`, `refs/heads/${'a'.repeat(39)}`]);
936 assert.deepEqual(unwrap(refCandidates('refs/heads/x')), ['refs/heads/x']);
937 assert.deepEqual(unwrap(refCandidates('main')), ['refs/main', 'refs/tags/main', 'refs/heads/main']);
938 assert.deepEqual(unwrap(refCandidates('heads/main')), ['refs/heads/main', 'refs/tags/heads/main', 'refs/heads/heads/main']);
939 assert.deepEqual(unwrap(refCandidates('xrefs/a')), ['refs/xrefs/a', 'refs/tags/xrefs/a', 'refs/heads/xrefs/a']);
940 throws(() => unwrap(refCandidates(5)), 'bad_request', 'ref must be a string');
941 throws(() => unwrap(refCandidates({})), 'bad_request', 'ref must be a string');
942 throws(() => unwrap(refCandidates('a..b')), 'bad_request', undefined, { what: 'ref' });
943 throws(() => unwrap(refCandidates('refs/a b')), 'bad_request', undefined, { what: 'ref' });
944 assert.deepEqual(['40000', '160000', '100644', '120000', '100755'].map(entryType), ['tree', 'commit', 'blob', 'blob', 'blob']);
945 assert.deepEqual(['40000', '100644', '160000', '1'].map(lsMode), ['040000', '100644', '160000', '000001']);
946});
948test('resolveCommit: HEAD\'s symref, a detached HEAD, tags over branches, peeled tags, the not-found messages', async () => {
949 const r = remote(); const c = r.commit({ a: '1' });
950 const c2 = r.commit({ a: '2' }, { ref: 'refs/heads/v1' });
951 const tag = r.put('tag', unwrap(encodeTag({ object: c, type: 'commit', tag: 'v1', tagger: 'A <a@x> 1 +0000', message: 'r\n' })));
952 r.refs.set('refs/tags/v1', tag);
953 const t = r.transport({ tracer: new Tracer() });
954 assert.deepEqual(await unwrapAsync(resolveCommit(t, null)), { name: 'refs/heads/main', commit: c });
955 assert.deepEqual(await unwrapAsync(resolveCommit(t, 'HEAD')), { name: 'refs/heads/main', commit: c });
956 assert.deepEqual(await unwrapAsync(resolveCommit(t, 'v1')), { name: 'refs/tags/v1', commit: c }, 'the tag wins and is peeled');
957 assert.deepEqual(await unwrapAsync(resolveCommit(t, 'refs/heads/v1')), { name: 'refs/heads/v1', commit: c2 });
958 assert.deepEqual(await unwrapAsync(resolveCommit(t, 'heads/v1')), { name: 'refs/heads/v1', commit: c2 });
959 assert.deepEqual(await unwrapAsync(resolveCommit(t, c2)), { name: c2, commit: c2 });
960 assert.deepEqual(r.log.filter(x => x.command === 'ls-refs').map(x => x.prefixes), [['HEAD'], ['HEAD'], ['refs/v1', 'refs/tags/v1', 'refs/heads/v1'], ['refs/heads/v1'], ['refs/heads/v1', 'refs/tags/heads/v1', 'refs/heads/heads/v1']]);
961 await rejects(unwrapAsync(resolveCommit(t, 'nope')), 'not_found', 'ref nope not found', { miss: { kind: 'ref', ref: 'nope', candidates: ['refs/nope', 'refs/tags/nope', 'refs/heads/nope'] } });
962 await rejects(unwrapAsync(resolveCommit(t, 'refs/heads/nope')), 'not_found', 'ref refs/heads/nope not found', { miss: { kind: 'ref', ref: 'refs/heads/nope', candidates: ['refs/heads/nope'] } });
963 const detached = remote({ head: c }); detached.objects.set(c, r.objects.get(c));
964 assert.deepEqual(await unwrapAsync(resolveCommit(detached.transport({ tracer: new Tracer() }), null)), { name: 'HEAD', commit: c });
965 const unborn = remote();
966 await rejects(unwrapAsync(resolveCommit(unborn.transport({ tracer: new Tracer() }), 'HEAD')), 'not_found', 'remote HEAD is unborn or absent (some hosts point HEAD at a deleted branch); pass ref explicitly', { miss: { kind: 'head_absent' } });
967 assert.equal(unborn.log.filter(x => x.command === 'ls-refs').length, 1, 'no ls-refs=unborn: the miss does not ask again');
968});
970test('an advertised unborn HEAD is empty for readCommit and head_unborn otherwise', async () => {
971 const target = 'refs/heads/main';
972 const advertise = async () => pkts(['version 2\n', 'ls-refs=unborn\n', 'fetch\n', 'object-format=sha1\n']);
973 const unbornLine = (text) => [dataFrame(text), { kind: 'flush' }];
974 const server = (text) => {
975 // an unborn answer that is not prefixed HEAD is the server's ordinary listing: the second ls-refs must ask for HEAD
976 const r = remote({ hook: async req => (req.command === 'ls-refs' && req.unborn && req.prefixes.includes('HEAD') ? unbornLine(text) : undefined) });
977 const ex = arrayExporter();
978 const tracer = new Tracer({ exporter: ex });
979 const transport = { ...r.transport({ tracer }), advertise };
980 return { r, opts: { tracer, transport }, transport, spans: () => ex.lines.flatMap(flattenLine) };
981 };
982 const born = server(`unborn HEAD symref-target:${target}\n`);
983 for (const ref of [null, undefined, '', 'HEAD', 'main', 'refs/heads/main']) {
984 assert.deepEqual(await unwrapAsync(readCommit('http://fake/r.git', { ref }, born.opts)), { empty: true, ref: target }, String(ref));
985 }
986 assert.ok(born.spans().filter(s => s.name === 'git.read_commit').every(s => s.attrs['git.read_commit.outcome'] === 'ok'));
987 await rejects(unwrapAsync(readFile('http://fake/r.git', { path: 'README' }, born.opts)), 'not_found', 'remote HEAD is unborn or absent (some hosts point HEAD at a deleted branch); pass ref explicitly', { miss: { kind: 'head_unborn', target } });
988 await rejects(unwrapAsync(resolveCommit(born.transport, 'other')), 'not_found', 'ref other not found', { miss: { kind: 'ref', ref: 'other', candidates: ['refs/other', 'refs/tags/other', 'refs/heads/other'] } });
989 await rejects(unwrapAsync(resolveCommit(born.transport, 'refs/heads/other')), 'not_found', 'ref refs/heads/other not found', { miss: { kind: 'ref', ref: 'refs/heads/other', candidates: ['refs/heads/other'] } });
990 await rejects(unwrapAsync(readCommit('http://fake/r.git', { ref: 'other' }, born.opts)), 'not_found', 'ref other not found', { miss: { kind: 'ref', ref: 'other', candidates: ['refs/other', 'refs/tags/other', 'refs/heads/other'] } });
991 const plain = { code: 'not_found', miss: { kind: 'head_unborn', target } };
992 const liar = remote({ hook: async req => { if (req.command === 'ls-refs') throw plain; } });
993 await assert.rejects(unwrapAsync(readCommit('http://fake/r.git', {}, { tracer: new Tracer(), transport: { ...liar.transport({ tracer: new Tracer() }), advertise } })), e => e === plain);
994 // a primitive a transport raises is no object to code: coded() builds the exchange's error around it (a low-level
995 // call on the transport itself: nothing between the transport and the exchange)
996 const raw = remote({ hook: async req => { if (req.command === 'ls-refs') throw 'reset'; } });
997 const rawLow = errOf(await resolveCommit({ ...raw.transport({ tracer: new Tracer() }), advertise }, 'HEAD'));
998 assert.deepEqual([rawLow.code, rawLow.message, rawLow.cause], ['protocol', 'reset', 'reset']);
999 // handed to an operation as its `transport`, the same raise is the caller's own code failing on a read request: the
1000 // Err names the method and carries the primitive as its cause (README "Your own code failing": bad_request)
1001 const rawErr = errOf(await readCommit('http://fake/r.git', {}, { tracer: new Tracer(), transport: { ...raw.transport({ tracer: new Tracer() }), advertise } }));
1002 assert.deepEqual([rawErr.code, rawErr.what, rawErr.cause], ['bad_request', 'transport.frames', 'reset']);
1003 const typed = remote({ hook: async req => { if (req.command === 'ls-refs') throw fail('pack_corrupt', 'odd', { miss: { kind: 'head_unborn', target } }); } });
1004 await rejects(unwrapAsync(readCommit('http://fake/r.git', {}, { tracer: new Tracer(), transport: { ...typed.transport({ tracer: new Tracer() }), advertise } })), 'pack_corrupt', 'odd');
1005 const blank = server('unborn HEAD symref-target:\n');
1006 await rejects(unwrapAsync(resolveCommit(blank.transport, 'HEAD')), 'not_found', 'remote HEAD is unborn or absent (some hosts point HEAD at a deleted branch); pass ref explicitly', { miss: { kind: 'head_absent' } });
1007 await rejects(unwrapAsync(resolveCommit(born.transport, undefined)), 'not_found', 'remote HEAD is unborn or absent (some hosts point HEAD at a deleted branch); pass ref explicitly', { miss: { kind: 'head_unborn', target } });
1008 const quiet = remote({ hook: async req => (req.command === 'ls-refs' && req.unborn ? [{ kind: 'flush' }] : undefined) });
1009 const quietT = { ...quiet.transport({ tracer: new Tracer() }), advertise };
1010 await rejects(unwrapAsync(resolveCommit(quietT, null)), 'not_found', 'remote HEAD is unborn or absent (some hosts point HEAD at a deleted branch); pass ref explicitly', { miss: { kind: 'head_absent' } });
1011 const held = remote(); held.commit(FILES());
1012 const walked = await walkPath(held.transport({ tracer: new Tracer() }), GIT.root, ['nope']);
1013 assert.equal(walked.ok, false);
1014 if (!walked.ok) assert.deepEqual(walked.error.miss, { kind: 'path', missing: 'nope', existingPath: '' });
1015 const cache = new Map();
1016 cache.get = () => { throw 'boom'; };
1017 await assert.rejects(walkPath(held.transport({ tracer: new Tracer() }), GIT.root, ['x'], { cache }), e => e === 'boom');
1018});
1020test('commitView: idents for JSON, signed, parents and message', () => {
1021 const tree = GIT.root;
1022 const view = unwrap(commitView(unwrap(encodeCommit({ tree, parents: [GIT.commit, GIT.commit2], author: 'Ada L <ada@x> 1700000000 +0530', committer: 'Bob <b@y> 0 -0100', message: 'subject\n\nbody\n' }))));
1023 assert.deepEqual(view, {
1024 tree, parents: [GIT.commit, GIT.commit2],
1025 author: { name: 'Ada L', email: 'ada@x', time: '2023-11-14T22:13:20Z', epoch: 1700000000, tz: '+0530' },
1026 committer: { name: 'Bob', email: 'b@y', time: '1970-01-01T00:00:00Z', epoch: 0, tz: '-0100' },
1027 message: 'subject\n\nbody\n', signed: false,
1028 });
1029 // milliseconds are never part of the time: whole seconds only
1030 assert.equal(unwrap(commitView(unwrap(encodeCommit({ tree, author: 'A <a@x> 1 +0000' })))).author.time, '1970-01-01T00:00:01Z');
1031 assert.equal(unwrap(commitView(unwrap(encodeCommit({ tree, author: 'A <a@x> 1 +0000' })))).message, '');
1032 const sig = '-----BEGIN SSH SIGNATURE-----\nU0lH\n-----END SSH SIGNATURE-----';
1033 const raw = (header) => te.encode(`tree ${tree}\nauthor A <a@x> 1 +0000\ncommitter A <a@x> 1 +0000\n${header}\n\nm\n`);
1034 assert.equal(unwrap(commitView(raw(`gpgsig ${sig.replace(/\n/g, '\n ')}`))).signed, true);
1035 assert.equal(unwrap(commitView(raw(`gpgsig-sha256 ${sig.replace(/\n/g, '\n ')}`))).signed, true);
1036 assert.equal(unwrap(commitView(raw('mergetag object x'))).signed, false);
1037 assert.equal(unwrap(commitView(raw('xgpgsig y'))).signed, false);
1038 assert.equal(unwrap(commitView(raw('gpgsigx y'))).signed, false);
1039 // a date fsck accepts but a JS Date cannot hold: time null, the epoch and the rest kept
1040 const odd = unwrap(commitView(te.encode(`tree ${tree}\nauthor A <a@x> 1 +0000\ncommitter A <a@x> 100000000000000000 +0000\n\nm\n`), withLimits({})));
1041 assert.deepEqual(odd.committer, { name: 'A', email: 'a@x', time: null, epoch: 1e17, tz: '+0000' });
1042 assert.equal(odd.message, 'm\n');
1043 // a commit fsck refuses is refused here too (#59): commitView reads through parseCommit
1044 assert.throws(() => unwrap(commitView(te.encode(`tree ${tree}\nauthor A <a@x>\ncommitter A <a@x> 1 +0000\n\nm\n`))), e => e.code === 'pack_corrupt' && e.msgId === 'missingSpaceBeforeDate');
1045 assert.throws(() => unwrap(commitView(te.encode(`tree ${tree}\nauthor A <a@x> 1 +0000\ncommitter A <a@x> 1 +0000\n\nm\0\n`))), e => e.code === 'pack_corrupt' && e.msgId === 'nulInCommit');
1046});
1048test('readCommit: the commit alone (tree:0, deepen 1), its view and span; a non-commit oid is not_found', async () => {
1049 const r = remote(); const c = r.commit(FILES());
1050 const e = env(r);
1051 const res = await unwrapAsync(readCommit('http://fake/r.git', {}, e.opts));
1052 assert.deepEqual(res, {
1053 ref: 'refs/heads/main', oid: c, tree: GIT.root, parents: [],
1054 author: { name: 'A', email: 'a@x', time: '2023-11-14T22:13:20Z', epoch: T0, tz: '+0000' }, committer: { name: 'A', email: 'a@x', time: '2023-11-14T22:13:20Z', epoch: T0, tz: '+0000' },
1055 message: 'deploy\n', signed: false,
1056 });
1057 assert.deepEqual(r.fetches().map(f => [f.wants, f.filter, f.deepen]), [[[c], 'tree:0', 1]]);
1058 const s = e.span('git.read_commit').attrs;
1059 assert.deepEqual([s['git.read_commit.ref'], s['git.read_commit.oid'], s['git.read_commit.parents'], s['git.read_commit.signed'], s['git.read_commit.outcome']], ['HEAD', c, 0, false, 'ok']);
1060 const child = r.commit({ x: '1' }, { parents: [c] });
1061 const e2 = env(r);
1062 assert.deepEqual((await unwrapAsync(readCommit('http://fake/r.git', { ref: 'main' }, e2.opts))).parents, [c]);
1063 assert.deepEqual([e2.span('git.read_commit').attrs['git.read_commit.ref'], e2.span('git.read_commit').attrs['git.read_commit.parents']], ['main', 1]);
1064 assert.equal((await unwrapAsync(readCommit('http://fake/r.git', { ref: child }, env(r).opts))).oid, child);
1065 await rejects(unwrapAsync(readCommit('http://fake/r.git', { ref: GIT.root }, env(r).opts)), 'not_found', `${GIT.root} is not a commit`, { miss: { kind: 'commit', miss: { kind: 'not_commit', oid: GIT.root, type: 'tree' } } });
1066 const absentId = 'f'.repeat(40);
1067 await rejects(unwrapAsync(readCommit('http://fake/r.git', { ref: absentId }, env(r).opts)), 'not_found', `commit ${absentId} not found on the remote`, { miss: { kind: 'commit', miss: { kind: 'absent', oid: absentId } } });
1068 const tagData = unwrap(encodeTag({ object: c, type: 'commit', tag: 'v1', tagger: 'A <a@x> 1 +0000', message: 't\n' }));
1069 const tagOid = hashObject('tag', tagData);
1070 r.objects.set(tagOid, { type: TYPE.TAG, data: tagData });
1071 const tagged = await fetchCommit(r.transport({ tracer: new Tracer() }), tagOid);
1072 assert.equal(tagged.ok, false);
1073 if (!tagged.ok) assert.deepEqual({ code: tagged.error.code, message: tagged.error.message, miss: tagged.error.miss }, { code: 'not_found', message: `${tagOid} is not a commit`, miss: { kind: 'commit', miss: { kind: 'not_commit', oid: tagOid, type: 'tag' } } });
1074 const blobbed = await fetchCommit(r.transport({ tracer: new Tracer() }), GIT.readme);
1075 assert.equal(blobbed.ok, false);
1076 if (!blobbed.ok) assert.deepEqual(blobbed.error.miss, { kind: 'commit', miss: { kind: 'not_commit', oid: GIT.readme, type: 'blob' } });
1077 const other = remote({ hook: async req => (req.command === 'fetch' ? [dataFrame('ERR upload-pack: busy')] : undefined) }); other.refs.set('refs/heads/main', c);
1078 await rejects(unwrapAsync(readCommit('http://fake/r.git', {}, env(other).opts)), 'remote_error', 'remote: upload-pack: busy');
1079 const busy = await fetchCommit(other.transport({ tracer: new Tracer() }), c);
1080 assert.equal(busy.ok, false);
1081 if (!busy.ok) { assert.equal(busy.error.code, 'remote_error'); assert.equal(busy.error.miss, undefined); }
1082 const corrupt = remote({ hook: async req => (req.command === 'fetch' ? packFrames((await buildPack([r.objects.get(GIT.readme)])).pack) : undefined) }); corrupt.refs.set('refs/heads/main', c);
1083 await rejects(unwrapAsync(readCommit('http://fake/r.git', {}, env(corrupt).opts)), 'pack_corrupt', `object ${GIT.readme} served for want ${c}`);
1084 const lied = await fetchCommit(corrupt.transport({ tracer: new Tracer() }), c);
1085 assert.equal(lied.ok, false);
1086 if (!lied.ok) { assert.equal(lied.error.code, 'pack_corrupt'); assert.equal(lied.error.message, `object ${GIT.readme} served for want ${c}`); assert.equal(lied.error.miss, undefined); }
1087 const base = r.transport({ tracer: new Tracer() });
1088 const odd = { ...base, frames: async function* frames() { const e = new Error('odd'); e.code = 'pack_corrupt'; e.reason = 'not_our_ref'; throw e; } };
1089 const oddCommit = await fetchCommit(odd, c);
1090 assert.equal(oddCommit.ok, false);
1091 if (!oddCommit.ok) assert.equal(oddCommit.error.code, 'pack_corrupt');
1092});
1094test('listTree: the root, a subdirectory, cache hits and every not_found', async () => {
1095 const r = remote(); const c = r.commit(FILES());
1096 const e = env(r);
1097 const res = await unwrapAsync(listTree('http://fake/r.git', {}, e.opts));
1098 assert.deepEqual([res.ref, res.commit, res.tree], ['refs/heads/main', c, GIT.root]);
1099 assert.deepEqual(res.entries, [
1100 { name: 'README', mode: '100644', oid: GIT.readme, type: 'blob' }, { name: 'link', mode: '120000', oid: GIT.link, type: 'blob' },
1101 { name: 'run.sh', mode: '100755', oid: GIT.run, type: 'blob' }, { name: 'src', mode: '040000', oid: GIT.src, type: 'tree' },
1102 { name: 'sub', mode: '160000', oid: GITLINK, type: 'commit' },
1103 ]);
1104 const s = e.span('git.ls_tree').attrs;
1105 assert.deepEqual([s['git.ls_tree.ref'], s['git.ls_tree.path'], s['git.ls_tree.path.depth'], s['git.ls_tree.tree'], s['git.ls_tree.entries'], s['git.ls_tree.outcome'], s['git.read.commit'], s['git.read.tree.root']],
1106 ['HEAD', '', 0, GIT.root, 5, 'ok', c, GIT.root]);
1107 const cache = new Map();
1108 const e2 = env(r);
1109 const sub = await unwrapAsync(listTree('http://fake/r.git', { ref: 'main', path: 'src/lib/' }, { ...e2.opts, cache }));
1110 assert.deepEqual([sub.tree, sub.entries], [GIT.lib, [{ name: 'b.js', mode: '100644', oid: GIT.b, type: 'blob' }]]);
1111 assert.deepEqual(e2.all('git.read.tree').map(x => [x.attrs['git.read.tree.depth'], x.attrs['git.read.tree.oid'], x.attrs['git.read.tree.lookup'], x.attrs['git.read.tree.cache_hit'], x.attrs['git.read.tree.entries'], x.attrs['git.read.tree.entry.found'], x.attrs['git.read.tree.entry.mode']]),
1112 [[0, GIT.root, 'src', false, 5, true, '40000'], [1, GIT.src, 'lib', false, 2, true, '40000']]);
1113 assert.deepEqual([e2.span('git.ls_tree').attrs['git.ls_tree.path'], e2.span('git.ls_tree').attrs['git.ls_tree.path.depth']], ['src/lib', 2]);
1114 assert.deepEqual([...cache.keys()], [GIT.root, GIT.src, GIT.lib]);
1115 // the same listing again: no tree fetched, every level a cache hit
1116 const before = r.fetches().length;
1117 const e3 = env(r);
1118 await unwrapAsync(listTree('http://fake/r.git', { path: 'src/lib' }, { ...e3.opts, cache }));
1119 assert.equal(r.fetches().length, before + 1, 'only the commit');
1120 assert.deepEqual(e3.all('git.read.tree').map(x => x.attrs['git.read.tree.cache_hit']), [true, true]);
1121 for (const path of [undefined, null, '', '/']) assert.equal((await unwrapAsync(listTree('http://fake/r.git', { path }, env(r).opts))).tree, GIT.root);
1122 await rejects(unwrapAsync(listTree('http://fake/r.git', { path: 'README' }, env(r).opts)), 'not_found', 'README is not a directory (mode 100644)', { miss: { kind: 'not_directory', path: 'README', mode: '100644' } });
1123 await rejects(unwrapAsync(listTree('http://fake/r.git', { path: 'sub' }, env(r).opts)), 'not_found', 'sub is not a directory (mode 160000)', { miss: { kind: 'not_directory', path: 'sub', mode: '160000' } });
1124 await rejects(unwrapAsync(listTree('http://fake/r.git', { path: 'README/x' }, env(r).opts)), 'not_found', 'README is not a directory', { miss: { kind: 'not_directory', path: 'README', mode: '100644' } });
1125 await rejects(unwrapAsync(listTree('http://fake/r.git', { path: 'src/a.js' }, env(r).opts)), 'not_found', 'src/a.js is not a directory (mode 100644)', { miss: { kind: 'not_directory', path: 'src/a.js', mode: '100644' } });
1126 await rejects(unwrapAsync(listTree('http://fake/r.git', { path: 'src/a.js/x' }, env(r).opts)), 'not_found', 'a.js is not a directory', { miss: { kind: 'not_directory', path: 'src/a.js', mode: '100644' } });
1127 const miss = env(r);
1128 await rejects(unwrapAsync(listTree('http://fake/r.git', { path: 'src/nope' }, miss.opts)), 'not_found', 'path component nope not found', { miss: { kind: 'path', missing: 'nope', existingPath: 'src' } });
1129 await rejects(unwrapAsync(listTree('http://fake/r.git', { path: 'src/lib/nope' }, env(r).opts)), 'not_found', 'path component nope not found', { miss: { kind: 'path', missing: 'nope', existingPath: 'src/lib' } });
1130 const last = miss.all('git.read.tree').at(-1).attrs;
1131 assert.deepEqual([last['git.read.tree.entry.found'], last['git.read.tree.entry.mode']], [false, '']);
1132 assert.equal(miss.span('git.ls_tree').attrs['git.ls_tree.outcome'], 'not_found');
1133 await rejects(unwrapAsync(listTree('http://fake/r.git', { path: '.git' }, env(r).opts)), 'bad_request');
1134});
1136test('readFile: the blob streamed and verified, its size first; directories and submodules are not files', async () => {
1137 const r = remote({ chunk: 7 }); const c = r.commit({ ...FILES(), 'big.txt': 'x'.repeat(5000) });
1138 const e = env(r);
1139 const res = await unwrapAsync(readFile('http://fake/r.git', { path: 'src/lib/b.js' }, e.opts));
1140 assert.deepEqual([res.ref, res.commit, res.blob, res.mode, res.size], ['refs/heads/main', c, GIT.b, '100644', 2]);
1141 assert.equal(await readAll(res.stream), 'b\n');
1142 const root = e.span('git.read').attrs;
1143 assert.deepEqual([root['git.read.ref'], root['git.read.path'], root['git.read.path.depth'], root['git.read.blob'], root['git.read.blob.size'], root['git.read.blob.mode'], root['git.read.outcome']],
1144 ['HEAD', 'src/lib/b.js', 3, GIT.b, 2, '100644', 'ok']);
1145 const blob = e.span('git.read.blob');
1146 assert.deepEqual([blob.attrs['git.read.blob.oid'], blob.attrs['git.read.blob.size'], blob.attrs['git.read.blob.bytes'], blob.attrs['git.read.blob.verified'], blob.attrs['git.read.blob.outcome']], [GIT.b, 2, 2, true, 'ok']);
1147 const readRoot = e.span('git.read');
1148 assert.deepEqual([blob.parentSpanId, blob.links.map(l => [l.traceId, l.spanId, l.attrs['ply.link']])], [null, [[readRoot.traceId, readRoot.spanId, 'follows_from']]]);
1149 assert.deepEqual(readRoot.links.map(l => [l.spanId, l.attrs['ply.link']]), [[blob.spanId, 'followed_by']]);
1150 // a big blob in 7-byte network chunks
1151 const big = await unwrapAsync(readFile('http://fake/r.git', { ref: 'main', path: 'big.txt' }, env(r).opts));
1152 assert.equal(big.size, 5000);
1153 assert.equal(await readAll(big.stream), 'x'.repeat(5000));
1154 const link = await unwrapAsync(readFile('http://fake/r.git', { path: 'link' }, env(r).opts));
1155 assert.deepEqual([link.mode, await readAll(link.stream)], ['120000', 'README']);
1156 // a trailing '/' names a directory (git: `git cat-file -t HEAD:run.sh/` is "path 'run.sh/' does not exist"), so a
1157 // file path that ends in one is refused before any request (#300-8; it was read as run.sh before)
1158 const slash = await readFile('http://fake/r.git', { path: 'run.sh/' }, env(r).opts);
1159 assert.equal(slash.ok, false);
1160 assert.deepEqual([slash.error.code, slash.error.what], ['bad_request', 'path']);
1161 const exe = await unwrapAsync(readFile('http://fake/r.git', { path: 'run.sh' }, env(r).opts));
1162 assert.equal(exe.mode, '100755');
1163 await readAll(exe.stream);
1164 await rejects(unwrapAsync(readFile('http://fake/r.git', { path: 'src' }, env(r).opts)), 'not_found', 'src is a directory', { miss: { kind: 'is_directory', path: 'src', tree: GIT.src } });
1165 await rejects(unwrapAsync(readFile('http://fake/r.git', { path: 'src/lib' }, env(r).opts)), 'not_found', 'src/lib is a directory', { miss: { kind: 'is_directory', path: 'src/lib', tree: GIT.lib } });
1166 await rejects(unwrapAsync(readFile('http://fake/r.git', { path: 'sub' }, env(r).opts)), 'not_found', `sub is a submodule (commit ${GITLINK})`, { miss: { kind: 'submodule', path: 'sub', commit: GITLINK } });
1167 const nested = remote(); nested.commit({ ...FILES(), 'nest/mod': { mode: '160000', oid: GITLINK } });
1168 await rejects(unwrapAsync(readFile('http://fake/r.git', { path: 'nest/mod' }, env(nested).opts)), 'not_found', `nest/mod is a submodule (commit ${GITLINK})`, { miss: { kind: 'submodule', path: 'nest/mod', commit: GITLINK } });
1169 for (const path of [undefined, '', '/']) await rejects(unwrapAsync(readFile('http://fake/r.git', { path }, env(r).opts)), 'bad_request', undefined);
1170 await rejects(unwrapAsync(readFile('http://fake/r.git', {}, env(r).opts)), 'bad_request');
1171});
1173// a one-object pack, by hand: `size` is what the header claims, `content` what the zlib stream holds
1174async function onePack(type, size, content) {
1175 const body = concat([Uint8Array.of(0x50, 0x41, 0x43, 0x4b, 0, 0, 0, 2, 0, 0, 0, 1), encodeObjHeader(type, size), await zlibDeflate(te.encode(content))]);
1176 return concat([body, new Sha1().update(body).digest()]);
1177}
1179test('openBlob: every way a streamed blob can lie is pack_corrupt on the stream, and the span says so', async () => {
1180 const oid = hashObject('blob', te.encode('hello'));
1181 const run = async (frames, want = oid) => {
1182 const r = remote({ hook: async () => frames });
1183 const ex = arrayExporter(); const tracer = new Tracer({ exporter: ex });
1184 const parent = tracer.start('p');
1185 const t = r.transport({ tracer });
1186 return { open: () => openBlob(t, want, { follows: parent }), spans: () => ex.lines.flatMap(flattenLine) };
1187 };
1188 const good = await run(packFrames(await onePack(TYPE.BLOB, 5, 'hello'), 3));
1189 const ok = streamOf(await good.open());
1190 assert.equal(ok.size, 5);
1191 assert.equal(await readAll(ok.stream), 'hello');
1192 const okSpan = good.spans().find(s => s.name === 'git.read.blob');
1193 assert.deepEqual([okSpan.attrs['git.read.blob.bytes'], okSpan.attrs['git.read.blob.verified'], okSpan.attrs['git.read.blob.outcome']], [5, true, 'ok']);
1194 assert.ok(['digeststream', 'js'].includes(okSpan.attrs['git.read.blob.hasher']));
1195 assert.equal(good.spans().find(s => s.name === 'git.upload-pack fetch').status.code, 1);
1196 const cases = [
1197 ['more than declared', await onePack(TYPE.BLOB, 3, 'hello'), 'server streamed more than the 3 bytes it declared'],
1198 ['fewer than declared', await onePack(TYPE.BLOB, 9, 'hello'), 'inflated 5 != declared 9'],
1199 ['other content', await onePack(TYPE.BLOB, 5, 'jello'), `blob content hashes to ${hashObject('blob', te.encode('jello'))}, not ${oid}`],
1200 ];
1201 for (const [label, pack, message] of cases) {
1202 const c = await run(packFrames(pack, 4));
1203 const { stream } = streamOf(await c.open());
1204 await rejects(readAll(stream), 'pack_corrupt', message);
1205 const s = c.spans().find(x => x.name === 'git.read.blob');
1206 assert.equal(s.attrs['git.read.blob.outcome'], 'pack_corrupt', label);
1207 assert.equal(s.status.code, 2, label);
1208 assert.equal(s.status.message, message, label);
1209 }
1210 // header-phase failures are the Result, and the spans still record them
1211 const tree = await run(packFrames(await onePack(TYPE.TREE, 0, '')));
1212 const treeErr = errOf(await tree.open());
1213 assert.equal(treeErr.code, 'pack_corrupt'); assert.equal(treeErr.message, 'expected blob, got tree');
1214 assert.equal(tree.spans().find(x => x.name === 'git.read.blob').attrs['git.read.blob.outcome'], 'pack_corrupt');
1215 assert.equal(tree.spans().find(x => x.name === 'git.upload-pack fetch').status.code, 2);
1216 const commit = await run(packFrames(await onePack(TYPE.COMMIT, 0, '')));
1217 assert.equal(errOf(await commit.open()).message, 'expected blob, got commit');
1218 const empty = await run([dataFrame('packfile\n'), band(1, Uint8Array.of(0x50, 0x41)), { kind: 'flush' }]);
1219 assert.equal(errOf(await empty.open()).message, 'pack ended before the object header');
1220 const err = await run([dataFrame('ERR upload-pack: not our ref x')]);
1221 const remoteErr = errOf(await err.open());
1222 assert.equal(remoteErr.code, 'remote_error'); assert.equal(remoteErr.message, 'remote: upload-pack: not our ref x');
1223 assert.equal(err.spans().find(x => x.name === 'git.read.blob').attrs['git.read.blob.outcome'], 'remote_error');
1224 // a failure after the header errors the stream typed, and still ends both spans with it (they used to be left open and never exported)
1225 const pack = await onePack(TYPE.BLOB, 5, 'hello'); pack[pack.length - 1] ^= 1;
1226 const midway = async (label, frames, code, message) => {
1227 const c = await run(frames);
1228 await rejects(readAll(streamOf(await c.open()).stream), code, message);
1229 await new Promise(resolve => setTimeout(resolve, 0));
1230 const [blob, fetch] = ['git.read.blob', 'git.upload-pack fetch'].map(n => c.spans().find(x => x.name === n));
1231 assert.ok(blob && fetch, `${label}: both spans exported`);
1232 assert.equal(blob.attrs['git.read.blob.outcome'], code, label);
1233 assert.match(blob.status.message, message instanceof RegExp ? message : new RegExp(`^${message}$`), label);
1234 assert.deepEqual([blob.status.code, fetch.status.code, blob.attrs['git.read.blob.verified']], [2, 2, undefined], label);
1235 };
1236 await midway('trailer', packFrames(pack, 5), 'pack_corrupt', /^pack: trailer [0-9a-f]{40} != [0-9a-f]{40}$/);
1237 const cut = async function* (err) { const all = packFrames(await onePack(TYPE.BLOB, 5, 'hello'), 25); yield all[0]; yield all[1]; throw err; };
1238 await midway('network', cut(fail('network', 'network: reset')), 'network', 'network: reset');
1239 // an untyped failure is typed on the way out
1240 await midway('untyped', cut(new Error('socket hang up')), 'pack_corrupt', 'blob stream: socket hang up');
1241});
1243// Pull until the stream ends or errors. The chunks are what the consumer was given; an error keeps the ones already released.
1244async function pullChunks(stream) {
1245 const chunks = [];
1246 const reader = stream.getReader();
1247 try {
1248 for (;;) {
1249 const r = await reader.read();
1250 if (r.done) return { chunks };
1251 chunks.push(r.value);
1252 }
1253 } catch (error) {
1254 return { chunks, error };
1255 }
1256}
1258test('openBlob holds the last chunk until the hash and the trailer verify', async () => {
1259 // 64 KiB of one byte inflates as several chunks when the pack is delivered in small frames (measured on this runtime).
1260 const text = 'a'.repeat(65536);
1261 const oid = hashObject('blob', te.encode(text));
1262 const other = hashObject('blob', te.encode('b'));
1263 const pack = await onePack(TYPE.BLOB, text.length, text);
1264 const frames = packFrames(pack, 32);
1265 const open = async (want, wire = frames) => {
1266 const r = remote({ hook: async () => wire });
1267 const ex = arrayExporter();
1268 const tracer = new Tracer({ exporter: ex });
1269 const opened = streamOf(await openBlob(r.transport({ tracer }), want, { follows: tracer.start('p') }));
1270 return { size: opened.size, ...(await pullChunks(opened.stream)), spans: () => ex.lines.flatMap(flattenLine) };
1271 };
1272 const good = await open(oid);
1273 assert.equal(good.error, undefined);
1274 assert.ok(good.chunks.length >= 2, `a valid blob yielded ${good.chunks.length} chunk(s); the hold needs a real tail`);
1275 const whole = concat(good.chunks);
1276 assert.equal(whole.length, text.length);
1277 assert.equal(td.decode(whole), text);
1278 const prefix = concat(good.chunks.slice(0, -1));
1279 const bad = await open(other);
1280 assert.equal(bad.error?.code, 'pack_corrupt');
1281 assert.equal(bad.error.message, `blob content hashes to ${oid}, not ${other}`);
1282 assert.deepEqual(concat(bad.chunks), prefix, 'the last chunk of a blob that fails the hash is never yielded');
1283 const badSpan = bad.spans().find(s => s.name === 'git.read.blob');
1284 assert.equal(badSpan.attrs['git.read.blob.outcome'], 'pack_corrupt');
1285 assert.equal(badSpan.attrs['git.read.blob.verified'], undefined);
1286 // one inflate chunk (the pack arrives whole): holding it means the consumer receives nothing
1287 const hello = packFrames(await onePack(TYPE.BLOB, 5, 'hello'));
1288 const helloOid = hashObject('blob', te.encode('hello'));
1289 const helloGood = await open(helloOid, hello);
1290 assert.equal(helloGood.error, undefined);
1291 assert.equal(helloGood.chunks.length, 1);
1292 assert.equal(td.decode(concat(helloGood.chunks)), 'hello');
1293 const helloBad = await open(other, hello);
1294 assert.equal(helloBad.chunks.length, 0);
1295 assert.equal(helloBad.error?.code, 'pack_corrupt');
1296 assert.match(helloBad.error.message, /^blob content hashes to /);
1297 // a one-byte chunk is a real chunk (an empty one is not)
1298 const zee = packFrames(await onePack(TYPE.BLOB, 1, 'Z'), 8);
1299 const zeeGood = await open(hashObject('blob', te.encode('Z')), zee);
1300 assert.equal(td.decode(concat(zeeGood.chunks)), 'Z');
1301 // the trailer fails after the bytes have inflated: the same tail stays held
1302 const torn = new Uint8Array(pack);
1303 torn[torn.length - 1] ^= 1;
1304 const trailer = await open(oid, packFrames(torn, 32));
1305 assert.equal(trailer.error?.code, 'pack_corrupt');
1306 assert.match(trailer.error.message, /^pack: trailer [0-9a-f]{40} != [0-9a-f]{40}$/);
1307 // aborting the inflate on a bad trailer delivers no further chunk, so the held tail is not among them
1308 assert.ok(trailer.chunks.length < good.chunks.length);
1309 const trailerBytes = trailer.chunks.length ? concat(trailer.chunks) : new Uint8Array();
1310 assert.ok(trailerBytes.length < text.length);
1311 assert.equal(td.decode(trailerBytes), text.slice(0, trailerBytes.length));
1312});
1314test('openBlob: maxBytes is the declared size, and a cap does not pull the rest of the pack', async () => {
1315 const text = 'hello world';
1316 const oid = hashObject('blob', te.encode(text));
1317 const pack = await onePack(TYPE.BLOB, text.length, text);
1318 const frames = packFrames(pack, 8);
1319 const serve = () => {
1320 let pulls = 0;
1321 const r = remote({ hook: async req => (req.command === 'fetch' ? (async function* () { for (const f of frames) { pulls += 1; yield f; } })() : undefined) });
1322 const ex = arrayExporter(); const tracer = new Tracer({ exporter: ex });
1323 return { t: r.transport({ tracer }), tracer, pulls: () => pulls, spans: () => ex.lines.flatMap(flattenLine) };
1324 };
1325 const exact = serve();
1326 const at = streamOf(await openBlob(exact.t, oid, { maxBytes: text.length, follows: exact.tracer.start('p') }));
1327 assert.equal(await readAll(at.stream), text);
1328 assert.equal(exact.spans().find(s => s.name === 'git.read.blob').attrs['git.read.blob.header_only'], undefined);
1329 assert.equal(exact.pulls(), frames.length);
1330 const over = serve();
1331 const big = await openBlob(over.t, oid, { maxBytes: text.length - 1, follows: over.tracer.start('p') });
1332 assert.equal(big.ok, true);
1333 if (big.ok) assert.deepEqual(big.value, { kind: 'too_big', size: text.length });
1334 const cap = over.spans().find(s => s.name === 'git.read.blob');
1335 const capFetch = over.spans().find(s => s.name === 'git.upload-pack fetch');
1336 assert.deepEqual([cap.attrs['git.read.blob.header_only'], cap.attrs['git.read.blob.outcome'], cap.attrs['git.read.blob.size']], [true, 'too_big', text.length]);
1337 // the fetch span is ended (exported, status ok) when the header refuses the blob; dropping end() leaves it unexported
1338 assert.equal(capFetch?.status.code, 1);
1339 assert.ok(over.pulls() < frames.length, `the cap pulled ${over.pulls()} of ${frames.length} frames`);
1340 const none = await openBlob(serve().t, oid, { maxBytes: 0 });
1341 assert.equal(none.ok && none.value.kind, 'too_big');
1342 const emptyOid = hashObject('blob', new Uint8Array(0));
1343 const empty = streamOf(await openBlob(remote({ hook: async () => packFrames(await onePack(TYPE.BLOB, 0, ''), 8) }).transport({ tracer: new Tracer() }), emptyOid, { maxBytes: 0 }));
1344 assert.equal(empty.size, 0);
1345 assert.equal(await readAll(empty.stream), '');
1346 for (const maxBytes of [-1, 1.5, NaN]) {
1347 const bad = await openBlob(serve().t, oid, { maxBytes });
1348 assert.equal(bad.ok, false);
1349 if (!bad.ok) {
1350 assert.equal(bad.error.code, 'bad_request');
1351 assert.equal(bad.error.message, 'maxBytes must be an integer >= 0');
1352 assert.equal(bad.error.what, 'maxBytes');
1353 }
1354 }
1355 // a thrown value that already carries a ply code, and is not an Error, is the Err as it was thrown (not rewrapped)
1356 const bare = { code: 'network', message: 'reset' };
1357 assert.equal(errOf(await openBlob(remote({ hook: async () => { throw bare; } }).transport({ tracer: new Tracer() }), oid)), bare);
1358 const buffered = await readBlob(serve().t, oid, { maxBytes: text.length });
1359 assert.equal(buffered.ok, true);
1360 assert.equal(buffered.ok && buffered.value.kind, 'bytes');
1361 if (buffered.ok && buffered.value.kind === 'bytes') assert.deepEqual(buffered.value.data, te.encode(text));
1362 const capped = await readBlob(serve().t, oid, { maxBytes: 4 });
1363 assert.equal(capped.ok && capped.value.kind, 'too_big');
1364 if (capped.ok && capped.value.kind === 'too_big') assert.equal(capped.value.size, text.length);
1365 // readBlob passes follows through both overloads, and a cap still wins when follows is set
1366 const followed = serve();
1367 const parent = followed.tracer.start('read');
1368 const via = await readBlob(followed.t, oid, { follows: parent });
1369 assert.equal(via.ok && via.value.kind, 'bytes');
1370 if (via.ok && via.value.kind === 'bytes') assert.deepEqual(via.value.data, te.encode(text));
1371 const viaSpan = followed.spans().find(s => s.name === 'git.read.blob');
1372 assert.deepEqual(viaSpan.links.map(l => [l.traceId, l.spanId, l.attrs['ply.link']]), [[parent.traceId, parent.spanId, 'follows_from']]);
1373 const followedCap = serve();
1374 const parentCap = followedCap.tracer.start('read');
1375 const viaCap = await readBlob(followedCap.t, oid, { maxBytes: 4, follows: parentCap });
1376 assert.equal(viaCap.ok && viaCap.value.kind, 'too_big');
1377 if (viaCap.ok && viaCap.value.kind === 'too_big') assert.equal(viaCap.value.size, text.length);
1378 const capLink = followedCap.spans().find(s => s.name === 'git.read.blob');
1379 assert.deepEqual(capLink.links.map(l => [l.traceId, l.spanId, l.attrs['ply.link']]), [[parentCap.traceId, parentCap.spanId, 'follows_from']]);
1380 // a header refusal is the Result, not a throw from reading a value the Err does not have
1381 const asCommit = await readBlob(remote({ hook: async () => packFrames(await onePack(TYPE.COMMIT, 0, '')) }).transport({ tracer: new Tracer() }), oid);
1382 assert.equal(asCommit.ok, false);
1383 if (!asCommit.ok) assert.equal(asCommit.error.message, 'expected blob, got commit');
1384 const torn = new Uint8Array(pack); torn[torn.length - 1] ^= 1;
1385 const lied = await readBlob(remote({ hook: async () => packFrames(torn, 8) }).transport({ tracer: new Tracer() }), oid);
1386 assert.equal(lied.ok, false);
1387 if (!lied.ok) { assert.equal(lied.error.code, 'pack_corrupt'); assert.equal(lied.value, undefined); }
1388 // a body failure that is not an Error is the Err as it was thrown, like a ply Error from the stream
1389 const cut = async function* () {
1390 const all = packFrames(await onePack(TYPE.BLOB, text.length, text), 25);
1391 yield all[0]; yield all[1]; throw bare;
1392 };
1393 assert.equal(errOf(await readBlob(remote({ hook: async () => cut() }).transport({ tracer: new Tracer() }), oid)), bare);
1394});
1396test('the details each operation stamps: prefixes, filters, nested paths, caller limits, error-code gates', async () => {
1397 // pushTree asks ls-refs for the base alone, honours the caller's limits, and reads nested trees by their full path
1398 const r = remote(); r.commit(FILES());
1399 const e = env(r);
1400 const res = await unwrapAsync(pushTree('http://fake/r.git', { files: { ...FILES(), 'src/lib/b.js': 'B\n' }, author: A, message: 'deploy', time: T0 }, e.opts));
1401 assert.deepEqual(r.log.filter(x => x.command === 'ls-refs').map(x => x.prefixes), [['refs/heads/main']]);
1402 assert.equal(res.treesRead, 3);
1403 assert.deepEqual(r.pushes()[0].pack.objects.map(o => (o.base.tag === 'ref' ? o.base.oid : null)), [null, GIT.root, GIT.src, GIT.lib, null]);
1404 await rejects(unwrapAsync(pushTree('http://fake/r.git', { files: { a: '1', b: '2' }, author: A, message: 'm', time: T0 }, { ...env(r).opts, limits: withLimits({ maxPackObjects: 1 }) })), 'limit_exceeded', 'files 2 exceeds limit 1');
1405 // a create whose tree equals the base's reads no parent tree (the server has the whole closure)
1406 const copy = await unwrapAsync(pushTree('http://fake/r.git', { ref: 'refs/heads/copy2', base: 'refs/heads/main', files: { ...FILES(), 'src/lib/b.js': 'B\n' }, author: A, message: 'm', time: T0 }, env(r).opts));
1407 assert.deepEqual([copy.treesRead, copy.objects.sent], [0, 1]);
1408 throws(() => unwrap(validateTreeSpec({ ref: 'main', base: 'refs/heads/main', files: {}, author: A, message: '' }, DEFAULT_LIMITS)), 'bad_request', undefined, { what: 'ref' });
1410 // checkoutTree: the subtree path on the span and the result, joined with '/'
1411 const ce = env(r);
1412 const sub = await unwrapAsync(checkoutTree('http://fake/r.git', { path: 'src/lib' }, ce.opts));
1413 assert.equal(sub.path, 'src/lib');
1414 assert.equal(ce.span('git.checkout').attrs['git.checkout.path'], 'src/lib');
1415 // an empty blob's stream ends without a chunk
1416 const z = remote(); z.commit({ e: '', f: 'x' });
1417 for await (const f of (await unwrapAsync(checkoutTree('http://fake/r.git', {}, env(z).opts))).files) {
1418 const chunks = []; for await (const c of f.stream) chunks.push(c);
1419 assert.equal(chunks.length, f.size ? 1 : 0, f.path);
1420 }
1421 // only a remote_error saying "not our ref" is not_found; the same words under another code are not
1422 const c = r.refs.get('refs/heads/main');
1423 const words = (code, message) => { const x = remote({ hook: async req => { if (req.command === 'fetch') throw fail(code, message); } }); x.refs.set('refs/heads/main', c); return x; };
1424 await rejects(unwrapAsync(checkoutTree('http://fake/r.git', {}, env(words('protocol', 'not our ref')).opts)), 'protocol', 'not our ref');
1425 await rejects(unwrapAsync(readCommit('http://fake/r.git', {}, env(words('protocol', 'not our ref')).opts)), 'protocol', 'not our ref');
1426 await rejects(unwrapAsync(readCommit('http://fake/r.git', {}, env(words('protocol', 'expected commit')).opts)), 'protocol', 'expected commit');
1428 // read.js: tree fetches are tree:0, nested paths in messages, caller limits
1429 const le = env(r); r.log.length = 0;
1430 await unwrapAsync(listTree('http://fake/r.git', { path: 'src' }, le.opts));
1431 assert.deepEqual(r.fetches().map(f => f.filter), ['tree:0', 'tree:0', 'tree:0']);
1432 await rejects(unwrapAsync(listTree('http://fake/r.git', { path: 'src/a.js' }, env(r).opts)), 'not_found', 'src/a.js is not a directory (mode 100644)');
1433 await rejects(unwrapAsync(readFile('http://fake/r.git', { path: 'src/lib' }, env(r).opts)), 'not_found', 'src/lib is a directory');
1434 await rejects(unwrapAsync(listTree('http://fake/r.git', { path: 'src/lib' }, { ...env(r).opts, limits: withLimits({ maxPathDepth: 1 }) })), 'bad_request', 'invalid path: depth 2 exceeds 1');
1435 await rejects(unwrapAsync(readFile('http://fake/r.git', { path: 'src/a.js' }, { ...env(r).opts, limits: withLimits({ maxPathDepth: 1 }) })), 'bad_request', 'invalid path: depth 2 exceeds 1');
1436 const nested = remote(); nested.commit({ 'm/sub': { mode: '160000', oid: GITLINK } });
1437 await rejects(unwrapAsync(readFile('http://fake/r.git', { path: 'm/sub' }, env(nested).opts)), 'not_found', `m/sub is a submodule (commit ${GITLINK})`);
1438 // the oid test is anchored at both ends
1439 assert.equal(unwrap(refCandidates(`x${'a'.repeat(40)}`)).length, 3);
1440 assert.equal(unwrap(refCandidates(`${'a'.repeat(40)}x`)).length, 3);
1441 // the blob's fetch span is a client span under git.read.blob
1442 const be = env(r);
1443 await readAll((await unwrapAsync(readFile('http://fake/r.git', { path: 'README' }, be.opts))).stream);
1444 const blob = be.span('git.read.blob');
1445 const fetch = be.all('git.upload-pack fetch').find(s => s.parentSpanId === blob.spanId);
1446 assert.equal(fetch?.kind, 3);
1447});
1449test('resolveAbbrev: advertised oids and peeled targets; resolveCommit asks once and lets a ref name win', async () => {
1450 const listed = rows => ({ refs: new Map(rows.map(r => [r.name, r])), head: null, unborn: null });
1451 const row = (name, oid, peeled = null) => ({ oid, name, peeled, 'symref-target': null });
1452 const abbrev = (prefix, rows) => {
1453 const r = resolveAbbrev(prefix, listed(rows));
1454 assert.equal(r.ok, true, r.ok ? '' : r.error.message);
1455 if (!r.ok) throw new Error('expected a resolution');
1456 return r.value;
1457 };
1458 const hit = `abcd${'1'.repeat(36)}`;
1459 const other = `abcd${'2'.repeat(36)}`;
1460 const first = `abcd${'0'.repeat(36)}`;
1461 const later = `ffffabcd${'0'.repeat(32)}`;
1462 const peeled = `abcd${'3'.repeat(36)}`;
1463 const tagOid = `eeee${'4'.repeat(36)}`;
1464 assert.deepEqual(abbrev('abcd', [row('refs/heads/z', later)]), { kind: 'none' });
1465 assert.deepEqual(abbrev('abcd', [row('HEAD', hit), row('refs/heads/main', hit), row('refs/heads/z', later)]), { kind: 'unique_among_advertised', oid: hit, refs: ['HEAD', 'refs/heads/main'] });
1466 assert.deepEqual(abbrev('abcd', [row('refs/tags/v', tagOid, peeled)]), { kind: 'unique_among_advertised', oid: peeled, refs: ['refs/tags/v'] });
1467 assert.deepEqual(abbrev('abcd', [row('refs/tags/v', hit, hit)]), { kind: 'unique_among_advertised', oid: hit, refs: ['refs/tags/v'] });
1468 assert.deepEqual(abbrev('abcd', [row('refs/heads/b', other), row('refs/heads/a', hit), row('refs/heads/c', first)]), { kind: 'ambiguous', oids: [first, hit, other] });
1469 const oid39 = `${'a'.repeat(39)}b`;
1470 assert.deepEqual(abbrev('a'.repeat(39), [row('refs/heads/a', oid39)]), { kind: 'unique_among_advertised', oid: oid39, refs: ['refs/heads/a'] });
1471 assert.deepEqual(abbrev('abcd1', [row('refs/heads/a', hit)]), { kind: 'unique_among_advertised', oid: hit, refs: ['refs/heads/a'] });
1472 for (const bad of ['abc', 'a'.repeat(40), 'ABCD', 'abcd!', '', 'gggg', `${'a'.repeat(39)}G`]) {
1473 const r = resolveAbbrev(bad, listed([row('refs/heads/a', hit)]));
1474 assert.equal(r.ok, false, bad);
1475 if (!r.ok) {
1476 assert.equal(r.error.code, 'bad_request', bad);
1477 assert.equal(r.error.message, 'abbreviated oid must be 4..39 lowercase hex', bad);
1478 assert.equal(r.error.what, 'ref', bad);
1479 }
1480 }
1482 const open = (head = 'refs/heads/main') => { const r = remote({ head }); return { r, t: r.transport({ tracer: new Tracer() }) }; };
1483 const prefixes = r => r.log.filter(x => x.command === 'ls-refs').map(x => x.prefixes);
1484 // default off: a short hex string is a name, one narrow ls-refs, even when a tip starts with it
1485 const off = open('refs/heads/hold');
1486 off.r.refs.set('refs/heads/hold', hit);
1487 await rejects(unwrapAsync(resolveCommit(off.t, 'abcd')), 'not_found', 'ref abcd not found');
1488 assert.deepEqual(prefixes(off.r), [unwrap(refCandidates('abcd'))]);
1489 // a full oid is not an abbreviation and does not ask
1490 off.r.log.length = 0;
1491 assert.deepEqual(await unwrapAsync(resolveCommit(off.t, hit, { abbrev: 'advertised' })), { name: hit, commit: hit });
1492 assert.deepEqual(prefixes(off.r), []);
1493 // advertised, and the name is not a prefix: still the name's own candidates
1494 const named = open();
1495 named.r.refs.set('refs/heads/main', hit);
1496 assert.deepEqual(await unwrapAsync(resolveCommit(named.t, 'main', { abbrev: 'advertised' })), { name: 'refs/heads/main', commit: hit });
1497 assert.deepEqual(prefixes(named.r), [unwrap(refCandidates('main'))]);
1498 // uppercase is a name, not a prefix
1499 named.r.log.length = 0;
1500 await rejects(unwrapAsync(resolveCommit(named.t, 'ABCD', { abbrev: 'advertised' })), 'not_found', 'ref ABCD not found');
1501 assert.deepEqual(prefixes(named.r), [unwrap(refCandidates('ABCD'))]);
1502 await rejects(unwrapAsync(resolveCommit(named.t, 'main', { abbrev: 'yes' })), 'bad_request', 'abbrev must be "advertised" or "off"', { what: 'abbrev' });
1503 // a ref whose name is the prefix wins over a tip that actually starts with it
1504 const elsewhere = `1234${'0'.repeat(36)}`;
1505 const win = open('refs/heads/hold');
1506 win.r.refs.set('refs/heads/abcd', elsewhere);
1507 win.r.refs.set('refs/heads/hold', hit);
1508 assert.deepEqual(await unwrapAsync(resolveCommit(win.t, 'abcd', { abbrev: 'advertised' })), { name: 'refs/heads/abcd', commit: elsewhere });
1509 assert.deepEqual(prefixes(win.r), [[...unwrap(refCandidates('abcd')), 'HEAD', 'refs/heads/', 'refs/tags/']]);
1510 assert.equal(win.r.log.filter(x => x.command === 'ls-refs').length, 1);
1511 // no such name: the one advertised tip
1512 const one = open('refs/heads/hold');
1513 one.r.refs.set('refs/heads/hold', hit);
1514 assert.deepEqual(await unwrapAsync(resolveCommit(one.t, 'abcd', { abbrev: 'advertised' })), { name: hit, commit: hit });
1515 assert.equal(one.r.log.filter(x => x.command === 'ls-refs').length, 1);
1516 // two tips, no ref by that name
1517 const two = open('refs/heads/a');
1518 two.r.refs.set('refs/heads/a', other);
1519 two.r.refs.set('refs/heads/b', first);
1520 await rejects(unwrapAsync(resolveCommit(two.t, 'abcd', { abbrev: 'advertised' })), 'bad_request', 'abbreviated oid abcd matches 2 advertised objects', { what: 'ref', got: [first, other] });
1521 assert.equal(two.r.log.filter(x => x.command === 'ls-refs').length, 1);
1522 // nothing advertised starts with it
1523 await rejects(unwrapAsync(resolveCommit(two.t, 'ffff', { abbrev: 'advertised' })), 'not_found', 'ref ffff not found');
1524});
1526test('commitChanges: one edit of an existing file is streamed through editFile; a HEAD naming no refname is refused before it', async () => {
1527 const { commitChanges } = await import('../src/changes.js');
1528 const r = remote();
1529 const base = r.commit(FILES());
1530 const e = env(r);
1531 const done = await unwrapAsync(commitChanges('http://fake/r.git', { ref: 'refs/heads/main', changes: [{ kind: 'edit', path: 'src/a.js', find: 'a', replace: 'A' }], author: A, message: 'deploy', time: T0 }, e.opts));
1532 // git's own tree and commit for src/a.js → 'A\n' on `commit`
1533 assert.equal(base, GIT.commit);
1534 assert.deepEqual(done, { outcome: 'commit', ref: 'refs/heads/main', oid: GIT.commit2, tree: GIT.root2, parent: GIT.commit, oldOid: GIT.commit, pushBytes: done.pushBytes, created: false });
1535 assert.ok(done.pushBytes > 0);
1536 assert.equal(r.refs.get('refs/heads/main'), GIT.commit2);
1537 assert.equal(e.span('git.commit').attrs['git.commit.stream'], true);
1538 // the remote's HEAD names a target that is not a refname: the streamed edit refuses it as a ref, and pushes nothing
1539 const bad = remote({ hook: async req => (req.command === 'ls-refs' ? [dataFrame(`${GIT.commit} HEAD symref-target:main\n`), { kind: 'flush' }] : undefined) });
1540 bad.commit(FILES());
1541 const b = env(bad);
1542 const refused = await commitChanges('http://fake/r.git', { changes: [{ kind: 'edit', path: 'src/a.js', find: 'a', replace: 'A' }], author: A, message: 'deploy', time: T0 }, b.opts);
1543 const x = errOf(refused);
1544 assert.deepEqual([x.code, x.message, x.what], ['bad_request', 'invalid ref: must be a full ref name starting with refs/', 'ref']);
1545 assert.equal(x.cause.message, 'invalid ref: must be a full ref name starting with refs/');
1546 assert.equal(bad.pushes().length, 0);
1547});
Diagnostics6 git calls · 122 ms
CallStatusSizeTime0122 ms
discover200191 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 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 KiB · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=11990 git.response.cache=HIT git.response.duration_ms=2 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 · 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=4347 git.response.cache=HIT git.response.duration_ms=2 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=10Open pack map
fetchtree:0200919 B · 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=919 git.response.cache=HIT git.response.duration_ms=2 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=5323b9266eea5ded637285d1c5320699f12be76eOpen pack map
fetchtree:02005.6 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=114 git.response.bytes=5724 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=58fabb6768b3f737ee9cd8bac647bdc27405fb4d 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=5724 git.pkt.payload.max=5697 git.sideband.band1.bytes=5697 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=5697 git.pack.trailer=be7e0453974a5fe832f9065a33bb7e3434c10e9bOpen pack map
fetch20028 KiB114 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=28257 git.response.cache=MISS git.response.duration_ms=114 git.fetch.wants=c0154c4f34d60893bffef2f642cb4b6231411bbb 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=5 git.pkt.data=4 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=28257 git.pkt.payload.max=20034 git.sideband.band1.bytes=28225 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
- a5e15f6ded75725b
- Request
- acd705a4…
- 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