pre-commit framework for managing pre-commit hooksIn the last video, we wrote a script ourselves called check-code-quality.sh which thinly wrapped pylint, isort, flake8, etc.
Then we manually ran that script as a pre-commit hook.
There is a fancier way to do the same thing as this. It’s called pre-commit.
🚨 Confusing name alert! 🚨
pre-commit is not the same thing as pre-commit!!!
The pre-commit framework which you can find at this website: https://pre-commit.com/, is NOT the same thing as git pre-commit hooks which is the git concept that we explored in the last video.
The pre-commit framework is a CLI tool that makes it easy to install linting tools such as pylint and “install” them so that they run as a git pre-commit hook AKA it adds a .git/hooks/pre-commit file and executes linting tools of your choice.
The pre-commit framework/CLI is a Git pre-commit hook manager (and can also manage the pre-push, post-push, etc. git hooks as well)
pre-commitWe wrote this .pre-commit-config.yaml file to get started with pre-commit.
# See <https://pre-commit.com> for more information
# See <https://pre-commit.com/hooks.html> for more hooks
repos:
- repo: <https://github.com/pre-commit/pre-commit-hooks>
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
You can use pre-commit autoupdate to automatically bump all of the rev fields to the latest version
You can use pre-commit install to execute the pre-commit run ... command in the .git/hooks/pre-commit file.
This would make it so that executing pre-commit hooks will automatically happen when you run git commit ....
If you choose to run pre-commit install, you can always run git commit -m "..." --no-verify to skip running the pre-commit hooks for a particular commit.
That can be useful if you’re just trying to save your work at the end of the day and don’t have time to address all linting warnings.
Install pre-commit yourself (must be in a git repo), add the basic .pre-commit-config.yaml file that we have above ^^^, and run it against your code.
pip install pre-commit
# execute all hooks selected in the .pre-commit-config.yaml file against all files
# in your repo
pre-commit run --all-files