Pre-commit hooks can help to keep your source code consistent and discover broken code before it makes it into the repository. This lightning talk describes pre-commit hooks that can be helpful when developing with Python, especially when using the Django framework. It also provides consistent example configurations for hooks that have conflicting defaults.
2. What is pre-commit?
●
A framework to manage git pre-commit hooks.
●
To perform quality check on code before accepting a
commit.
●
Many handy hooks available through it.
●
Helps you catch many (often minor issues) before they make
it into the repository.
3. How to get pre-commit
●
Visit https://pre-commit.com/
●
Various methods to install:
– Plain Python: pip install pre-commit
– Anaconda: conda install -c conda-forge pre-commit
– Ubuntu: sudo apt-get install pre-commit
– MacOS: brew install pre-commit
4. Configuration
●
Add a file “.pre-commit-config”
●
Add the github repositories and hooks you want to run before each commit.
●
Example:
●
Run “pre-commit install”
●
Hooks are checked on
“git commit ...” and commit in IDE
●
Manually run hooks using
“pre-commit run --all-files”
exclude: "^.idea"
repos:
- repo: git://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: mixed-line-ending
args: ["--fix=auto"]
5. Configuration
●
Add a file “.pre-commit-config”
●
Add the github repositories and hooks you want to run
before each commit.
●
Example: exclude: "^.idea"
repos:
- repo: git://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: mixed-line-ending
args: ["--fix=auto"]
Regex for files and
folders not to check
Source code repository
and version of hooks
to use
Hooks to use
Parameters for a
specific hook
6. Standard hooks – Text files
●
- repo: git://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
●
trailing-whitespace: remove white space at end of line in
text files
●
mixed-line-ending: detect or fix mixed line endings (e.g
developers on Windows and Unix working on same code)
●
end-of-file-fixer: Ensures that text files end with a newline
character.
8. Standard hooks – Unwanted files
●
check-byte-order-marker: rejects text files starting with
UTF-8 BOM (as created by various Microsoft tools like
Notepad, causing many Unix tools to fail)
●
check-added-large-files: rejects large files; possibly specify
what is large: args: ["--maxkb=768"]
●
Check-merge-conflict: refuse to commit before all merge
conflicts are resolved
9. Standard hooks - Python
●
check-ast: check Python syntax
●
requirements-txt-fixer: sorts entries in requirements.txt
●
debug-statements: refuse to commit calls to breakpoints()
and imports of debugger
11. Pretty print Python
●
Using black (opinionated): https://github.com/psf/black
●
Configure using pyproject.toml
- repo: https://github.com/ambv/black
rev: stable
hooks:
- id: black
[tool.black]
line-length = 120
include = '.pyi?$'
exclude = '''
/(
.eggs
| .git
| venv
| build
| dist
)/
'''
12. Sort imports consistently
●
Using isort:
https://github.com/timothycrosley/isort
●
Configure using setup.cfg
●
Can be tricky to work with black
→ see example to the right
- repo: https://github.com/timothycrosley/isort
rev: 5.4.2
hooks:
- id: isort
[tool:isort]
# Use project line length.
line_length = 120
# Packages from own projetc
default_section = THIRDPARTY
known_first_party = mytool, niftysite
# Use same style as black.
force_grid_wrap=0
include_trailing_comma = True
multi_line_output=3
use_parentheses=True
# Skip virtual environments.
skip_glob = venv
14. Prettier – Pretty print various formats
for web development
●
Supports JavaScript, CSS, Markdown, HTML, GraphQL, YAML
and others by means of plugins
●
Does not work with Django HTML templates → exclude using
“.prettierignore” (uses same format as .gitignore)
- repo: https://github.com/prettier/prettier
rev: 2.0.5
hooks:
- id: prettier
**/coverage.xml
**/templates/**/*.html
15. Summary
●
Pre-commit is easy to install and configure.
●
Solves minor minor issues arising when multiple developers
work on the same code.
●
Plenty of available hooks.