Adding a new hook
Guidance for extending a2c-pre-commit-hooks without restructuring the repository.
Naming
- Hook ids are short (
check-changelog, nota2c-check-changelog); the repository name is the namespace. - Console script names match hook ids where possible (kebab-case in
pyproject.tomlentry points).
Implementation checklist
-
Pure logic — add a module under
src/a2c_pre_commit_hooks/with testable functions (no subprocess in core logic). -
CLI wrapper — add
src/a2c_pre_commit_hooks/hooks/<hook_name>.pywithmain()returning exit code0/1. -
Entry point — register in
pyproject.toml:[project.scripts]my-new-hook = "a2c_pre_commit_hooks.hooks.my_new_hook:main" -
Hook catalog — append to
.pre-commit-hooks.yaml:- id: my-new-hookname: Human-readable namedescription: One-line summary for pre-commit.comentry: my-new-hooklanguage: pythonpass_filenames: false # or true with files:/types: as needed -
Tests — add
tests/test_my_new_hook.pycovering success and failure paths; mock git I/O. -
Documentation — update
README.mdhook table,docs/usage.md, andCHANGELOG.md[Unreleased]. -
Dogfood — optionally add the hook to
.pre-commit-config.yaml(local repo or after tag).
Optional PyPI dependencies (example: validate)
When a hook needs an external package only for that hook, add a [project.optional-dependencies]
extra and reference it from .pre-commit-hooks.yaml:
[project.optional-dependencies]
validate = ["a2c-cli==0.2.1"]
- id: validate
additional_dependencies:
- ".[validate]"
Do not put hook-specific deps in top-level [project.dependencies] — that would install them
for check-changelog-only consumers too.
File layout example
src/a2c_pre_commit_hooks/
├── my_policy.py # pure logic
└── hooks/
└── my_new_hook.py # argparse + main()
tests/
└── test_my_new_hook.py
Versioning new hooks
- Patch — bug fix or message tweak to existing hook
- Minor — new hook or backward-compatible behavior extension
- Major — breaking change to hook arguments or failure semantics consumers rely on
Document behavior changes in CHANGELOG.md so consuming repos can pin rev confidently.
Consumer impact
New hooks are opt-in: existing .pre-commit-config.yaml files list explicit hook ids. Adding a
hook here does not affect consumers until they add the id under hooks:.
Review bar
Before merge:
- Unit tests pass (
pytest) - Ruff clean
- Hook entry in
.pre-commit-hooks.yamlmatchespyproject.tomlscript name - Usage docs include copy-paste
.pre-commit-config.yamlsnippet -
CHANGELOG.mdupdated when the change is release-relevant