Notes

Introducing the pre-commit framework for managing pre-commit hooks

In the last video, we wrote a script ourselves called check-code-quality.sh which thinly wrapped pylint, isort, flake8, etc.

Then we manually ran that script as a pre-commit hook.

There is a fancier way to do the same thing as this. It’s called pre-commit.

🚨 Confusing name alert! 🚨

pre-commit is not the same thing as pre-commit!!!

The pre-commit framework which you can find at this website: https://pre-commit.com/, is NOT the same thing as git pre-commit hooks which is the git concept that we explored in the last video.

The pre-commit framework is a CLI tool that makes it easy to install linting tools such as pylint and “install” them so that they run as a git pre-commit hook AKA it adds a .git/hooks/pre-commit file and executes linting tools of your choice.

The pre-commit framework/CLI is a Git pre-commit hook manager (and can also manage the pre-push, post-push, etc. git hooks as well)

Using pre-commit

We wrote this .pre-commit-config.yaml file to get started with pre-commit.

# See <https://pre-commit.com> for more information
# See <https://pre-commit.com/hooks.html> for more hooks
repos:
  - repo: <https://github.com/pre-commit/pre-commit-hooks>
    rev: v4.4.0
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer
      - id: check-yaml
      - id: check-added-large-files

Assignments

Install pre-commit yourself (must be in a git repo), add the basic .pre-commit-config.yaml file that we have above ^^^, and run it against your code.

pip install pre-commit

# execute all hooks selected in the .pre-commit-config.yaml file against all files
# in your repo
pre-commit run --all-files

Resources / Links