Skip to content

Instantly share code, notes, and snippets.

@drumrick
Last active June 22, 2016 17:24
Show Gist options
  • Save drumrick/6c0d21817395c36143b453f88fe13406 to your computer and use it in GitHub Desktop.
Save drumrick/6c0d21817395c36143b453f88fe13406 to your computer and use it in GitHub Desktop.
git PEP8 pre-commit hook
# put this in your .git/hooks/pre-commit
# `pip install pep8` before use
# THIS SHOULD BE EXECUTABLE
exec 1>&2 # redirect all output to error output
FILES_HAVING_ERROR=0 # counting files having error
for FILE in $(git diff --cached --name-only) # only check modified & staged files
do
if [ ${FILE: -3} == ".py" ]; then
pep8 --show-source --show-pep8 --ignore=E501 $FILE # ignoring E501 which is max-line-length=80
if [ $? -ne 0 ]; then # $FILE failed to pass PEP8 check
FILES_HAVING_ERROR=$(($FILES_HAVING_ERROR + 1)); # counting files having error
fi
fi
done
# colors for output
WHITE='\033[1;37m'
RED='\033[1;31m'
GREEN='\033[1;32m'
NC='\033[0m'
# PEP8:fail
if [ $FILES_HAVING_ERROR -ne 0 ]; then
echo "${WHITE}PEP8${NC}:${RED}FAIL${NC}"
exit 1
fi
# PEP8:PASS
echo "${WHITE}PEP8${NC}:${GREEN}PASS${NC}"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment