Installing pre-commit
pre-commit is a python program which can be installed with pip
Minimum requirements
- Python 3.7 and higher
Installation on RHEL8 (defaults to Python 3.6)
-
Install Python 3.9
sudo dnf install python39-3.9.7 -
Create a venv and activate it
/usr/bin/python3.9 -m venv ~/.venv/pre-commit . ~/.venv/pre-commit/bin/activate -
Install pre-commit
pip install --upgrade pip pip install pre-commit -
Set an alias or link to
~/.venv/pre-commit/bin/pre-committo use it
On other Linux distributions with python 3.7 and later, you can either create a virtual environment or install with pip install --user pre-commit
Using pre-commit
Configuration files
The config file for pre-commit is created in the root directory of the GitHub repository and is called .pre-commit-config.yml.
In case of ansible collections other config files may apply such as .ansible-lint and .yamllint.
The required config files should be created and maintained by the repo maintainers.
Hence they are automatically downloaded with git clone and git pull commands
Activate pre-commit
A repository maintainer activates pre-commit by running pre-commit install within a directory of the cloned repository
Deactivate pre-commit
run pre-commit uninstall
Useful commands
When you first install pre-commit on a repository and you have created your config file, you should run:
pre-commit run --all-files [--show-diff-on-failure]
The optional parameter --show-diff-on-failure prints the problematic code at the end of the run.