main
ply4 git calls44 ms43 KiB3 cachedprotocol v2
202 lines9.7 KiBRaw
1// Pure reading of a write body. Every rejection here is bad_request and happens before any fetch.
2import { plyError, parseOid, validateRef, ZERO_OID, type Oid } from 'ply';
3import { unwrap } from '../ply-result.ts';
4import { fromNullable, recordGet } from '../absent.ts';
5import { BLOB_MODES, MODE_BLOB, OP_CREATE, OP_DELETE, OP_EDIT, OP_RENAME, OP_WRITE } from './constants.ts';
6import { stamp, type Stamp, type UnixSeconds } from './identity.ts';
8export type BlobMode = typeof BLOB_MODES[number];
10export type BaseSpec =
11 | { readonly kind: 'omit' }
12 | { readonly kind: 'oid'; readonly oid: Oid }
13 | { readonly kind: 'ref'; readonly ref: string };
15export type ExpectedHead =
16 | { readonly kind: 'any' }
17 | { readonly kind: 'oid'; readonly oid: Oid };
19export type Change =
20 | { readonly kind: 'create'; readonly path: string; readonly content: string; readonly mode: BlobMode }
21 | { readonly kind: 'write'; readonly path: string; readonly content: string; readonly mode: BlobMode }
22 | { readonly kind: 'edit'; readonly path: string; readonly find: string; readonly replace: string }
23 | { readonly kind: 'delete'; readonly path: string }
24 | { readonly kind: 'rename'; readonly from: string; readonly to: string };
26export type EditRequest = {
27 readonly ref: string | null;
28 readonly base: BaseSpec;
29 readonly changes: readonly Change[];
30 readonly author: Stamp;
31 readonly committer: Stamp | null;
32 readonly message: string;
33 readonly head: ExpectedHead;
34 readonly hint: Oid | null;
35};
37type Body = Record<string, unknown>;
39function parsedOid(value: unknown): Oid | null {
40 const parsed = parseOid(value);
41 return parsed.ok ? parsed.value : null;
44export function asBody(value: unknown): Body {
45 if (value === null || typeof value !== 'object' || Array.isArray(value)) throw plyError('bad_request', 'body must be a JSON object');
46 return value as Body;
49export function fullRef(name: string): string {
50 return name.startsWith('refs/') ? name : `refs/heads/${name}`;
53/** A branch we may push. A 40-hex string is a commit id, never a branch called that. */
54export function branchRef(value: unknown, field: string): string {
55 if (typeof value !== 'string' || value === '') throw plyError('bad_request', `${field} is required`);
56 if (value === ZERO_OID) throw plyError('bad_request', `${field} must not be the zero oid`);
57 if (parsedOid(value)) throw plyError('bad_request', `${field} must be a branch name, not a commit id`);
58 return unwrap(validateRef(fullRef(value))) as string;
61/**
62 * A merge `from` or a rebase `onto`. A lowercase 40-hex string is that commit (ply fetches it and 404s a non-commit).
63 * The zero oid is rejected here, before a transport opens. Anything else is a branch name.
64 */
65export function refOrCommit(value: unknown, field: string): string {
66 if (typeof value !== 'string' || value === '') throw plyError('bad_request', `${field} is required`);
67 if (value === ZERO_OID) throw plyError('bad_request', `${field} must not be the zero oid`);
68 if (parsedOid(value)) return value;
69 return branchRef(value, field);
72export function tagRef(value: unknown): string {
73 if (typeof value !== 'string' || value === '') throw plyError('bad_request', 'name is required');
74 if (parsedOid(value)) throw plyError('bad_request', 'name must be a tag name, not an object id');
75 const full = value.startsWith('refs/tags/') ? value : `refs/tags/${value}`;
76 return unwrap(validateRef(full)) as string;
79/** Missing, null, and empty text. A present 0 or false is not blank. */
80function blank(value: unknown): boolean {
81 const found = fromNullable(value);
82 return found.tag === 'none' || found.value === '';
85function field(body: Body, key: string) {
86 return recordGet(body, key);
89export function parseBase(value: unknown): BaseSpec {
90 if (blank(value)) return { kind: 'omit' };
91 if (typeof value !== 'string') throw plyError('bad_request', 'base must be a branch name or a 40-hex commit id');
92 if (value === ZERO_OID) throw plyError('bad_request', 'base must not be the zero oid; omit base to create a root commit');
93 const baseOid = parsedOid(value);
94 if (baseOid) return { kind: 'oid', oid: baseOid };
95 return { kind: 'ref', ref: branchRef(value, 'base') };
98/** The oid a delete compares against. Omitted means "whatever ls-refs shows now". The zero oid is not a tip. */
99export function parseDeleteOid(value: unknown): Oid | null {
100 if (blank(value)) return null;
101 if (value === ZERO_OID) throw plyError('bad_request', 'oid must not be the zero oid');
102 const oid = parsedOid(value);
103 if (!oid) throw plyError('bad_request', 'oid must be a 40-hex oid');
104 return oid;
107export function parseExpectedHead(value: unknown): ExpectedHead {
108 if (blank(value)) return { kind: 'any' };
109 const oid = parsedOid(value);
110 if (!oid) throw plyError('bad_request', 'head must be a 40-hex oid');
111 return { kind: 'oid', oid };
114function parseHint(value: unknown): Oid | null {
115 if (blank(value)) return null;
116 const oid = parsedOid(value);
117 if (value === ZERO_OID || !oid) throw plyError('bad_request', 'hint must be a 40-hex commit id');
118 return oid;
121export function parseMode(value: unknown): BlobMode {
122 const found = fromNullable(value);
123 if (found.tag === 'none') return MODE_BLOB;
124 if (typeof value === 'string' && (BLOB_MODES as readonly string[]).includes(value)) return value as BlobMode;
125 throw plyError('bad_request', 'mode must be 100644, 100755, or 120000');
128function text(value: unknown, field: string): string {
129 if (typeof value !== 'string') throw plyError('bad_request', `${field} must be a string`);
130 return value;
133function contentOf(obj: Body): string {
134 const content = field(obj, 'content');
135 const replace = field(obj, 'replace');
136 if (content.tag === 'some' && typeof content.value !== 'string') throw plyError('bad_request', 'content must be a string');
137 if (replace.tag === 'some' && typeof replace.value !== 'string') throw plyError('bad_request', 'replace must be a string');
138 if (content.tag === 'some' && replace.tag === 'some' && content.value !== replace.value) throw plyError('bad_request', 'content and replace differ');
139 if (content.tag === 'some' && typeof content.value === 'string') return content.value;
140 if (replace.tag === 'some' && typeof replace.value === 'string') return replace.value;
141 return '';
144function parseOp(obj: Body): Change {
145 const op = obj.op;
146 if (op === OP_CREATE || op === OP_WRITE) {
147 const content = field(obj, 'content');
148 return { kind: op, path: text(obj.path, 'path'), content: text(content.tag === 'none' ? '' : content.value, 'content'), mode: parseMode(obj.mode) };
149 }
150 if (op === OP_EDIT) {
151 const find = text(obj.find, 'find');
152 if (find === '') throw plyError('bad_request', 'edit find must be non-empty; omit find, or use op create, to create a file');
153 const replace = field(obj, 'replace');
154 return { kind: 'edit', path: text(obj.path, 'path'), find, replace: replace.tag === 'none' ? '' : text(replace.value, 'replace') };
155 }
156 if (op === OP_DELETE) return { kind: 'delete', path: text(obj.path, 'path') };
157 if (op === OP_RENAME) {
158 const path = field(obj, 'path');
159 const from = path.tag === 'some' ? path.value : obj.from;
160 return { kind: 'rename', from: text(from, 'path'), to: text(obj.to, 'to') };
161 }
162 throw plyError('bad_request', `op must be ${OP_CREATE}, ${OP_WRITE}, ${OP_EDIT}, ${OP_DELETE}, or ${OP_RENAME}`);
165function parseChanges(body: Body): readonly Change[] {
166 const changes = field(body, 'changes');
167 if (changes.tag === 'some') {
168 if (field(body, 'path').tag === 'some' || field(body, 'find').tag === 'some' || field(body, 'to').tag === 'some' || field(body, 'delete').tag === 'some') throw plyError('bad_request', 'pass changes or a single path, not both');
169 if (!Array.isArray(changes.value) || changes.value.length === 0) throw plyError('bad_request', 'changes must be a non-empty array');
170 return changes.value.map(item => {
171 if (item === null || typeof item !== 'object' || Array.isArray(item)) throw plyError('bad_request', 'each change must be an object');
172 return parseOp(item as Body);
173 });
174 }
175 const path = text(body.path, 'path');
176 const deleted = field(body, 'delete');
177 if (deleted.tag === 'some' && deleted.value === true) return [{ kind: 'delete', path }];
178 if (deleted.tag === 'some') throw plyError('bad_request', 'delete must be true');
179 const to = field(body, 'to');
180 if (to.tag === 'some') return [{ kind: 'rename', from: path, to: text(to.value, 'to') }];
181 const find = field(body, 'find');
182 const creating = find.tag === 'none' || find.value === '';
183 if (creating) return [{ kind: 'create', path, content: contentOf(body), mode: parseMode(body.mode) }];
184 if (typeof find.value !== 'string') throw plyError('bad_request', 'find must be a string');
185 const replace = field(body, 'replace');
186 return [{ kind: 'edit', path, find: find.value, replace: replace.tag === 'none' ? '' : text(replace.value, 'replace') }];
189export function parseEdit(body: Body, now: UnixSeconds): EditRequest {
190 const ref = blank(body.ref) ? null : branchRef(body.ref, 'ref');
191 const message = text(body.message, 'message');
192 if (message.includes('\0')) throw plyError('bad_request', 'message must not contain NUL');
193 const author = stamp(body.author, 'author', now);
194 const committer = field(body, 'committer');
195 const stampedCommitter = committer.tag === 'none' ? null : stamp(committer.value, 'committer', now);
196 return { ref, base: parseBase(body.base), changes: parseChanges(body), author, committer: stampedCommitter, message, head: parseExpectedHead(body.head), hint: parseHint(body.hint) };
199export function sameBranch(requested: string, full: string): boolean {
200 if (requested === full || requested === 'HEAD') return true;
201 return !requested.startsWith('refs/') && full === `refs/heads/${requested}`;
Diagnostics4 git calls · 44 ms
CallStatusSizeTime044 ms
discover200152 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
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 B · hit4 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=4
git.response.bytes=145
git.response.cache=HIT
git.response.duration_ms=4
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 1020040 KiB · hit17 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=16
git.response.bytes=41349
git.response.cache=HIT
git.response.duration_ms=17
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=526b305930b8ef382f2d7a1f9c0238730a585921
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=41349
git.pkt.payload.max=41254
git.sideband.band1.bytes=41253
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=61
git.pack.bytes=41253
git.pack.ofs_delta=0
git.pack.ref_delta=0
git.pack.chain.max=0
git.pack.trailer=ab60efef4b7243678c75de64a4386986ee885c1c
git.pack.full=61
Open pack map
fetch2002.6 KiB · hit23 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=23
git.response.bytes=2641
git.response.cache=HIT
git.response.duration_ms=23
git.fetch.wants=287fd76811f1785a7fcd4737d52af1af7222cb55
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=2641
git.pkt.payload.max=2620
git.sideband.band1.bytes=2619
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-web.git
Ref
refs/heads/main
Commit
526b305930b8ef382f2d7a1f9c0238730a585921
cf-ray
a5e2c0ffaa7c89da
Request
f022a4c5…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format