main
ply4 git calls327 ms43 KiB2 cachedprotocol v2
108 lines6.0 KiBRaw
1// Create or delete a ref. `from: 000…0` is rejected before any transport is opened, so nothing is pushed.
2// An existing ref is ref_exists (retry no). A delete whose expected oid is stale is stale_expected and carries
3// the oid the ref actually points at.
4import { plyError, fetchCommit, operation, parseOid, parseRefname, push, resolveCommit, ZERO_OID } from 'ply'
5import { fromNullable } from '../absent.ts'
6import { err, ok, type Result } from '../http/machine.ts'
7import { type PlyError } from '../ply-result.ts'
8import { advertisedOid } from './advertised.ts'
9import { type CallOpts, type GitOutcome, type GitSpan, type GitTransport } from './call.ts'
10import { assertNever, done, refExistsAt, settle, staleAt, type WorkerError, type Written } from './errors.ts'
11import { readHead } from './head.ts'
12import { branchRef } from './request.ts'
13import { writeResult } from './result.ts'
15type Transport = GitTransport
16type Root = GitSpan
17type Outcome = GitOutcome
19async function tipOf(t: Transport, from: string): Promise<Result<string, PlyError>> {
20 if (parseOid(from).ok) {
21 const fetched = await fetchCommit(t, from)
22 if (!fetched.ok) {
23 const error = fetched.error
24 if (error.code === 'not_found' && error.miss) {
25 // fetchCommit's not_found is a commit miss. Every kind stays that error: not_found is 404, message unchanged.
26 switch (error.miss.kind) {
27 case 'commit':
28 case 'head_absent':
29 case 'head_unborn':
30 case 'is_directory':
31 case 'not_directory':
32 case 'path':
33 case 'ref':
34 case 'repository':
35 case 'submodule':
36 return err(error)
37 default:
38 return assertNever(error.miss)
39 }
40 }
41 return err(error)
42 }
43 return ok(from)
44 }
45 const resolved = await resolveCommit(t, from)
46 return resolved.ok ? ok(resolved.value.commit) : resolved
49// readHead runs in its own span (tracer.span records a throw); an unusable HEAD is this function's Err.
50async function bornTip(t: Transport): Promise<Result<string, PlyError>> {
51 const head = await readHead(t)
52 if (head.kind === 'born') return ok(head.oid)
53 if (head.kind === 'unborn') return err(plyError('bad_request', `from is required when the remote HEAD is unborn (${head.ref})`))
54 if (head.kind === 'detached') return err(plyError('bad_request', 'from is required when the remote HEAD is detached'))
55 return err(plyError('bad_request', 'from is required when the remote HEAD is absent'))
58// The Worker's own conflicts (ref_exists, stale_expected) are not PlyErrors, and a throw from an operation() body is
59// ply's bad_request 'callback' (or 'ambiguous' after a push handover; ply #277). So the body returns them as values
60// (Written) and settle() turns them into the Worker error after operation() has returned.
61export async function createBranch(remote: string, refName: unknown, from: unknown, opts: CallOpts): Promise<Result<Record<string, unknown>, PlyError | WorkerError>> {
62 const ref = branchRef(refName, 'ref')
63 // Zero is refused here, before operation() opens a transport, so receive-pack is never called.
64 if (from === ZERO_OID) throw plyError('bad_request', 'from must not be the zero oid')
65 const present = fromNullable(from)
66 if (present.tag === 'some' && present.value !== '' && typeof present.value !== 'string') throw plyError('bad_request', 'from must be a branch name or a commit id')
67 const specified = typeof from === 'string' && from !== '' ? from : null
68 return settle(await operation<Written<Record<string, unknown>>>('branch_create', remote, { ...opts, attrs: { 'git.branch_create.ref': ref } }, async ({ t, root, outcome }: { t: Transport; root: Root; outcome: Outcome }) => {
69 const existing = await advertisedOid(t, ref)
70 if (!existing.ok) return existing
71 if (existing.value) return outcome('ref_exists', refExistsAt(ref, existing.value))
72 const tip = specified === null ? await bornTip(t) : await tipOf(t, specified)
73 if (!tip.ok) return tip
74 const commit = tip.value
75 root.set({ 'git.branch_create.oid': commit })
76 const name = parseRefname(ref)
77 if (!name.ok) return name
78 const pushed = await push(t, { ref: name.value, oldOid: ZERO_OID, newOid: commit, objects: [] })
79 if (!pushed.ok) {
80 if (pushed.error.code !== 'cas_conflict') return pushed
81 const cur = await advertisedOid(t, ref)
82 if (!cur.ok) return cur
83 return outcome('ref_exists', refExistsAt(ref, cur.value === null ? commit : cur.value))
84 }
85 return outcome('created', done(writeResult('created', ref, commit, null, { pushBytes: pushed.value.packBytes })))
86 }))
89export async function deleteRef(remote: string, ref: string, expected: string | null, opts: CallOpts, op: 'branch_delete' | 'tag_delete'): Promise<Result<Record<string, unknown>, PlyError | WorkerError>> {
90 return settle(await operation<Written<Record<string, unknown>>>(op, remote, { ...opts, attrs: { [`git.${op}.ref`]: ref } }, async ({ t, root, outcome }: { t: Transport; root: Root; outcome: Outcome }) => {
91 const advertised = await advertisedOid(t, ref)
92 if (!advertised.ok) return advertised
93 const current = advertised.value
94 if (!current) return err(plyError('not_found', `ref ${ref} not found`, { miss: { kind: 'ref', ref, candidates: [ref] } }))
95 if (expected && expected !== current) return outcome('stale_expected', staleAt(ref, expected, current))
96 root.set({ [`git.${op}.oid`]: current, [`git.${op}.cas_from_caller`]: expected !== null })
97 const name = parseRefname(ref)
98 if (!name.ok) return name
99 const pushed = await push(t, { ref: name.value, oldOid: current, newOid: ZERO_OID, objects: [] })
100 if (!pushed.ok) {
101 if (pushed.error.code !== 'cas_conflict') return pushed
102 const now = await advertisedOid(t, ref)
103 if (!now.ok) return now
104 return outcome('stale_expected', staleAt(ref, expected === null ? current : expected, now.value === null ? current : now.value))
105 }
106 return outcome('deleted', done(writeResult('deleted', ref, null, current, { deleted: current })))
107 }))
Diagnostics4 git calls · 327 ms
CallStatusSizeTime0327 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 B · 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=145
git.response.cache=HIT
git.response.duration_ms=2
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 · hit3 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=41349
git.response.cache=HIT
git.response.duration_ms=3
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.0 KiB322 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=322
git.response.bytes=2052
git.response.cache=MISS
git.response.duration_ms=322
git.fetch.wants=84e4702f9f676fb401de23c92989825cf549e179
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=2052
git.pkt.payload.max=2031
git.sideband.band1.bytes=2030
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
a5e2e866ec7fecfa
Request
5664fd0d…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format