GitHubivarvong
ply
main
1// Pure: the objects one edit commit is made of (new trees along the path, the commit, the blob as a delta), and a
2// commit's bytes from its tree and parents. A caller's identity is validated here (a bad one is the Err validateIdent
3// returns); a validated one is used as it is.
4import { ident, encodeCommitOf, hashObject, treeReplaceOid, TYPE } from './objects.js';
5import type { TreeEntry } from './objects.js';
6import { spliceDelta } from './pack.js';
7import type { PackInput } from './pack.js';
8import { fromHex } from './sha1.js';
9import { parseOid, type Oid } from './oid.js';
10import type { ValidatedIdent } from './validate.js';
11import { validateIdent, isValidatedIdent } from './validate.js';
12import type { ErrorOf } from './errors.js';
13import type { Input } from './absent.js';
14import { fromNullable, fromNullish } from './absent.js';
15import { andThen, getOrElse, matchOption, ok } from './result.js';
16import type { Result } from './result.js';
18// A caller's identity, before validateIdent. `time` overrides the commit's time for this person; `tz` defaults to UTC.
19export type Author = { readonly name: string; readonly email: string } & Input<{ readonly tz: string; readonly time: number }>;
20export type AuthorLike = Author | ValidatedIdent;
21type BadRequest = ErrorOf<'bad_request'>;
23function identLine(person: AuthorLike, time: number): Result<string, BadRequest> {
24 if (isValidatedIdent(person)) return ident(person);
25 const when = getOrElse(fromNullish(person.time), time);
26 // tz: only the missing value means UTC (validateIdent's default); a null tz is the caller's, and refused there
27 const tz = fromNullable(person.tz);
28 return andThen(validateIdent(tz.tag === 'some' ? { name: person.name, email: person.email, time: when, tz: tz.value } : { name: person.name, email: person.email, time: when }), ident);
29}
30// The committer line: the author's when none is given.
31const committerLine = (committer: AuthorLike | null, who: string, time: number): Result<string, BadRequest> =>
32 matchOption(fromNullish(committer), { some: c => identLine(c, time), none: () => ok(who) });
33// The author and committer lines, the author's first (its refusal wins).
34function people(author: AuthorLike, committer: AuthorLike | null, time: number): Result<{ readonly who: string; readonly written: string }, BadRequest> {
35 const who = identLine(author, time);
36 if (!who.ok) return who;
37 const written = committerLine(committer, who.value, time);
38 return written.ok ? ok({ who: who.value, written: written.value }) : written;
39}
41export type Level = { readonly treeOid: string; readonly data: Uint8Array; readonly entry: TreeEntry };
42export type BlobEditLite = { readonly oldOid: string; readonly newOid: string; readonly newLen: number; readonly delta: Uint8Array };
44export type NewTree = { readonly type: number; readonly data: Uint8Array; readonly baseOid: string; readonly newOid: Oid; readonly newData: Uint8Array };
45export type Built = {
46 readonly trees: readonly NewTree[];
47 readonly newTree: Oid;
48 readonly commitData: Uint8Array;
49 readonly newCommit: Oid;
50 readonly objects: readonly PackInput[];
51};
52// The commit's people and words. `committer` null (or left out) is the author.
53type Stamp = { readonly author: AuthorLike; readonly time: number; readonly message: string } & Input<{ readonly committer: AuthorLike | null }>;
55export function buildCommitObjects({ levels, blob, oldCommit, author, committer = null, time, message }: Stamp & {
56 readonly levels: readonly Level[];
57 readonly blob: Pick<BlobEditLite, 'oldOid' | 'newOid' | 'delta' | 'newLen'>;
58 readonly oldCommit: string;
59}): Result<Built, BadRequest> {
60 const trees: NewTree[] = [];
61 // the new blob's id is parsed once, here: every tree above it is spliced from it
62 const newBlob = parseOid(blob.newOid);
63 if (!newBlob.ok) return newBlob;
64 // and the parent's, once: the commit is written on it
65 const parent = parseOid(oldCommit);
66 if (!parent.ok) return parent;
67 let child: Oid = newBlob.value;
68 for (const { treeOid: oldOid, data, entry } of levels.toReversed()) {
69 const newData: Uint8Array = treeReplaceOid(data, entry, child);
70 const newOid = hashObject('tree', newData);
71 const delta = spliceDelta({ baseLen: data.length, off: entry.oidOffset, oldLen: 20, insert: fromHex(child) }).delta;
72 trees.unshift({ type: TYPE.TREE, data: delta, baseOid: oldOid, newOid, newData });
73 child = newOid;
74 }
75 const lines = people(author, committer, time);
76 if (!lines.ok) return lines;
77 const body = message.endsWith('\n') ? message : `${message}\n`;
78 const encoded = encodeCommitOf({ tree: child, parents: [parent.value], author: lines.value.who, committer: lines.value.written, message: body });
79 if (!encoded.ok) return encoded;
80 const commitData = encoded.value;
81 const newCommit = hashObject('commit', commitData);
82 const objects: PackInput[] = [
83 { type: TYPE.COMMIT, data: commitData },
84 ...trees.map((t): PackInput => ({ type: t.type, data: t.data, base: { tag: 'ref', oid: t.baseOid } })),
85 blob.delta.length >= 4 ? { type: TYPE.BLOB, data: blob.delta, base: { tag: 'ref', oid: blob.oldOid } } : { type: TYPE.BLOB, data: new Uint8Array(blob.newLen) },
86 ];
87 return ok({ trees, newTree: child, commitData, newCommit, objects });
88}
90export function withCommitData(built: Built, commitData: Uint8Array): Built {
91 const newCommit = hashObject('commit', commitData);
92 return { ...built, commitData, newCommit, objects: [{ type: TYPE.COMMIT, data: commitData }, ...built.objects.slice(1)] };
93}
95export function buildCommitData({ tree, parents, author, committer = null, time, message }: Stamp & { readonly tree: Oid; readonly parents: readonly Oid[] }): Result<{ readonly data: Uint8Array; readonly oid: Oid }, BadRequest> {
96 const lines = people(author, committer, time);
97 if (!lines.ok) return lines;
98 const body = message.endsWith('\n') ? message : `${message}\n`;
99 const encoded = encodeCommitOf({ tree, parents, author: lines.value.who, committer: lines.value.written, message: body });
100 if (!encoded.ok) return encoded;
101 return ok({ data: encoded.value, oid: hashObject('commit', encoded.value) });
102}
Diagnostics6 git calls · 55 ms
CallStatusSizeTime055 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=2 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 · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=1 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:02002.4 KiB · hit1 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=48 git.response.bytes=2507 git.response.cache=HIT git.response.duration_ms=1 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
fetch2002.0 KiB · hit48 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2050 git.response.cache=HIT git.response.duration_ms=48 git.fetch.wants=9e5cb5f57f734002cb9f3d26496997996b778b2b 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=2050 git.pkt.payload.max=2023 git.sideband.band1.bytes=2023 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
- a5e0f8262b05332c
- Request
- 66b215ac…
- 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