|
| 1 | +// Edit index.mts; rebuild index.mjs with Hub's extension:modules:build command. |
| 2 | +import { runCommand } from "../run-command/index.mjs" |
| 3 | + |
| 4 | +type BranchIdentity = { |
| 5 | + repository: string | null |
| 6 | + branch: string | null |
| 7 | + commitSha: string | null |
| 8 | +} |
| 9 | + |
| 10 | +type RemoteBranchResult = BranchIdentity & |
| 11 | + ( |
| 12 | + | { state: "detached" | "unknown"; error: string } |
| 13 | + | { state: "present"; remoteSha: string } |
| 14 | + | { state: "absent"; remoteSha: null } |
| 15 | + ) |
| 16 | + |
| 17 | +type BranchRef = { ref: string; object: { type: "commit"; sha: string } } |
| 18 | + |
| 19 | +// Matching refs returns [] for a missing branch; authorization/HTTP errors stay unknown. |
| 20 | +export async function readRemoteBranch( |
| 21 | + { repository, branch, commitSha, repositoryPath }: BranchIdentity & { repositoryPath: string }, |
| 22 | + run: typeof runCommand = runCommand, |
| 23 | +): Promise<RemoteBranchResult> { |
| 24 | + const identity = { repository, branch, commitSha } |
| 25 | + if (!branch) |
| 26 | + return { |
| 27 | + ...identity, |
| 28 | + state: "detached", |
| 29 | + error: "Open a named branch before publishing.", |
| 30 | + } |
| 31 | + if (!repository || !commitSha) |
| 32 | + return { |
| 33 | + ...identity, |
| 34 | + state: "unknown", |
| 35 | + error: "The GitHub origin or checkout commit could not be verified.", |
| 36 | + } |
| 37 | + const result = await run( |
| 38 | + "gh", |
| 39 | + [ |
| 40 | + "api", |
| 41 | + "--hostname", |
| 42 | + "github.com", |
| 43 | + `repos/${repository}/git/matching-refs/heads/${encodeURIComponent(branch)}`, |
| 44 | + ], |
| 45 | + repositoryPath, |
| 46 | + ) |
| 47 | + if (!result.ok) return { ...identity, state: "unknown", error: result.error } |
| 48 | + let refs: unknown |
| 49 | + try { |
| 50 | + refs = JSON.parse(result.stdout) |
| 51 | + } catch { |
| 52 | + return { |
| 53 | + ...identity, |
| 54 | + state: "unknown", |
| 55 | + error: "GitHub returned invalid branch data.", |
| 56 | + } |
| 57 | + } |
| 58 | + if ( |
| 59 | + !Array.isArray(refs) || |
| 60 | + !refs.every( |
| 61 | + (ref: unknown): ref is BranchRef => |
| 62 | + typeof ref === "object" && |
| 63 | + ref !== null && |
| 64 | + "ref" in ref && |
| 65 | + typeof ref.ref === "string" && |
| 66 | + /^refs\/heads\//.test(ref.ref) && |
| 67 | + "object" in ref && |
| 68 | + typeof ref.object === "object" && |
| 69 | + ref.object !== null && |
| 70 | + "type" in ref.object && |
| 71 | + ref.object.type === "commit" && |
| 72 | + "sha" in ref.object && |
| 73 | + typeof ref.object.sha === "string" && |
| 74 | + /^[0-9a-f]{40}$/.test(ref.object.sha), |
| 75 | + ) |
| 76 | + ) |
| 77 | + return { |
| 78 | + ...identity, |
| 79 | + state: "unknown", |
| 80 | + error: "GitHub returned invalid branch refs.", |
| 81 | + } |
| 82 | + const exact = refs.find((ref) => ref.ref === `refs/heads/${branch}`) |
| 83 | + return exact |
| 84 | + ? { ...identity, state: "present", remoteSha: exact.object.sha } |
| 85 | + : { ...identity, state: "absent", remoteSha: null } |
| 86 | +} |
0 commit comments