Setup Pre-Commit Hooks
What This Sets Up
- A pre-commit hook that runs formatting, linting, type checking, and/or tests before each commit
- Uses the appropriate hook framework for the project's language/toolchain
Steps
1. Detect the project's language and toolchain
Explore the repo to determine:
- Primary language(s) — look at file extensions, build files, config files
- Package manager — cargo, go, pip/poetry/uv, npm/pnpm/yarn/bun, mix, gem, etc.
- Existing formatter/linter — check for config files (.prettierrc, rustfmt.toml, .golangci.yml, pyproject.toml, .rubocop.yml, etc.)
- Existing test runner — check for test scripts, test directories, test config
- Existing type checker — mypy, pyright, tsc, etc.
2. Choose hook framework
Pick the appropriate approach based on what the project already uses:
- If the project already has a hook framework (husky, pre-commit, lefthook, etc.) — use it
- Otherwise, prefer the simplest option for the ecosystem:
- Python/polyglot: pre-commit framework
- JS/TS: Husky + lint-staged
- Rust: cargo-husky or a simple git hook script
- Go: a simple git hook script or lefthook
- Other: a direct
.git/hooks/pre-commit script
3. Install and configure
Install the chosen framework and configure it to run:
- Formatter on staged files (fast, staged-only)
- Linter if available (fast, staged-only where possible)
- Type checker if applicable (full project check)
- Tests if a test script exists (full suite or relevant subset)
Order matters — run fast checks first so failures are caught early.
4. Verify
5. Commit
Stage all changed/created files and commit. This commit will itself run through the new hooks — a good smoke test.
Notes
- Always check what's already in place before installing anything new
- Prefer running formatters on staged files only (faster feedback)
- Type checkers and test suites typically need the full project, not just staged files
- If the project has no formatter, linter, or tests, tell the user what's missing rather than installing defaults
1---2name: setup-pre-commit3description: Set up pre-commit hooks for formatting, linting, type checking, and tests in the current repo. Detects the project's language and toolchain to choose the right hook framework. Use when user wants to add pre-commit hooks, set up commit-time checks, or add formatting/linting/testing on commit.4---56# Setup Pre-Commit Hooks78## What This Sets Up910- A **pre-commit hook** that runs formatting, linting, type checking, and/or tests before each commit11- Uses the appropriate hook framework for the project's language/toolchain1213## Steps1415### 1. Detect the project's language and toolchain1617Explore the repo to determine:1819- **Primary language(s)** — look at file extensions, build files, config files20- **Package manager** — cargo, go, pip/poetry/uv, npm/pnpm/yarn/bun, mix, gem, etc.21- **Existing formatter/linter** — check for config files (.prettierrc, rustfmt.toml, .golangci.yml, pyproject.toml, .rubocop.yml, etc.)22- **Existing test runner** — check for test scripts, test directories, test config23- **Existing type checker** — mypy, pyright, tsc, etc.2425### 2. Choose hook framework2627Pick the appropriate approach based on what the project already uses:2829- **If the project already has a hook framework** (husky, pre-commit, lefthook, etc.) — use it30- **Otherwise, prefer the simplest option for the ecosystem:**31 - Python/polyglot: [pre-commit](https://pre-commit.com/) framework32 - JS/TS: Husky + lint-staged33 - Rust: cargo-husky or a simple git hook script34 - Go: a simple git hook script or lefthook35 - Other: a direct `.git/hooks/pre-commit` script3637### 3. Install and configure3839Install the chosen framework and configure it to run:40411. **Formatter** on staged files (fast, staged-only)422. **Linter** if available (fast, staged-only where possible)433. **Type checker** if applicable (full project check)444. **Tests** if a test script exists (full suite or relevant subset)4546Order matters — run fast checks first so failures are caught early.4748### 4. Verify4950- [ ] Hook file exists and is executable51- [ ] Run the hook manually to verify it works52- [ ] Confirm each check runs in the right order5354### 5. Commit5556Stage all changed/created files and commit. This commit will itself run through the new hooks — a good smoke test.5758## Notes5960- Always check what's already in place before installing anything new61- Prefer running formatters on staged files only (faster feedback)62- Type checkers and test suites typically need the full project, not just staged files63- If the project has no formatter, linter, or tests, tell the user what's missing rather than installing defaults