// THE CLIENT HALF OF SMART HTTP (gitprotocol-http, protocol v0): what `git fetch` and `git push` send to a server, // over a transport the caller supplies. The library opens no socket, holds no credential and names no host; a root's // refresh and deploy reach the vendor's git through it over the kernel executor (derived-real.ts, ADR 0031). It asks // only for what it reads: no side-band (the pack arrives bare), no multi_ack (one answer), no thin pack (every delta // base is in the pack or the store), ofs-delta (readPack resolves both delta kinds), and on a push report-status, with // atomic when the server offers it. import { concat, type GitObjectStore } from './objects.ts'; import { readPack } from './pack.ts'; import { FLUSH, ZERO_SHA, packObjects, pktLine, readPktLines, type Service } from './smart-http.ts'; const DEC = new TextDecoder(); const SHA = /^[0-9a-f]{40}$/; /** One exchange: `GET info/refs?service=` when there is no body, `POST ` when there is. It answers * the response's bytes, and throws when the server did not answer 200. */ export type SmartHttpTransport = (request: { service: Service; body?: Uint8Array }) => Promise; /** What a server advertises: its refs (peeled `^{}` lines dropped), the ref its HEAD names, and its capabilities. */ export type RemoteRefs = { refs: Map; head: string | null; caps: string[] }; /** `GET info/refs?service=`, read. */ export async function lsRemote(transport: SmartHttpTransport, service: Service): Promise { let { lines, rest } = readPktLines(await transport({ service })); if (lines[0]?.startsWith('# service=')) ({ lines, rest } = readPktLines(rest)); const refs = new Map(); let head: string | null = null; let caps: string[] = []; for (const raw of lines) { const [line, offered] = raw.replace(/\n$/, '').split('\0') as [string, string | undefined]; if (line.startsWith('ERR ')) throw new Error(`git ${service}: ${line.slice(4)}`); if (offered !== undefined) { caps = offered.split(' ').filter(Boolean); const symref = caps.find((c) => c.startsWith('symref=HEAD:')); if (symref) head = symref.slice('symref=HEAD:'.length); } const sha = line.slice(0, 40); const name = line.slice(41); if (!SHA.test(sha) || sha === ZERO_SHA || name === 'HEAD' || name.endsWith('^{}')) continue; refs.set(name, sha); } return { refs, head, caps }; } const isPack = (bytes: Uint8Array, at: number): boolean => DEC.decode(bytes.subarray(at, at + 4)) === 'PACK'; /** `POST git-upload-pack` with `done`: every object `wants` reach and `haves` do not, read into `store`. */ export async function fetchPack(transport: SmartHttpTransport, store: GitObjectStore, wants: string[], haves: string[]): Promise { if (wants.length === 0) return; const answer = await transport({ service: 'git-upload-pack', body: concat( pktLine(`want ${wants[0]} ofs-delta no-progress agent=volter-twin\n`), ...wants.slice(1).map((w) => pktLine(`want ${w}\n`)), FLUSH, ...haves.map((h) => pktLine(`have ${h}\n`)), pktLine('done\n'), ), }); // the negotiation's ACK / NAK lines, then the pack let at = 0; while (at + 4 <= answer.length && !isPack(answer, at)) { const len = parseInt(DEC.decode(answer.subarray(at, at + 4)), 16); if (Number.isNaN(len)) break; if (len < 4) { at += 4; continue; } const line = DEC.decode(answer.subarray(at + 4, at + len)); if (line.startsWith('ERR ')) throw new Error(`git upload-pack: ${line.slice(4).trim()}`); at += len; } if (!isPack(answer, at)) throw new Error('git upload-pack: the server answered no pack'); await readPack(answer.subarray(at), store); } /** One ref update a push asks for: `old` is the server's value as advertised (ZERO_SHA to create), `new` the object * to point it at (ZERO_SHA to delete). */ export type PushCommand = { name: string; old: string; new: string }; /** `POST git-receive-pack`: the commands and the pack of what they need beyond `haves`, atomic when `atomic` (the * server offered it). Answers each command's outcome by ref name: null when the server took it, else its reason. */ export async function sendPack(transport: SmartHttpTransport, store: GitObjectStore, commands: PushCommand[], haves: string[], atomic = false): Promise> { const updates = commands.filter((c) => c.new !== ZERO_SHA).map((c) => c.new); const caps = `report-status${atomic ? ' atomic' : ''} agent=volter-twin`; const answer = await transport({ service: 'git-receive-pack', body: concat( ...commands.map((c, i) => pktLine(`${c.old} ${c.new} ${c.name}${i === 0 ? `\0${caps}` : ''}\n`)), FLUSH, // a push of deletions alone sends no pack (gitprotocol-pack, "Pushing data to a server") ...(updates.length ? [await packObjects(store, updates, haves)] : []), ), }); const report = readPktLines(answer).lines.map((l) => l.replace(/\n$/, '')); const failure = report.find((l) => l.startsWith('ERR ')); if (failure) throw new Error(`git receive-pack: ${failure.slice(4)}`); const unpack = report.find((l) => l.startsWith('unpack '))?.slice('unpack '.length); if (unpack === undefined) throw new Error('git receive-pack: the server sent no report'); const out = new Map(commands.map((c) => [c.name, unpack === 'ok' ? 'the server reported nothing for it' : `unpacker error: ${unpack}`])); for (const l of report) { if (l.startsWith('ok ')) out.set(l.slice(3), null); else if (l.startsWith('ng ')) { const rest = l.slice(3); const sp = rest.indexOf(' '); out.set(sp === -1 ? rest : rest.slice(0, sp), sp === -1 ? 'refused' : rest.slice(sp + 1)); } } return out; }