我试图在预提交钩子中只在git diff中更改的文件上运行flake8,同时排除配置文件中的文件。
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现在在我测试的时候,这两种方法都不起作用。有谁有什么想法吗?
发布于 2016-05-24 22:16:29
如果你想要的是在未提交和暂存的python文件上运行flake8,那么下面这一行代码就可以解决这个问题:
flake8 $(git status -s | grep -E '\.py$' | cut -c 4-)
git status列出了更改的文件,grep for python,去掉了开头的M/S位with cut。
要使其成为预提交钩子,您需要添加shell hashbang:
#!/bin/sh flake8 $(git status -s | grep -E '\.py$' | cut -c 4-)
将其保存为.git/hooks/pre-commit,chmod +x。
发布于 2017-03-25 01:06:10
你的问题有两个部分:
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希望这能有所帮助。
发布于 2019-09-08 05:50:58
如果您想在提交之前使用flake8检查文件,只需使用
$ flake8 --install-hook githttps://stackoverflow.com/questions/26656281
复制相似问题