fix(desktop): make git hook failures non-fatal during worktree creation#1437
Conversation
When git hooks (e.g., Husky post-checkout) fail during worktree creation, the worktree is still created on disk but execFileAsync throws due to a non-zero exit code. This caused workspace creation to fail entirely even though the worktree was ready. Introduces execWorktreeAdd() helper that checks if the worktree exists on disk after a failure — if it does, the hook failure is logged as a warning and creation continues normally. Closes #961, closes #1432
📝 WalkthroughWalkthroughAdds a post-checkout-hook tolerance layer and guarded worktree-add wrapper, replaces direct exec calls for worktree operations, integrates tolerance into branch switch logic, and adds unit tests validating tolerant and error propagation behaviors. Changes
Sequence Diagram(s)sequenceDiagram
participant App as Desktop App
participant Wrapper as execWorktreeAdd / runWithPostCheckoutHookTolerance
participant Git as Git CLI
participant FS as Filesystem
participant Hook as post-checkout hook
App->>Wrapper: request worktree add / checkout (params, env)
Wrapper->>Git: run `git worktree add` / `git checkout`
Git->>Hook: invoke post-checkout hook
alt Hook succeeds
Git-->>Wrapper: success
Wrapper->>FS: check isWorktreeRegistered(worktreePath)
FS-->>Wrapper: registered
Wrapper-->>App: success
else Hook fails (hook-related error)
Git-->>Wrapper: error (hook failure)
Wrapper->>Wrapper: detect post-checkout hook failure
Wrapper->>FS: poll isWorktreeRegistered (retry until true or timeout)
alt Registered becomes true
FS-->>Wrapper: registered
Wrapper-->>App: treat as success (non-fatal hook failure)
else Not registered / timeout
Wrapper-->>App: rethrow original git error
end
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 🧹 Recent nitpick comments
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
🧹 Preview Cleanup CompleteThe following preview resources have been cleaned up:
Thank you for your contribution! 🎉 |
Summary
post-checkout) that fail duringgit worktree addno longer block workspace creationexecWorktreeAdd()helper that tolerates hook failures when the worktree was successfully created on diskcore.hooksPath=/dev/null(Revert "fix(desktop): disable git hooks during worktree creation" #1434) to restore legitimate hook executionChanges
apps/desktop/src/lib/trpc/routers/workspaces/utils/git.ts:pathExists()helper usingstatfromnode:fs/promisesexecWorktreeAdd()— wrapsexecFileAsync("git", ...)for worktree creation; on failure, checks if the worktree exists on disk (hook failed but worktree created) and logs a warning instead of throwingcreateWorktree()to useexecWorktreeAdd()createWorktreeFromExistingBranch()to useexecWorktreeAdd()(both local and remote branch paths)createWorktreeFromPr()to useexecWorktreeAdd()(both existing-branch and new-branch paths)Test Plan
bun test src/lib/trpc/routers/workspaces/utils/git.test.ts— 12 passbun test src/lib/trpc/routers/workspaces/utils/teardown.test.ts— 11 passbun run typecheck— cleanCloses #961, closes #1432
Summary by CodeRabbit