-
-
Save danwerner/1006520 to your computer and use it in GitHub Desktop.
My .git/hooks/pre-commit - save yourself from embarassment using pyflakes
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/bash | |
# Original version (C) 2011 by jonasvp | |
# Modified version by danwerner | |
# Save this file as .git/hooks/pre-commit, make it executable, and it will | |
# thank you by saving you from common mistakes *before* deployment. | |
# Conflict markers | |
git diff --cached --diff-filter=ACMR | awk '/\+(<<<[<]<<<|>>>[>]>>>|===[=]===$)/ { exit 1 }' | |
CODE=$? | |
if [ "$CODE" != "0" ]; then | |
echo "COMMIT ABORTED: Conflict markers found." >&2 | |
exit $CODE | |
fi | |
# Pyflakes | |
TMPDIR=`mktemp -d` | |
TMPFILE=`mktemp` | |
git diff --cached --name-only --diff-filter=ACMR | xargs git checkout-index --prefix=$TMPDIR/ -- | |
pyflakes $TMPDIR >$TMPFILE 2>&1 | |
grep -v "unable to detect undefined names" $TMPFILE >$TMPFILE.2 | |
egrep -v "migrations.*'(datetime|models)' imported but unused" $TMPFILE.2 >$TMPFILE | |
# PDB breakpoints | |
grep -rn "pdb.set_trace" $TMPDIR >>$TMPFILE | |
sed "s/^${TMPDIR//\//.}.//" $TMPFILE >$TMPFILE.2 | |
mv $TMPFILE.2 $TMPFILE | |
CODE=0 | |
OUTPUT=`cat $TMPFILE` | |
if [ -n "$OUTPUT" ]; then | |
cat $TMPFILE >&2 | |
echo "COMMIT ABORTED: Python programming errors detected." >&2 | |
CODE=1 | |
fi | |
rm -Rf $TMPDIR $TMPFILE | |
exit $CODE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment