`

git commit添加静态代码检查hook

阅读更多

静态代码检查对于成熟的程序/程序员来说非常重要,保持代码风格一致,避免低级错误,而对于vim党,强制要求在commit前检查,如果有不符合规范的不允许提交代码,可以通过如下方法:

 

修改代码仓库根目录下.git/hook/pre-commit

#!/bin/bash
function python_style_check() {
    #check python code in a git repo
    root=`git rev-parse --show-toplevel`
    exit=0
    for file in `git diff --name-only --cached --diff-filter=ACMRTUXB HEAD|grep '\.py$'`; do
        flake8 --config=$root/.flake8 $root/$file
        if [ $? -ne 0 ]; then
            let exit=1
        fi
    done
    exit $exit
}
python_style_check

 附flake8的配置:

[flake8]
ignore = W291,W391,W601,E123,E124,E125,E126,E127,E128,E221,E225,E226,E261,E262,E265,E272,E302,E501,E502,E711,E712
show-source = true

 

0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics