(bytes: Uint8Array)
| 67 | |
| 68 | // Parse the response of GET /info/refs?service=git-upload-pack (protocol v1). |
| 69 | export function parseInfoRefs(bytes: Uint8Array): RefAdvertisement { |
| 70 | const { tokens } = parsePktLines(bytes, { requireComplete: true }); |
| 71 | const refs = new Map<string, string>(); |
| 72 | let capabilities: string[] = []; |
| 73 | let headTarget: string | undefined; |
| 74 | let sawService = false; |
| 75 | let first = true; |
| 76 | |
| 77 | for (const t of tokens) { |
| 78 | if (t.kind !== "line" || !t.data) continue; |
| 79 | let line = td.decode(t.data); |
| 80 | if (line.endsWith("\n")) line = line.slice(0, -1); |
| 81 | if (line.startsWith("# service=")) { |
| 82 | sawService = true; |
| 83 | continue; |
| 84 | } |
| 85 | // first ref line carries \0-separated capabilities |
| 86 | if (first) { |
| 87 | const nul = line.indexOf("\0"); |
| 88 | if (nul >= 0) { |
| 89 | capabilities = line |
| 90 | .slice(nul + 1) |
| 91 | .split(" ") |
| 92 | .filter(Boolean); |
| 93 | line = line.slice(0, nul); |
| 94 | } |
| 95 | first = false; |
| 96 | } |
| 97 | const sp = line.indexOf(" "); |
| 98 | if (sp < 0) continue; |
| 99 | const sha = line.slice(0, sp); |
| 100 | const name = line.slice(sp + 1); |
| 101 | if (/^[0-9a-f]{40,64}$/.test(sha)) refs.set(name, sha); |
| 102 | } |
| 103 | |
| 104 | // symref HEAD from capabilities: symref=HEAD:refs/heads/main |
| 105 | for (const cap of capabilities) { |
| 106 | if (cap.startsWith("symref=HEAD:")) headTarget = cap.slice("symref=HEAD:".length); |
| 107 | } |
| 108 | |
| 109 | return { refs, headTarget, capabilities, protocolVersion: sawService ? 1 : 1 }; |
| 110 | } |
| 111 | |
| 112 | // Resolve which sha a caller wants given a ref advertisement + optional ref name. |
| 113 | export function resolveWant( |
no test coverage detected