This is a framework allowing execution of pre-commit hooks.
It means it will act before a commit is done.
This is perfect to react after your code writing, but before your code commit.
Here are the hooks of interest for me:
nbdev_clean will force cleaning of metadata in notebooks, here is a nice explanation Git-Friendly Jupyter
black-jupyter– will make your python code in notebooks PEP compliant
black– will make your python code PEP compliant
ruff which is a way to integrate black+isort+flake8 in one step
check-added-large-files with 90MB will ensure you don’t push large files to gitlab (internal limit of 100 MB and it is tedious to clean)
install setup
1st you need to install pre_commit
pip install pre-commit
2nd you need a config file .pre-commit-config.yaml
3rd you need to activate pre-commit
pre-commit install
Here is an example of .pre-commit-config.yaml
!cat ../files/pre-commit-config.yaml
repos:
- repo: https://github.com/fastai/nbdev
rev: 2.3.13
hooks:
- id: nbdev_clean
# Using this mirror lets us use mypyc-compiled black, which is about 2x faster
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 24.2.0
hooks:
- id: black
language_version: python3.10
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 24.2.0
hooks:
- id: black-jupyter
language_version: python3.10
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0 # Use the version you want
hooks:
- id: check-added-large-files
args: ["--maxkb=90000"]
how to use it
The magic happens when you run git commit
git commit -m'pre-commit setup'[INFO] Initializing environment for https://github.com/pre-commit/pre-commit-hooks.[INFO] Installing environment for https://github.com/fastai/nbdev.[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...[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...nbdev_clean..............................................................Passedblack-jupyter............................................................Failed- hook id: black-jupyter- files were modified by this hookreformatted nbs/index.ipynbAll done! ✨ 🍰 ✨1 file reformatted, 1 file left unchanged.
When a modif is done by a pre-hook, you have to re-add this modification in git.
This is a matter of repeating git addgit commit or to run git add -u && !!
recommit function
Instead of re-running git add -u && !!, I need an alias (e.g. recommit) because I would never remember this command.
If I want to apply my pre-commit rules to modified files in the working directory, I have just to invoke this script pre-staged.sh at this directory
#!/bin/bash -efiles="$(git ls-file -m)"echo"$files"echo"$files"|tr'\n''\0'|xargs-0 pre-commit run --filesecho"$files"|tr'\n''\0'|xargs-0 git add add it to your path and run w/o any argument.
test of check-added-large-files
# create a 100 MB filetruncate-s 100M big_filegit add big_filegit commit -m'test with big file'[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...nbdev_clean..............................................................Passedblack-jupyter........................................(no files to check)Skippedcheck for added large files..............................................Failed- hook id: check-added-large-files- exit code: 1big_file(102400 KB)exceeds 90000 KB.