Skip to content
TypeScript

fix(web): deleting a thread no longer offers to remove a shared worktree (#24489)

Victor Vogelf5bc64b7b7bc
diff --git a/apps/web/src/worktreeCleanup.test.ts b/apps/web/src/worktreeCleanup.test.ts
index 2f831dfac91..76256b38336 100644
--- a/apps/web/src/worktreeCleanup.test.ts
+++ b/apps/web/src/worktreeCleanup.test.ts
@@ -51,6 +51,29 @@ describe("getOrphanedWorktreePathForThread", () => {
expect(result).toBeNull();
});
+ it.each([
+ ["/tmp/repo/worktrees/feature-a", "/tmp/repo/worktrees/feature-a/"],
+ ["/tmp/repo/worktrees/feature-a", "/tmp/repo/worktrees/feature-a//"],
+ ["C:\\repo\\worktrees\\feature-a", "c:/repo/worktrees/feature-a/"],
+ ])("treats %s and %s as the same worktree", (targetPath, otherPath) => {
+ const threads = [
+ makeThread({ id: ThreadId.make("thread-1"), worktreePath: targetPath }),
+ makeThread({ id: ThreadId.make("thread-2"), worktreePath: otherPath }),
+ ];
+ expect(getOrphanedWorktreePathForThread(threads, ThreadId.make("thread-1"))).toBeNull();
+ expect(getOrphanedWorktreePathForThread(threads, ThreadId.make("thread-2"))).toBeNull();
+ });
+
+ it("keeps case-sensitive POSIX worktrees apart", () => {
+ const threads = [
+ makeThread({ id: ThreadId.make("thread-1"), worktreePath: "/tmp/repo/worktrees/Feature" }),
+ makeThread({ id: ThreadId.make("thread-2"), worktreePath: "/tmp/repo/worktrees/feature" }),
+ ];
+ expect(getOrphanedWorktreePathForThread(threads, ThreadId.make("thread-1"))).toBe(
+ "/tmp/repo/worktrees/Feature",
+ );
+ });
+
it("ignores threads linked to different worktrees", () => {
const threads = [
makeThread({
diff --git a/apps/web/src/worktreeCleanup.ts b/apps/web/src/worktreeCleanup.ts
index 109f71ccd9a..1c2a4bb12e8 100644
--- a/apps/web/src/worktreeCleanup.ts
+++ b/apps/web/src/worktreeCleanup.ts
@@ -1,12 +1,6 @@
-import type { ThreadShell } from "./types";
+import { normalizeProjectPathForComparison } from "@t3tools/shared/path";
-function normalizeWorktreePath(path: string | null): string | null {
- const trimmed = path?.trim();
- if (!trimmed) {
- return null;
- }
- return trimmed;
-}
+import type { ThreadShell } from "./types";
export function getOrphanedWorktreePathForThread(
threads: ReadonlyArray<Pick<ThreadShell, "id" | "worktreePath">>,
@@ -17,17 +11,20 @@ export function getOrphanedWorktreePathForThread(
return null;
}
- const targetWorktreePath = normalizeWorktreePath(targetThread.worktreePath);
+ const targetWorktreePath = targetThread.worktreePath?.trim();
if (!targetWorktreePath) {
return null;
}
- const isShared = threads.some((thread) => {
- if (thread.id === threadId) {
- return false;
- }
- return normalizeWorktreePath(thread.worktreePath) === targetWorktreePath;
- });
+ // Threads can store one worktree as `/repo/wt` and `/repo/wt/`, and
+ // confirming deletion force-removes the checkout for every thread on it.
+ const targetKey = normalizeProjectPathForComparison(targetWorktreePath);
+ const isShared = threads.some(
+ (thread) =>
+ thread.id !== threadId &&
+ thread.worktreePath !== null &&
+ normalizeProjectPathForComparison(thread.worktreePath) === targetKey,
+ );
return isShared ? null : targetWorktreePath;
}