diff --git a/src/cli.ts b/src/cli.ts index 56c14f5..a811bcc 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -149,4 +149,7 @@ vmCmd console.log("VM destroyed") }) -program.parse() +program.parseAsync().catch((err) => { + console.error(err.message ?? err) + process.exit(1) +}) diff --git a/src/git.ts b/src/git.ts index 102bd73..48b1634 100644 --- a/src/git.ts +++ b/src/git.ts @@ -4,14 +4,20 @@ import { $ } from "bun" /** Get the repo root from a working directory. */ export async function repoRoot(cwd?: string): Promise { - const result = await $`git rev-parse --show-toplevel`.cwd(cwd ?? ".").text() - return result.trim() + const result = await $`git rev-parse --show-toplevel`.cwd(cwd ?? ".").nothrow().quiet() + if (result.exitCode !== 0) { + throw new Error("Not a git repository. Run this command from inside a git repo.") + } + return result.text().trim() } /** Get the current branch name. */ export async function currentBranch(cwd?: string): Promise { - const result = await $`git rev-parse --abbrev-ref HEAD`.cwd(cwd ?? ".").text() - return result.trim() + const result = await $`git rev-parse --abbrev-ref HEAD`.cwd(cwd ?? ".").nothrow().quiet() + if (result.exitCode !== 0) { + throw new Error("Could not determine current branch.") + } + return result.text().trim() } /** Check if a branch exists locally or remotely. Returns "local", "remote", or null. */ @@ -40,19 +46,26 @@ export async function createWorktree(branch: string, worktreePath: string, cwd: const exists = await branchExists(branch, cwd) + let result if (exists === "local") { - await $`git worktree add ${worktreePath} ${branch}`.cwd(cwd).quiet() + result = await $`git worktree add ${worktreePath} ${branch}`.cwd(cwd).nothrow().quiet() } else if (exists === "remote") { - await $`git worktree add ${worktreePath} -b ${branch} origin/${branch}`.cwd(cwd).quiet() + result = await $`git worktree add ${worktreePath} -b ${branch} origin/${branch}`.cwd(cwd).nothrow().quiet() } else { // New branch from current HEAD - await $`git worktree add -b ${branch} ${worktreePath}`.cwd(cwd).quiet() + result = await $`git worktree add -b ${branch} ${worktreePath}`.cwd(cwd).nothrow().quiet() + } + if (result.exitCode !== 0) { + throw new Error(`Failed to create worktree for "${branch}": ${result.stderr.toString().trim()}`) } } /** Remove a worktree. */ export async function removeWorktree(worktreePath: string, cwd: string): Promise { - await $`git worktree remove ${worktreePath} --force`.cwd(cwd) + const result = await $`git worktree remove ${worktreePath} --force`.cwd(cwd).nothrow().quiet() + if (result.exitCode !== 0) { + throw new Error(`Failed to remove worktree: ${result.stderr.toString().trim()}`) + } } /** Delete a local branch. */ @@ -62,5 +75,8 @@ export async function deleteLocalBranch(branch: string, cwd: string): Promise { - await $`git checkout ${branch}`.cwd(cwd) + const result = await $`git checkout ${branch}`.cwd(cwd).nothrow().quiet() + if (result.exitCode !== 0) { + throw new Error(`Failed to checkout branch "${branch}": ${result.stderr.toString().trim()}`) + } }