From 8dca6b70fcc08785d69c642ad6b18f0f24538012 Mon Sep 17 00:00:00 2001 From: Donald Silveira Date: Wed, 30 Sep 2026 14:31:26 -0300 Subject: [PATCH 1/2] feat(web): delete multiple worktrees at once - Add worktree selection and bulk deletion with per-worktree failure reporting - Document multi-select deletion in project settings --- .../components/settings/WorktreeManager.tsx | 236 +++++++++++++----- docs/user/project-settings.md | 5 +- 2 files changed, 174 insertions(+), 67 deletions(-) diff --git a/apps/web/src/components/settings/WorktreeManager.tsx b/apps/web/src/components/settings/WorktreeManager.tsx index 6acc46e31395a..3a315e9ad8e37 100644 --- a/apps/web/src/components/settings/WorktreeManager.tsx +++ b/apps/web/src/components/settings/WorktreeManager.tsx @@ -16,6 +16,7 @@ import { useEnvironmentQuery } from "../../state/query"; import { useAtomCommand } from "../../state/use-atom-command"; import { vcsEnvironment } from "../../state/vcs"; import { Button } from "../ui/button"; +import { Checkbox } from "../ui/checkbox"; import { Empty, EmptyDescription, EmptyHeader, EmptyMedia, EmptyTitle } from "../ui/empty"; import { Dialog, @@ -182,12 +183,17 @@ function CreateWorktreeDialog({ ); } +function worktreeName(tree: VcsListedWorktree) { + return tree.path.split(/[\\/]/).at(-1) ?? tree.path; +} + export function WorktreeManager() { const { scope, connectedEnvironments } = useSettingsScope(); const [selectedMemberKey, setSelectedMemberKey] = useState(null); const [creating, setCreating] = useState(false); - const [deleting, setDeleting] = useState(null); - // Keep the target through the close animation. + const [selectedPaths, setSelectedPaths] = useState>(() => new Set()); + const [deleting, setDeleting] = useState | null>(null); + // Keep the targets through the close animation. const [shownDeleting, setShownDeleting] = useState(deleting); if (deleting !== null && deleting !== shownDeleting) setShownDeleting(deleting); const [busy, setBusy] = useState(false); @@ -218,29 +224,84 @@ export function WorktreeManager() { ? worktreeThreads(threads, projects, member.environmentId) : null; const associatedThreads = (tree: VcsListedWorktree) => threadsByWorktree?.get(tree.path) ?? []; - const deletingThreads = shownDeleting - ? (worktreeThreads(threads, projects, shownDeleting.environmentId).get( - shownDeleting.worktree.path, - ) ?? []) + const rows = member + ? worktrees.map((tree) => { + const usedBy = associatedThreads(tree); + const target = { + environmentId: member.environmentId, + cwd: member.workspaceRoot, + worktree: tree, + }; + return { + tree, + target, + usedBy, + blocked: worktreeDeletionBlockReason(target, member, usedBy), + }; + }) : []; - const deletionBlocked = deleting - ? worktreeDeletionBlockReason(deleting, member, deletingThreads) + const selectedTargets = rows + .filter((row) => !row.blocked && selectedPaths.has(row.tree.path)) + .map((row) => row.target); + const deletingThreadsByWorktree = shownDeleting?.[0] + ? worktreeThreads(threads, projects, shownDeleting[0].environmentId) : null; - const deleteWorktree = async () => { + const deletingThreadCount = + shownDeleting?.reduce( + (count, target) => + count + (deletingThreadsByWorktree?.get(target.worktree.path)?.length ?? 0), + 0, + ) ?? 0; + const deletionBlocked = (() => { + if (!deleting) return null; + for (const target of deleting) { + const reason = worktreeDeletionBlockReason( + target, + member, + deletingThreadsByWorktree?.get(target.worktree.path) ?? [], + ); + if (reason) + return deleting.length > 1 ? `${worktreeName(target.worktree)}: ${reason}` : reason; + } + return null; + })(); + const deleteWorktrees = async () => { if (!deleting || deletionBlocked || busy) return; setBusy(true); setError(null); - const result = await removeWorktree({ - environmentId: deleting.environmentId, - input: { cwd: deleting.cwd, path: deleting.worktree.path, force: true }, - }); + const failed: Array<{ target: WorktreeDeletionTarget; message: string }> = []; + const removed = new Set(); + // One at a time: each removal rewrites the repository's shared worktree metadata. + for (const target of deleting) { + const result = await removeWorktree({ + environmentId: target.environmentId, + input: { cwd: target.cwd, path: target.worktree.path, force: true }, + }); + if (result._tag === "Success") removed.add(target.worktree.path); + else failed.push({ target, message: commandError(result) }); + } setBusy(false); - if (result._tag === "Success") { + setSelectedPaths((current) => new Set([...current].filter((path) => !removed.has(path)))); + if (failed.length === 0) { setDeleting(null); - } else { - setError(commandError(result)); + return; } + setDeleting(failed.map((failure) => failure.target)); + setError( + deleting.length === 1 + ? failed[0]!.message + : failed + .map((failure) => `${worktreeName(failure.target.worktree)}: ${failure.message}`) + .join("\n"), + ); }; + const toggleSelected = (path: string, checked: boolean) => + setSelectedPaths((current) => { + const next = new Set(current); + if (checked) next.add(path); + else next.delete(path); + return next; + }); return (
@@ -253,6 +314,7 @@ export function WorktreeManager() { setSelectedMemberKey(value); setError(null); setDeleting(null); + setSelectedPaths(new Set()); } }} > @@ -293,6 +355,25 @@ export function WorktreeManager() { Refresh + {selectedTargets.length > 0 && ( +
+ + +
+ )}

Threads can share a worktree. Deleting one keeps its threads and branch, but discards @@ -334,22 +415,28 @@ export function WorktreeManager() { ) : ( -

- {worktrees.map((tree) => { - const usedBy = associatedThreads(tree); - const target = { - environmentId: member.environmentId, - cwd: member.workspaceRoot, - worktree: tree, - }; - const blocked = worktreeDeletionBlockReason(target, member, usedBy); +
0 ? "" : undefined} + > + {rows.map(({ tree, target, usedBy, blocked }) => { + const name = worktreeName(tree); return ( -
- +
+ + + + toggleSelected(tree.path, checked)} + /> + +
-

- {tree.path.split(/[\\/]/).at(-1) ?? tree.path} -

+

{name}

{tree.path}

@@ -369,7 +456,7 @@ export function WorktreeManager() { disabled={!!blocked} onClick={() => { setError(null); - setDeleting(target); + setDeleting([target]); }} > @@ -398,52 +485,67 @@ export function WorktreeManager() { > - Delete worktree? + + {shownDeleting && shownDeleting.length > 1 + ? `Delete ${shownDeleting.length} worktrees?` + : "Delete worktree?"} + - {shownDeleting?.worktree.prunable - ? "Its folder is already missing. This removes the stale Git worktree record." - : "This permanently deletes the working copy and everything inside it."} + {shownDeleting?.every((target) => target.worktree.prunable) + ? shownDeleting.length > 1 + ? "Their folders are already missing. This removes the stale Git worktree records." + : "Its folder is already missing. This removes the stale Git worktree record." + : shownDeleting && shownDeleting.length > 1 + ? "This permanently deletes the working copies and everything inside them." + : "This permanently deletes the working copy and everything inside it."} {shownDeleting && ( -

    -
  • - - - {shownDeleting.worktree.path} - -
  • -
  • - - {shownDeleting.worktree.branch ? ( - - The {shownDeleting.worktree.branch} branch - and its commits are kept. - - ) : ( - - Detached at{" "} - - {shownDeleting.worktree.head.slice(0, 7)} +
      + {shownDeleting.map(({ worktree }) => ( +
    • + +
      + {worktree.path} + + + {worktree.branch ? ( + + The {worktree.branch}{" "} + branch and its commits are kept. + + ) : ( + + Detached at{" "} + + {worktree.head.slice(0, 7)} + + . Commits that aren't on a branch can be lost. + + )} - . Commits that aren't on a branch can be lost. - - )} -
    • - {deletingThreads.length > 0 && ( +
+ + ))} + {deletingThreadCount > 0 && (
  • - {deletingThreads.length === 1 - ? "Its thread is kept." - : `Its ${deletingThreads.length} threads are kept.`} + {shownDeleting.length > 1 + ? `${deletingThreadCount} ${deletingThreadCount === 1 ? "thread is" : "threads are"} kept.` + : deletingThreadCount === 1 + ? "Its thread is kept." + : `Its ${deletingThreadCount} threads are kept.`}
  • )} )} {(error || deletionBlocked) && ( -

    +

    {deletionBlocked ?? error}

    )} @@ -454,9 +556,13 @@ export function WorktreeManager() { diff --git a/docs/user/project-settings.md b/docs/user/project-settings.md index 8516e55e49b04..3e3e960f245e6 100644 --- a/docs/user/project-settings.md +++ b/docs/user/project-settings.md @@ -98,8 +98,9 @@ be filtered by profile. Use **Manage worktrees** in **Settings → Storage** to choose a project and checkout, then create a worktree from a new or existing branch. The list includes linked worktrees created outside T3 Code. Deleting one removes its files, including uncommitted changes, but keeps the branch, commits, and -threads. Select another checkout before deleting the project folder you are managing. T3 Code can -recreate a thread's worktree when you continue that thread. +threads. Select several worktrees to delete them together. Select another checkout before deleting +the project folder you are managing. T3 Code can recreate a thread's worktree when you continue that +thread. Open **Settings → Storage** to enable automatic cleanup. When you open it from Settings with no target selected, it selects this client's primary environment. Use the environment picker to choose another machine or From 858cf3a16798c5c71bb9030931a726c20745c489 Mon Sep 17 00:00:00 2001 From: Donald Silveira Date: Wed, 30 Sep 2026 17:07:36 -0300 Subject: [PATCH 2/2] fix(web): scope worktree selection to its checkout - Prevent stale selections from carrying over when the manager falls back to another checkout --- .../components/settings/WorktreeManager.tsx | 31 ++++++++++++------- .../settings/worktreeManager.logic.test.ts | 10 ++++++ .../settings/worktreeManager.logic.ts | 22 +++++++++++++ 3 files changed, 52 insertions(+), 11 deletions(-) diff --git a/apps/web/src/components/settings/WorktreeManager.tsx b/apps/web/src/components/settings/WorktreeManager.tsx index 3a315e9ad8e37..dacfbc5bf52ce 100644 --- a/apps/web/src/components/settings/WorktreeManager.tsx +++ b/apps/web/src/components/settings/WorktreeManager.tsx @@ -48,9 +48,11 @@ import { import { useSettingsScope } from "./SettingsScopeContext"; import { SettingsSection } from "./settingsLayout"; import { + selectedWorktreePaths, worktreeThreads, worktreeDeletionBlockReason, type WorktreeDeletionTarget, + type WorktreeSelection, } from "./worktreeManager.logic"; function commandError(result: Parameters[0]): string { @@ -191,7 +193,7 @@ export function WorktreeManager() { const { scope, connectedEnvironments } = useSettingsScope(); const [selectedMemberKey, setSelectedMemberKey] = useState(null); const [creating, setCreating] = useState(false); - const [selectedPaths, setSelectedPaths] = useState>(() => new Set()); + const [selection, setSelection] = useState(null); const [deleting, setDeleting] = useState | null>(null); // Keep the targets through the close animation. const [shownDeleting, setShownDeleting] = useState(deleting); @@ -218,6 +220,7 @@ export function WorktreeManager() { input: { cwd: member.workspaceRoot }, }), ); + const selectedPaths = selectedWorktreePaths(selection, member); const removeWorktree = useAtomCommand(vcsEnvironment.removeWorktree, { reportFailure: false }); const worktrees = query.data?.worktrees.filter((tree) => !tree.isMain) ?? []; const threadsByWorktree = member @@ -281,7 +284,13 @@ export function WorktreeManager() { else failed.push({ target, message: commandError(result) }); } setBusy(false); - setSelectedPaths((current) => new Set([...current].filter((path) => !removed.has(path)))); + setSelection( + (current) => + current && { + ...current, + paths: new Set([...current.paths].filter((path) => !removed.has(path))), + }, + ); if (failed.length === 0) { setDeleting(null); return; @@ -295,13 +304,13 @@ export function WorktreeManager() { .join("\n"), ); }; - const toggleSelected = (path: string, checked: boolean) => - setSelectedPaths((current) => { - const next = new Set(current); - if (checked) next.add(path); - else next.delete(path); - return next; - }); + const toggleSelected = (path: string, checked: boolean) => { + if (!member) return; + const paths = new Set(selectedPaths); + if (checked) paths.add(path); + else paths.delete(path); + setSelection({ environmentId: member.environmentId, cwd: member.workspaceRoot, paths }); + }; return (
    @@ -314,7 +323,7 @@ export function WorktreeManager() { setSelectedMemberKey(value); setError(null); setDeleting(null); - setSelectedPaths(new Set()); + setSelection(null); } }} > @@ -357,7 +366,7 @@ export function WorktreeManager() { {selectedTargets.length > 0 && (
    -