Add ruff lint check PreToolUse hook for git commits #81

Merged
forgejo_admin merged 1 commit from 78-add-ruff-pre-commit-hook into main 2026-03-13 19:41:58 +00:00
Contributor

Summary

Adds a PreToolUse hook that blocks git commit in Python repos when ruff check finds lint violations. This prevents CI breakage from committed lint issues (e.g., basketball-api #59 incident).

Changes

  • hooks/check-ruff-before-commit.sh (new) -- PreToolUse hook that fires on Bash commands containing git commit. Detects Python repos via pyproject.toml, resolves repo root (works in worktrees), runs ruff check, and blocks with a helpful error message showing violations and fix command. Fails open on any error (missing ruff, non-Python repo, non-git directory).
  • settings.json -- Wires the new hook into the Bash PreToolUse matcher list, after pypi-pr-checklist.sh.

Test Plan

  • Verified: non-commit commands pass through silently
  • Verified: commits in non-Python repos (no pyproject.toml) pass through
  • Verified: commits in clean Python repos pass through
  • Verified: commits in Python repos with ruff violations are blocked with violation count and details
  • Verified: chained commands (git add && git commit) are detected correctly
  • Verified: settings.json is valid JSON after edit

Review Checklist

  • Hook follows existing pattern (block-main-commits.sh style)
  • Fails open via trap 'exit 0' ERR
  • Works in agent worktrees (uses git rev-parse --show-toplevel)
  • No modifications to existing hooks
  • Only two files changed (new hook + settings.json wiring)
  • Closes #78
  • basketball-api #59 -- the incident that motivated this
  • pal-e-agency -- project this belongs to
## Summary Adds a PreToolUse hook that blocks `git commit` in Python repos when `ruff check` finds lint violations. This prevents CI breakage from committed lint issues (e.g., basketball-api #59 incident). ## Changes - **`hooks/check-ruff-before-commit.sh`** (new) -- PreToolUse hook that fires on Bash commands containing `git commit`. Detects Python repos via `pyproject.toml`, resolves repo root (works in worktrees), runs `ruff check`, and blocks with a helpful error message showing violations and fix command. Fails open on any error (missing ruff, non-Python repo, non-git directory). - **`settings.json`** -- Wires the new hook into the Bash PreToolUse matcher list, after `pypi-pr-checklist.sh`. ## Test Plan - Verified: non-commit commands pass through silently - Verified: commits in non-Python repos (no pyproject.toml) pass through - Verified: commits in clean Python repos pass through - Verified: commits in Python repos with ruff violations are blocked with violation count and details - Verified: chained commands (`git add && git commit`) are detected correctly - Verified: settings.json is valid JSON after edit ## Review Checklist - [x] Hook follows existing pattern (block-main-commits.sh style) - [x] Fails open via `trap 'exit 0' ERR` - [x] Works in agent worktrees (uses `git rev-parse --show-toplevel`) - [x] No modifications to existing hooks - [x] Only two files changed (new hook + settings.json wiring) ## Related - Closes #78 - `basketball-api #59` -- the incident that motivated this - `pal-e-agency` -- project this belongs to
The opt-out check was only looking at $cwd and $git_dir, but when
Claude Code passes a different cwd than the file's repo root, the
.claude-no-enforce file wasn't found. Now also resolves the repo
root from the file path being edited.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Adds a PreToolUse hook that blocks git commits in Python repos when
ruff check finds lint violations. Detects Python repos via pyproject.toml,
resolves repo root (works in worktrees), and fails open on any error
(missing ruff, non-Python repo, etc). Wired into settings.json Bash
matcher after existing hooks.

Closes #78

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
forgejo_admin force-pushed 78-add-ruff-pre-commit-hook from 22713a943a to a24086b754 2026-03-13 19:21:41 +00:00 Compare
forgejo_admin deleted branch 78-add-ruff-pre-commit-hook 2026-03-13 19:41:58 +00:00
Sign in to join this conversation.
No description provided.