( worktreePath: string, worktreeBranch?: string, gitRoot?: string, hookBased?: boolean, )
| 973 | * since the worktree directory is deleted during this operation. |
| 974 | */ |
| 975 | export async function removeAgentWorktree( |
| 976 | worktreePath: string, |
| 977 | worktreeBranch?: string, |
| 978 | gitRoot?: string, |
| 979 | hookBased?: boolean, |
| 980 | ): Promise<boolean> { |
| 981 | if (hookBased) { |
| 982 | const hookRan = await executeWorktreeRemoveHook(worktreePath) |
| 983 | if (hookRan) { |
| 984 | logForDebugging(`Removed hook-based agent worktree at: ${worktreePath}`) |
| 985 | } else { |
| 986 | logForDebugging( |
| 987 | `No WorktreeRemove hook configured, hook-based agent worktree left at: ${worktreePath}`, |
| 988 | { level: 'warn' }, |
| 989 | ) |
| 990 | } |
| 991 | return hookRan |
| 992 | } |
| 993 | |
| 994 | if (!gitRoot) { |
| 995 | logForDebugging('Cannot remove agent worktree: no git root provided', { |
| 996 | level: 'error', |
| 997 | }) |
| 998 | return false |
| 999 | } |
| 1000 | |
| 1001 | // Run from the main repo root, not the worktree (which we're about to delete) |
| 1002 | const { code: removeCode, stderr: removeError } = |
| 1003 | await execFileNoThrowWithCwd( |
| 1004 | gitExe(), |
| 1005 | ['worktree', 'remove', '--force', worktreePath], |
| 1006 | { cwd: gitRoot }, |
| 1007 | ) |
| 1008 | |
| 1009 | if (removeCode !== 0) { |
| 1010 | logForDebugging(`Failed to remove agent worktree: ${removeError}`, { |
| 1011 | level: 'error', |
| 1012 | }) |
| 1013 | return false |
| 1014 | } |
| 1015 | logForDebugging(`Removed agent worktree at: ${worktreePath}`) |
| 1016 | |
| 1017 | if (!worktreeBranch) { |
| 1018 | return true |
| 1019 | } |
| 1020 | |
| 1021 | // Delete the temporary worktree branch from the main repo |
| 1022 | const { code: deleteBranchCode, stderr: deleteBranchError } = |
| 1023 | await execFileNoThrowWithCwd(gitExe(), ['branch', '-D', worktreeBranch], { |
| 1024 | cwd: gitRoot, |
| 1025 | }) |
| 1026 | |
| 1027 | if (deleteBranchCode !== 0) { |
| 1028 | logForDebugging( |
| 1029 | `Could not delete agent worktree branch: ${deleteBranchError}`, |
| 1030 | { level: 'error' }, |
| 1031 | ) |
| 1032 | } |
no test coverage detected