flake8,仅限于diff和exclude

bzm*_*zmw 6 git pep8 flake8

我试图在预先提交的钩子中运行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
Run Code Online (Sandbox Code Playgroud)

我基本上想做:

flake8 --exclude=/foo/ /foo/stuff.py
Run Code Online (Sandbox Code Playgroud)

然后让flake8跳过我传入的文件,因为它在exclude变量中.

我也希望它排除不是.py文件的文件.例如:

flake8 example.js
Run Code Online (Sandbox Code Playgroud)

现在我正在测试,这些都不起作用.有人有主意吗?

Kip*_*Kip 7

如果您正在使用flake8在未提交和暂存的python文件上运行更改,这个单线程将完成这一操作:

flake8 $(git status -s | grep -E '\.py$' | cut -c 4-)

git status列出已更改的文件,grep为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.