MCPcopy Index your code
hub / github.com/claude-code-best/claude-code / removeAgentWorktree

Function removeAgentWorktree

src/utils/worktree.ts:961–1020  ·  view source on GitHub ↗
(
  worktreePath: string,
  worktreeBranch?: string,
  gitRoot?: string,
  hookBased?: boolean,
)

Source from the content-addressed store, hash-verified

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

Callers 5

cleanupWorktreeIfNeededFunction · 0.85
onSessionDoneFunction · 0.85
runBridgeLoopFunction · 0.85
cleanupWorkflowWorktreeFunction · 0.85

Calls 3

execFileNoThrowWithCwdFunction · 0.85
logForDebuggingFunction · 0.70

Tested by

no test coverage detected