Flake8, only on diff and exclude

I am trying to run flake8 in a pre-commit hook only on modified files in my git diff, as well as excluding files in my configuration 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 really want to do:

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

And then with flake8, skip the file that I transferred, because it is in the exclude variable.

I also want it to exclude files that are not .py files. For instance:

flake8 example.js

Now when I test, none of them work. Does anyone have any ideas?

+6
source share
3 answers

If after that you use flake8 in both unoccupied and delivered python files with changes, this single-line file will do the trick:

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

git status , grep python, M/S .

, hashbang :

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

.git/hooks/pre-commit, chmod + x.

+6

:

1. flake8 git> 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

, .

0

If you want to use flake8to check files before committing, just use

$ flake8 --install-hook git

link: https://flake8.pycqa.org/en/latest/user/using-hooks.html

0
source

All Articles