🪝 Hooks
✨ Features
Advanced hook features and configuration patterns
Basic Commands
Run any shell command:
hooks:
pre-commit:
custom:
- command: "npm run lint"
description: "Run ESLint"Template Variables
Use variables in commands:
| Variable | Description | Example |
|---|---|---|
{staged_files} | Space-separated list of staged files | src/main.rs src/lib.rs |
{all_files} | All modified files | README.md src/main.rs |
{commit_msg_file} | Path to commit message file | .git/COMMIT_EDITMSG |
{changed_files} | Files changed in commit | src/app.js test/app.test.js |
hooks:
pre-commit:
custom:
- command: "eslint {staged_files} --fix"
description: "Fix ESLint issues"
glob: ["*.{js,jsx,ts,tsx}"]
stage_fixed: trueGlob Patterns
Only run commands on matching files:
hooks:
pre-commit:
custom:
- command: "black {staged_files}"
description: "Format Python files"
glob: ["*.py", "**/*.py"]
- command: "rustfmt {staged_files}"
description: "Format Rust files"
glob: ["*.rs"]Stage Fixed Files
Automatically stage files modified by the hook:
hooks:
pre-commit:
custom:
- command: "prettier --write {staged_files}"
description: "Format with Prettier"
glob: ["*.{js,json,md}"]
stage_fixed: true # Auto-stage formatted filesParallel Execution
Run multiple commands simultaneously:
hooks:
pre-commit:
parallel: true # Enable parallel execution
custom:
- command: "cargo fmt -- --check"
description: "Check Rust formatting"
- command: "cargo clippy"
description: "Run Clippy"
- command: "cargo test"
description: "Run tests"Conditional Execution
Only run when files change:
hooks:
post-checkout:
custom:
- command: "pnpm install"
description: "Install dependencies"
when: "{changed_files} contains package.json"Custom Messages
hooks:
pre-commit:
custom:
- command: "cargo test"
description: "Running tests"
success_message: "✅ All tests passed!"
failure_message: "❌ Tests failed. Fix before committing."Environment Variables
Hooks have access to Git environment variables:
GIT_AUTHOR_NAME- Commit author nameGIT_AUTHOR_EMAIL- Commit author emailGIT_COMMITTER_NAME- Committer nameGIT_COMMITTER_EMAIL- Committer email
Performance Tips
Use Parallel Execution
Enable parallel: true for independent checks:
hooks:
pre-commit:
parallel: true # Runs all commands simultaneouslyTarget Specific Files
Use glob patterns to avoid checking irrelevant files:
custom:
- command: "eslint {staged_files}"
glob: ["src/**/*.{js,jsx}"] # Only lint source filesCache Results
Some tools support caching:
custom:
- command: "eslint {staged_files} --cache"
description: "Lint with cache"