Last active
December 18, 2015 22:49
-
-
Save pferreir/5857189 to your computer and use it in GitHub Desktop.
pre-commit hook for PEP8 checking
This file contains 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_CMD='pep8' | |
PEP8_OPTIONS='--max-line-length=120' | |
RED=`echo $"\033[1;31m"` | |
YELLOW=`echo $"\033[0;33m"` | |
CYAN=`echo $"\033[0;36m"` | |
RESET=`echo $"\033[0;0m"` | |
BRIGHTYELLOW=`echo $"\033[1;33m"` | |
BRIGHTBLUE=`echo $"\033[1;34m"` | |
WHITE=`echo $"\033[1;37m"` | |
RE_PEP8="s/\([^:]*\):\([0-9]*\):\([0-9]*\): \([EW][0-9]*\) \(.*\)/$WHITE[$CYAN\1$RESET $BRIGHTYELLOW\2:\3$WHITE] $RED\4 $YELLOW\5$RESET/g" | |
RE_DBG="s/^\([0-9]*\):+\(.*\)$/\1 \2/g" | |
PEP8_STATUS=0 | |
DBG_STATUS=0 | |
for FILE in `git diff-index --name-status HEAD | awk '$1 != "D" {print $2}'` ; | |
do | |
RESULT=`git diff --cached $FILE | $PEP8_CMD --diff $PEP8_OPTIONS` | |
RC=$? | |
if [[ $RC != 0 ]] ; then | |
if [[ $PEP8_STATUS != 1 ]] ; then | |
echo "${RED}There are PEP8 issues in your code:${RESET}\n" | |
fi | |
PEP8_STATUS=1 | |
fi | |
if [[ -n "$RESULT" ]] ; then | |
echo "$RESULT" | sed -e "$RE_PEP8" | |
echo '\n' | |
fi | |
RESULT=`git diff --cached $FILE | grep -nE '(^\+[[:blank:]]*debugger)|(^\+[[:blank:]]*console\..*)|(^\+[[:blank:]]*\print.*)'` | |
RC=$? | |
if [[ $RC == 0 ]] ; then | |
if [[ $DBG_STATUS != 1 ]] ; then | |
echo "${RED}You may have forgotten some debug code:${RESET}\n" | |
fi | |
DBG_STATUS=1 | |
fi | |
if [[ -n "$RESULT" ]] ; then | |
echo $CYAN[$BRIGHTYELLOW$FILE$CYAN]$RESET | |
echo "$RESULT" | sed -e "$RE_DBG" | awk '{printf "%c[1;33m%5s%c[1;31m:%c[0;0m ", 27, $1, 27, 27 ; print substr($0, index($0, $2))}' | |
echo | |
fi | |
done | |
if [[ $PEP8_STATUS != 0 || $DBG_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 1;; | |
* ) exit 1;; | |
esac | |
# close stdin | |
exec <&- | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment