f11b645andbde9f7fswept check-manifests and skill-frontmatter references left over frome647f14andc8a7c9e, but missed two worked examples in this vendored pre-commit research doc: a "Validate a generated file" hook naming the deleted scripts/check-manifests.sh, and a "SKILL.md frontmatter validation (inline bash, as used in this repo)" example for the skill-frontmatter hook, folded into skill-size-check.sh inc8a7c9e. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YR2CjVumUbEGWcMikcoXBD
3.7 KiB
3.7 KiB
topic, source_keys
| topic | source_keys | |||
|---|---|---|---|---|
| examples |
|
Starter config
pre-commit sample-config > .pre-commit-config.yaml
pre-commit validate-config
pre-commit install
pre-commit run --all-files
Common config patterns
File hygiene only
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
- id: check-yaml
- id: check-json
- id: check-toml
- id: check-merge-conflict
- id: detect-private-key
With auto-formatting
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
- id: pretty-format-json
args: [--autofix]
- id: mixed-line-ending
args: [--fix=lf]
With branch protection and secret scanning
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: detect-private-key
- id: detect-aws-credentials
- id: no-commit-to-branch
args: [--branch, main, --branch, master]
- id: check-added-large-files
args: [--maxkb=500]
Multi-stage config (pre-commit + pre-push + commit-msg)
default_install_hook_types: [pre-commit, pre-push, commit-msg]
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: end-of-file-fixer
stages: [pre-commit]
- id: check-yaml
stages: [pre-commit]
- repo: https://github.com/compilerla/conventional-pre-commit
rev: v2.4.0
hooks:
- id: conventional-pre-commit
stages: [commit-msg]
- repo: local
hooks:
- id: run-tests
name: Run test suite
entry: bash tests/run-tests.sh
language: unsupported_script
pass_filenames: false
always_run: true
stages: [pre-push]
Kubernetes-style YAML (needs --unsafe for custom tags)
- id: check-yaml
args: ['--unsafe']
exclude: ^helm/templates/
Monorepo with subdirectory scope
- repo: local
hooks:
- id: validate-frontend
name: Validate frontend
entry: pre-commit hazmat cd frontend npm run lint --
language: unsupported
files: ^frontend/
pass_filenames: false
Testing and validation workflow
# After writing a new config:
pre-commit validate-config
# Test all hooks against entire repo:
pre-commit run --all-files
# Test a single hook:
pre-commit run check-yaml --all-files
# Test a hook repo before adding it to config:
pre-commit try-repo https://github.com/psf/black --all-files
# Update all rev pins to latest tags:
pre-commit autoupdate
# Freeze revs to commit SHAs for reproducibility:
pre-commit autoupdate --freeze
# Skip a hook for one commit:
SKIP=check-yaml git commit -m "add yaml with custom tags"
# Run manually against a diff (CI):
pre-commit run --from-ref origin/main --to-ref HEAD
Local hook patterns
Forbidden file guard (cheapest possible hook)
- repo: local
hooks:
- id: no-env-files
name: No .env files
entry: .env files must not be committed — use environment variables
language: fail
files: \.env(\..+)?$
Run full test suite pre-push
- repo: local
hooks:
- id: run-tests
name: Run test suite
entry: bash tests/run-tests.sh
language: unsupported_script
pass_filenames: false
always_run: true
stages: [pre-push]
Adding meta-validation
Add after all other repos to catch dead hooks/excludes:
- repo: meta
hooks:
- id: check-hooks-apply
- id: check-useless-excludes