diff --git a/apps/web/src/components/settings/WorktreeManager.tsx b/apps/web/src/components/settings/WorktreeManager.tsx index 6acc46e31395a..dacfbc5bf52ce 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, @@ -47,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 { @@ -182,12 +185,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 [selection, setSelection] = useState(null); + 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); @@ -212,34 +220,96 @@ 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 ? 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") { + setSelection( + (current) => + current && { + ...current, + paths: new Set([...current.paths].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) => { + 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 ( @@ -253,6 +323,7 @@ export function WorktreeManager() { setSelectedMemberKey(value); setError(null); setDeleting(null); + setSelection(null); } }} > @@ -293,6 +364,25 @@ export function WorktreeManager() { Refresh + {selectedTargets.length > 0 && ( +
+ + +
+ )}

Threads can share a worktree. Deleting one keeps its threads and branch, but discards @@ -334,22 +424,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 +465,7 @@ export function WorktreeManager() { disabled={!!blocked} onClick={() => { setError(null); - setDeleting(target); + setDeleting([target]); }} > @@ -398,52 +494,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 +565,13 @@ export function WorktreeManager() { diff --git a/apps/web/src/components/settings/worktreeManager.logic.test.ts b/apps/web/src/components/settings/worktreeManager.logic.test.ts index 9e7eb6285d5bf..5c4f49ee28d86 100644 --- a/apps/web/src/components/settings/worktreeManager.logic.test.ts +++ b/apps/web/src/components/settings/worktreeManager.logic.test.ts @@ -2,6 +2,7 @@ import { EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts"; import { describe, expect, it } from "vite-plus/test"; import { + selectedWorktreePaths, worktreeDeletionBlockReason, worktreeThreads, type WorktreeDeletionTarget, @@ -82,4 +83,13 @@ describe("worktree deletion", () => { worktreeDeletionBlockReason(target, member, [{ ...idle, hasPendingApprovals: true }]), ).toBe("A thread is running here"); }); + + it("does not carry a selection to the checkout the manager falls back to", () => { + const selection = { environmentId: laptop, cwd: "/repo", paths: new Set(["/linked"]) }; + expect(selectedWorktreePaths(selection, member)).toEqual(new Set(["/linked"])); + // The laptop disconnected and the server has a worktree at the same path. + expect(selectedWorktreePaths(selection, { ...member, environmentId: server }).size).toBe(0); + expect(selectedWorktreePaths(selection, { ...member, workspaceRoot: "/other" }).size).toBe(0); + expect(selectedWorktreePaths(selection, null).size).toBe(0); + }); }); diff --git a/apps/web/src/components/settings/worktreeManager.logic.ts b/apps/web/src/components/settings/worktreeManager.logic.ts index 794edb77cffae..7cc83de410943 100644 --- a/apps/web/src/components/settings/worktreeManager.logic.ts +++ b/apps/web/src/components/settings/worktreeManager.logic.ts @@ -10,6 +10,28 @@ export interface WorktreeDeletionTarget { worktree: VcsListedWorktree; } +/** Worktree paths checked in one checkout. Paths alone are ambiguous across checkouts. */ +export interface WorktreeSelection { + environmentId: EnvironmentId; + cwd: string; + paths: ReadonlySet; +} + +const noSelection: ReadonlySet = new Set(); + +/** The selection visible in `member`; empty when the manager fell back to another checkout. */ +export function selectedWorktreePaths( + selection: WorktreeSelection | null, + member: { environmentId: EnvironmentId; workspaceRoot: string } | null, +): ReadonlySet { + return selection && + member && + selection.environmentId === member.environmentId && + selection.cwd === member.workspaceRoot + ? selection.paths + : noSelection; +} + export function worktreeThreads< T extends Pick, >( 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