error handling
This commit is contained in:
parent
606b440891
commit
4cb3b10047
|
|
@ -149,4 +149,7 @@ vmCmd
|
||||||
console.log("VM destroyed")
|
console.log("VM destroyed")
|
||||||
})
|
})
|
||||||
|
|
||||||
program.parse()
|
program.parseAsync().catch((err) => {
|
||||||
|
console.error(err.message ?? err)
|
||||||
|
process.exit(1)
|
||||||
|
})
|
||||||
|
|
|
||||||
34
src/git.ts
34
src/git.ts
|
|
@ -4,14 +4,20 @@ import { $ } from "bun"
|
||||||
|
|
||||||
/** Get the repo root from a working directory. */
|
/** Get the repo root from a working directory. */
|
||||||
export async function repoRoot(cwd?: string): Promise<string> {
|
export async function repoRoot(cwd?: string): Promise<string> {
|
||||||
const result = await $`git rev-parse --show-toplevel`.cwd(cwd ?? ".").text()
|
const result = await $`git rev-parse --show-toplevel`.cwd(cwd ?? ".").nothrow().quiet()
|
||||||
return result.trim()
|
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. */
|
/** Get the current branch name. */
|
||||||
export async function currentBranch(cwd?: string): Promise<string> {
|
export async function currentBranch(cwd?: string): Promise<string> {
|
||||||
const result = await $`git rev-parse --abbrev-ref HEAD`.cwd(cwd ?? ".").text()
|
const result = await $`git rev-parse --abbrev-ref HEAD`.cwd(cwd ?? ".").nothrow().quiet()
|
||||||
return result.trim()
|
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. */
|
/** 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)
|
const exists = await branchExists(branch, cwd)
|
||||||
|
|
||||||
|
let result
|
||||||
if (exists === "local") {
|
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") {
|
} 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 {
|
} else {
|
||||||
// New branch from current HEAD
|
// 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. */
|
/** Remove a worktree. */
|
||||||
export async function removeWorktree(worktreePath: string, cwd: string): Promise<void> {
|
export async function removeWorktree(worktreePath: string, cwd: string): Promise<void> {
|
||||||
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. */
|
/** Delete a local branch. */
|
||||||
|
|
@ -62,5 +75,8 @@ export async function deleteLocalBranch(branch: string, cwd: string): Promise<vo
|
||||||
|
|
||||||
/** Checkout a branch. */
|
/** Checkout a branch. */
|
||||||
export async function checkout(branch: string, cwd: string): Promise<void> {
|
export async function checkout(branch: string, cwd: string): Promise<void> {
|
||||||
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()}`)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user