Skip to content

Instantly share code, notes, and snippets.

@umr55766
Last active December 14, 2021 03:54
Show Gist options
  • Save umr55766/ece111837cfb468211ccacb9e65fb5be to your computer and use it in GitHub Desktop.
Save umr55766/ece111837cfb468211ccacb9e65fb5be to your computer and use it in GitHub Desktop.
A pre-commit hook to blacked the python code before verfying it with flake8 , this hook only blacken and verify those files which have extension "py" i.e. [*.py] and have been added to stage for committing. Unstaged files and other files are not considered. This hook also abort commit if flake8 throws any message.
#!/bin/sh
# blacken the staged files
for file in $(git diff --cached --name-only | grep -E '\.(py)$')
do
black "$file"
$(git add "$file")
done
for file in $(git diff --cached --name-only | grep -E '\.(py)$')
do
flake8 "$file"
if [ $? -ne 0 ]; then
echo "flake8 failed on staged file '$file'. Please check your code and try again. To check output manually run flake8 $file"
exit 1 # exit with failure status
fi
done
[flake8]
exclude = .git
max-line-length = 119
ignore = E203, W503
@karan-freightwalla
Copy link

karan-freightwalla commented Nov 6, 2019

Where I can find setup.cfg file ?

create a file named setup.cfg in the project root

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment