-
-
Save ThiefMaster/5857934 to your computer and use it in GitHub Desktop.
pre-commit hook to PEP8-check the code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# vim: et ts=4 sw=4 ft=sh | |
# Interesting post on max line length: | |
# http://stackoverflow.com/questions/88942/why-should-python-pep-8-specify-a-maximum-line-length-of-79-characters | |
PEP8_OPTIONS='--max-line-length=120' | |
RED=`echo -e "\033[1;31m"` | |
YELLOW=`echo -e "\033[0;33m"` | |
CYAN=`echo -e "\033[0;36m"` | |
RESET=`echo -e "\033[0;0m"` | |
BRIGHTYELLOW=`echo -e "\033[1;33m"` | |
WHITE=`echo -e "\033[1;37m"` | |
RE="s/\([^:]*\):\([0-9]*\):\([0-9]*\): \([EW][0-9]*\) \(.*\)/$WHITE[$CYAN\1$RESET $BRIGHTYELLOW\2:\3$WHITE] $RED\4 $YELLOW\5$RESET/g" | |
STATUS=0 | |
for FILE in `git diff-index --name-status HEAD | awk '{print $2}'`; | |
do | |
RESULT=`git diff --cached $FILE | python -W ignore -m pep8 --diff $PEP8_OPTIONS` | |
RC=$? | |
if [[ $RC != 0 ]] ; then | |
if [[ $STATUS == 0 ]] ; then | |
echo "${RED}There are PEP8 issues in your code:${RESET}" | |
fi | |
STATUS=1 | |
fi | |
if [[ -n "$RESULT" ]] ; then | |
echo "$RESULT" | sed -e "$RE" | |
fi | |
done | |
if [[ $STATUS != 0 ]] ; then | |
# claim stdin back | |
exec < /dev/tty | |
echo | |
read -p "${RED}Do you wish to commit it anyway ${CYAN}[${WHITE}y${CYAN}/${WHITE}N${CYAN}]${RESET}? " yn | |
case $yn in | |
[Yy]* ) exit 0; break;; | |
[Nn]* ) exit $STATUS;; | |
* ) exit $STATUS;; | |
esac | |
# close stdin | |
exec <&- | |
fi | |
exit $STATUS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment