|
| 1 | +# Pre-Commit Hooks Setup Guide |
| 2 | + |
| 3 | +## Installation |
| 4 | + |
| 5 | +### Recommended: Using uv |
| 6 | + |
| 7 | +[uv](https://github.com/astral-sh/uv) is recommended for Python dependency management. It provides dependency locking with package hashes (supply-chain protection), virtual environment management, and is 10-100x faster than pip. |
| 8 | + |
| 9 | +**Install uv:** |
| 10 | +```bash |
| 11 | +# macOS/Linux |
| 12 | +curl -LsSf https://astral.sh/uv/install.sh | sh |
| 13 | + |
| 14 | +# Windows |
| 15 | +powershell -c "irm https://astral.sh/uv/install.ps1 | iex" |
| 16 | + |
| 17 | +# Via pip |
| 18 | +pip install uv |
| 19 | +``` |
| 20 | + |
| 21 | +**First-time setup:** |
| 22 | +```bash |
| 23 | +uv init --bare # creates pyproject.toml |
| 24 | +uv add --dev pre-commit==4.6.0 # adds dependency, generates uv.lock |
| 25 | +source .venv/bin/activate # macOS/Linux (.venv\Scripts\activate on Windows) |
| 26 | +pre-commit install |
| 27 | +``` |
| 28 | + |
| 29 | +**Subsequent setup** (when `pyproject.toml` and `uv.lock` exist): |
| 30 | +```bash |
| 31 | +uv sync |
| 32 | +source .venv/bin/activate |
| 33 | +pre-commit install |
| 34 | +``` |
| 35 | + |
| 36 | +### Alternative: Using pip |
| 37 | + |
| 38 | +```bash |
| 39 | +pip install 'pre-commit==4.6.0' # pinned version (Golden Rule 15) |
| 40 | +pre-commit install |
| 41 | +``` |
| 42 | + |
| 43 | +Add to `requirements-dev.txt`: `pre-commit==4.6.0` |
| 44 | + |
| 45 | +## First-Time Setup |
| 46 | + |
| 47 | +Run on all files to catch existing issues: |
| 48 | +```bash |
| 49 | +pre-commit run --all-files |
| 50 | +``` |
| 51 | + |
| 52 | +Auto-fix hooks will modify files on first run. Stage and commit these separately: |
| 53 | +```bash |
| 54 | +git diff |
| 55 | +git add . |
| 56 | +git commit -m "Fix: Apply pre-commit auto-fixes" |
| 57 | +``` |
| 58 | + |
| 59 | +**Exclude fix commits from git blame:** |
| 60 | +```bash |
| 61 | +# Create .git-blame-ignore-revs with commit hashes |
| 62 | +git config blame.ignoreRevsFile .git-blame-ignore-revs |
| 63 | +``` |
| 64 | + |
| 65 | +See [git-blame docs](https://git-scm.com/docs/git-blame#Documentation/git-blame.txt---ignore-revs-filefile). |
| 66 | + |
| 67 | +## Usage |
| 68 | + |
| 69 | +**Automatic** (runs on `git commit`): |
| 70 | +```bash |
| 71 | +git add <files> |
| 72 | +git commit -m "Message" |
| 73 | +``` |
| 74 | + |
| 75 | +**Manual:** |
| 76 | +```bash |
| 77 | +pre-commit run # staged files only |
| 78 | +pre-commit run --all-files # entire repo |
| 79 | +pre-commit run --files path/to/file # specific files |
| 80 | +``` |
| 81 | + |
| 82 | +**Bypass (use sparingly):** |
| 83 | +```bash |
| 84 | +SKIP=hook-id git commit -m "Message" # skip one hook |
| 85 | +git commit --no-verify # NEVER use (Golden Rule 16) |
| 86 | +``` |
| 87 | + |
| 88 | +Rules: Agents never bypass hooks. Security hooks (gitleaks) never bypassable. |
| 89 | + |
| 90 | +## Troubleshooting |
| 91 | + |
| 92 | +**macOS timeout issues:** |
| 93 | +```bash |
| 94 | +brew install coreutils # provides gtimeout |
| 95 | +``` |
| 96 | + |
| 97 | +**Virtual environment not found:** |
| 98 | +```bash |
| 99 | +source .venv/bin/activate |
| 100 | +uv sync |
| 101 | +``` |
| 102 | + |
| 103 | +**Hooks not running:** |
| 104 | +```bash |
| 105 | +ls -la .git/hooks/pre-commit # verify installation |
| 106 | +pre-commit install # reinstall |
| 107 | +``` |
| 108 | + |
| 109 | +**Hook failures:** Read error messages and fix issues: |
| 110 | +- `go-build`: Fix compilation errors |
| 111 | +- `go-mod-tidy`: Run `go mod tidy` and stage go.mod/go.sum |
| 112 | +- `check-yaml`: Fix YAML syntax |
| 113 | + |
| 114 | +## CI Integration |
| 115 | + |
| 116 | +Pre-commit mirrors `ci/prow/lint`. CI is authoritative; pre-commit is developer convenience. All hooks run in CI with same config. |
| 117 | + |
| 118 | +If pre-commit passes but CI fails: `pre-commit autoupdate` |
| 119 | + |
| 120 | +## Resources |
| 121 | + |
| 122 | +- [Pre-Commit Documentation](https://pre-commit.com/) |
| 123 | +- [uv Documentation](https://github.com/astral-sh/uv) |
0 commit comments