import { SimpleGit } from 'simple-git' import { rejectFlagLike } from './forgeArgGuards' /** * Thin wrappers around `git bisect ` for the TUI's in-bisect * action keys (#784). Each returns the raw stdout so the surface can * surface git's own "Bisecting: N revisions left to test after this * (roughly K steps)" hint as a status message — that wording is the * single most useful piece of feedback git emits during bisect, and * mirroring it keeps the TUI's status line authoritative. * * No try/catch here — git itself returns non-zero on user errors * (already-bisecting, no good ref, etc.) and `simple-git` surfaces * those as rejections. The runtime catches them and routes to the * status line. */ export async function bisectStart( git: SimpleGit, badRef: string, goodRef: string ): Promise { const badErr = rejectFlagLike(badRef, `Bad ref '${badRef}'`) if (badErr) throw new Error(badErr) const goodErr = rejectFlagLike(goodRef, `Good ref '${goodRef}'`) if (goodErr) throw new Error(goodErr) return git.raw(['bisect', 'start', badRef, goodRef]) } export async function bisectGood(git: SimpleGit, ref?: string): Promise { const args = ['bisect', 'good'] if (ref) { const err = rejectFlagLike(ref, `Good ref '${ref}'`) if (err) throw new Error(err) args.push(ref) } return git.raw(args) } export async function bisectBad(git: SimpleGit, ref?: string): Promise { const args = ['bisect', 'bad'] if (ref) { const err = rejectFlagLike(ref, `Bad ref '${ref}'`) if (err) throw new Error(err) args.push(ref) } return git.raw(args) } export async function bisectSkip(git: SimpleGit, ref?: string): Promise { const args = ['bisect', 'skip'] if (ref) { const err = rejectFlagLike(ref, `Skip ref '${ref}'`) if (err) throw new Error(err) args.push(ref) } return git.raw(args) } export async function bisectReset(git: SimpleGit): Promise { return git.raw(['bisect', 'reset']) } /** * Drive an automated bisect run via `git bisect run sh -c ''` * (#879 item 5). Git handles the per-candidate loop itself, running * the command for each commit and interpreting the exit code: * * 0 → mark good * 125 → skip (untestable, e.g. doesn't build) * 1..127 → mark bad (any non-zero except 125) * 128+ → abort (SIGSEGV, killed, etc.) * * The `sh -c` form lets the user pass shell snippets (pipes, env * vars, flag-laden test invocations) without escaping into git's * argv. Runs synchronously from the runtime's perspective — git * blocks until the run terminates and we surface the final result * (typically " is the first bad commit") via the status line. */ export async function bisectRun(git: SimpleGit, command: string): Promise { return git.raw(['bisect', 'run', 'sh', '-c', command]) } /** * Pull the user-facing remaining-revisions hint out of `git bisect` * stdout. Looks for the canonical line: * * `Bisecting: N revisions left to test after this (roughly K steps)` * * Returns undefined when the line isn't present (e.g. the run * finished and git emitted a " is the first bad commit" line * instead). Callers fall back to an empty status update in that case. */ export function extractBisectRemainingHint(stdout: string): string | undefined { for (const line of stdout.split('\n').reverse()) { const trimmed = line.trim() if (trimmed.startsWith('Bisecting:')) return trimmed if (trimmed.includes('is the first bad commit')) return trimmed } return undefined }