Skip to content

fix(squash): run pre-commit hooks before staging so their edits reach the commit - #4321

Merged
max-sixty merged 1 commit into
mainfrom
nightly/clean-36532459830-squash-hook-order
Sep 29, 2026
Merged

max-sixty merged 1 commit into
mainfrom
nightly/clean-36532459830-squash-hook-order

Conversation

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

wt step squash, and the squash path wt merge takes by default, staged changes before running pre-commit hooks. Anything a formatter hook changed was therefore left out of the squash commit and stayed in the working tree. wt step commit already runs hooks first and then stages. This PR gives handle_squash the same order. The wt hook docs list pre-commit for "Formatters, linters, type checking … wt step squash, and the commit wt merge makes".

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 existing user_pre_commit_executes snapshot had locked that in. Its hook writes user_precommit.txt, and the merge committed only uncommitted.txt. The snapshot now shows both files in the commit.

Verification:

  • New test test_step_squash_includes_pre_commit_hook_edits: a pre-commit hook writes fmt.txt during wt step squash. Before the fix, feature:fmt.txt did not exist; now it is in the squash commit and the worktree is clean.
  • The other three snapshot updates only move the Auto-staging N untracked paths warning after the hook output, which is the order wt step commit already prints. When the hook fails, the warning disappears, because nothing is staged.
  • The merge, hook, approval and commit integration tests pass locally. The exception is test_merge_no_ff_signs_merge_commit, which fails the same way on unmodified main in 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.

… 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.
@worktrunk-bot worktrunk-bot added the nightly-cleanup Issues found by nightly code quality sweep label Sep 29, 2026
@max-sixty
max-sixty merged commit 3949c21 into main Sep 29, 2026
37 of 47 checks passed
@max-sixty
max-sixty deleted the nightly/clean-36532459830-squash-hook-order branch September 29, 2026 07:34
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

nightly-cleanup Issues found by nightly code quality sweep

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants