Skip to content

Instantly share code, notes, and snippets.

@maiksprenger
Forked from jonasvp/pre-commit
Last active December 12, 2015 02:18
Show Gist options
  • Save maiksprenger/4697134 to your computer and use it in GitHub Desktop.
Save maiksprenger/4697134 to your computer and use it in GitHub Desktop.
Removes copying to separate dir, and is a bit more explicit.
#!/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