Add ruff auto-format PreToolUse hook for git commits #79

Closed
opened 2026-03-13 19:16:34 +00:00 by forgejo_admin · 0 comments
Contributor

Lineage

plan-pal-e-platform → Phase 6 (CI Hardening) — prerequisite fix

Repo

forgejo_admin/claude-custom

User Story

As a platform operator
I want Python files auto-formatted before every git commit
So that CI never fails on ruff format --check again

Context

Ruff formatting failures are the #1 cause of CI failures platform-wide. Dev agents write Python code but don't run ruff format before committing. The CI test step runs ruff format --check . and fails, blocking image builds. With Woodpecker logs broken, we can't even see the failure — we just get silent deploy failures.

This has caused manual build+push workarounds for the last 25+ pipelines across multiple repos.

File Targets

Files to create:

  • hooks/auto-ruff-format.sh — PreToolUse hook that auto-formats staged .py files before git commit

Files to modify:

  • settings.json — register the new hook under PreToolUse → Bash matcher

Acceptance Criteria

  • Hook detects git commit in Bash commands
  • Hook finds staged .py files (git diff --cached --name-only --diff-filter=d -- '*.py')
  • Hook runs ruff format on staged .py files (skips gracefully if ruff not found)
  • Hook re-stages formatted files (git add)
  • Hook exits 0 (allows the commit to proceed)
  • Hook handles CWD correctly (respects cd in commands, like block-main-commits.sh)
  • Hook is registered in settings.json under PreToolUse → Bash
  • Hook does NOT block/deny — it silently fixes

Test Expectations

  • Manual: stage an unformatted .py file, run git commit, verify file is formatted in the resulting commit

Constraints

  • Follow existing hook patterns (block-main-commits.sh for CWD resolution, JSON stdin parsing)
  • Check for ruff in: PATH, local .venv/bin/ruff, venv/bin/ruff
  • Skip gracefully if ruff is not available (exit 0, do nothing)
  • Must work in worktrees (dev agents use isolation: worktree)

Checklist

  • PR opened
  • No unrelated changes
  • Closes #21 in PR body
  • pal-e-platform — CI failures caused by unformatted Python
  • pal-e-agency — agent operating system improvement
### Lineage `plan-pal-e-platform` → Phase 6 (CI Hardening) — prerequisite fix ### Repo `forgejo_admin/claude-custom` ### User Story As a platform operator I want Python files auto-formatted before every git commit So that CI never fails on `ruff format --check` again ### Context Ruff formatting failures are the #1 cause of CI failures platform-wide. Dev agents write Python code but don't run `ruff format` before committing. The CI test step runs `ruff format --check .` and fails, blocking image builds. With Woodpecker logs broken, we can't even see the failure — we just get silent deploy failures. This has caused manual build+push workarounds for the last 25+ pipelines across multiple repos. ### File Targets Files to create: - `hooks/auto-ruff-format.sh` — PreToolUse hook that auto-formats staged .py files before git commit Files to modify: - `settings.json` — register the new hook under PreToolUse → Bash matcher ### Acceptance Criteria - [ ] Hook detects `git commit` in Bash commands - [ ] Hook finds staged .py files (git diff --cached --name-only --diff-filter=d -- '*.py') - [ ] Hook runs `ruff format` on staged .py files (skips gracefully if ruff not found) - [ ] Hook re-stages formatted files (git add) - [ ] Hook exits 0 (allows the commit to proceed) - [ ] Hook handles CWD correctly (respects `cd` in commands, like block-main-commits.sh) - [ ] Hook is registered in settings.json under PreToolUse → Bash - [ ] Hook does NOT block/deny — it silently fixes ### Test Expectations - Manual: stage an unformatted .py file, run git commit, verify file is formatted in the resulting commit ### Constraints - Follow existing hook patterns (block-main-commits.sh for CWD resolution, JSON stdin parsing) - Check for ruff in: PATH, local .venv/bin/ruff, venv/bin/ruff - Skip gracefully if ruff is not available (exit 0, do nothing) - Must work in worktrees (dev agents use isolation: worktree) ### Checklist - [ ] PR opened - [ ] No unrelated changes - [ ] `Closes #21` in PR body ### Related - `pal-e-platform` — CI failures caused by unformatted Python - `pal-e-agency` — agent operating system improvement
forgejo_admin 2026-03-13 19:24:29 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ldraney/claude-custom#79
No description provided.