Artifactspublic-v0

ply

main
ply4 git calls23 ms41 KiB3 cachedprotocol v2
156 lines10 KiBRaw
1// Parse once (#156, part 2): each public input is parsed at its edge into the brand core takes, and refused there with
2// one code and `what`. The git-oracle side of fetch wants is test/want-oracle.test.js.
3import { test } from 'vitest';
4import assert from 'node:assert/strict';
5import { lsRefsBody, refPrefixes, parseLsRefLine } from '../src/lsrefs.js';
6import { encodeTree, treeEntryOf, isMode, parseCommit, encodeCommit, hashObject } from '../src/objects.js';
7import { objectId, fetchObject } from '../src/fetch.js';
8import { fetchCommit } from '../src/commit.js';
9import { mergeBase } from '../src/merge.js';
10import { walkPath } from '../src/read.js';
11import { validateSpec, baseName } from '../src/edit.js';
12import { buildCommitObjects } from '../src/commit-build.js';
13import { DEFAULT_LIMITS } from '../src/limits.js';
14import { unwrap } from './unwrap.js';
16const A = 'a'.repeat(40), B = 'b'.repeat(40);
17// a transport no call may use: every refusal below comes before any request
18const noIO = () => {
19 const seen = { n: 0 };
20 const fail = () => { seen.n++; throw new Error('no request expected'); };
21 return { seen, t: { kind: 'http', url: 'http://x/r.git', tracer: { span: fail, start: fail }, parent: null, limits: DEFAULT_LIMITS, signal: null, quirks: { treeFilter: 'tree:0', blobWants: 'many' }, objects: new Map(), scrub: null, frames: fail, advertise: fail } };
22};
23const refused = (r, what, message) => {
24 assert.equal(r.ok, false, 'refused');
25 assert.equal(r.error.code, 'bad_request');
26 assert.equal(r.error.what, what);
27 if (message !== undefined) assert.equal(r.error.message, message);
28};
30test('lsRefsBody parses its prefixes once, as lsRefs does: a NUL, CR or LF is `invalid ref prefix`', () => {
31 for (const p of ['refs/heads/\n', 'a\rb', 'a\0']) {
32 refused(lsRefsBody([p]), 'ref prefix', 'invalid ref prefix: must be a string without NUL, CR, or LF');
33 refused(refPrefixes(['refs/tags/', p]), 'ref prefix');
34 }
35 assert.deepEqual(unwrap(refPrefixes(['refs/heads/', '', 'HEAD'])), ['refs/heads/', '', 'HEAD']);
36 // the body of parsed prefixes is the body it always was
37 assert.equal(new TextDecoder().decode(unwrap(lsRefsBody(['refs/heads/']))), '0014command=ls-refs\n00010009peel\n000csymrefs\n001bref-prefix refs/heads/\n0000');
38});
40test('a listed name is the field ls-refs wrote: HEAD, a refs/ name, and what a malformed line spells, as git lists them', () => {
41 for (const name of ['HEAD', 'refs/heads/main', '', 'capabilities^{}', 'refs/heads/a..b']) {
42 const line = name === '' ? `${A} ` : `${A} ${name}`;
43 assert.equal(unwrap(parseLsRefLine(line)).name, name);
44 }
45});
47test('encodeTree parses each entry once: a mode git does not write is `mode`, an id that is not one is `oid`', () => {
48 for (const mode of ['100664', '040000', '644', '', 'x', 100644]) refused(encodeTree([{ mode, name: 'f', oid: A }]), 'mode');
49 for (const oid of [A.toUpperCase(), A.slice(1), `${A}0`, '', 'z'.repeat(40)]) refused(encodeTree([{ mode: '100644', name: 'f', oid }]), 'oid');
50 const r = treeEntryOf({ mode: '100644', name: 'n', oid: 'x' });
51 assert.equal(r.error.entry, 'n');
52 assert.equal(r.error.message, 'tree: entry "n": oid must be 40 lowercase hex characters');
53 assert.equal(treeEntryOf({ mode: '1', name: 'n', oid: A }).error.message, 'tree: entry "n": invalid mode "1"');
54 // in the caller's order: the first bad entry is the refusal, before a duplicate later on
55 refused(encodeTree([{ mode: '100644', name: 'a', oid: A }, { mode: '9', name: 'b', oid: A }, { mode: '100644', name: 'a', oid: A }]), 'mode');
56 for (const m of ['100644', '100755', '120000', '40000', '160000']) assert.equal(isMode(m), true);
57 for (const m of ['100664', '40755', 100644, null]) assert.equal(isMode(m), false);
58 // the five modes, and parsed entries, encode as before
59 const entries = [{ mode: '40000', name: 'd', oid: A }, { mode: '100644', name: 'f', oid: B }, { mode: '120000', name: 'l', oid: A }, { mode: '160000', name: 's', oid: B }, { mode: '100755', name: 'x', oid: A }];
60 assert.equal(hashObject('tree', unwrap(encodeTree(entries))), hashObject('tree', unwrap(encodeTree(entries.toReversed()))));
61});
63test('parseCommit parses tree and parents once into lowercase ids (either case on the wire, as git reads them)', () => {
64 const data = unwrap(encodeCommit({ tree: A.toUpperCase(), parents: [B.toUpperCase(), A], author: 'a <a@x> 1 +0000', message: 'm\n' }));
65 const c = unwrap(parseCommit(data));
66 assert.equal(c.tree, A);
67 assert.deepEqual(c.parents, [B, A]);
68 // a short id at the end of the object is git's bad sha1, as before
69 const cut = new TextEncoder().encode(`tree ${A.slice(0, 20)}\n`);
70 assert.equal(parseCommit(cut).error.message, "commit: badTreeSha1: invalid 'tree' line format - bad sha1");
71 // an expanding lowercase (U+0130 is two code units lowercased) is no hex digit
72 const wide = new TextEncoder().encode(`tree ${A.slice(0, 39)}İ\nauthor a <a@x> 1 +0000\ncommitter a <a@x> 1 +0000\n`);
73 assert.equal(parseCommit(wide).error.message, "commit: badTreeSha1: invalid 'tree' line format - bad sha1");
74});
76test('fetchObject, fetchCommit, mergeBase and walkPath parse their ids at the edge, before any request', async () => {
77 const { seen, t } = noIO();
78 const msg = s => `fetchObject: oid must be 40-hex, got ${JSON.stringify(s)}`;
79 for (const bad of ['HEAD', A.toUpperCase(), A.slice(1)]) {
80 const o = await fetchObject(t, { oid: bad });
81 assert.equal(o.error.code, 'bad_request'); assert.equal(o.error.message, msg(bad)); assert.equal(o.error.what, undefined);
82 const c = await fetchCommit(t, bad);
83 assert.equal(c.error.code, 'bad_request'); assert.equal(c.error.message, msg(bad));
84 const w = await walkPath(t, bad, []);
85 assert.equal(w.error.code, 'bad_request'); assert.equal(w.error.message, msg(bad));
86 refused(await mergeBase(t, bad, A), 'oid');
87 refused(await mergeBase(t, A, bad), 'oid');
88 }
89 assert.equal(unwrap(objectId(A)), A);
90 // a non-string is checked as String() renders it (none as ''), and quoted as String() renders it, as before
91 assert.equal(objectId(undefined).error.message, msg('undefined'));
92 assert.equal(objectId(null).error.message, 'fetchObject: oid must be 40-hex, got "null"');
93 // a String object renders as its text, and is that id (as the String() check always read it)
94 assert.equal(unwrap(objectId(new String(A))), A);
95 assert.equal(objectId(new String('x')).error.message, msg('x'));
96 // the refusal quotes the first 48 characters
97 assert.equal(objectId('x'.repeat(100)).error.message, msg('x'.repeat(48)));
98 assert.equal(seen.n, 0);
99});
101test('validateSpec parses the base once into a ref or a commit id, and the head hint into an id', () => {
102 const spec = { path: 'a', find: 'x', replace: 'y', author: { name: 'n', email: 'e@x' }, message: 'm', time: 1 };
103 const onRef = unwrap(validateSpec(spec, DEFAULT_LIMITS));
104 assert.deepEqual(onRef.base, { kind: 'ref', ref: 'refs/heads/main' }); assert.equal(onRef.create, false); assert.equal(baseName(onRef.base), 'refs/heads/main');
105 const other = unwrap(validateSpec({ ...spec, base: 'refs/heads/dev' }, DEFAULT_LIMITS));
106 assert.deepEqual(other.base, { kind: 'ref', ref: 'refs/heads/dev' }); assert.equal(other.create, true);
107 const onCommit = unwrap(validateSpec({ ...spec, base: A }, DEFAULT_LIMITS, B));
108 assert.deepEqual(onCommit.base, { kind: 'commit', oid: A }); assert.equal(onCommit.create, false); assert.equal(baseName(onCommit.base), A); assert.equal(onCommit.head, B);
109 // the refusals are the ones they were
110 refused(validateSpec({ ...spec, base: '0'.repeat(40) }, DEFAULT_LIMITS), 'commit');
111 assert.equal(validateSpec({ ...spec, base: A.toUpperCase() }, DEFAULT_LIMITS).error.code, 'bad_request');
112 assert.equal(validateSpec(spec, DEFAULT_LIMITS, A.toUpperCase()).error.message, 'head must be a 40-hex oid');
113 assert.equal(validateSpec(spec, DEFAULT_LIMITS, 'short').error.message, 'head must be a 40-hex oid');
114});
116test('buildCommitObjects parses the new blob id once: every tree above it is spliced from that id', () => {
117 const r = buildCommitObjects({ levels: [], blob: { oldOid: A, newOid: 'zz', delta: new Uint8Array(0), newLen: 0 }, oldCommit: A, author: { name: 'n', email: 'e@x' }, time: 1, message: 'm' });
118 refused(r, 'oid');
119});
121test('a merge `from` / rebase `onto` and a picked commit are parsed once: an id is an Oid, a name a ref', async () => {
122 const { givenOid } = await import('../src/mergeio.js');
123 const { pickCommit } = await import('../src/rebase.js');
124 assert.deepEqual(unwrap(givenOid(A, 'from')), { tag: 'some', value: A });
125 assert.deepEqual(unwrap(givenOid('refs/heads/main', 'from')), { tag: 'none' });
126 refused(givenOid('main', 'onto'), 'ref');
127 refused(givenOid('0'.repeat(40), 'onto'), 'onto', 'onto must not be the zero oid');
128 refused(givenOid('refs/heads/a..b', 'from'), 'ref');
129 // an upper-case id is no id: it is read as a name, as before
130 refused(givenOid(A.toUpperCase(), 'from'), 'ref');
131 const who = { name: 'n', email: 'e@x' };
132 for (const commit of ['HEAD', A.toUpperCase(), A.slice(1)]) {
133 const r = await pickCommit('http://127.0.0.1:9/r.git', { commit, committer: who }, { tracer: { span: () => { throw new Error('no I/O'); } } });
134 assert.equal(r.error.code, 'bad_request'); assert.equal(r.error.message, 'commit must be a 40-hex oid');
135 }
136});
138test('attributeTree parses an oid attribute once: an id is fetched as a commit, a name is a ref', async () => {
139 const { attributeTree } = await import('../src/merge.js');
140 const { seen, t } = noIO();
141 const byId = await attributeTree(t, A, {});
142 assert.notEqual(byId.error.code, 'bad_request', 'an id goes to the fetch, not the ref check');
143 assert.ok(seen.n > 0);
144 const before = seen.n;
145 refused(await attributeTree(t, 'refs/heads/a..b', {}), 'ref');
146 assert.equal(seen.n, before, 'a bad name is refused before any request');
147});
149test('a name with no attributes has none: a name that spells `key:value` past its first byte is still only the name', () => {
150 // the attributes start after the space that ends the name; with no such space there are none, whatever the name holds
151 for (const name of ['xsymref-target:refs/heads/main', `xpeeled:${B}`]) {
152 const r = unwrap(parseLsRefLine(`${A} ${name}`));
153 assert.equal(r.name, name);
154 assert.deepEqual([r['symref-target'], r.peeled], [null, null], name);
155 }
156});
Diagnostics4 git calls · 23 ms
CallStatusSizeTime023 ms
discover200152 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200145 B · 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=145
git.response.cache=HIT
git.response.duration_ms=2
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=145
git.pkt.payload.max=76
git.ls_refs.count=2
git.ls_refs.head=refs/heads/main
fetchblob:none · deepen 1020038 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=18
git.response.bytes=38434
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
git.fetch.wants=19912563f540228593df0c489082e5e2508e21e4
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=6
git.pkt.data=4
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=38434
git.pkt.payload.max=38339
git.sideband.band1.bytes=38338
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=63
git.pack.bytes=38338
git.pack.ofs_delta=0
git.pack.ref_delta=8
git.pack.chain.max=8
git.pack.trailer=0bc36ab7adf82d3109177f11f1ea93f1ce071f77
git.pack.full=55
Open pack map
fetch2003.3 KiB · hit18 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=3424
git.response.cache=HIT
git.response.duration_ms=18
git.fetch.wants=d7e0cb2977725e0327f7a24722203f15c1173b8d
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=3424
git.pkt.payload.max=3403
git.sideband.band1.bytes=3402
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/ply.git
Ref
refs/heads/main
Commit
19912563f540228593df0c489082e5e2508e21e4
cf-ray
a5e1f9d3eb0324ce
Request
dd81f196…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format