Global PEP8 Git pre-commit check hook

Many fixup Git commits are for forgetting to check project code standards like:

  • PEP8 compliance
  • type annotation checks
  • clang-format

before git push. Eliminate this git commit fixup clutter by making a simple Git pre-commit hook that applies to all repositories. This precommit hook also does a simple check for YaML syntax.

This procedure is for Unix-like systems (Linux, macOS) and native Windows. The script used is written in Python to make it OS agnostic.

Tell Git the directory for Git hooks:

git config --global core.hooksPath ~/.git/hooks

Create executable file ~/.git/hooks/pre-commit from this script.

This global pre-commit check hook does several things:

  1. get a list of Python file names that changed.
  2. check PEP8, mypy type hinting and that Python breakpoint debug statements are not present.
  3. check YaML syntax
  4. clears IPython notebook output cells
  5. Checks for trailing whitespaces in any code language

One can easily extend the concept of lint-checking for other programming languages.

These checks can be bypassed at any time for a commit by:

git commit -n

Override this global pre-commit check, substituting a per-repo .git/hooks/pre-commit by in that repo directory typing:

git config core.hooksPath .git/hooks

For example, a website made of Markdown files may wish to run a local-link check via Linkchecker-Markdown.