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