technology

Pre-commit

This a simple tutorial of pre-commit

← All essays
Tags
Categories

Install pre-commit

pip install pre-commit

Create .pre-commit-config.yaml

pre-commit sample-config > .pre-commit-config.yaml

Add hooks

repos:
  - repo: https://github.com/timothycrosley/isort
    rev: 5.12.0
    hooks:
      - id: isort
        exclude: "(migrations|.cache)/"
  - repo: https://github.com/psf/black
    rev: 23.3.0
    hooks:
      - id: black
  - repo: https://github.com/pycqa/flake8
    rev: 6.0.0
    hooks:
      - id: flake8
        args: ["-j8", "--ignore=E402, E203, E501, F401"]
        additional_dependencies:
          [flake8-comprehensions>=3.2.2, flake8-builtins>=1.5.2]

Install hooks

pre-commit install

Run hooks

pre-commit run --all-files

Run hooks on staged files

pre-commit run --files $(git diff --name-only --cached)

Config

fielddescriptionrequired
idThe id of the hook.Yes
nameThe name of the hook.No
aliasAn alias for the hook.No
language_versionThe version of the language to use.No
filesThe files to run the hook on.No
excludeThe files to exclude from running the hook on.No
typesThe types of files to run the hook on.No
exclude_typesThe types of files to exclude from running the hook on.No
argsThe arguments to pass to the hook.No
stagesThe stages to run the hook on.No
additional_dependenciesThe additional dependencies to install for the hook.No
always_runWhether to run the hook even if the file is unmodified.No
verboseWhether to run the hook in verbose mode.No
log_fileThe file to log the hook output to.No