Repository navigation
fix(git-hooks): keep unstaged bytes out of pre-commit formatting - #137723
Conversation
format-staged.sh formatted working-tree files and then git-added the complete working-tree contents of every staged file, so partial staging (hunk-level git apply --cached) silently committed unstaged work, and dirty non-format files were restaged wholesale. Fully staged files keep the fast batch oxfmt --write + restage path, now scoped to format-eligible files only. Partially staged files are formatted index-only: the staged blob goes through oxfmt --stdin-filepath (same repo config and ignorePatterns as batch mode, verified live), then git hash-object --no-filters and git update-index --index-info, preserving the staged mode and never touching working-tree bytes. Non-format staged files are no longer restaged at all; staged deletions, unstaged working-tree deletions, symlinks, and executable modes are preserved, and a formatter that exits 0 with empty output for nonempty input fails the hook instead of staging an emptied file. Security scans (pre/post-format literal scan), filename protections, and the sequencer early-exit are unchanged. Regression tests fail on the pre-fix script; the guard/boundary suites now protect the staged-bytes-only invariant instead of the old restage behavior.
1656bd8 to
3af07a4
Compare
|
Codex review: blocked before merge. Reviewed September 3, 2026, 9:18 PM ET / September 4, 2026, 01:18 UTC. ClawSweeper reviewWhat this changesThe PR formats partially staged, format-eligible files from their staged blobs and updates only Git’s index, preserving unstaged working-tree bytes. Regression provenancePossible regression — suspected (reviewed change). No predecessor PR is attributed. Merge readiness⛔ Blocked before merge - 1 item remains No blocking correctness finding remains. The prior literal-pathspec finding is fixed on the exact head, and current main still has the broad restage behavior this PR repairs; the member-authored PR should remain open for normal exact-head CI and maintainer handling. Priority: P2 Review scores
Verification
How this fits togetherThe pre-commit hook receives staged paths, filters files eligible for formatting, then prepares Git’s index for the commit. The new branch separates clean worktree files from files with unstaged changes so a commit cannot absorb the latter’s bytes. flowchart LR
A[Staged paths] --> B[Format eligibility filter]
B --> C[Compare working tree to index]
C --> D[Fully staged files]
C --> E[Partially staged files]
D --> F[Format worktree and restage]
E --> G[Format staged blob and update index]
F --> H[Commit index]
G --> H
Before merge
Agent review detailsSecurityNone. PR surfaceTests +149, Other +79. Total +228 across 4 files. View PR surface stats
Review metrics
Merge-risk optionsMaintainer options:
Technical reviewBest possible solution: Keep the separate index-only branch: no net-neutral restage-based shape can format a partial staged snapshot without reading or overwriting unstaged working-tree bytes. Do we have a high-confidence way to reproduce the issue? Yes in source: stage a format-eligible file, modify it without staging the change, and commit through the hook; current main’s broad Is this the best way to solve the issue? Yes. The repair removes the broad restage at the index-owning hook and uses Git’s documented working-tree/index comparison plus explicit blob/index interfaces; a stash or downstream guard would leave the owner boundary unsafe. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 3c0eb9fd822f. LabelsLabel changes:
Label justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (1 earlier review cycle)
|
Enumerated staged filenames re-enter Git as pathspecs in the new partial-state diff, staged-entry lookup, and restage add. Production runs under the guard's GIT_LITERAL_PATHSPECS env, but the script now pins --literal-pathspecs itself so bracket or ":(...)" magic filenames cannot misclassify partial staging when invoked without that env. Regression covers direct invocation with magic names; it fails without the flags.
|
Addressed the ClawSweeper review at head e7d77cf:
Suites: 71/71 at this head; Codex autoreview scoped-clean on the full branch diff. |
|
Merged via squash.
|
* origin/main: fix(git-hooks): keep unstaged bytes out of pre-commit formatting (#137723) test(gateway): drain session tool fixture cleanup (#137743) fix: avoid duplicate errors when a desktop disconnects (#137724) refactor: streamline temporary mention retention (#137714) fix(gateway): prevent hosted lifecycle crashes and self-waits (#137456) test(daemon): make managerless port fixture hermetic (#137692)
…nclaw#137723) * fix(git-hooks): keep unstaged bytes out of pre-commit formatting format-staged.sh formatted working-tree files and then git-added the complete working-tree contents of every staged file, so partial staging (hunk-level git apply --cached) silently committed unstaged work, and dirty non-format files were restaged wholesale. Fully staged files keep the fast batch oxfmt --write + restage path, now scoped to format-eligible files only. Partially staged files are formatted index-only: the staged blob goes through oxfmt --stdin-filepath (same repo config and ignorePatterns as batch mode, verified live), then git hash-object --no-filters and git update-index --index-info, preserving the staged mode and never touching working-tree bytes. Non-format staged files are no longer restaged at all; staged deletions, unstaged working-tree deletions, symlinks, and executable modes are preserved, and a formatter that exits 0 with empty output for nonempty input fails the hook instead of staging an emptied file. Security scans (pre/post-format literal scan), filename protections, and the sequencer early-exit are unchanged. Regression tests fail on the pre-fix script; the guard/boundary suites now protect the staged-bytes-only invariant instead of the old restage behavior. * fix(git-hooks): pin literal pathspecs in formatter Git lookups Enumerated staged filenames re-enter Git as pathspecs in the new partial-state diff, staged-entry lookup, and restage add. Production runs under the guard's GIT_LITERAL_PATHSPECS env, but the script now pins --literal-pathspecs itself so bracket or ":(...)" magic filenames cannot misclassify partial staging when invoked without that env. Regression covers direct invocation with magic names; it fails without the flags.
What Problem This Solves
Fixes an issue where committing with partial staging (hunk-level
git apply --cached/git add -p) would silently include unstaged working-tree bytes in the commit. The pre-commit formatter selected staged files from the index, formatted the working-tree copies, and thengit added the complete working-tree contents of every staged non-ignored file — including files not eligible for formatting at all. Deliberately-unstaged hunks (observed live: an unstaged import hunk during a Vitest fixture commit), unstaged edits to staged.txt/.jsonfiles, and even unstaged working-tree deletions were committed without the author asking for them.Why This Change Was Made
Partial staging is an ordinary Git workflow; a formatting hook must never widen the commit beyond what the author staged. The fix repairs the formatter at its index owner:
oxfmt --writeplus restage — now scoped to format-eligible files only.oxfmt --stdin-filepath=<path>(verified live to apply the same repo config andignorePatternsas batch mode), thengit hash-object --no-filtersand onegit update-index --index-infocall. Working-tree bytes are never touched; staged file modes are preserved; symlinks and conflict stages are skipped.set -u(latent pre-existing hazard).User Impact
Maintainers and contributors using partial staging get exactly their staged hunks committed (formatted), with unstaged work preserved byte-for-byte in the working tree. Fully-staged commits behave and perform exactly as before. Unstaged working-tree deletions of a staged file no longer silently commit the deletion.
Evidence
/bin/bash3.2.57.test/git-hooks-pre-commit.test.tsandtest/git-hooks-pre-commit-boundaries.test.ts: 70/70 pass. Nine new/rewritten regression tests fail on the pre-fix script for the intended reasons (verified by temporarily restoring the old script).node scripts/check-changed.mjs -- <touched files>exit 0; Codex autoreview scoped-clean on this exact diff.scripts/pre-commit/format-staged.sh; the growth is the index-only formatting path — the old 6-line tail was the bug). Tests: +180/-47.