A personal collection of pre-commit hooks that can makes my life easier :)
I created a blog post Why You Need To Stop Using Git-Hooks, if you would like to read more about pre-commit.
Add this to your .pre-commit-config.yaml
- repo: https://github.com/mmphego/pre-commit-hooks
rev: master # Use the ref you want to point at
hooks:
- id: flake8
- id: black
- id: pytest
On the root directory, you will find a script: setup_hooks.sh
and the script will:
- Find all directories under the
/home
which contain any.git
directory - Install the hooks into them
- Sets
init.templateDir
to point totemplates/git-commit-template.txt
# clone this repository and install pre-commit hooks
git clone git@github.com:mmphego/pre-commit-hooks.git
# change directory
cd pre-commit-hooks.git
# [Blind Install]
# Install pre-commit package and current hooks across all packages in your /home
./setup_hooks.sh install
# [Self-Aware install]
# Install pre-commit package
pip install pre-commit
# verify and modify `.pre-commit-config.yaml` for your python enviroment (default Python3.6)
vim .pre-commit-config.yaml
# Install pre-commit to your repository
pre-commit install
Skip the rule checker pytest
with the following command:
SKIP=pytest pre-commit run --all-files
If you want to "skip", "ignore" or "add" the git hook rule, trying to modify pre-commit config file and take off the checker id in .pre-commit-config.yaml
and run pre-commit install
again.
Use git commit
command to run the hooks and commit staged files.
Use pre-commit run --all
to run the hooks without commiting.
Use pre-commit autoupdate
to update the repo rev
if you experience any problems running the hooks.
Black
: Formats your staged files with Black formatter (black should be installed and configured in your project).Flake8
: Runsflake8
linter to find any problems with file formatting according to specified rules flake8.- autopep8: Automatically formats Python code to conform to the PEP 8 style guide with autopep8
Pytest
: Runspytest
with flags-sv
. It means that tests will be run with no capture and verbose.shellcheck
: Runs shellcheck which lints shell/bash scripts.isort
: Automatically sort Python imports.pydocstyle
: Static analysis tool for checking compliance with Python docstring conventions.- and many more - checkout the
.pre-commit-config.yaml
files.
- Be careful using different linters / formatters at the same time (like
black
,autopep8
and flake8`). They may have different conflicts so in order to use them together you should solve them first or have a configuration file handy.
Find more about pre-commit hooks here.
- Use
black
to autoformat - Use
flake8
to run static analysis tests (black doesn't fix everything) - Have your CI separated into 2 pipelines
- Run
flake8
andpytest
- To publish
- Run
- Have CI tests pass to be able to merge to master