INNER CODE UNIT · Go
runWithWorktree
umputun/ralphex · cmd/ralphex/main.go:703
func runWithWorktree(ctx context.Context, o opts, req executePlanRequest) (err error) {
wtPath, planNeedsCommit, err := req.GitSvc.CreateWorktreeForPlan(req.PlanFile, req.DefaultBranch, req.BranchOverride)
if err != nil {
return fmt.Errorf("create worktree: %w", err)
}
// register early cleanup so the interrupt handler's force-exit path (os.Exit after 5s)
// can remove the worktree even during setup. overwritten with full cleanup after chdir.
// RemoveWorktree is idempotent, so double-call from both early and safety-net defer is safe.
req.WtCleanup.set(func() {
if rmErr := req.GitSvc.RemoveWorktree(wtPath); rmErr != nil {
fmt.Fprintf(os.Stderr, "warning: failed to remove worktree: %v\n", rmErr)
}
})
// safety net: remove worktree if setup fails before main cleanup is registered.
// once main cleanup takes over (setupDone=true), this defer becomes a no-op.
setupDone := false