({
onDone,
onCancel
}: Props)
| 27 | onCancel?: () => void; |
| 28 | }; |
| 29 | export function WorktreeExitDialog({ |
| 30 | onDone, |
| 31 | onCancel |
| 32 | }: Props): React.ReactNode { |
| 33 | const [status, setStatus] = useState<'loading' | 'asking' | 'keeping' | 'removing' | 'done'>('loading'); |
| 34 | const [changes, setChanges] = useState<string[]>([]); |
| 35 | const [commitCount, setCommitCount] = useState<number>(0); |
| 36 | const [resultMessage, setResultMessage] = useState<string | undefined>(); |
| 37 | const worktreeSession = getCurrentWorktreeSession(); |
| 38 | useEffect(() => { |
| 39 | async function loadChanges() { |
| 40 | let changeLines: string[] = []; |
| 41 | const gitStatus = await execFileNoThrow('git', ['status', '--porcelain']); |
| 42 | if (gitStatus.stdout) { |
| 43 | changeLines = gitStatus.stdout.split('\n').filter(_ => _.trim() !== ''); |
| 44 | setChanges(changeLines); |
| 45 | } |
| 46 | |
| 47 | // Check for commits to eject |
| 48 | if (worktreeSession) { |
| 49 | // Get commits in worktree that are not in original branch |
| 50 | const { |
| 51 | stdout: commitsStr |
| 52 | } = await execFileNoThrow('git', ['rev-list', '--count', `${worktreeSession.originalHeadCommit}..HEAD`]); |
| 53 | const count = parseInt(commitsStr.trim()) || 0; |
| 54 | setCommitCount(count); |
| 55 | |
| 56 | // If no changes and no commits, clean up silently |
| 57 | if (changeLines.length === 0 && count === 0) { |
| 58 | setStatus('removing'); |
| 59 | void cleanupWorktree().then(() => { |
| 60 | process.chdir(worktreeSession.originalCwd); |
| 61 | setCwd(worktreeSession.originalCwd); |
| 62 | recordWorktreeExit(); |
| 63 | getPlansDirectory.cache.clear?.(); |
| 64 | setResultMessage('Worktree removed (no changes)'); |
| 65 | }).catch(error => { |
| 66 | logForDebugging(`Failed to clean up worktree: ${error}`, { |
| 67 | level: 'error' |
| 68 | }); |
| 69 | setResultMessage('Worktree cleanup failed, exiting anyway'); |
| 70 | }).then(() => { |
| 71 | setStatus('done'); |
| 72 | }); |
| 73 | return; |
| 74 | } else { |
| 75 | setStatus('asking'); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | void loadChanges(); |
| 80 | // eslint-disable-next-line react-hooks/exhaustive-deps |
| 81 | // biome-ignore lint/correctness/useExhaustiveDependencies: intentional |
| 82 | }, [worktreeSession]); |
| 83 | useEffect(() => { |
| 84 | if (status === 'done') { |
| 85 | onDone(resultMessage); |
| 86 | } |
nothing calls this directly
no test coverage detected