既存のプロジェクトにpre-commitを適用する
from pre-commit
既存のプロジェクトにpre-commitを適用する
pre-commitをpipxを使ってインストールする。
code:sh
pipx install pre-commit
お望みのツールに.pre-commit-configのフックがあるか探す。
Black
https://black.readthedocs.io/en/stable/integrations/source_version_control.html
autoflake
https://github.com/PyCQA/autoflake/blob/main/.pre-commit-config.yaml
isort
https://pycqa.github.io/isort/docs/configuration/pre-commit.html
実際に書いてみたやつ
code:yaml
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: debug-statements
- repo: https://github.com/pycqa/autoflake
rev: v2.3.1
hooks:
- id: autoflake
args:
- --in-place
- --remove-all-unused-imports
- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 24.10.0
hooks:
- id: black
language_version: python3.12
args:
- --skip-magic-trailing-comma
フックを一括アップデート可能
code:sh
pre-commit autoupdate
全てのファイルにフックを適用する
isort等を含むlinterで--recursiveのオプションを付けて実行する必要はない!
code:sh
pre-commit run --all-files
特定のファイルにのみフックを適用する
code:sh
pre-commit run --files FILES