GitHubivarvong
ply
main
1// An in-memory tree for one multi-path commit. A lazy node is an object the server already has (its oid only);
2// a directory is expanded only when a change walks into it. Encoding writes the directories this call rebuilt;
3// a lazy node contributes its oid and no bytes. Empty directories are pruned; only the root may be empty.
4// Every refusal is a value (Result): the commit path (changes.ts) returns it, and nothing here throws.
5import { fail } from './errors.js';
6import type { ErrorOf } from './errors.js';
7import { err, ok } from './result.js';
8import type { Result } from './result.js';
9import { encodeEntries, hashObject } from './objects.js';
10import type { EntryOf, Mode } from './objects.js';
11import type { Oid } from './oid.js';
12import { indexOfBytes } from './bytes.js';
13import { limitExceeded } from './limits.js';
14import type { CheckedLimits } from './limits.js';
15import { MODE_LINK, MODE_TREE, BLOB_MODES } from './format.js';
16import { atOr, mapGet } from './absent.js';
18export type BlobMode = (typeof BLOB_MODES)[number];
19const BLOB_MODE_SET: ReadonlySet<string> = new Set(BLOB_MODES);
21export type Dir = { readonly kind: 'dir'; readonly entries: Map<string, Mem> };
22// An object the server already has: its mode and id as a parsed tree entry carries them.
23export type Lazy = { readonly kind: 'lazy'; readonly mode: Mode; readonly oid: Oid };
24export type Mem = Dir | Lazy;
26// What an edit of the memory tree can refuse: a path that is not what the change needs, a bound, a bad limit.
27export type MemError = ErrorOf<'bad_request' | 'not_found' | 'limit_exceeded'>;
29export const emptyDir = (): Dir => ({ kind: 'dir', entries: new Map() });
30export const lazyNode = (mode: Mode, oid: Oid): Lazy => ({ kind: 'lazy', mode, oid });
31export const isDir = (node: Mem): node is Extract<Mem, { readonly kind: 'dir' }> => node.kind === 'dir';
33// The directory's entries, or bad_request when the node is not a directory. Exported so a cell can reach this
34// refusal on a lazy node; the live path expands trees before it edits them.
35export function requireDir(node: Mem, path: string): Result<Map<string, Mem>, ErrorOf<'bad_request'>> {
36 if (node.kind !== 'dir') return fail('bad_request', `${path || '.'} is not a directory`, { what: 'path', path });
37 return ok(node.entries);
38}
40export type Leaf = { readonly mode: BlobMode; readonly oid: Oid; readonly data: Uint8Array };
41// Hash one blob the caller is adding. A symlink target is a path: git cannot check out an empty one, and a NUL ends it.
42export function fileLeaf(mode: BlobMode, data: Uint8Array, limits: CheckedLimits, path = 'blob'): Result<Leaf, ErrorOf<'bad_request' | 'limit_exceeded'>> {
43 const max = limits.maxObjectSize;
44 if (data.length > max) return err(limitExceeded('object size', data.length, max));
45 if (mode === MODE_LINK && (data.length === 0 || data.includes(0))) return fail('bad_request', `${path}: a symlink target must be non-empty and contain no NUL`, { what: 'symlink', path });
46 return ok({ mode, oid: hashObject('blob', data), data });
47}
49// validatePath refuses an empty path, so every component list that reaches here has a last name ('' is never one).
50const leafName = (comps: readonly string[]): string => atOr(comps, comps.length - 1, '');
52function joined(comps: readonly string[]): string {
53 return comps.join('/');
54}
56// The missing component and the path that existed above it. Not a fail wrapper: the refusal stays at the call.
57function pathMiss(path: string): { readonly kind: 'path'; readonly missing: string; readonly existingPath: string } {
58 const at = path.lastIndexOf('/');
59 if (at < 0) return { kind: 'path', missing: path, existingPath: '' };
60 return { kind: 'path', missing: path.slice(at + 1), existingPath: path.slice(0, at) };
61}
63export function insertChild(entries: Map<string, Mem>, name: string, node: Mem, limits: CheckedLimits): Result<null, ErrorOf<'bad_request' | 'limit_exceeded'>> {
64 if (!entries.has(name)) {
65 const max = limits.maxTreeEntries;
66 if (entries.size >= max) return err(limitExceeded('tree entries', entries.size + 1, max));
67 }
68 entries.set(name, node);
69 return ok(null);
70}
72// One step of a walk down existing directories: the entries under `name`, or the refusal for what is there instead.
73type Frame = { readonly entries: Map<string, Mem>; readonly name: string };
74function stepInto(entries: Map<string, Mem>, name: string, walked: string): Result<Map<string, Mem>, ErrorOf<'not_found'>> {
75 const child = mapGet(entries, name);
76 if (child.tag === 'none') return fail('not_found', `${walked} not found`, { what: 'path', path: walked, miss: pathMiss(walked) });
77 if (child.value.kind !== 'dir') return fail('not_found', `${walked} is not a directory`, { what: 'path', path: walked, miss: { kind: 'not_directory', path: walked, mode: child.value.mode } });
78 return ok(child.value.entries);
79}
81// Prune the directories, innermost first, that a removal left empty. The root may stay empty.
82function prune(stack: readonly Frame[]): void {
83 for (const frame of stack.toReversed()) {
84 const child = mapGet(frame.entries, frame.name);
85 if (child.tag === 'none' || child.value.kind !== 'dir' || child.value.entries.size > 0) break;
86 frame.entries.delete(frame.name);
87 }
88}
90// Put `leaf` at `comps`. `overwrite` is write (replace a file); create fails when the path exists.
91// A directory at the path is a refusal either way. Intermediate components must already be directories.
92export function putAt(root: Mem, comps: readonly string[], leaf: Mem, overwrite: boolean, limits: CheckedLimits): Result<null, MemError> {
93 const top = requireDir(root, '');
94 if (!top.ok) return top;
95 let entries = top.value;
96 let walked = '';
97 for (const name of comps.slice(0, -1)) {
98 walked = walked === '' ? name : `${walked}/${name}`;
99 const child = mapGet(entries, name);
100 if (child.tag === 'none' || child.value.kind !== 'dir') return fail('bad_request', `${walked} is not a directory`, { what: 'path', path: walked });
101 entries = child.value.entries;
102 }
103 const name = leafName(comps);
104 const full = walked === '' ? name : `${walked}/${name}`;
105 const existing = mapGet(entries, name);
106 if (existing.tag === 'some' && (existing.value.kind === 'dir' || existing.value.mode === MODE_TREE)) {
107 return fail('bad_request', `${full} is a directory`, { what: 'path', path: full });
108 }
109 if (existing.tag === 'some' && !overwrite) return fail('bad_request', `${full} already exists`, { what: 'path', path: full });
110 return insertChild(entries, name, leaf, limits);
111}
113// Drop the node at `comps` and prune directories that this left empty. The root may stay empty.
114export function removeAt(root: Mem, comps: readonly string[]): Result<null, ErrorOf<'bad_request' | 'not_found'>> {
115 const stack: Frame[] = [];
116 const top = requireDir(root, '');
117 if (!top.ok) return top;
118 let entries = top.value;
119 let walked = '';
120 for (const name of comps.slice(0, -1)) {
121 walked = walked === '' ? name : `${walked}/${name}`;
122 const next = stepInto(entries, name, walked);
123 if (!next.ok) return next;
124 stack.push({ entries, name });
125 entries = next.value;
126 }
127 const name = leafName(comps);
128 const full = walked === '' ? name : `${walked}/${name}`;
129 if (!entries.has(name)) return fail('not_found', `${full} not found`, { what: 'path', path: full, miss: pathMiss(full) });
130 entries.delete(name);
131 prune(stack);
132 return ok(null);
133}
135// Move `from` to `to`. The same path, and a destination inside the source, are bad_request before any lookup
136// (a missing source still is). The destination's parent must already exist; a destination that exists is bad_request.
137export function renameAt(root: Mem, from: readonly string[], to: readonly string[]): Result<null, ErrorOf<'bad_request' | 'not_found'>> {
138 const fromPath = joined(from);
139 const toPath = joined(to);
140 if (fromPath === toPath) return fail('bad_request', `${fromPath} is already named ${toPath}`, { what: 'path', path: fromPath });
141 if (toPath.startsWith(`${fromPath}/`)) return fail('bad_request', `cannot rename ${fromPath} into itself`, { what: 'path', path: fromPath });
142 const top = requireDir(root, '');
143 if (!top.ok) return top;
144 let srcEntries = top.value;
145 let walked = '';
146 const stack: Frame[] = [];
147 for (const name of from.slice(0, -1)) {
148 walked = walked === '' ? name : `${walked}/${name}`;
149 const next = stepInto(srcEntries, name, walked);
150 if (!next.ok) return next;
151 stack.push({ entries: srcEntries, name });
152 srcEntries = next.value;
153 }
154 const srcName = leafName(from);
155 const node = mapGet(srcEntries, srcName);
156 if (node.tag === 'none') return fail('not_found', `${fromPath} not found`, { what: 'path', path: fromPath, miss: pathMiss(fromPath) });
157 let destEntries = top.value;
158 let destWalked = '';
159 for (const name of to.slice(0, -1)) {
160 destWalked = destWalked === '' ? name : `${destWalked}/${name}`;
161 const next = stepInto(destEntries, name, destWalked);
162 if (!next.ok) return next;
163 destEntries = next.value;
164 }
165 const destName = leafName(to);
166 if (destEntries.has(destName)) return fail('bad_request', `${toPath} already exists`, { what: 'path', path: toPath });
167 srcEntries.delete(srcName);
168 destEntries.set(destName, node.value);
169 prune(stack);
170 return ok(null);
171}
173// Exactly one non-overlapping match. Zero or several is match_count; an empty find is bad_request.
174export function replaceOnce(data: Uint8Array, find: Uint8Array, replace: Uint8Array): Result<Uint8Array, ErrorOf<'bad_request' | 'match_count'>> {
175 if (find.length === 0) return fail('bad_request', 'find must be non-empty');
176 let count = 0;
177 let at = 0;
178 let from = 0;
179 for (;;) {
180 const i = indexOfBytes(data, find, from);
181 if (i < 0) break;
182 if (count === 0) at = i;
183 count += 1;
184 from = i + find.length;
185 }
186 if (count !== 1) return fail('match_count', `expected exactly one match, found ${count}`, { matches: count });
187 const out = new Uint8Array(data.length - find.length + replace.length);
188 out.set(data.subarray(0, at), 0);
189 out.set(replace, at);
190 out.set(data.subarray(at + find.length), at + replace.length);
191 return ok(out);
192}
194export type EncodedTree = { readonly oid: Oid; readonly data: Uint8Array };
195export type Encoded = { readonly oid: Oid; readonly trees: readonly EncodedTree[] };
196// The trees encoded so far, once per oid: two directories a call rebuilt to the same content are one tree object,
197// and a pack that carries one object twice is refused by a strict receiver ("unpacker error").
198type Trees = { readonly list: EncodedTree[]; readonly seen: Set<string> };
199function encodeNode(node: Mem, max: number, trees: Trees): Result<Oid, ErrorOf<'bad_request' | 'limit_exceeded'>> {
200 if (node.kind === 'lazy') return ok(node.oid);
201 if (node.entries.size > max) return err(limitExceeded('tree entries', node.entries.size, max));
202 const entries: EntryOf[] = [];
203 for (const [name, child] of node.entries) {
204 if (child.kind === 'dir') {
205 const sub = encodeNode(child, max, trees);
206 if (!sub.ok) return sub;
207 entries.push({ mode: MODE_TREE, name, oid: sub.value });
208 } else entries.push({ mode: child.mode, name, oid: child.oid });
209 }
210 const encoded = encodeEntries(entries);
211 if (!encoded.ok) return encoded;
212 const data = encoded.value;
213 const oid = hashObject('tree', data);
214 if (!trees.seen.has(oid)) {
215 trees.seen.add(oid);
216 trees.list.push({ oid, data });
217 }
218 return ok(oid);
219}
220// The trees this memory holds, deepest first, each oid once. A lazy node contributes its oid and no bytes.
221export function encodeMem(node: Mem, limits: CheckedLimits): Result<Encoded, ErrorOf<'bad_request' | 'limit_exceeded'>> {
222 if (node.kind === 'lazy') return ok({ oid: node.oid, trees: [] });
223 const trees: Trees = { list: [], seen: new Set() };
224 const oid = encodeNode(node, limits.maxTreeEntries, trees);
225 if (!oid.ok) return oid;
226 return ok({ oid: oid.value, trees: trees.list });
227}
229export function blobOids(node: Mem, into: Set<string>): void {
230 if (node.kind === 'lazy') {
231 if (BLOB_MODE_SET.has(node.mode)) into.add(node.oid);
232 return;
233 }
234 for (const child of node.entries.values()) blobOids(child, into);
235}
Diagnostics6 git calls · 64 ms
CallStatusSizeTime064 ms
discover200191 B3 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 · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=11990 git.response.cache=HIT git.response.duration_ms=3 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=10Open 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=5323b9266eea5ded637285d1c5320699f12be76eOpen pack map
fetchtree:02002.4 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=54 git.response.bytes=2507 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=8f720d29ff85ce682ffdad9c393b04a3834c3203 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=2507 git.pkt.payload.max=2480 git.sideband.band1.bytes=2480 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=2480 git.pack.trailer=723b6bf1d4d6021fe5eb940c622b9ff00ee4a381Open pack map
fetch2003.6 KiB54 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=3637 git.response.cache=MISS git.response.duration_ms=54 git.fetch.wants=51de34d5387dd8df4fc87047854f39d315377e01 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=3637 git.pkt.payload.max=3610 git.sideband.band1.bytes=3610 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
- a5e0fe6e2c44332c
- Request
- 033bbd68…
- 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