git flake8,仅用于diff和exclude

xytpbqjk  于 11个月前  发布在  Git
关注(0)|答案(4)|浏览(96)

我尝试在一个pre-commit hook中只对git diff中更改的文件运行flake 8,同时也排除了配置文件中的文件。

files=$(git diff --cached --name-only --diff-filter=ACM);
if flake8 --config=/path/to/config/flake8-hook.ini $files; then
    exit 1;
fi

字符串
我基本上想做的是:

flake8 --exclude=/foo/ /foo/stuff.py


然后让flake 8跳过我传入的文件,因为它在exclude变量中。
我还希望它排除不是.py文件的文件。例如:

flake8 example.js


现在我正在测试,这两个都不起作用。有人有什么想法吗?

vptzau2j

vptzau2j1#

如果你想要在未提交的和暂存的python文件上运行flake 8,那么这个一行程序就可以做到:
第一个月
git status列出更改的文件,grep用于python,用cut去掉开头的M / S位。
要使其成为预提交钩子,您需要添加shell hashbang:
#!/bin/sh flake8 $(git status -s | grep -E '\.py$' | cut -c 4-)
保存为.git/hooks/pre-commit,chmod +x。

x9ybnkn6

x9ybnkn62#

你的问题有两个部分:

1.仅在我的git > diff中更改的文件上在预提交钩子中运行flake8

为了只在diff文件上运行flake8(已暂存),我将.git/hooks/pre-commit脚本修改为:

#!/bin/sh
export PATH=/usr/local/bin:$PATH  
export files=$(git diff --staged --name-only HEAD)  
echo $files  
if [ $files != "" ]  
then  
    flake8 $files  
fi

字符串
我使用的是export PATH=/usr/local/bin:$PATH,因为我通常从sourceTree提交,它不会选择flake8所在的路径
--staged选项仅允许拾取暂存区域中的文件
第二部分:

2.排除配置文件中的文件

你可以在仓库根目录下创建一个.flake8文件来解决这个问题。我的.flake8文件看起来像这样:

[flake8]  
ignore = E501  
exclude =  
        .git,  
        docs/*,  
        tests/*,  
        build/*  
max-complexity = 16


希望这对你有帮助。

jobtbby3

jobtbby33#

如果您想在提交前使用flake8检查文件,只需使用

flake8 --install-hook git

字符串
Ref:https://flake8.pycqa.org/en/latest/user/using-hooks.html

y4ekin9u

y4ekin9u4#

如果您打算使用flake8作为预提交钩子,this article将指导您如何编写一个胶水脚本,该脚本将分析结果过滤为仅在修改的行上的结果。

相关问题