GitHubivarvong

ply

main
ply6 git calls109 ms26 KiB4 cachedprotocol v2
132 lines8.8 KiBRaw
1// encodeCommit is held to the reader (objects/encodecommit-not-held-to-parsecommit): what it returns ok, git stores as
2// is and parseCommit reads back as the commit that was given; anything else is bad_request, never a throw.
3// The judges are git's: `git hash-object -t commit --stdin` (fsck) on bytes built here by hand from the commit format,
4// `git cat-file commit` for what git stored, and `git show -s --format` for how git reads it.
5import { test, beforeAll, afterAll } from 'vitest';
6import assert from 'node:assert/strict';
7import { spawnSync } from 'node:child_process';
8import { mkdtempSync, rmSync } from 'node:fs';
9import { tmpdir } from 'node:os';
10import { join } from 'node:path';
11import { encodeCommit, parseCommit } from '../src/objects.js';
13let dir;
14beforeAll(() => {
15 dir = mkdtempSync(join(tmpdir(), 'ply-enc-commit-'));
16 assert.equal(spawnSync('git', ['-C', dir, 'init', '-q', '-b', 'main']).status, 0);
17 // the empty tree, so `git show` can read a commit that points at it
18 assert.equal(`${spawnSync('git', ['-C', dir, 'hash-object', '-t', 'tree', '-w', '--stdin'], { input: '' }).stdout}`.trim(), TREE);
19});
20afterAll(() => rmSync(dir, { recursive: true, force: true }));
21function git(args, input) {
22 const r = spawnSync('git', ['-C', dir, ...args], { input, maxBuffer: 1 << 26 });
23 return { code: r.status ?? 1, out: r.stdout ?? Buffer.alloc(0), err: `${r.stderr ?? ''}`.trim() };
26const TREE = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'; // git's empty tree
27const WHO = 'A U Thor <a@example.com> 1700000000 +0000';
28// The commit object format, written out by hand (not ply's encoder): tree, parents, author, committer, encoding, the
29// other header lines, then a blank line and the message when there is one.
30function handBuilt({ tree, parents = [], author, committer = author, encoding = null, extra = [], message = null }) {
31 const lines = [`tree ${tree}`, ...parents.map(p => `parent ${p}`), `author ${author}`, `committer ${committer}`];
32 if (encoding !== null) lines.push(`encoding ${encoding}`);
33 return Buffer.from(`${[...lines, ...extra].join('\n')}\n${message === null ? '' : `\n${message}`}`, 'utf8');
36// [what, input, git's verdict on the hand-built bytes, why ply refuses when git accepts]
37const REFUSED = [
38 ['author with no email or date', { author: 'garbage' }, 'refuses'],
39 ['author timezone of two digits', { author: 'A <a@b> 1 +00' }, 'refuses'],
40 ['author with a negative time', { author: 'A <a@b> -5 +0000' }, 'refuses'],
41 ['committer with a zero-padded time', { author: WHO, committer: 'A <a@b> 01 +0000' }, 'refuses'],
42 ['author time past 2^63 - 1', { author: 'A <a@b> 99999999999999999999 +0000' }, 'refuses'],
43 ['author with no space before the email', { author: 'A<a@b> 1 +0000' }, 'refuses'],
44 // fsck reads only the first tree / author / committer line, so git stores these; cat-file then shows a second one
45 // that %an, %T and %P do not. parseCommit refuses that object (duplicateHeader), so ply must not build it.
46 ['extra line that is a second tree', { author: WHO, extra: [`tree ${'1'.repeat(40)}`] }, 'accepts', 'shadow'],
47 ['extra line that is a second author', { author: WHO, extra: ['author Evil <e@x> 1 +0000'] }, 'accepts', 'shadow'],
48 ['extra line that is a second committer', { author: WHO, extra: ['committer Evil <e@x> 1 +0000'] }, 'accepts', 'shadow'],
49 ['extra line that is a late parent', { author: WHO, extra: [`parent ${'2'.repeat(40)}`] }, 'accepts', 'shadow'],
50 ['encoding given twice', { author: WHO, encoding: 'UTF-8', extra: ['encoding ISO-8859-1'] }, 'accepts', 'shadow'],
51 // git stores these too, and reads back another commit than the one given: the blank line ends the header, so the
52 // lines after it are message text; an `encoding ` extra line is the encoding the caller said was absent.
53 // (with no message the bytes are those of another input, no extra line and an empty message: one object, two inputs)
54 ['empty extra line, no message', { author: WHO, extra: [''] }, 'accepts', 'alias'],
55 ['empty extra line before another', { author: WHO, extra: ['', 'x-later 1'], message: 'hello\n' }, 'accepts', 'body'],
56 ['empty extra line between two', { author: WHO, extra: ['x-first 1', '', 'x-later 1'], message: 'hello\n' }, 'accepts', 'body'],
57 ['encoding as an extra line', { author: WHO, extra: ['encoding ISO-8859-1'], message: 'hello\n' }, 'accepts', 'encoding'],
58];
60test('encodeCommit refuses, as bad_request, every commit git refuses or reads back as another commit', () => {
61 const wrong = [];
62 for (const [what, fields, verdict, why] of REFUSED) {
63 const input = { tree: TREE, ...fields };
64 const theirs = git(['hash-object', '-t', 'commit', '-w', '--stdin'], handBuilt(input));
65 assert.equal(theirs.code === 0 ? 'accepts' : 'refuses', verdict, `${what}: git ${theirs.err}`);
66 if (why === 'body') {
67 // git's own reading of the message is not the message that was given
68 const body = `${git(['show', '-s', '--format=%B', `${theirs.out}`.trim()]).out}`;
69 assert.notEqual(body.replace(/\n$/u, ''), (input.message ?? '').replace(/\n$/u, ''), `${what}: git reads the given message back`);
70 }
71 if (why === 'alias') assert.deepEqual(handBuilt(input), handBuilt({ tree: TREE, author: WHO, message: '' }), `${what}: the bytes of another input`);
72 if (why === 'encoding') assert.equal(`${git(['show', '-s', '--format=%e', `${theirs.out}`.trim()]).out}`.trim(), 'ISO-8859-1', `${what}: git reads an encoding the caller did not give`);
73 let ours;
74 try { ours = encodeCommit(input); } catch (e) { wrong.push(`${what}: encodeCommit threw ${e}`); continue; }
75 if (ours.ok) wrong.push(`${what}: git ${verdict}${theirs.err ? ` (${theirs.err})` : ''}, ply encodes ${ours.value.length} bytes`);
76 else if (ours.error.code !== 'bad_request') wrong.push(`${what}: ${ours.error.code}`);
77 // owner decision: an error never grows with the caller's text
78 else assert.ok(ours.error.message.length < 200, ours.error.message);
79 }
80 assert.deepEqual(wrong, []);
81});
83// mulberry32: a fixed seed, so a failure names a reproducible case.
84function prng(seed) {
85 let a = seed >>> 0;
86 return () => {
87 a = (a + 0x6d2b79f5) >>> 0;
88 let t = Math.imul(a ^ (a >>> 15), a | 1);
89 t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
90 return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
91 };
93const IDENTS = [
94 WHO, 'A <a@b> 0 +0000', 'Ünï Çødé <u@x.y> 1700000000 -0730', 'A B C <> 9223372036854775807 +9999', 'x <a b@c> 12 -0000', 'tree <author@parent> 1 +1400',
95];
96const EXTRAS = [
97 'x-custom value', 'mergetag object 1111111111111111111111111111111111111111', ' type commit', ' tag v1', 'gpgsig -----BEGIN SSH SIGNATURE-----', ' AAAA', ' -----END SSH SIGNATURE-----',
98 'x', 'HG:rename-source hg', 'encodingx y', 'treex z', 'authorship 1', 'parents 2', 'committers 3', 'ünï cødé',
99];
100const MESSAGES = [null, '', 'hello\n', 'no newline', '\n\nleading blank lines\n', 'ünï cødé\n', 'tree fake\nauthor fake\n\nparent fake\n', 'subject\n\nbody\n\n\n'];
101const ENCODINGS = [null, null, 'UTF-8', 'ISO-8859-1'];
103test('generated commits round-trip both ways: encodeCommit writes what git stores, and parseCommit of git\'s bytes encodes back to them', () => {
104 const rand = prng(0xC0FFEE);
105 const pick = list => list[Math.floor(rand() * list.length)];
106 const hex = () => Array.from({ length: 40 }, () => Math.floor(rand() * 16).toString(16)).join('');
107 for (let i = 0; i < 80; i++) {
108 const given = {
109 tree: hex(), parents: Array.from({ length: Math.floor(rand() * 4) }, hex), author: pick(IDENTS), committer: pick(IDENTS),
110 encoding: pick(ENCODINGS), extra: Array.from({ length: Math.floor(rand() * 4) }, () => pick(EXTRAS)), message: pick(MESSAGES),
111 };
112 const label = `case ${i}: ${JSON.stringify(given)}`;
113 const expected = handBuilt(given);
114 // git's verdict and git's stored bytes for the hand-built object
115 const stored = git(['hash-object', '-t', 'commit', '-w', '--stdin'], expected);
116 assert.equal(stored.code, 0, `${label}: git refuses a generated commit: ${stored.err}`);
117 const oid = `${stored.out}`.trim();
118 const cat = git(['cat-file', 'commit', oid]).out;
119 assert.deepEqual(cat, expected, `${label}: git stores the hand-built bytes`);
120 // one way: encodeCommit writes exactly those bytes
121 const ours = encodeCommit(given);
122 assert.equal(ours.ok, true, `${label}: ${ours.ok ? '' : ours.error.message}`);
123 assert.deepEqual(Buffer.from(ours.value), cat, `${label}: encodeCommit's bytes are git's`);
124 // the other way: parseCommit of git's bytes is the commit that was given, and encodes back to git's bytes
125 const parsed = parseCommit(new Uint8Array(cat));
126 assert.equal(parsed.ok, true, `${label}: ${parsed.ok ? '' : parsed.error.message}`);
127 assert.deepEqual({ ...parsed.value }, given, label);
128 const again = encodeCommit(parsed.value);
129 assert.equal(again.ok, true, label);
130 assert.deepEqual(Buffer.from(again.value), cat, `${label}: parse then encode is git's bytes`);
131 }
132});
Diagnostics6 git calls · 109 ms
CallStatusSizeTime0109 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=3
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=10
Open pack map
fetchtree:0200919 B · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=919
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=800d30ba8923860bfc460ca884b7f9ba22083588
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=919
git.pkt.payload.max=892
git.sideband.band1.bytes=892
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=892
git.pack.trailer=5323b9266eea5ded637285d1c5320699f12be76e
Open pack map
fetchtree: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=100
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=be7e0453974a5fe832f9065a33bb7e3434c10e9b
Open pack map
fetch2003.4 KiB100 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=3441
git.response.cache=MISS
git.response.duration_ms=100
git.fetch.wants=0aa458fe62914ebfce2b187fd8b546bb615c025b
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=3441
git.pkt.payload.max=3414
git.sideband.band1.bytes=3414
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
a5e0f821aa48332c
Request
57e6e0e9…
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