chore: pre-commit hook auto-fixes ruff violations #181
No reviewers
Labels
No Label
ai-changes-requested
ai-failed
ai-merged
ai-pr-opened
ai-reviewed
ai-reviewing
ai-reviewing
ai-working
bug
enhancement
evolution
performance
phase-0
phase-1a
phase-1b
phase-1c
phase-1d
security
tech-debt
todo
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: cal/paper-dynasty-database#181
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "chore/pre-commit-autofix"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Pre-commit hook now auto-fixes ruff violations before blocking, eliminating the lint-fail-recommit loop that caused 37 hook blocks in the last 2 weeks of sessions.
Changes
.githooks/pre-commit— runsruff check --fixfirst, re-stages fixed files, then checks for remaining unfixable issues.githooks/install-hooks.sh— helper script to configurecore.hooksPathTesting
Tested locally — ruff auto-fixable violations (unused imports, etc.) are now silently fixed and staged. Only truly unfixable issues block the commit.
AI Code Review
Files Reviewed
.githooks/pre-commit(added).githooks/install-hooks.sh(added)Findings
Correctness
[BLOCKING]
pre-commit:git addafterruff --fixcan silently include unstaged changesThe auto-fix + re-stage sequence has a correctness bug when a developer uses partial staging (
git add -porgit add <specific-lines>):foo.py, leaving other edits unstagedruff check --fixruns on the working tree file (which includes both staged and unstaged changes)git add foo.pyre-stages the entire file — including the previously-unstaged changesThe fix is to stash unstaged changes before running ruff, then restore them after:
This is the standard pattern for pre-commit hooks that modify files. Without it,
git add -pworkflows are unsafe with this hook enabled.Security
Style & Conventions
[MINOR]
install-hooks.sh: echo messages don't match new behavior (.githooks/install-hooks.sh, lines 27–30)The completion message still says:
This is stale — the hook now auto-fixes before blocking. Should read something like:
[MINOR] PR body /
install-hooks.sh: description sayscore.hooksPathbut script usescpThe PR body says install-hooks.sh is "a helper script to configure
core.hooksPath", but the script usescpto copy the hook into.git/hooks/. These are different mechanisms —core.hooksPathis a git config pointing to a directory (so future changes to.githooks/pre-commitare picked up automatically), whilecprequires re-running the install script after every update. Non-blocking, but the description is inaccurate and the design choice means contributors will silently run stale hooks after future updates.[MINOR]
pre-commit:xargswithout null delimiterecho "$STAGED_PY" | xargs ruff checkwill misbehave if any Python filename contains spaces (xargs splits on whitespace by default). Use-zpiping for correctness:Extremely unlikely to matter in this project, but worth fixing while the script is being written.
Suggestions
install-hooks.shto actually usecore.hooksPathas advertised — it's one line (git config core.hooksPath .githooks) and means contributors automatically get future hook updates without re-running the install script.REPO_ROOTguard inpre-commit(git rev-parse --show-toplevelis called without2>/dev/null) will print an error to stderr if somehow invoked outside a repo. Low risk but minor robustness issue.Verdict: REQUEST_CHANGES
The partial-staging correctness bug (ruff auto-fix + unconditional
git addsilently expanding a partial commit) is actionable and fixable. Thegit stash --keep-indexpattern is the standard solution. The other findings are minor. Overall the PR solves a real problem well — this is one targeted fix away from ready.Automated review by Claude PR Reviewer
Review Feedback Addressed
Pushed commit
bbb5689addressing all findings from the REQUEST_CHANGES review.Blocking fix
pre-commit:git stash --keep-indexguards the ruff fix + restageAdded a stash/pop wrapper around the auto-fix block so ruff only sees staged content when running. Without this,
ruff --fixoperated on the full working tree file and the unconditionalgit addwould silently fold in unstaged hunks — breakinggit add -pworkflows. The stash exit code is checked so we only pop if a stash actually occurred (no unstaged changes = no stash needed).Minor fixes
pre-commit: null-delimiter xargs — switchedgit diff --cachedto-zflag and allxargscalls toxargs -0, so filenames with spaces are handled correctly.install-hooks.sh: stale echo messages updated — now reads "Auto-fix ruff lint violations" / "Block commits only on truly unfixable issues" to match actual hook behaviour.Checkout
From your project repository, check out a new branch and test the changes.