Add a pre-commit hook
This MR adds a pre-commit hook.
The point of this hook is to avoid pushing commits that will trigger silly
CI failures, to save CI runs and have a faster feedback loop (by having
local feedback as opposed to CI feedback). To be fast the hook is incomplete: it doesn't check everything that gitlab CI will check; but it is correct: what the hook checks is required
for gitlab CI to pass. The hook must be fast so that git commit
stays fast.
This hook:
- executes python tests of staged
*.py
test files The point is to execute a subset ofpytest
that is required for a commit to pass CI. This is most useful when working on the python tests themselves. - lints and typechecks staged
*.py
files. - calls ocamlformat on staged
*.ml
,*.mli
files in a way faster thanmake fmt
. If staged files are being inspected (i.e. if--unstaged
is not passed, see below), formatted files are added before creating the commit; hereby autoformatting the files upon committing.
The first two points are blocking: if a pytest
call fails the hook returns a non-zero exit code, forbidding to commit. The same goes for *.py
files that do not pass linting. The third point is not blocking, because the hook does not try to format a partially staged file (because the call to ocamlformat
doesn't distinguish between staged and unstaged parts of the file). It seemed overkill to forbid a commit because some unstaged parts are not formatted.
The hook is installed as follows: ln -sr tests_python/pre_commit.py .git/hooks/pre-commit
(rm .git/hooks/pre-commit
if you have a hook installed already)
You can call the hook manually on modified but not yet staged files to make sure an upcoming call to git commit
will succeed using the --unstaged
flag: ./tests_python/scripts/pre_commit.py --unstaged
.
You can pass --lint-only
to avoid calling pytest
, so that the hook
is always fast. In this case, install it as follows:
cd .git/hooks
echo '#!/usr/bin/env bash' > pre-commit
echo './tests_python/scripts/pre_commit.py --lint-only "$@"' >> pre-commit
chmod +x pre-commit
I modified the documentation to mention the hook although I'm not really satisfied with the modifications. I feel this hook could be better advertised but I didn't find a better way.