GitHubivarvong

ply

main
ply6 git calls71 ms26 KiB4 cachedprotocol v2
171 lines10 KiBRaw
1// encodeTag against git (issue #103, item 5). Two oracles, both git's own:
2// 1. `git hash-object -t tag --stdin` (fsck_tag_standalone, every message an error): it stores what encodeTag
3// returns, under the oid hashObject gives, and it refuses each tag encodeTag refuses (the same fields, spelled out
4// as bytes by hand here, independently of src/objects.ts).
5// 2. `git tag -a --cleanup=verbatim -F <file>`: for the cells git's porcelain can write (a UTF-8 tag name and tagger,
6// no extra header), `git cat-file tag` is byte-for-byte what encodeTag returns.
7// Cells: target type (commit, tree, blob, an annotated tag; a lightweight tag is a ref, not an object) × tagger
8// (present, absent) × message (none, empty, with and without a trailing newline, paragraphs, a signature block) ×
9// bytes (UTF-8, non-UTF-8 in the name/message, non-UTF-8 in the tagger) × an extra header (none, a gpgsig-sha256 block).
10import { test, beforeAll as before, afterAll as after } from 'vitest';
11import assert from 'node:assert/strict';
12import { spawnSync } from 'node:child_process';
13import { join } from 'node:path';
14import { mkdirSync, writeFileSync } from 'node:fs';
15import { git, tmpRoot, initRepo } from './server.js';
16import { encodeTag, hashObject } from '../src/index.js';
17import { decodeBytes } from '../src/bytes.js';
18import { unwrap } from './unwrap.js';
20let root, cleanup, work;
21const targets = {};
22before(() => {
23 ({ root, cleanup } = tmpRoot());
24 work = join(root, 'w'); mkdirSync(work);
25 initRepo(work);
26 writeFileSync(join(work, 'f'), 'f\n');
27 git(work, ['add', 'f']);
28 git(work, ['-c', 'user.name=s', '-c', 'user.email=s@x', 'commit', '-q', '-m', 'c']);
29 targets.commit = git(work, ['rev-parse', 'HEAD']).trim();
30 targets.tree = git(work, ['rev-parse', 'HEAD^{tree}']).trim();
31 targets.blob = git(work, ['rev-parse', 'HEAD:f']).trim();
32 git(work, ['-c', 'user.name=s', '-c', 'user.email=s@x', 'tag', '-a', '-m', 'inner', 'inner', 'HEAD']);
33 targets.tag = git(work, ['rev-parse', 'refs/tags/inner']).trim();
34});
35after(() => cleanup());
37const raw = (...parts) => decodeBytes(Buffer.concat(parts.map(p => (typeof p === 'string' ? Buffer.from(p) : Buffer.from(p)))));
38const enc = s => Buffer.from(Uint8Array.from([...s].map(ch => { const c = ch.codePointAt(0); return c >= 0xdc80 && c <= 0xdcff ? [c - 0xdc00] : [...Buffer.from(ch)]; }).flat()));
39const SIG = '-----BEGIN PGP SIGNATURE-----\n\niQEzBAABCAAdFiEE\n=abcd\n-----END PGP SIGNATURE-----\n';
40const MESSAGES = { none: null, empty: '', newline: 'release\n', 'no-newline': 'release', paragraphs: 'subject\n\nbody line\n\ntrailer: x\n', signed: `release\n${SIG}` };
41const BYTES = {
42 utf8: { name: 'v1.0', tagger: 'Ada Lovelace <ada@x> 1700000000 -0700', msg: m => m },
43 'non-utf8-name-msg': { name: raw('v', [0xe9], '-', [0xff]), tagger: 'Ada <ada@x> 1700000000 +0000', msg: m => (m === null ? null : raw(m, [0xe9, 0x80, 0xc3], '\n')) },
44 'non-utf8-tagger': { name: 'v2', tagger: raw('Ad', [0xe9, 0xff], ' <a', [0xfe], '@x> 0 +1400'), msg: m => m },
45};
46const EXTRA = { none: [], 'gpgsig-sha256': ['gpgsig-sha256 -----BEGIN PGP SIGNATURE-----', ' ', ' iQEzBAABCAAdFiEE', ' -----END PGP SIGNATURE-----'] };
48// the tag object, spelled out as git writes it (builtin/tag.c create_tag; fsck_tag_standalone's order)
49const handBytes = ({ object, type, tag, tagger, extra, message }) => Buffer.concat([
50 enc(`object ${object}\ntype ${type}\ntag ${tag}\n${tagger === null ? '' : `tagger ${tagger}\n`}${extra.map(l => `${l}\n`).join('')}`),
51 message === null ? Buffer.alloc(0) : Buffer.concat([Buffer.from('\n'), enc(message)]),
52]);
53const gitHash = bytes => {
54 const r = spawnSync('git', ['hash-object', '-t', 'tag', '--stdin'], { cwd: work, input: bytes, encoding: 'buffer' });
55 return r.status === 0 ? { oid: r.stdout.toString().trim() } : { refused: r.stderr.toString().trim() };
56};
58test('encodeTag: git hash-object stores its bytes under hashObject\'s oid, and refuses exactly the tags it refuses', () => {
59 let cells = 0, stored = 0, refused = 0;
60 for (const [type, object] of Object.entries(targets)) {
61 for (const withTagger of [true, false]) {
62 for (const [mName, message0] of Object.entries(MESSAGES)) {
63 for (const [bName, b] of Object.entries(BYTES)) {
64 for (const [xName, extra] of Object.entries(EXTRA)) {
65 const cell = `${type} tagger=${withTagger} message=${mName} bytes=${bName} extra=${xName}`;
66 const fields = { object, type, tag: b.name, tagger: withTagger ? b.tagger : null, extra, message: b.msg(message0) };
67 const want = gitHash(handBytes(fields));
68 let got;
69 try { got = unwrap(encodeTag({ ...fields, tagger: fields.tagger ?? undefined })); } catch (e) { got = e; }
70 if (want.refused) {
71 assert.ok(got instanceof Error && got.code === 'bad_request', `${cell}: git refuses (${want.refused}), encodeTag returned bytes`);
72 refused++;
73 } else {
74 assert.ok(!(got instanceof Error), `${cell}: git stores it, encodeTag threw ${got?.message}`);
75 assert.deepEqual(Buffer.from(got), handBytes(fields), `${cell}: bytes`);
76 assert.equal(hashObject('tag', got), want.oid, `${cell}: oid`);
77 stored++;
78 }
79 cells++;
80 }
81 }
82 }
83 }
84 }
85 assert.equal(cells, 4 * 2 * 6 * 3 * 2);
86 // every tagger-less cell is refused (missingTaggerEntry is an error to hash-object), every other is stored
87 assert.equal(refused, cells / 2);
88 assert.equal(stored, cells / 2);
89 console.log(` encodeTag vs git hash-object: ${cells} cells, ${stored} stored, ${refused} refused by both`);
90});
92test('encodeTag: byte-equal to the tag `git tag -a` writes, for every cell git\'s porcelain can write', () => {
93 let cells = 0;
94 const env = { ...process.env, GIT_COMMITTER_NAME: 'Ada Lovelace', GIT_COMMITTER_EMAIL: 'ada@x', GIT_COMMITTER_DATE: '1700000000 -0700' };
95 for (const [type, object] of Object.entries(targets)) {
96 for (const [mName, message] of Object.entries(MESSAGES)) {
97 if (message === null) continue; // git tag always writes the blank line
98 for (const msgBytes of ['utf8', 'non-utf8']) {
99 const name = `t-${type}-${mName}-${msgBytes}`;
100 const body = msgBytes === 'utf8' ? Buffer.from(message) : Buffer.concat([Buffer.from(message), Buffer.from([0xe9, 0x80, 0xc3, 0x0a])]);
101 const file = join(root, `${name}.msg`);
102 writeFileSync(file, body);
103 const r = spawnSync('git', ['tag', '-a', '--cleanup=verbatim', '-F', file, name, object], { cwd: work, env, encoding: 'utf8' });
104 assert.equal(r.status, 0, r.stderr);
105 const written = git(work, ['cat-file', 'tag', `refs/tags/${name}`], { encoding: 'buffer' });
106 const bytes = unwrap(encodeTag({ object, type, tag: name, tagger: 'Ada Lovelace <ada@x> 1700000000 -0700', message: decodeBytes(body) }));
107 assert.deepEqual(Buffer.from(bytes), written, name);
108 assert.equal(hashObject('tag', bytes), git(work, ['rev-parse', `refs/tags/${name}`]).trim(), name);
109 cells++;
110 }
111 }
112 }
113 assert.equal(cells, 4 * 5 * 2);
114 console.log(` encodeTag vs git tag -a: ${cells} cells byte-equal`);
115});
117test('encodeTag refuses what git hash-object refuses: fsck_tag_standalone, cell by cell', () => {
118 const object = 'ab'.repeat(20);
119 const tagger = 'A <a@x> 1700000000 +0000';
120 // [what changes, fields]: each is refused by git hash-object (asserted) and by encodeTag
121 const refusedCells = [
122 ['bad object id', { object: 'zz'.repeat(20) }],
123 ['short object id', { object: object.slice(2) }],
124 ['type outside the four', { type: 'bogus' }],
125 ['type ofs_delta', { type: 'ofs_delta' }],
126 ['empty tag name', { tag: '' }],
127 ['tag name with a space', { tag: 'v 1' }],
128 ['tag name with ..', { tag: 'a..b' }],
129 ['tag name ending .lock', { tag: 'v1.lock' }],
130 ['tag name ending /', { tag: 'v1/' }],
131 ['tag name with @{', { tag: 'v@{1' }],
132 ['tag name with a component starting .', { tag: 'a/.b' }],
133 ['tag name ending .', { tag: 'v1.' }],
134 ['tag name with DEL', { tag: 'v\u007f' }],
135 ['tag name with *', { tag: 'v*' }],
136 ['tagger without email', { tagger: 'A a@x 1700000000 +0000' }],
137 ['tagger with a bad date', { tagger: 'A <a@x> x +0000' }],
138 ['tagger zero-padded date', { tagger: 'A <a@x> 01 +0000' }],
139 ['tagger with a bad timezone', { tagger: 'A <a@x> 1 +00' }],
140 ['tagger missing name space', { tagger: 'A<a@x> 1 +0000' }],
141 ];
142 for (const [what, change] of refusedCells) {
143 const fields = { object, type: 'commit', tag: 'v1', tagger, extra: [], message: 'm\n', ...change };
144 assert.ok(gitHash(handBytes(fields)).refused, `${what}: git refuses`);
145 assert.throws(() => unwrap(encodeTag(fields)), e => e.code === 'bad_request' && e.what !== undefined, what);
146 }
147 // accepted by both: what fsck lets through
148 const storedCells = [
149 ['tag name with a slash', { tag: 'release/v1' }],
150 ['tag name a./b', { tag: 'a./b' }],
151 ['tag name @', { tag: '@' }],
152 ['upper-case object id', { object: 'AB'.repeat(20) }],
153 ['type naming a blob for a commit id (no lookup)', { type: 'blob' }],
154 ['tagger at epoch 0', { tagger: 'A <a@x> 0 +0000' }],
155 ['tagger with an empty name', { tagger: ' <a@x> 1 +0000' }],
156 ['an extra header', { extra: ['x-custom value'] }],
157 ];
158 for (const [what, change] of storedCells) {
159 const fields = { object, type: 'commit', tag: 'v1', tagger, extra: [], message: 'm\n', ...change };
160 const want = gitHash(handBytes(fields));
161 assert.ok(want.oid, `${what}: git stores it (${want.refused})`);
162 assert.equal(hashObject('tag', unwrap(encodeTag(fields))), want.oid, what);
163 }
164 // ply is stricter than hash-object in two places, both already ply's reader rules: a NUL in the message (fsck
165 // never reads a tag body) and an extra header that would shadow one of the four (parseTag's typed refusal)
166 for (const [what, change] of [['NUL in the message', { message: 'a\0b\n' }], ['extra header shadowing tagger', { extra: [`tagger ${tagger}`] }]]) {
167 const fields = { object, type: 'commit', tag: 'v1', tagger, extra: [], message: 'm\n', ...change };
168 assert.ok(gitHash(handBytes(fields)).oid, `${what}: git stores it`);
169 assert.throws(() => unwrap(encodeTag(fields)), e => e.code === 'bad_request', what);
170 }
171});
Diagnostics6 git calls · 71 ms
CallStatusSizeTime071 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=1
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 · hit1 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=1
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 · 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=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=5323b9266eea5ded637285d1c5320699f12be76e
Open pack map
fetchtree:02005.6 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=63
git.response.bytes=5724
git.response.cache=HIT
git.response.duration_ms=3
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.5 KiB63 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=3623
git.response.cache=MISS
git.response.duration_ms=63
git.fetch.wants=023d898050d3805467273323b6e3335449e29f93
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=3623
git.pkt.payload.max=3596
git.sideband.band1.bytes=3596
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
a5e0f8420a1b332c
Request
d7b11a77…
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