Skip to content
This repository was archived by the owner on Oct 21, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
245 changes: 180 additions & 65 deletions apps/web/src/components/settings/WorktreeManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<typeof squashAtomCommandFailure>[0]): string {
Expand Down Expand Up @@ -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<string | null>(null);
const [creating, setCreating] = useState(false);
const [deleting, setDeleting] = useState<WorktreeDeletionTarget | null>(null);
// Keep the target through the close animation.
const [selection, setSelection] = useState<WorktreeSelection | null>(null);
const [deleting, setDeleting] = useState<ReadonlyArray<WorktreeDeletionTarget> | 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);
Expand All @@ -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<string>();
// 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 (
<SettingsSection id="storage-manage-worktrees" title="Manage worktrees">
Expand All @@ -253,6 +323,7 @@ export function WorktreeManager() {
setSelectedMemberKey(value);
setError(null);
setDeleting(null);
setSelection(null);
}
}}
>
Expand Down Expand Up @@ -293,6 +364,25 @@ export function WorktreeManager() {
<RefreshCwIcon data-icon="inline-start" />
Refresh
</Button>
{selectedTargets.length > 0 && (
<div className="ml-auto flex items-center gap-2">
<Button size="sm" variant="ghost" onClick={() => setSelection(null)}>
Clear selection
</Button>
<Button
size="sm"
variant="destructive-outline"
onClick={() => {
setError(null);
setDeleting(selectedTargets);
}}
>
<Trash2Icon data-icon="inline-start" />
Delete {selectedTargets.length}{" "}
{selectedTargets.length === 1 ? "worktree" : "worktrees"}
</Button>
</div>
)}
</div>
<p className="text-xs text-muted-foreground">
Threads can share a worktree. Deleting one keeps its threads and branch, but discards
Expand Down Expand Up @@ -334,22 +424,28 @@ export function WorktreeManager() {
</Empty>
</div>
) : (
<div className="divide-y rounded-lg border">
{worktrees.map((tree) => {
const usedBy = associatedThreads(tree);
const target = {
environmentId: member.environmentId,
cwd: member.workspaceRoot,
worktree: tree,
};
const blocked = worktreeDeletionBlockReason(target, member, usedBy);
<div
className="group/worktrees divide-y rounded-lg border"
data-selecting={selectedTargets.length > 0 ? "" : undefined}
>
{rows.map(({ tree, target, usedBy, blocked }) => {
const name = worktreeName(tree);
return (
<div key={tree.path} className="flex items-start gap-3 p-4">
<FolderGit2Icon className="mt-0.5 size-4 shrink-0 text-muted-foreground" />
<div key={tree.path} className="group/worktree flex items-start gap-3 p-4">
<span className="relative mt-0.5 flex size-4 shrink-0 items-center justify-center">
<FolderGit2Icon className="size-4 text-muted-foreground transition-opacity group-focus-within/worktree:opacity-0 group-hover/worktree:opacity-0 group-data-selecting/worktrees:opacity-0 pointer-coarse:opacity-0" />
<span className="absolute inset-0 flex items-center justify-center opacity-0 transition-opacity group-focus-within/worktree:opacity-100 group-hover/worktree:opacity-100 group-data-selecting/worktrees:opacity-100 pointer-coarse:opacity-100">
<Checkbox
aria-label={`Select ${name}`}
title={blocked ?? undefined}
checked={!blocked && selectedPaths.has(tree.path)}
disabled={!!blocked}
onCheckedChange={(checked) => toggleSelected(tree.path, checked)}
/>
</span>
</span>
<div className="min-w-0 flex-1">
<p className="truncate text-sm font-medium">
{tree.path.split(/[\\/]/).at(-1) ?? tree.path}
</p>
<p className="truncate text-sm font-medium">{name}</p>
<p className="break-all text-xs text-muted-foreground">{tree.path}</p>
<p className="mt-2 flex items-center gap-1.5 text-xs text-muted-foreground">
<GitBranchIcon className="size-3" />
Expand All @@ -369,7 +465,7 @@ export function WorktreeManager() {
disabled={!!blocked}
onClick={() => {
setError(null);
setDeleting(target);
setDeleting([target]);
}}
>
<Trash2Icon />
Expand Down Expand Up @@ -398,52 +494,67 @@ export function WorktreeManager() {
>
<AlertDialogPopup>
<AlertDialogHeader>
<AlertDialogTitle>Delete worktree?</AlertDialogTitle>
<AlertDialogTitle>
{shownDeleting && shownDeleting.length > 1
? `Delete ${shownDeleting.length} worktrees?`
: "Delete worktree?"}
</AlertDialogTitle>
<AlertDialogDescription>
{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."}
</AlertDialogDescription>
</AlertDialogHeader>
{shownDeleting && (
<ul className="mx-6 mb-6 divide-y rounded-lg border bg-muted/40 text-sm max-sm:mb-4">
<li className="flex gap-3 px-3 py-2.5">
<FolderIcon className="mt-0.5 size-4 shrink-0 text-muted-foreground" />
<span className="min-w-0 break-all font-mono text-xs leading-5">
{shownDeleting.worktree.path}
</span>
</li>
<li className="flex gap-3 px-3 py-2.5">
<GitBranchIcon className="mt-0.5 size-4 shrink-0 text-muted-foreground" />
{shownDeleting.worktree.branch ? (
<span>
The <span className="font-medium">{shownDeleting.worktree.branch}</span> branch
and its commits are kept.
</span>
) : (
<span>
Detached at{" "}
<span className="font-medium font-mono">
{shownDeleting.worktree.head.slice(0, 7)}
<ul className="mx-6 mb-6 max-h-80 divide-y overflow-y-auto rounded-lg border bg-muted/40 text-sm max-sm:mb-4">
{shownDeleting.map(({ worktree }) => (
<li key={worktree.path} className="flex gap-3 px-3 py-2.5">
<FolderIcon className="mt-0.5 size-4 shrink-0 text-muted-foreground" />
<div className="flex min-w-0 flex-col gap-1">
<span className="break-all font-mono text-xs leading-5">{worktree.path}</span>
<span className="flex gap-1.5 text-xs text-muted-foreground">
<GitBranchIcon className="mt-0.5 size-3 shrink-0" />
{worktree.branch ? (
<span>
The <span className="font-medium text-foreground">{worktree.branch}</span>{" "}
branch and its commits are kept.
</span>
) : (
<span>
Detached at{" "}
<span className="font-medium font-mono text-foreground">
{worktree.head.slice(0, 7)}
</span>
. Commits that aren&apos;t on a branch can be lost.
</span>
)}
</span>
. Commits that aren&apos;t on a branch can be lost.
</span>
)}
</li>
{deletingThreads.length > 0 && (
</div>
</li>
))}
{deletingThreadCount > 0 && (
<li className="flex gap-3 px-3 py-2.5">
<MessageSquareIcon className="mt-0.5 size-4 shrink-0 text-muted-foreground" />
<span>
{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.`}
</span>
</li>
)}
</ul>
)}
{(error || deletionBlocked) && (
<p role="alert" className="px-6 pb-6 text-sm text-destructive max-sm:pb-4">
<p
role="alert"
className="whitespace-pre-line px-6 pb-6 text-sm text-destructive max-sm:pb-4"
>
{deletionBlocked ?? error}
</p>
)}
Expand All @@ -454,9 +565,13 @@ export function WorktreeManager() {
<Button
variant="destructive"
disabled={busy || !!deletionBlocked}
onClick={() => void deleteWorktree()}
onClick={() => void deleteWorktrees()}
>
{busy ? "Deleting…" : "Delete worktree"}
{busy
? "Deleting…"
: deleting && deleting.length > 1
? `Delete ${deleting.length} worktrees`
: "Delete worktree"}
</Button>
</AlertDialogFooter>
</AlertDialogPopup>
Expand Down
10 changes: 10 additions & 0 deletions apps/web/src/components/settings/worktreeManager.logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
});
});
Loading
Loading