-
-
Save maiksprenger/4697134 to your computer and use it in GitHub Desktop.
Removes copying to separate dir, and is a bit more explicit.
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/bash | |
# original jonasvp https://gist.github.com/858223 | |
# also worth noting | |
# https://github.com/lbolla/dotfiles/blob/master/githooks/pre-commit | |
# http://python.dzone.com/articles/tips-using-git-pre-commit-hook | |
# stash unstaged changes, keeps our working dir clean | |
git stash -q --keep-index | |
# keeps our messages | |
TMPFILE=`mktemp` | |
# get list of modified python files | |
# --staged: only staged files | |
# --relative: print file names relative to current dir | |
FILES=`git diff --staged --name-only --relative --diff-filter=ACMR` | |
# If we have conflict markers, drop out right away | |
# https://gist.github.com/1006520 | |
echo $FILES | awk '/\+(<<<[<]<<<|>>>[>]>>>|===[=]===$)/ { exit 1 }' | |
CODE=$? | |
if [ "$CODE" != "0" ]; then | |
echo "COMMIT ABORTED: Conflict markers found." >&2 | |
exit $CODE | |
fi | |
# run pyflakes, run | |
PYFILES=`echo $FILES | grep \.py$` | |
echo $PYFILES | xargs pyflakes > $TMPFILE 2>&1 | |
# massage the list of warnings | |
# sed -i: inplace | |
sed -i '/unable to detect undefined names/d' $TMPFILE | |
sed -i "/migrations.* '\(datetime\|models\)' imported but unused/d" $TMPFILE | |
# check for leftover debug statements | |
echo $PYFILES | xargs grep -l "pdb.set_trace()" | sed 's/$/ contains pdb.set_trace() statement/g' >> $TMPFILE | |
CODE=0 | |
OUTPUT=`cat $TMPFILE` | |
if [ -n "$OUTPUT" ]; then | |
# show errors | |
echo "COMMIT ABORTED: Errors remaining." >&2 | |
cat $TMPFILE; | |
CODE=1; | |
fi | |
# clean up and pop unstashed changes | |
rm $TMPFILE | |
git stash pop -q | |
exit $CODE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment