Skip to content

Instantly share code, notes, and snippets.

@cyeong
Forked from voldmar/pre-commit
Created December 10, 2012 08:05
Show Gist options
  • Save cyeong/4249217 to your computer and use it in GitHub Desktop.
Save cyeong/4249217 to your computer and use it in GitHub Desktop.
git pre-commit hook that checks forgotten print’s and pyflakes errors
#!/bin/bash
cd ./$(git rev-parse --show-cdup)
result=$(mktemp /tmp/pre-commit.XXXXX)
STAGED=${STAGED:---staged}
changed_py=$(git diff --name-only --diff-filter=AM $STAGED | grep -E '\.py$')
changed_js=$(git diff --name-only --diff-filter=AM $STAGED | grep -E '\.js$')
[[ -z $changed ]] && exit 0
if pip freeze -r requirements.txt 2>&- >&-
then
status=1
[[ -z extra_new_line ]] || { echo; echo; }
echo You have errors in requirements.txt
echo
extra_new_line=yes
fi
if grep -HEn '^\s* print' $changed_py > $result
then
status=1
[[ -z extra_new_line ]] || { echo; echo; }
echo You have some print’s in your python files
echo
extra_new_line=yes
cat $result
fi
if grep -HEn '^\s*console.log' $changed_py > $result
then
status=1
[[ -z extra_new_line ]] || { echo; echo; }
echo You have some console.log in you JS files
echo
extra_new_line=yes
cat $result
fi
if grep -HEn $USER $changed_py > $result
then
status=1
[[ -z extra_new_line ]] || { echo; echo; }
echo You have some mentions about your username in files
echo
extra_new_line=yes
cat $result
fi
if ! pyflakes $changed_py > $result
then
status=1
[[ -z extra_new_line ]] || { echo; echo; }
echo pyflakes says you have some errors
echo
cat $result
fi
rm -f $result
exit $status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment