The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | /** |
| 2 | * Protocol-v0 ref advertisement builders (DESIGN §2.3, §4). |
| 3 | * |
| 4 | * The smart-HTTP advertisement body is two pkt-line streams: |
| 5 | * |
| 6 | * ``` |
| 7 | * 001e# service=git-upload-pack\n |
| 8 | * 0000 |
| 9 | * <oid> HEAD\0<capabilities>\n ← first ref carries caps after a NUL |
| 10 | * <oid> refs/heads/main\n |
| 11 | * <oid> refs/tags/v1\n |
| 12 | * <peeled> refs/tags/v1^{}\n ← annotated tags peeled |
| 13 | * 0000 |
| 14 | * ``` |
| 15 | * |
| 16 | * Empty repo: a single `<zero-id> capabilities^{}\0<caps>` line. The response |
| 17 | * must carry `Content-Type: application/x-git-{service}-advertisement` and |
| 18 | * `Cache-Control: no-cache` (the client validates the first five bytes match |
| 19 | * `^[0-9a-f]{4}#`). |
| 20 | */ |
| 21 | import { concatBytes, utf8Encode, ZERO_OID } from "./ObjectCodec.ts"; |
| 22 | import { flushPkt, pktLine } from "./Pkt.ts"; |
| 23 | import type { RefRecord, RefsSnapshot } from "./Store.ts"; |
| 24 | |
| 25 | /** The agent string advertised by both services. */ |
| 26 | export const AGENT = "git-service/1"; |
| 27 | |
| 28 | /** The two smart-HTTP services. */ |
| 29 | export type GitService = "git-upload-pack" | "git-receive-pack"; |
| 30 | |
| 31 | /** |
| 32 | * The v0 upload-pack capability set (DESIGN §2.3): stateless |
| 33 | * `multi_ack_detailed` + `no-done` negotiation, side-band-64k muxing, shallow |
| 34 | * (`deepen <n>` only), and the default-branch symref. |
| 35 | */ |
| 36 | export const uploadPackCapabilities = (defaultBranch: string): string => |
| 37 | `multi_ack_detailed no-done side-band-64k shallow ofs-delta agent=${AGENT} symref=HEAD:refs/heads/${defaultBranch} object-format=sha1`; |
| 38 | |
| 39 | /** |
| 40 | * The v0 receive-pack capability set (DESIGN §2.3): per-ref report-status |
| 41 | * (v1 and v2 — v2 is implemented identically, with no `option` lines, which |
| 42 | * is valid), ref deletion, atomic pushes, and OFS_DELTA in pushed packs. |
| 43 | */ |
| 44 | export const receivePackCapabilities = (): string => |
| 45 | `report-status report-status-v2 delete-refs side-band-64k atomic ofs-delta object-format=sha1 agent=${AGENT}`; |
| 46 | |
| 47 | /** |
| 48 | * The `Content-Type` of a `GET info/refs?service=` advertisement response. |
| 49 | */ |
| 50 | export const advertisementContentType = (service: GitService): string => |
| 51 | `application/x-${service}-advertisement`; |
| 52 | |
| 53 | /** |
| 54 | * The `Content-Type` of a `POST /git-{upload,receive}-pack` result response. |
| 55 | */ |
| 56 | export const resultContentType = (service: GitService): string => `application/x-${service}-result`; |
| 57 | |
| 58 | /** |
| 59 | * Builds the `# service=` prelude: one pkt-line plus a flush. Sent before the |
| 60 | * ref list on `info/refs` for both services. |
| 61 | */ |
| 62 | export const servicePrelude = (service: GitService): Uint8Array => |
| 63 | concatBytes([pktLine(utf8Encode(`# service=${service}\n`)), flushPkt]); |
| 64 | |
| 65 | /** One advertised `<oid> <name>` record (already flattened, incl. `^{}`). */ |
| 66 | interface AdvertisedRecord { |
| 67 | readonly oid: string; |
| 68 | readonly name: string; |
| 69 | } |
| 70 | |
| 71 | const flattenRefs = (refs: ReadonlyArray<RefRecord>): Array<AdvertisedRecord> => { |
| 72 | const sorted = [...refs].sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0)); |
| 73 | const records: Array<AdvertisedRecord> = []; |
| 74 | for (const ref of sorted) { |
| 75 | records.push({ oid: ref.oid, name: ref.name }); |
| 76 | if (ref.peeled !== undefined) { |
| 77 | records.push({ oid: ref.peeled, name: `${ref.name}^{}` }); |
| 78 | } |
| 79 | } |
| 80 | return records; |
| 81 | }; |
| 82 | |
| 83 | /** |
| 84 | * Encodes the ref list section: first record carries the capability list |
| 85 | * after a NUL; an empty list becomes the zero-id `capabilities^{}` line; |
| 86 | * terminated by a flush. |
| 87 | */ |
| 88 | const refListSection = ( |
| 89 | records: ReadonlyArray<AdvertisedRecord>, |
| 90 | capabilities: string, |
| 91 | ): Uint8Array => { |
| 92 | const lines: Array<Uint8Array> = []; |
| 93 | if (records.length === 0) { |
| 94 | lines.push(pktLine(utf8Encode(`${ZERO_OID} capabilities^{}\0${capabilities}\n`))); |
| 95 | } else { |
| 96 | const first = records[0]!; |
| 97 | lines.push(pktLine(utf8Encode(`${first.oid} ${first.name}\0${capabilities}\n`))); |
| 98 | for (let i = 1; i < records.length; i++) { |
| 99 | const record = records[i]!; |
| 100 | lines.push(pktLine(utf8Encode(`${record.oid} ${record.name}\n`))); |
| 101 | } |
| 102 | } |
| 103 | lines.push(flushPkt); |
| 104 | return concatBytes(lines); |
| 105 | }; |
| 106 | |
| 107 | /** |
| 108 | * Builds the complete `info/refs?service=git-upload-pack` advertisement body: |
| 109 | * prelude, then `HEAD` first (its oid = the default branch tip, when that |
| 110 | * branch exists) with the capability line, then refs sorted by name with |
| 111 | * peeled `^{}` lines for annotated tags. An empty repo advertises the |
| 112 | * zero-id `capabilities^{}` form. |
| 113 | */ |
| 114 | export const uploadPackAdvertisement = (snapshot: RefsSnapshot): Uint8Array => { |
| 115 | const records = flattenRefs(snapshot.refs); |
| 116 | const head = snapshot.refs.find((ref) => ref.name === `refs/heads/${snapshot.defaultBranch}`); |
| 117 | const withHead = head === undefined ? records : [{ oid: head.oid, name: "HEAD" }, ...records]; |
| 118 | return concatBytes([ |
| 119 | servicePrelude("git-upload-pack"), |
| 120 | refListSection(withHead, uploadPackCapabilities(snapshot.defaultBranch)), |
| 121 | ]); |
| 122 | }; |
| 123 | |
| 124 | /** |
| 125 | * Builds the complete `info/refs?service=git-receive-pack` advertisement |
| 126 | * body. `HEAD` is not advertised (it is not pushable); refs are sorted with |
| 127 | * peeled lines; an empty repo advertises the zero-id `capabilities^{}` form |
| 128 | * (which is how the first-ever push learns the capability set). |
| 129 | */ |
| 130 | export const receivePackAdvertisement = (snapshot: RefsSnapshot): Uint8Array => |
| 131 | concatBytes([ |
| 132 | servicePrelude("git-receive-pack"), |
| 133 | refListSection(flattenRefs(snapshot.refs), receivePackCapabilities()), |
| 134 | ]); |
| 135 | |
| 136 | /** |
| 137 | * Builds the advertisement for either service. |
| 138 | */ |
| 139 | export const advertisement = (service: GitService, snapshot: RefsSnapshot): Uint8Array => |
| 140 | service === "git-upload-pack" |
| 141 | ? uploadPackAdvertisement(snapshot) |
| 142 | : receivePackAdvertisement(snapshot); |