main
ply4 git calls572 ms46 KiB1 cachedprotocol v2
312 lines16 KiBRaw
1// Build a commit from create/write/edit/delete/rename and push it. A missing ref is created (old oid = zero):
2// that is the root commit on an empty repository, and a new branch when `base` names another ref or a commit.
3// One find/replace on an existing file still goes through ply's editFile, which streams the blob.
4import { plyError, editFile, encodeCommit, fetchCommit, fetchObject, hashObject, isLfsPointerPrefix, operation, parseOid, parseRefname, parseTree, push, resolveCommit, TYPE, validatePath, ZERO_OID, KIND, type Oid, type Span } from 'ply'
5import { pathIn, unwrap, unwrapAsync, type PlyError } from '../ply-result.ts'
6import { atIndex, mapGet } from '../absent.ts'
7import { err, ok, type Result } from '../http/machine.ts'
8import { advertisedOid } from './advertised.ts'
9import { type CallOpts, type GitOutcome, type GitSpan, type GitTransport } from './call.ts'
10import { MODE_TREE, BLOB_MODES } from './constants.ts'
11import { assertNever, done, refExistsAt, settle, type WorkerError, type Written } from './errors.ts'
12import { readHead } from './head.ts'
13import { plyPerson, type Stamp } from './identity.ts'
14import { sameBranch, type Change, type EditRequest } from './request.ts'
15import { writeResult } from './result.ts'
16import { emptyDir, encodeMem, fileLeaf, isTree, lazyNode, lookup, putAt, removeAt, renameAt, replaceOnce, setChild, type Mem } from './tree.ts'
18type Transport = GitTransport
19type Root = GitSpan
20type Outcome = GitOutcome
22type Target = { readonly ref: string; readonly oid: Oid | null }
23type Parent = { readonly oid: Oid | null; readonly tree: string | null }
25const text = new TextEncoder()
26const FILE_MODES: ReadonlySet<string> = new Set(BLOB_MODES)
27/**
28 * No partial-clone filter. `tree:0` and `blob:none` both omit blobs (gitprotocol-v2 / partial clone),
29 * and an edit has to read the blob itself. Trees and commits stay on `tree:0`, which ply widens to
30 * `blob:none` when the server refuses the narrow filter.
31 */
32const NO_FILTER = ''
34function referencedBlobs(node: Mem, into: Set<string>): void {
35 if (node.kind === 'lazy') {
36 if (node.mode !== MODE_TREE) into.add(node.oid)
37 return
38 }
39 for (const child of node.entries.values()) referencedBlobs(child, into)
42function messageBody(message: string): string {
43 return message.endsWith('\n') ? message : `${message}\n`
46// readHead runs in its own span (tracer.span records a throw), so its failure is thrown and operation() returns it.
47async function resolveTarget(t: Transport, ref: string | null): Promise<Result<Target, PlyError>> {
48 if (ref === null) {
49 const head = await readHead(t)
50 if (head.kind === 'born') return ok({ ref: head.ref, oid: head.oid })
51 if (head.kind === 'unborn') return ok({ ref: head.ref, oid: null })
52 if (head.kind === 'detached') return err(plyError('bad_request', 'remote HEAD is detached; pass ref explicitly'))
53 return err(plyError('not_found', 'remote HEAD is absent; pass ref explicitly', { miss: { kind: 'head_absent' } }))
54 }
55 const listed = await advertisedOid(t, ref)
56 if (!listed.ok) return listed
57 if (!listed.value) return ok({ ref, oid: null })
58 const oid = parseOid(listed.value)
59 return oid.ok ? ok({ ref, oid: oid.value }) : oid
62async function commitOf(t: Transport, oid: string): Promise<Result<{ tree: string; parents: readonly string[] }, PlyError>> {
63 const fetched = await fetchCommit(t, oid)
64 if (!fetched.ok) {
65 const error = fetched.error
66 if (error.code === 'not_found' && error.miss) {
67 // A commit id that is missing or not a commit stays 404. The other kinds are the same not_found error.
68 switch (error.miss.kind) {
69 case 'commit':
70 case 'head_absent':
71 case 'head_unborn':
72 case 'is_directory':
73 case 'not_directory':
74 case 'path':
75 case 'ref':
76 case 'repository':
77 case 'submodule':
78 return err(error)
79 default:
80 return assertNever(error.miss)
81 }
82 }
83 return err(error)
84 }
85 return ok({ tree: fetched.value.commit.tree, parents: fetched.value.commit.parents })
88async function parentAt(t: Transport, oid: Oid): Promise<Result<Parent, PlyError>> {
89 const commit = await commitOf(t, oid)
90 return commit.ok ? ok({ oid, tree: commit.value.tree }) : commit
93async function resolveParent(t: Transport, req: EditRequest, target: Target): Promise<Result<Parent, PlyError>> {
94 if (req.base.kind === 'oid') return parentAt(t, req.base.oid)
95 if (req.base.kind === 'ref' && !sameBranch(req.base.ref, target.ref)) {
96 const resolved = await resolveCommit(t, req.base.ref)
97 if (!resolved.ok) return resolved
98 const oid = parseOid(resolved.value.commit)
99 return oid.ok ? parentAt(t, oid.value) : oid
100 }
101 if (target.oid === null) return ok({ oid: null, tree: null })
102 return parentAt(t, target.oid)
105function checkHead(target: Target, req: EditRequest): Result<null, PlyError> {
106 if (req.head.kind !== 'oid') return ok(null)
107 const actual = target.oid === null ? ZERO_OID : target.oid
108 if (req.head.oid !== actual) return err(plyError('cas_conflict', `${target.ref} is at ${actual}, not the expected head ${req.head.oid}`, { expected: req.head.oid, actual }))
109 return ok(null)
112async function asDir(t: Transport, node: Mem, trees: Map<string, Uint8Array>): Promise<Mem> {
113 if (node.kind === 'dir') return node
114 if (node.mode !== MODE_TREE) return node
115 let data = trees.get(node.oid)
116 if (!data) {
117 const obj = await unwrapAsync(fetchObject(t, { oid: node.oid, filter: 'tree:0', expectType: TYPE.TREE }))
118 data = obj.data as Uint8Array
119 trees.set(node.oid, data)
120 }
121 const entries = unwrap(parseTree(data, t.limits)) as { name: string; mode: string; oid: string }[]
122 const map = new Map<string, Mem>()
123 for (const entry of entries) map.set(entry.name, lazyNode(entry.mode, entry.oid))
124 return { kind: 'dir', entries: map }
127/** Expand directory nodes along `comps`, stopping before the leaf. Missing components are left for putAt to create. */
128async function expandDirs(t: Transport, node: Mem, comps: readonly string[], trees: Map<string, Uint8Array>): Promise<Mem> {
129 const dir = await asDir(t, node, trees)
130 if (dir.kind !== 'dir' || comps.length <= 1) return dir
131 const [name, ...rest] = comps
132 if (!name) return dir
133 const child = dir.entries.get(name)
134 if (!child || !isTree(child)) return dir
135 return setChild(dir, name, await expandDirs(t, child, rest, trees))
138function compsOf(t: Transport, path: string): string[] {
139 return unwrap(validatePath(path, t.limits)).components
142type Blobs = { readonly reads: Map<string, Uint8Array>; readonly created: Map<string, Uint8Array> }
144async function blobBytes(t: Transport, oid: string, blobs: Blobs): Promise<Uint8Array> {
145 const createdBlob = mapGet(blobs.created, oid)
146 const readBlob = createdBlob.tag === 'some' ? createdBlob : mapGet(blobs.reads, oid)
147 const held = readBlob.tag === 'some' ? readBlob.value : null
148 if (held) return held
149 const obj = await unwrapAsync(fetchObject(t, { oid, filter: NO_FILTER, expectType: TYPE.BLOB }))
150 const data = obj.data as Uint8Array
151 blobs.reads.set(oid, data)
152 return data
155function remember(blobs: Blobs, data: Uint8Array): string {
156 const oid = hashObject('blob', data) as string
157 blobs.created.set(oid, data)
158 return oid
161function rejectPointer(data: Uint8Array, oid: string): void {
162 if (isLfsPointerPrefix(data)) throw plyError('lfs_pointer', `${oid} is a Git LFS pointer; the file's content is in the LFS store, not in git`)
165async function applyOne(t: Transport, mem: Mem, change: Change, blobs: Blobs, trees: Map<string, Uint8Array>): Promise<Mem> {
166 if (change.kind === 'rename') {
167 const from = compsOf(t, change.from)
168 const to = compsOf(t, change.to)
169 mem = await expandDirs(t, mem, from, trees)
170 mem = await expandDirs(t, mem, to, trees)
171 return renameAt(mem, from, to)
172 }
173 const path = change.path
174 const comps = compsOf(t, path)
175 mem = await expandDirs(t, mem, comps, trees)
176 if (change.kind === 'delete') return removeAt(mem, comps, path)
177 if (change.kind === 'edit') {
178 const leaf = lookup(mem, comps)
179 if (!leaf) throw plyError('not_found', `${path} not found`, { miss: { kind: 'path', missing: path, existingPath: '' } })
180 if (leaf.kind === 'lazy' && leaf.mode === '160000') throw plyError('not_found', `${path} not found`, { miss: { kind: 'submodule', path, commit: leaf.oid } })
181 if (leaf.kind === 'lazy' && leaf.mode === MODE_TREE) throw plyError('not_found', `${path} not found`, { miss: { kind: 'is_directory', path, tree: leaf.oid } })
182 if (leaf.kind === 'dir' || !FILE_MODES.has(leaf.mode)) throw plyError('not_found', `${path} not found`, { miss: { kind: 'path', missing: path, existingPath: '' } })
183 const bytes = await blobBytes(t, leaf.oid, blobs)
184 rejectPointer(bytes, leaf.oid)
185 const replaced = replaceOnce(bytes, text.encode(change.find), text.encode(change.replace))
186 if (replaced.kind === 'count') throw plyError('match_count', `expected exactly one match, found ${replaced.matches}`, { matches: replaced.matches })
187 return putAt(mem, comps, fileLeaf(leaf.mode as typeof BLOB_MODES[number], remember(blobs, replaced.data)), true, path)
188 }
189 const oid = remember(blobs, text.encode(change.content))
190 return putAt(mem, comps, fileLeaf(change.mode, oid), change.kind === 'write', path)
193function singleEdit(req: EditRequest): { path: string; find: string; replace: string } | null {
194 if (req.changes.length !== 1) return null
195 const change = atIndex(req.changes, 0)
196 if (change.tag === 'none' || change.value.kind !== 'edit') return null
197 return change.value
200async function streamEdit(remote: string, req: EditRequest, target: Target, edit: { path: string; find: string; replace: string }, creating: boolean, opts: CallOpts): Promise<Result<Record<string, unknown>, PlyError>> {
201 const base = req.base.kind === 'ref' ? req.base.ref : target.ref
202 const head = req.head.kind === 'oid' ? { head: req.head.oid, strictHead: true } : req.hint ? { head: req.hint } : {}
203 const ref = parseRefname(target.ref)
204 if (!ref.ok) return ref
205 const path = pathIn(edit.path, opts)
206 if (!path.ok) return path
207 const edited = await editFile(remote, {
208 ref: ref.value,
209 base,
210 path: path.value,
211 find: edit.find,
212 replace: edit.replace,
213 author: plyPerson(req.author),
214 message: req.message,
215 time: req.author.time,
216 }, { ...opts, ...head } as Parameters<typeof editFile>[2])
217 if (!edited.ok) return edited
218 const result = edited.value
219 const oid = result.newCommit
220 const oldOid = result.noop || !creating ? result.oldCommit : null
221 return ok(writeResult(result.noop ? 'noop' : 'commit', target.ref, oid, oldOid, {
222 ...result,
223 tree: result.newTree,
224 onto: creating ? result.oldCommit : oldOid,
225 }))
228/**
229 * Apply `req` and push. Returns the unified write body.
230 * `author` stamps both author and committer unless `committer` is set, in which case the commit is built here
231 * (editFile has only one identity).
232 * ref_exists is the Worker's own conflict, not a PlyError: the body returns it as a value (see branch.ts). The tree
233 * edits run inside the git.commit.apply span, before any push: applyOne and its helpers throw only PlyErrors there,
234 * which operation() returns as that Err.
235 */
236export async function commitEdit(remote: string, req: EditRequest, opts: CallOpts): Promise<Result<Record<string, unknown>, PlyError | WorkerError>> {
237 return settle(await operation<Written<Record<string, unknown>>>('commit', remote, {
238 ...opts,
239 attrs: { 'git.commit.ops': req.changes.map(change => change.kind), 'git.commit.ref': req.ref === null ? 'HEAD' : req.ref },
240 }, async ({ t, root, outcome }: { t: Transport; root: Root; outcome: Outcome }) => {
241 const resolvedTarget = await resolveTarget(t, req.ref)
242 if (!resolvedTarget.ok) return resolvedTarget
243 const target = resolvedTarget.value
244 const headCheck = checkHead(target, req)
245 if (!headCheck.ok) return headCheck
246 const baseIsOther = req.base.kind === 'ref' && !sameBranch(req.base.ref, target.ref)
247 if (target.oid !== null && baseIsOther) return outcome('ref_exists', refExistsAt(target.ref, target.oid))
248 const creating = target.oid === null
249 const edit = singleEdit(req)
250 const stream = edit !== null && req.committer === null && req.base.kind !== 'oid' && (target.oid !== null || baseIsOther)
251 if (edit && !stream && creating && !baseIsOther) return err(plyError('not_found', `ref ${target.ref} is unborn or absent; a root commit needs a create`, { miss: { kind: 'ref', ref: target.ref, candidates: [target.ref] } }))
252 root.set({ 'git.commit.ref': target.ref, 'git.commit.creating': creating, 'git.commit.base': req.base.kind === 'omit' ? '' : req.base.kind === 'oid' ? req.base.oid : req.base.ref })
253 if (stream && edit) {
254 const streamed = await streamEdit(remote, req, target, edit, creating, opts)
255 if (streamed.ok) return outcome(String(streamed.value.outcome), done(streamed.value))
256 if (!creating || streamed.error.code !== 'cas_conflict') return streamed
257 const cur = await advertisedOid(t, target.ref)
258 if (!cur.ok) return cur
259 return outcome('ref_exists', refExistsAt(target.ref, cur.value === null ? '' : cur.value))
260 }
261 const resolvedParent = await resolveParent(t, req, target)
262 if (!resolvedParent.ok) return resolvedParent
263 const parent = resolvedParent.value
264 if (!creating && parent.oid !== target.oid) return err(plyError('bad_request', `base ${parent.oid} is not the tip of ${target.ref}; omit base to commit on the tip, or name a new ref to start from that commit`))
265 const blobs: Blobs = { reads: new Map(), created: new Map() }
266 const trees = new Map<string, Uint8Array>()
267 let mem: Mem = parent.tree === null ? emptyDir() : lazyNode(MODE_TREE, parent.tree)
268 mem = await t.tracer.span('git.commit.apply', {
269 parent: root,
270 kind: KIND.CLIENT,
271 attrs: { 'git.commit.ops': req.changes.map(change => change.kind), 'git.commit.creating': creating },
272 }, async (span: Span) => {
273 for (const change of req.changes) mem = await applyOne(t, mem, change, blobs, trees)
274 span.set({ 'git.commit.applied': req.changes.length })
275 return mem
276 })
277 const built = encodeMem(mem)
278 const committer: Stamp = req.committer === null ? req.author : req.committer
279 if (!creating && parent.oid === target.oid && built.oid === parent.tree) {
280 root.set({ 'git.commit.tree': built.oid, 'git.commit.oid': target.oid })
281 return outcome('noop', done(writeResult('noop', target.ref, target.oid, target.oid, { tree: built.oid, pushBytes: 0 })))
282 }
283 const encoded = encodeCommit({
284 tree: built.oid,
285 parents: parent.oid ? [parent.oid] : [],
286 author: req.author.line,
287 committer: committer.line,
288 message: messageBody(req.message),
289 })
290 if (!encoded.ok) return encoded
291 const data = encoded.value
292 const oid = hashObject('commit', data)
293 const seen = new Set<string>()
294 const used = new Set<string>()
295 referencedBlobs(mem, used)
296 const objects: { type: number; data: Uint8Array }[] = [{ type: TYPE.COMMIT, data }]
297 for (const tree of built.trees) if (!seen.has(tree.oid)) { seen.add(tree.oid); objects.push({ type: TYPE.TREE, data: tree.data }) }
298 for (const [blob, bytes] of blobs.created) if (used.has(blob) && !seen.has(blob)) { seen.add(blob); objects.push({ type: TYPE.BLOB, data: bytes }) }
299 root.set({ 'git.commit.tree': built.oid, 'git.commit.oid': oid, 'git.commit.parent': parent.oid === null ? '' : parent.oid, 'git.commit.objects': objects.length })
300 const oldOid = creating ? ZERO_OID : target.oid
301 const name = parseRefname(target.ref)
302 if (!name.ok) return name
303 const pushed = await push(t, { ref: name.value, oldOid, newOid: oid, objects })
304 if (!pushed.ok) {
305 if (!creating || pushed.error.code !== 'cas_conflict') return pushed
306 const cur = await advertisedOid(t, target.ref)
307 if (!cur.ok) return cur
308 return outcome('ref_exists', refExistsAt(target.ref, cur.value === null ? '' : cur.value))
309 }
310 return outcome('commit', done(writeResult('commit', target.ref, oid, creating ? null : target.oid, { tree: built.oid, pushBytes: pushed.value.packBytes, onto: parent.oid })))
311 }))
Diagnostics4 git calls · 572 ms
CallStatusSizeTime0572 ms
discover200152 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
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 B143 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=143
git.response.bytes=145
git.response.cache=EXPIRED
git.response.duration_ms=143
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 · hit5 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=5
git.response.bytes=41349
git.response.cache=HIT
git.response.duration_ms=5
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
fetch2005.0 KiB424 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=424
git.response.bytes=5105
git.response.cache=MISS
git.response.duration_ms=424
git.fetch.wants=9bdcb11da8f1da830b3e533fb6bed18daebefd30
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=5105
git.pkt.payload.max=5084
git.sideband.band1.bytes=5083
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
a5e24a18694c0ecd
Request
b7344a58…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format