fix(hooks): keep a partly staged file partly staged - #1874
Merged
Conversation
The hook formats staged source files and then re-stages them, and `git add` stages the whole worktree file rather than the hunks that were chosen. So a file staged in part with `git add -p` was committed whole: the hunks the author deliberately left out of the index went into the commit, with nothing on stdout and nothing in the diff review to say so. The re-stage now skips a file that already had unstaged changes when the hook started, and names it. Its formatting still reaches the worktree, so the next commit carries it; what is committed now is exactly what was staged. That can be unformatted, which CI reports out loud — the failure this replaces was silent, which is the one worth refusing. The dirty list is taken before the formatter runs, because afterwards every file it rewrote reads as unstaged. The three sections shared one copy of the re-stage loop each; they now share one function. The comment above the old loop claimed it re-staged the files the formatter had modified. Its condition only tested that the file still existed, which is a guard for a staged file deleted from the worktree and is kept.
danielcopper
enabled auto-merge (squash)
September 10, 2026 11:14
|
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.



The hook formats staged source files and then re-stages them, and
git addstages the whole worktree file ratherthan the hunks that were chosen. So a file staged in part with
git add -pwas committed whole: the hunks theauthor deliberately left out of the index went into the commit, with nothing on stdout and nothing in the diff
review to say so.
The re-stage now skips a file that already had unstaged changes when the hook started, and names it. Its formatting
still reaches the worktree, so the next commit carries it; what is committed now is exactly what was staged. That
can be unformatted, which CI reports out loud — the failure this replaces was silent, which is the one worth
refusing.
The dirty list is taken before the formatter runs, because afterwards every file it rewrote reads as unstaged. The
three sections shared one copy of the re-stage loop each; they now share one function.
The comment above the old loop claimed it re-staged the files the formatter had modified. Its condition only tested
that the file still existed, which is a guard for a staged file deleted from the worktree and is kept.
Verified
Against a scratch repository, with the same hook the repo installs:
pre-commit: <file> has unstaged changes — leaving the index exactly as you staged it, and still has theformatter's fixes applied in the worktree.
content, exactly as before.
ruff check. That behaviour isunchanged — the same scenario aborts identically on
main's hook, so it is not introduced here and is out ofscope for this cut.
docs: N/A — tooling only; the hook's own contract is stated in
CLAUDE.md's Development section, updated here.