Skip to content
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
26 changes: 26 additions & 0 deletions apps/server/src/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,32 @@ describe("git integration", () => {
await removeGitWorktree({ cwd: tmp.path, path: wtPath });
expect(existsSync(wtPath)).toBe(false);
});

it("removeGitWorktree force removes a dirty worktree", async () => {
await using tmp = await makeTmpDir();
await initRepoWithCommit(tmp.path);

const wtPath = path.join(tmp.path, "wt-dirty-dir");
const currentBranch = (await listGitBranches({ cwd: tmp.path })).branches.find(
(b) => b.current,
)!.name;

await createGitWorktree({
cwd: tmp.path,
branch: currentBranch,
newBranch: "wt-dirty",
path: wtPath,
});
expect(existsSync(wtPath)).toBe(true);

await writeFile(path.join(wtPath, "README.md"), "dirty change\n");

await expect(removeGitWorktree({ cwd: tmp.path, path: wtPath })).rejects.toThrow();
expect(existsSync(wtPath)).toBe(true);

await removeGitWorktree({ cwd: tmp.path, path: wtPath, force: true });
expect(existsSync(wtPath)).toBe(false);
});
});

// ── Full flow: local branch checkout ──
Expand Down
6 changes: 5 additions & 1 deletion apps/server/src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,11 @@ export class GitCoreService {
}

async removeWorktree(input: GitRemoveWorktreeInput): Promise<void> {
const args = ["worktree", "remove", input.path] as const;
const args = ["worktree", "remove"];
if (input.force) {
args.push("--force");
}
args.push(input.path);
try {
await executeGit(input.cwd, args, {
timeoutMs: 15_000,
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ export default function Sidebar() {
await removeWorktreeMutation.mutateAsync({
cwd: threadProject.cwd,
path: orphanedWorktreePath,
force: true,
});
} catch (error) {
const message = error instanceof Error ? error.message : "Unknown error removing worktree.";
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/lib/gitReactQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ export function gitRemoveWorktreeMutationOptions(input: {
queryClient: QueryClient;
}) {
return mutationOptions({
mutationFn: async ({ cwd, path }: { cwd: string; path: string }) => {
mutationFn: async ({ cwd, path, force }: { cwd: string; path: string; force?: boolean }) => {
if (!input.api) {
throw new Error("Git worktree removal is unavailable.");
}
return input.api.git.removeWorktree({ cwd, path });
return input.api.git.removeWorktree({ cwd, path, force });
},
onSettled: async () => {
await invalidateGitQueries(input.queryClient);
Expand Down
1 change: 1 addition & 0 deletions packages/contracts/src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const gitCreateWorktreeInputSchema = z.object({
export const gitRemoveWorktreeInputSchema = z.object({
cwd: z.string().min(1),
path: z.string().min(1),
force: z.boolean().optional(),
});

export const gitCreateBranchInputSchema = z.object({
Expand Down