我试图在预提交 Hook 中运行 flake8,仅针对我的 git diff 中更改的文件,同时排除我的配置文件中的文件。
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
然后让 flake8 跳过我传入的文件,因为它在排除变量中。
我还希望它排除不是 .py 文件的文件。例如:
flake8 example.js
现在我正在测试,这些都不起作用。有人有什么想法吗?
最佳答案
如果您要在未提交的和暂存的 python 文件上运行 flake8 并进行更改,那么这个单行代码就可以解决问题:
flake8 $(git status -s | grep -E '\.py$' | cut -c 4-)
git status 列出修改过的文件,python grep,用cut去掉开头的M/S位。
要使其成为预提交 Hook ,您需要添加 shell hashbang:
#!/bin/sh
flake8 $(git status -s | grep -E '\.py$' | cut -c 4-)
保存为.git/hooks/pre-commit,chmod +x。
关于git - flake8,仅在 diff 和 exclude 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26656281/