Repository navigation
fix(squash): run pre-commit hooks before staging so their edits reach the commit - #4321
Merged
Merged
Conversation
… the commit wt step squash and the squash path of wt merge staged first and ran pre-commit hooks after, so whatever a formatter hook changed was left in the working tree: missing from the squash commit, and a dirty tree that made wt merge refuse to remove the worktree. wt step commit already runs hooks before staging; squash now does the same.
max-sixty
added a commit
that referenced
this pull request
Oct 20, 2026
Since v0.80.0, pre-commit hooks during `wt merge` and `wt step squash` no longer see the uncommitted changes in the index. #4321 moved the squash path to run hooks before staging so a formatter hook's edits would land in the squash commit. A hook that reads the index (`pre-commit run`, lint-staged, `git diff --cached`) then saw nothing staged and passed, so a merge that v0.80.0 blocked committed the offending changes. All commit paths now stage, run the pre-commit hooks, and stage again with the same `--stage` mode. Hooks see the changes, formatter edits still land in the commit, and the generated commit message reads the post-hook index. The second staging warns only about untracked paths the hooks created. This also applies to `wt step commit` and `wt merge --no-squash`, which ran hooks before staging even in v0.80.0, so an index-reading hook now blocks those commits too, and a failed hook leaves the changes staged there as it already did for squash. Verified with the release-review repros: a `git diff --cached` gate and a pre-commit-framework `pre-commit run` gate both block `wt merge` and `wt step squash` again, as on v0.80.0, where main committed the changes. A `sed` formatter hook's edit is in the commit and in the prompt the commit-message command receives, with a clean tree afterwards. The new `test_pre_commit_hook_sees_staged_changes` covers `wt merge` and `wt step commit` and fails without the fix. `test_step_commit_refuses_hook_created_conflict` now unstages in its hook, because `git merge` refuses to start once the index is staged. The other snapshot changes move the auto-staging warning ahead of the hook output. > _This was written by Claude Code on behalf of max-sixty_ Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
wt step squash, and the squash pathwt mergetakes by default, staged changes before runningpre-commithooks. Anything a formatter hook changed was therefore left out of the squash commit and stayed in the working tree.wt step commitalready runs hooks first and then stages. This PR giveshandle_squashthe same order. Thewt hookdocs listpre-commitfor "Formatters, linters, type checking …wt step squash, and the commitwt mergemakes".Effect on
wt merge: after the squash commit, the hook's edits leave a dirty tree, so the merge lands but cannot remove the worktree ("Cannot remove worktree after merge: feat has uncommitted changes"). The existinguser_pre_commit_executessnapshot had locked that in. Its hook writesuser_precommit.txt, and the merge committed onlyuncommitted.txt. The snapshot now shows both files in the commit.Verification:
test_step_squash_includes_pre_commit_hook_edits: a pre-commit hook writesfmt.txtduringwt step squash. Before the fix,feature:fmt.txtdid not exist; now it is in the squash commit and the worktree is clean.Auto-staging N untracked pathswarning after the hook output, which is the orderwt step commitalready prints. When the hook fails, the warning disappears, because nothing is staged.test_merge_no_ff_signs_merge_commit, which fails the same way on unmodifiedmainin this sandbox (it rejects the test signing key's 0644 permissions).Related behavior this PR leaves alone
Squash still runs
pre-commit(and prompts for its approval) before checking whether there is anything to squash, so a single clean commit or zero commits ahead still runs the hooks and then reports "Nothing to squash". Moving the hooks after that check is a separate design question: a formatter hook on a single unformatted commit is exactly the case where running it produces a commit. With this PR, those edits are now staged and squashed together with the commit instead of being left dirty.