10

I'm trying to run flake8 in a pre-commit hook on only the changed files in my git diff, while also excluding files in my config file.

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

I'm essentially wanting to do:

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

And then have flake8 skip the file that I passed in because it is in the exclude variable.

I'm also wanting it to exclude files that are not .py files. For example:

flake8 example.js

Right now as I'm testing, neither of these work. Anyone have any ideas?

4

4 に答える 4

12

コミットされていない Python ファイルとステージングされた Python ファイルの両方で flake8 を実行して変更を加えたい場合は、このワンライナーでうまくいきます。

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

git status は変更されたファイルを一覧表示し、python の場合は grep を実行し、先頭の M/S ビットをカットで取り除きます。

これを pre-commit フックにするには、シェル ハッシュバンを追加する必要があります。

#!/bin/sh flake8 $(git status -s | grep -E '\.py$' | cut -c 4-)

.git/hooks/pre-commit、chmod +x として保存します。

于 2016-05-24T14:16:28.797 に答える
0

あなたの質問には2つの部分があります:

1. git > diff で変更されたファイルのみに対して pre-commit フックで flake8 を実行する

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 番目の部分:

2.構成ファイル内のファイルを除外する

.flake8これを処理できるファイルをリポジトリルートに作成できます。私の.flake8ファイルは次のようになります。

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

お役に立てれば。

于 2017-03-24T17:06:10.487 に答える