安装pre-commit
pip install pre-commit
配置文件指定使用的hooks
在git项目根目录创建如下内容的配置文件: .pre-commit-config.yaml 。
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: check-yaml
- id: check-json
- id: end-of-file-fixer
- id: trailing-whitespace
- id: requirements-txt-fixer
- repo: https://gitee.com/mirrors/black
rev: 19.3b0
hooks:
- id: black
args:
- --line-length=120 #设置代码行最大长度
- repo: https://gitee.com/mirrors_PyCQA/flake8.git
rev: 3.8.4
hooks:
- id: flake8
args:
- --max-line-length=120 #设置代码行最大长度
- --extend-ignore=E1,E203,E722,E402,F401,F403,F405 #设置忽略的警告代码
#- --max-complexity=12 # 设置代码复杂度
exclude: ^.*test/
additional_dependencies:
- "pep8-naming"
安装hooks
pre-commit install
这样在git commit 的时候会自动执行配置文件中的hooks,检查代码风格是否符合规范。也可以手动执行pre-commit run --all-files进行代码风格检查。