Everything to know about pre-commit
Jan 16, 2020··
2 min read
Markus Hofbauer
Everything to know about pre-commit
Automate whenever possible
- Guidelines
- Code format
- Static code analysis
- …
How to manage all these tools
- Shell scripts for every tool?
- Makefiles or similar frameworks?
- Git hooks calling the scripts?
Drawbacks
- Script/tool maintenance
- Tooling setup/dependencies
- Forgot to run the checks locally
pre-commit
A framework for managing and maintaining multi-language pre-commit hooks
Features
- Automatically setup all dependencies in virtual environments without root access required
- Manage all tools in a single configuration file
.pre-commit-config.yaml
Configuration
default_stages: [commit]
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- repo: https://github.com/psf/black
rev: 22.1.0
hooks:
- id: black
language_version: python3
args: [--quiet]
Usage
- Setup once:
pre-commit install
- Now
pre-commit
will run automatically ongit commit
Optional Usage
Manually run pre-commit
- All files:
pre-commit run --all-files
- Selected files:
pre-commit run --files <list of files>
- Selected hooks:
pre-commit run black
Output
pre-commit run --all-files
[INFO] Initializing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Initializing environment for https://github.com/psf/black.
[INFO] Installing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
[INFO] Installing environment for https://github.com/psf/black.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
Check Yaml...............................................................Passed
Fix End of Files.........................................................Passed
Trim Trailing Whitespace.................................................Failed
- hook id: trailing-whitespace
- exit code: 1
Files were modified by this hook. Additional output:
Fixing sample.py
black....................................................................Passed