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}
Diagnostics4 git calls · 126 ms
CallStatusSizeTime0126 ms
discover200152 B4 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=4 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200145 B102 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=98 git.response.bytes=145 git.response.cache=EXPIRED git.response.duration_ms=98 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=145 git.pkt.payload.max=76 git.ls_refs.count=2 git.ls_refs.head=refs/heads/main
fetchblob:none · deepen 1020038 KiB · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=38434 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false 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=6 git.pkt.data=4 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=38434 git.pkt.payload.max=38339 git.sideband.band1.bytes=38338 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=63 git.pack.bytes=38338 git.pack.ofs_delta=0 git.pack.ref_delta=8 git.pack.chain.max=8 git.pack.trailer=0bc36ab7adf82d3109177f11f1ea93f1ce071f77 git.pack.full=55Open pack map
fetch2002.0 KiB · hit20 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=20 git.response.bytes=2038 git.response.cache=HIT git.response.duration_ms=20 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=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=2038 git.pkt.payload.max=2017 git.sideband.band1.bytes=2016 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://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/ply.git
- Ref
- refs/heads/main
- Commit
- 19912563f540228593df0c489082e5e2508e21e4
- cf-ray
- a5ea3bf09a02b87b
- Request
- dc025193…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format