我尝试在一个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
型
现在我正在测试,这两个都不起作用。有人有什么想法吗?
4条答案
按热度按时间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。
x9ybnkn62#
你的问题有两个部分:
1.仅在我的git > diff中更改的文件上在预提交钩子中运行flake8
为了只在diff文件上运行
flake8
(已暂存),我将.git/hooks/pre-commit
脚本修改为:字符串
我使用的是
export PATH=/usr/local/bin:$PATH
,因为我通常从sourceTree提交,它不会选择flake8所在的路径--staged
选项仅允许拾取暂存区域中的文件第二部分:
2.排除配置文件中的文件
你可以在仓库根目录下创建一个
.flake8
文件来解决这个问题。我的.flake8
文件看起来像这样:型
希望这对你有帮助。
jobtbby33#
如果您想在提交前使用
flake8
检查文件,只需使用字符串
Ref:https://flake8.pycqa.org/en/latest/user/using-hooks.html
y4ekin9u4#
如果您打算使用
flake8
作为预提交钩子,this article将指导您如何编写一个胶水脚本,该脚本将分析结果过滤为仅在修改的行上的结果。