diff --git a/src/git.ts b/src/git.ts index 8190e0d..e8f6a7c 100644 --- a/src/git.ts +++ b/src/git.ts @@ -60,11 +60,15 @@ export async function createWorktree(branch: string, worktreePath: string, cwd: } } -/** Remove a worktree. */ +/** Remove a worktree. Silently succeeds if the worktree is already gone. */ export async function removeWorktree(worktreePath: string, cwd: string): Promise { 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()}`) + // Worktree may already be gone or stale — prune and clean up the directory + await $`git worktree prune`.cwd(cwd).nothrow().quiet() + if (existsSync(worktreePath)) { + await rm(worktreePath, { recursive: true }) + } } }