1.pylint 安装
pip install pylint
使用, 测试代码:
(venv) debby@DEBBYSZHANG-MB0 test_lint % pylint testfile.py
************* Module testfile
testfile.py:1:0: C0114: Missing module docstring (missing-module-docstring)
-----------------------------------
Your code has been rated at 0.00/10
修复问题:
再次运行,效果:
(venv) debby@DEBBYSZHANG-MB0 test_lint % pylint testfile.py
--------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 0.00/10, +10.00)
2.pre-commit 安装
pip install pre-commit
创建文件.pre-commit-config.yaml,添加如下内容:
default_stages: [commit]
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.1.0
hooks:
- id: check-case-conflict
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.4.0
hooks:
- id: flake8
测试代码:
效果:
修改:
再次检查:
配置commit-msg(可选,也可以在项目根目录创建一个文件,然后软链接过去,这样项目组成员就可以统一)
mv .git/hooks/commit-msg.sample .git/hooks/commit-msg
添加配置:
vi .git/hooks/commit-msg
#!/bin/sh
# 用 `` 可以将命令的输出结果赋值给变量
# 获取当前提交的 commit msg
commit_msg=`cat $1`
# 获取用户 email
email=`git config user.email`
msg_re="^(feat|fix|docs|style|refactor|perf|test|workflow|build|ci|chore|release|workflow)(\(.+\))?: .{1,100}"
if [[ ! $commit_msg =~ $msg_re ]]
then
echo "\n\033[31m 不合法的 commit 消息提交格式,请使用正确的格式:\
\nfeat: add comments\
\nfix: handle events on blur (close #28)\
\n详情请查看 git commit 提交规范:https://github.com/woai3c/Front-end-articles/blob/master/git%20commit%20style.md \033[0m"
# 异常退出
exit 1
else
echo "\033[32m commit success! \033[0m"
fi
效果:
参考:https://www.jianshu.com/p/eba337fe0828