From 814bbb39a01444778343bc2ef5551a285d1d1dbc Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Fri, 20 Feb 2026 07:41:24 -0800 Subject: [PATCH] Make worktree removal idempotent by pruning and cleaning up stale directories instead of throwing on failure --- src/git.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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 }) + } } }