GitHubivarvong
ply
main
1// The exported treeSort against git's own ordering, and against a counter: each entry's key is computed once.
2// The order is git's: `git mktree` builds the tree from the entries (given in a shuffled order) and `git ls-tree`
3// lists it. The names differ only by what git's directory rule decides: a directory `a` sorts as `a/` (0x2f), so it
4// goes after `a-` (0x2d) and `a.` (0x2e), before `a0` (0x30), and after the file that is only a longer name's prefix.
5import { test } 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 { treeSort } from '../src/objects.js';
13const BLOB = 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'; // git's empty blob
14const TREE = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'; // git's empty tree
15const TYPE_OF = { 100644: 'blob', 100755: 'blob', 120000: 'blob', 40000: 'tree', 160000: 'commit' };
17function git(dir, args, input) {
18 const r = spawnSync('git', ['-C', dir, ...args], { input, maxBuffer: 1 << 28 });
19 return { code: r.status ?? 1, out: `${r.stdout ?? ''}`, err: `${r.stderr ?? ''}` };
20}
21// git's order for these entries: what ls-tree lists for the tree mktree built from them
22function gitOrder(dir, entries) {
23 const stdin = entries.map(e => `${e.mode} ${TYPE_OF[e.mode]} ${e.mode === '40000' ? TREE : BLOB}\t${e.name}\0`).join('');
24 const made = git(dir, ['mktree', '-z', '--missing'], stdin);
25 assert.equal(made.code, 0, made.err);
26 return git(dir, ['ls-tree', '-z', '--name-only', made.out.trim()]).out.split('\0').filter(Boolean);
27}
28// mulberry32: the same shuffle on every run
29function shuffled(list, seed) {
30 let a = seed >>> 0;
31 const next = () => { a = (a + 0x6d2b79f5) >>> 0; let t = a; t = Math.imul(t ^ (t >>> 15), t | 1); t ^= t + Math.imul(t ^ (t >>> 7), t | 61); return ((t ^ (t >>> 14)) >>> 0) / 4294967296; };
32 const out = [...list];
33 for (let i = out.length - 1; i > 0; i--) { const j = Math.floor(next() * (i + 1)); [out[i], out[j]] = [out[j], out[i]]; }
34 return out;
35}
37test('treeSort is git ls-tree\'s order on names that differ only by the directory slash rule', () => {
38 const dir = mkdtempSync(join(tmpdir(), 'ply-tree-sort-'));
39 try {
40 assert.equal(git(dir, ['init', '-q', '-b', 'main']).code, 0);
41 let moved = 0;
42 for (const base of ['a', 'ab', 'é', '日本', 'a b', '😀']) {
43 // the bytes on both sides of '/' after the base, and longer names the base is a prefix of
44 const around = ['-', '.', '0', '+', ' ', '~', '\u0001', 'é', '-x', '.x', '0x'].map(s => `${base}${s}`);
45 for (const baseMode of ['40000', '100644', '160000', '120000']) {
46 for (const otherMode of ['100644', '40000']) {
47 const entries = shuffled([{ mode: baseMode, name: base }, ...around.map(name => ({ mode: otherMode, name }))], base.length + around.length);
48 const want = gitOrder(dir, entries);
49 const got = treeSort(entries);
50 assert.ok(got.ok);
51 assert.deepEqual(got.value.map(e => e.name), want, `${base} as ${baseMode} among ${otherMode}`);
52 // the entries themselves come back, not copies
53 for (const e of got.value) assert.ok(entries.includes(e));
54 if (want[0] !== base) moved += 1;
55 }
56 }
57 }
58 // the rule is exercised: a directory base does not sort first (`a-`, `a.` and more go before `a/`), a file base does
59 assert.ok(moved >= 12, `the base moved in only ${moved} trees`);
60 // a larger tree: 3 000 names in three shapes, a third of them directories
61 const big = shuffled(Array.from({ length: 3000 }, (_, i) => ({ mode: i % 3 === 0 ? '40000' : '100644', name: `${['n', 'n.', 'n-'][i % 3]}${i % 7}${i}` })), 3000);
62 const sorted = treeSort(big);
63 assert.ok(sorted.ok);
64 assert.deepEqual(sorted.value.map(e => e.name), gitOrder(dir, big));
65 } finally {
66 rmSync(dir, { recursive: true, force: true });
67 }
68});
70test('treeSort computes each key once: an entry\'s name is read once, however many comparisons the sort makes', () => {
71 // The expected count is the definition, not a measurement: n entries, n reads. A sort that builds its keys inside the
72 // comparator reads each name about 2 log2(n) times (2 000 entries: over 40 000 reads).
73 for (const n of [1, 2, 50, 2000]) {
74 let reads = 0;
75 const entries = shuffled(Array.from({ length: n }, (_, i) => {
76 const name = `f${i}`;
77 return { mode: i % 2 === 0 ? '40000' : '100644', get name() { reads += 1; return name; } };
78 }), n);
79 const got = treeSort(entries);
80 assert.ok(got.ok);
81 assert.equal(got.value.length, n);
82 assert.equal(reads, n, `${n} entries`);
83 }
84});
86test('treeSort refuses the first entry, in the caller\'s order, whose name has no byte form: every key is computed, compared or not', () => {
87 const e = (name, mode = '100644') => ({ mode, name });
88 // By hand: U+D800 to U+D803 are lone surrogates; the refusal names the first one in the list as given
89 const lone = hex => ({ ok: false, error: `tree entry name: lone surrogate U+${hex} is no byte sequence` });
90 const run = entries => { const r = treeSort(entries); return r.ok ? { ok: true } : { ok: false, error: r.error.message }; };
91 assert.deepEqual(run([e('\ud800')]), lone('D800'), 'one entry: its key is computed too');
92 assert.deepEqual(run([e('a'), e('\ud801')]), lone('D801'));
93 assert.deepEqual(run([e('\ud801'), e('\ud800')]), lone('D801'));
94 assert.deepEqual(run([e('z'), e('\ud802'), e('a'), e('\ud803', '40000')]), lone('D802'));
95 assert.deepEqual(run([e('z'), e('y'), e('x'), e('w'), e('v'), e('u'), e('t'), e('s'), e('r'), e('q'), e('p'), e('\ud803', '40000'), e('\ud800')]), lone('D803'));
96 assert.deepEqual(run([]), { ok: true });
97});
99test('treeSort refuses a name that is not a string, and an entry that is no object, as bad_request at 1, 2 and 3 entries: it never throws', () => {
100 // By hand, from the rule: a name that is not a string is refused as `tree entry name must be a string`, whatever the
101 // mode, the position and the list length. None of this is computed by the code under test.
102 const WANT = { ok: false, code: 'bad_request', message: 'tree entry name must be a string', what: 'tree entry name' };
103 const run = entries => {
104 try {
105 const r = treeSort(entries);
106 return r.ok ? { ok: true, names: r.value.map(e => e.name) } : { ok: false, code: r.error.code, message: r.error.message, what: r.error.what };
107 } catch (error) {
108 return { threw: String(error) };
109 }
110 };
111 const a = { mode: '100644', name: 'a' };
112 const b = { mode: '40000', name: 'b' };
113 let conversions = 0;
114 // the four kinds of name: the last two cannot be turned into a string at all
115 const kinds = {
116 null: () => null,
117 undefined: () => undefined,
118 'a Symbol': () => Symbol('s'),
119 'an object whose toString throws': () => ({ toString() { conversions += 1; throw new Error('boom'); } }),
120 };
121 let cases = 0;
122 for (const [kind, make] of Object.entries(kinds)) {
123 // a directory's key is its name and a `/` (where the name used to be concatenated), a file's key is its name
124 for (const mode of ['40000', '100644']) {
125 const bad = () => ({ mode, name: make() });
126 // every position of the bad entry, in a list of one, of two and of three
127 const lists = { 1: [[bad()]], 2: [[bad(), a], [a, bad()]], 3: [[bad(), a, b], [a, bad(), b], [a, b, bad()]] };
128 for (const [length, sameLength] of Object.entries(lists)) {
129 for (const [at, list] of sameLength.entries()) {
130 assert.equal(list.length, Number(length));
131 assert.deepEqual(run(list), WANT, `name ${kind}, mode ${mode}, ${length} entries, bad entry at ${at}`);
132 cases += 1;
133 }
134 }
135 }
136 }
137 // 4 kinds x 2 modes x (1 + 2 + 3) lists
138 assert.equal(cases, 48);
139 assert.equal(conversions, 0, 'a name that is not a string is never converted to one');
140 // an entry that is no object has no name: the same refusal, at the same three lengths
141 for (const none of [null, undefined]) {
142 assert.deepEqual(run([none]), WANT, `[${none}]`);
143 assert.deepEqual(run([none, a]), WANT, `[${none}, a]`);
144 assert.deepEqual(run([a, none]), WANT, `[a, ${none}]`);
145 assert.deepEqual(run([none, a, b]), WANT, `[${none}, a, b]`);
146 assert.deepEqual(run([a, none, b]), WANT, `[a, ${none}, b]`);
147 assert.deepEqual(run([a, b, none]), WANT, `[a, b, ${none}]`);
148 }
149 // the refusal is still the first entry in the caller's order: U+D800 is a lone surrogate, a string with no byte form
150 const lone = { mode: '100644', name: '\ud800' };
151 assert.deepEqual(run([lone, { mode: '40000', name: null }]), { ...WANT, message: 'tree entry name: lone surrogate U+D800 is no byte sequence' });
152 assert.deepEqual(run([{ mode: '40000', name: null }, lone]), WANT);
153 // and string names are sorted as before, at the same lengths. By hand: the directory b is `b/`; `a` (0x61) < `b` (0x62),
154 // and `b-` (0x2d after the b) < `b/` (0x2f)
155 assert.deepEqual(run([b]), { ok: true, names: ['b'] });
156 assert.deepEqual(run([b, a]), { ok: true, names: ['a', 'b'] });
157 assert.deepEqual(run([b, { mode: '100644', name: 'b-' }, a]), { ok: true, names: ['a', 'b-', 'b'] });
158});
Diagnostics6 git calls · 91 ms
CallStatusSizeTime091 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 · hit1 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=1 git.response.bytes=11990 git.response.cache=HIT git.response.duration_ms=1 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=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=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=5323b9266eea5ded637285d1c5320699f12be76eOpen 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=83 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=be7e0453974a5fe832f9065a33bb7e3434c10e9bOpen pack map
fetch2003.5 KiB83 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=3535 git.response.cache=MISS git.response.duration_ms=83 git.fetch.wants=dac0ce80390645443a877c6a9191735ba526de81 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=3535 git.pkt.payload.max=3508 git.sideband.band1.bytes=3508 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
- a5e0f831a990332c
- Request
- ad6e6a58…
- 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