MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / removeAgentWorktree

Function removeAgentWorktree

source code/utils/worktree.ts:963–1022  ·  view source on GitHub ↗
(
  worktreePath: string,
  worktreeBranch?: string,
  gitRoot?: string,
  hookBased?: boolean,
)

Source from the content-addressed store, hash-verified

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

Callers 4

cleanupWorktreeIfNeededFunction · 0.85
onSessionDoneFunction · 0.85
runBridgeLoopFunction · 0.85

Calls 3

logForDebuggingFunction · 0.85
execFileNoThrowWithCwdFunction · 0.85

Tested by

no test coverage detected