Skip to content

Commit a49a6ec

Browse files
pauldambraclaude
andcommitted
refactor(worktree): simplify deleteWorktree failure handling and unify parent-dir cleanup
/simplify pass: - Replace the "prune-failed" string sentinel with null — both the rename-couldn't-happen and prune-failed cases fall through to the same in-place remove, so the sentinel was only there to be discarded. - Remove the worktree's now-empty <base>/<name> parent directory in both delete branches instead of only the trash-success path, so a deleted worktree never leaves a stray parent behind regardless of which branch ran. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 52051ae commit a49a6ec

1 file changed

Lines changed: 17 additions & 12 deletions

File tree

packages/git/src/worktree.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -574,22 +574,27 @@ export class WorktreeManager {
574574
throw pruneError;
575575
}
576576
})
577-
.catch(() => "prune-failed" as const);
577+
// A null (rename couldn't happen) or a throw (prune failed, rename rolled
578+
// back) both fall through to the in-place remove below.
579+
.catch(() => null);
578580

579-
if (trashedPath && trashedPath !== "prune-failed") {
580-
await fs.rmdir(path.dirname(resolvedWorktreePath)).catch(() => {});
581+
if (trashedPath) {
581582
void forceRemove(trashedPath).catch(() => {});
582-
return;
583+
} else {
584+
await manager.executeWrite(this.mainRepoPath, async (git) => {
585+
try {
586+
await git.raw(["worktree", "remove", worktreePath, "--force"]);
587+
} catch {
588+
await forceRemove(worktreePath);
589+
await git.raw(["worktree", "prune"]);
590+
}
591+
});
583592
}
584593

585-
await manager.executeWrite(this.mainRepoPath, async (git) => {
586-
try {
587-
await git.raw(["worktree", "remove", worktreePath, "--force"]);
588-
} catch {
589-
await forceRemove(worktreePath);
590-
await git.raw(["worktree", "prune"]);
591-
}
592-
});
594+
// Both branches leave the worktree's `<base>/<name>` parent empty; remove it
595+
// so a deleted worktree never leaves a stray directory behind (best-effort:
596+
// rmdir no-ops if it isn't empty).
597+
await fs.rmdir(path.dirname(resolvedWorktreePath)).catch(() => {});
593598
}
594599

595600
private getTrashFolderPath(): string {

0 commit comments

Comments
 (0)