MCPcopy
hub / github.com/codeaashu/claude-code / cleanupWorktree

Function cleanupWorktree

src/utils/worktree.ts:813–894  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

811}
812
813export async function cleanupWorktree(): Promise<void> {
814 if (!currentWorktreeSession) {
815 return
816 }
817
818 try {
819 const { worktreePath, originalCwd, worktreeBranch, hookBased } =
820 currentWorktreeSession
821
822 // Change back to original directory first
823 process.chdir(originalCwd)
824
825 if (hookBased) {
826 // Hook-based worktree: delegate cleanup to WorktreeRemove hook
827 const hookRan = await executeWorktreeRemoveHook(worktreePath)
828 if (hookRan) {
829 logForDebugging(`Removed hook-based worktree at: ${worktreePath}`)
830 } else {
831 logForDebugging(
832 `No WorktreeRemove hook configured, hook-based worktree left at: ${worktreePath}`,
833 { level: 'warn' },
834 )
835 }
836 } else {
837 // Git-based worktree: use git worktree remove.
838 // Explicit cwd: process.chdir above does NOT update getCwd() (the state
839 // CWD that execFileNoThrow defaults to). If the model cd'd to a non-repo
840 // dir, the bare execFileNoThrow variant would fail silently here.
841 const { code: removeCode, stderr: removeError } =
842 await execFileNoThrowWithCwd(
843 gitExe(),
844 ['worktree', 'remove', '--force', worktreePath],
845 { cwd: originalCwd },
846 )
847
848 if (removeCode !== 0) {
849 logForDebugging(`Failed to remove linked worktree: ${removeError}`, {
850 level: 'error',
851 })
852 } else {
853 logForDebugging(`Removed linked worktree at: ${worktreePath}`)
854 }
855 }
856
857 // Clear the session
858 currentWorktreeSession = null
859
860 // Update config
861 saveCurrentProjectConfig(current => ({
862 ...current,
863 activeWorktreeSession: undefined,
864 }))
865
866 // Delete the temporary worktree branch (git-based only)
867 if (!hookBased && worktreeBranch) {
868 // Wait a bit to ensure git has released all locks
869 await sleep(100)
870

Callers 3

callFunction · 0.85
loadChangesFunction · 0.85
handleSelectFunction · 0.85

Calls 5

logForDebuggingFunction · 0.85
execFileNoThrowWithCwdFunction · 0.85
saveCurrentProjectConfigFunction · 0.85
sleepFunction · 0.70

Tested by

no test coverage detected