|
| 1 | +import { HunkUserError } from "./errors"; |
| 2 | +import type { GitCommandInput, ShowCommandInput, StashShowCommandInput } from "./types"; |
| 3 | + |
| 4 | +export type GitBackedInput = GitCommandInput | ShowCommandInput | StashShowCommandInput; |
| 5 | + |
| 6 | +export interface RunGitTextOptions { |
| 7 | + input: GitBackedInput; |
| 8 | + args: string[]; |
| 9 | + cwd?: string; |
| 10 | + gitExecutable?: string; |
| 11 | +} |
| 12 | + |
| 13 | +export function formatGitCommandLabel(input: GitBackedInput) { |
| 14 | + switch (input.kind) { |
| 15 | + case "git": |
| 16 | + if (input.staged) { |
| 17 | + return "hunk diff --staged"; |
| 18 | + } |
| 19 | + |
| 20 | + return input.range ? `hunk diff ${input.range}` : "hunk diff"; |
| 21 | + case "show": |
| 22 | + return input.ref ? `hunk show ${input.ref}` : "hunk show"; |
| 23 | + case "stash-show": |
| 24 | + return input.ref ? `hunk stash show ${input.ref}` : "hunk stash show"; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +function getMissingRepoHelp(input: GitBackedInput) { |
| 29 | + if (input.kind === "git") { |
| 30 | + return [ |
| 31 | + "Run the command from a Git checkout, or compare files directly instead:", |
| 32 | + " hunk diff <before-file> <after-file>", |
| 33 | + " hunk patch <file.patch>", |
| 34 | + ]; |
| 35 | + } |
| 36 | + |
| 37 | + return ["Run the command from a Git checkout."]; |
| 38 | +} |
| 39 | + |
| 40 | +function trimGitPrefix(message: string) { |
| 41 | + return message.replace(/^(fatal|error):\s*/i, "").trim(); |
| 42 | +} |
| 43 | + |
| 44 | +function firstGitErrorLine(stderr: string) { |
| 45 | + const line = stderr |
| 46 | + .split("\n") |
| 47 | + .map((entry) => entry.trim()) |
| 48 | + .find(Boolean); |
| 49 | + |
| 50 | + return trimGitPrefix((line ?? stderr.trim()) || "Git command failed."); |
| 51 | +} |
| 52 | + |
| 53 | +function isMissingGitRepoMessage(stderr: string) { |
| 54 | + return stderr.includes("not a git repository"); |
| 55 | +} |
| 56 | + |
| 57 | +function isUnknownRevisionMessage(stderr: string) { |
| 58 | + return [ |
| 59 | + "bad revision", |
| 60 | + "unknown revision or path not in the working tree", |
| 61 | + "ambiguous argument", |
| 62 | + ].some((fragment) => stderr.includes(fragment)); |
| 63 | +} |
| 64 | + |
| 65 | +function isNoStashEntriesMessage(stderr: string) { |
| 66 | + return ["No stash entries found.", "log for 'stash' only has"].some((fragment) => |
| 67 | + stderr.includes(fragment), |
| 68 | + ); |
| 69 | +} |
| 70 | + |
| 71 | +function createMissingGitExecutableError(input: GitBackedInput, gitExecutable: string) { |
| 72 | + return new HunkUserError( |
| 73 | + `Git is required for \`${formatGitCommandLabel(input)}\`, but \`${gitExecutable}\` was not found in PATH.`, |
| 74 | + ["Install Git or make it available on PATH, then try again."], |
| 75 | + ); |
| 76 | +} |
| 77 | + |
| 78 | +function createMissingRepoError(input: GitBackedInput) { |
| 79 | + return new HunkUserError( |
| 80 | + `\`${formatGitCommandLabel(input)}\` must be run inside a Git repository.`, |
| 81 | + getMissingRepoHelp(input), |
| 82 | + ); |
| 83 | +} |
| 84 | + |
| 85 | +function createInvalidRevisionError(input: GitCommandInput | ShowCommandInput) { |
| 86 | + if (input.kind === "git") { |
| 87 | + return new HunkUserError( |
| 88 | + `\`${formatGitCommandLabel(input)}\` could not resolve Git revision or range \`${input.range}\`.`, |
| 89 | + ["Check the revision or range and try again."], |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + const ref = input.ref ?? "HEAD"; |
| 94 | + return new HunkUserError( |
| 95 | + `\`${formatGitCommandLabel(input)}\` could not resolve Git ref \`${ref}\`.`, |
| 96 | + ["Check the ref name and try again."], |
| 97 | + ); |
| 98 | +} |
| 99 | + |
| 100 | +function createMissingStashError(input: StashShowCommandInput) { |
| 101 | + if (input.ref) { |
| 102 | + return new HunkUserError( |
| 103 | + `\`${formatGitCommandLabel(input)}\` could not resolve stash entry \`${input.ref}\`.`, |
| 104 | + ["List available stashes with `git stash list`, then try again."], |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + return new HunkUserError("`hunk stash show` could not find a stash entry to show.", [ |
| 109 | + "Create one with `git stash push`, or pass an explicit stash ref like `hunk stash show stash@{0}`.", |
| 110 | + ]); |
| 111 | +} |
| 112 | + |
| 113 | +function createGenericGitError(input: GitBackedInput, stderr: string) { |
| 114 | + return new HunkUserError(`\`${formatGitCommandLabel(input)}\` failed.`, [ |
| 115 | + firstGitErrorLine(stderr), |
| 116 | + ]); |
| 117 | +} |
| 118 | + |
| 119 | +function translateGitSpawnFailure( |
| 120 | + input: GitBackedInput, |
| 121 | + error: unknown, |
| 122 | + gitExecutable: string, |
| 123 | +): Error { |
| 124 | + if (error instanceof HunkUserError) { |
| 125 | + return error; |
| 126 | + } |
| 127 | + |
| 128 | + if (error instanceof Error && error.message.includes("Executable not found in $PATH")) { |
| 129 | + return createMissingGitExecutableError(input, gitExecutable); |
| 130 | + } |
| 131 | + |
| 132 | + return error instanceof Error ? error : new Error(String(error)); |
| 133 | +} |
| 134 | + |
| 135 | +function translateGitExitFailure(input: GitBackedInput, stderr: string) { |
| 136 | + if (isMissingGitRepoMessage(stderr)) { |
| 137 | + return createMissingRepoError(input); |
| 138 | + } |
| 139 | + |
| 140 | + if (input.kind === "stash-show" && isNoStashEntriesMessage(stderr)) { |
| 141 | + return createMissingStashError(input); |
| 142 | + } |
| 143 | + |
| 144 | + if (input.kind === "git" && input.range && isUnknownRevisionMessage(stderr)) { |
| 145 | + return createInvalidRevisionError(input); |
| 146 | + } |
| 147 | + |
| 148 | + if (input.kind === "show" && isUnknownRevisionMessage(stderr)) { |
| 149 | + return createInvalidRevisionError(input); |
| 150 | + } |
| 151 | + |
| 152 | + if (input.kind === "stash-show" && input.ref && isUnknownRevisionMessage(stderr)) { |
| 153 | + return createMissingStashError(input); |
| 154 | + } |
| 155 | + |
| 156 | + return createGenericGitError(input, stderr); |
| 157 | +} |
| 158 | + |
| 159 | +/** Run a git command and translate common failures into user-facing Hunk errors. */ |
| 160 | +export function runGitText({ |
| 161 | + input, |
| 162 | + args, |
| 163 | + cwd = process.cwd(), |
| 164 | + gitExecutable = "git", |
| 165 | +}: RunGitTextOptions) { |
| 166 | + let proc: ReturnType<typeof Bun.spawnSync>; |
| 167 | + |
| 168 | + try { |
| 169 | + proc = Bun.spawnSync([gitExecutable, ...args], { |
| 170 | + cwd, |
| 171 | + stdin: "ignore", |
| 172 | + stdout: "pipe", |
| 173 | + stderr: "pipe", |
| 174 | + }); |
| 175 | + } catch (error) { |
| 176 | + throw translateGitSpawnFailure(input, error, gitExecutable); |
| 177 | + } |
| 178 | + |
| 179 | + const stdout = Buffer.from(proc.stdout ?? []).toString("utf8"); |
| 180 | + const stderr = Buffer.from(proc.stderr ?? []).toString("utf8"); |
| 181 | + |
| 182 | + if (proc.exitCode !== 0) { |
| 183 | + throw translateGitExitFailure( |
| 184 | + input, |
| 185 | + stderr.trim() || `Command failed: ${gitExecutable} ${args.join(" ")}`, |
| 186 | + ); |
| 187 | + } |
| 188 | + |
| 189 | + return stdout; |
| 190 | +} |
| 191 | + |
| 192 | +export function resolveGitRepoRoot( |
| 193 | + input: GitBackedInput, |
| 194 | + options: Omit<RunGitTextOptions, "input" | "args"> = {}, |
| 195 | +) { |
| 196 | + return runGitText({ |
| 197 | + input, |
| 198 | + args: ["rev-parse", "--show-toplevel"], |
| 199 | + ...options, |
| 200 | + }).trim(); |
| 201 | +} |
0 commit comments