GitHookEm is a Python-based tool designed to be a starter kit for making your own custom hooks.
GitHookEm
provides a GitValidator
(example usage here) class that allows for easy extension of custom hooks.
Its interface just needs the base
method for the core logic of how to validate.
To make it more extensible with subclasses, I also have empty implementations of a pre
and post
method and a protocol
function to turn switches on and off for the three validation steps you wish to incorporate into potential subclasses.
Nothing else is needed from you; all the interfacing with git to get things working is done through the base class GitValidator
.
See the gitvalidator.py
file to see how everything works.
I use pre-commit
and gitlint
as central dependencies.
gitlint
provides a class CommitRule
(example usage here) similar to GitValidator
, but it seems to only work on the commit-msg
level.
pre-commit
works on any commit stage, but does not provide a class similar to GitValidator
or CommitRule
.
The two points above are my motivation for this repo.
If I am mistaken about this or if there is yet another repo out there that provides a base class that works on all stages like GitValidator
, then please let me know!
Using that as a dependency would be a better design decision, given that said hypothetical class is likely to be more mature than GitValidator
.
To use my hooks, just add the following to your .pre-commit-config.yaml file at REPO_PATH
repos:
- repo: https://github.com/tmasthay/GitHookEm
rev: main # The tag/commit to clone from
hooks:
- id: ban-super-secret
- id: HOOK_ID_2
.
.
.
REST OF YAML FILE
Currently support GitHookEm
ids are below.
id | Description | See for reference |
---|---|---|
ban-super-secret | Check for file with super_secret as a substring, in case .gitignore error occurred. |
ban_souper_secret.py |
GitHookEm uses pre-commit
, which is straightforward to setup. Directions are below.
pip install pre-commit
cd repo_root
pre-commit install
Now try git commit --allow-empty
and see how it works!
GitHookEm
also supports a gitlint
extension for commit message formatting, similar to commitizen
. To set this up, perform the following steps.
First, backup or remove your previous commit-msg
executable.
cd repo_root
mv .git/hooks/commit-msg .git/hooks/commit-msg-backup
Then put the gitlint
hidden files in the repository's root directory and then install the hook; this installation creates a file in .git/hooks/commit-msg
.
cp git_hook_em_path/.gitlint .
cp -r git_hook_em_path/.gitlint_rules .
gitlint install-hook
There are a lot of hooks on GitHub already out there that are easy to plugin once you are setup.
Try to avoid making custom hooks unless you absolutely need to; it just saves time and headaches.
See my .pre-commit-config.yaml
file for a starter kit.
Below is a table briefly describing the hooks external to this repo.
Name | Description | Github page |
---|---|---|
black | Opinionated Python style formatter | black GitHub |
pre-commit | Framework for managing git hooks | pre-commit GitHub and pre-commit hooks |
isort | Sorts Python imports alphabetically | isort GitHub |
flake8 | Python linter and style guide | flake8 GitHub |
yamllint | YAML linter | yamllint GitHub |