python后端项目编码规范检查——pre-commit的使用
1、安装pre-commit
python3 -m pip install pre-commit
2、在项目根目录下,创建一文件—— .pre-commit-config.yaml
文件,配置需要的验证规则:
default_stages: [commit]
repos:
- repo: https://github.com/yingzi113/pre-commit-hooks
rev: 5863e162f1bed1f63eeb716e77d622ff8e3d9af9
hooks:
- id: check-case-conflict
- repo: https://github.com/pre-commit/mirrors-autopep8
rev: v1.4.4
hooks:
- id: autopep8
args: [-i, --global-config=.flake8, -v]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.4.0
hooks:
- id: flake8
3、install pre-commit
进入Python虚拟环境,安装pre-commit
pre-commit install
4、每次在代码发生变动需要add并commit时,会执行该commit hooks,做代码规范检测,没通过的话,会自动更改格式,然后才会通过,最后commit进仓库。
git add .
git commit -m "update"
参考:
https://www.cnblogs.com/wangyingblock/p/11984463.html
以上。