Skip to content

Instantly share code, notes, and snippets.

@Shaunwei
Forked from msiemens/pre-commit.sh
Created June 25, 2014 22:23
Show Gist options
  • Save Shaunwei/dbde19a2e85104ef1891 to your computer and use it in GitHub Desktop.
Save Shaunwei/dbde19a2e85104ef1891 to your computer and use it in GitHub Desktop.
#!/bin/sh
# Git pre-commit hook
#####################
#
# - check for whitespace problems (trailing spaces, ...)
# - check for lines with 'FIXME'
# - running tests with Python 2.7, Python 3.4 and PyPy
# - running code style check (pep8) on modified files
# - designed for Windows, for Linux replace `> NUL` with `> /dev/null`
########################################################################
# Check for whitespace errors
git diff --cached --check # Check for whitespace errors
code=$?
if [ "$code" -ne "0" ]; then
echo
echo "Your changes introduced whitespace errors!"
exit $code
fi;
########################################################################
# Check for 'FIXME' lines
todo_lines=$(git diff --cached | grep ^+ | grep -v pre-commit | grep FIXME | sed 's_^+\s*__g')
if [ "$todo_lines" != "" ]; then
echo "You probably didn't want to commit these lines:"
echo -e "$todo_lines"
exit 1
fi
########################################################################
# Run tests (Python 2)
echo "Running tests..."
git stash -q --keep-index
nosetests > NUL
code=$?
git stash pop -q
if [ "$code" -eq "0" ]; then
echo
echo
echo "All tests passed. Continuing..."
echo
else
echo
echo
echo "Please (re)check tests!"
exit $code
fi;
########################################################################
# Run tests (Python 3)
echo "Running tests (Python 3)..."
git stash -q --keep-index
python3 setup.py nosetests > NUL
code=$?
git stash pop -q
if [ "$code" -eq "0" ]; then
echo
echo
echo "Compatibility to Python 3 is okay. Continuing..."
echo
else
echo
echo
echo "Please check Python 3 compatibility!"
exit $code
fi;
########################################################################
# Run tests (PyPy)
echo "Running tests (PyPy)..."
git stash -q --keep-index
pypy setup.py nosetests > NUL
code=$?
git stash pop -q
if [ "$code" -eq "0" ]; then
echo
echo
echo "Compatibility to PyPy is okay. Continuing..."
echo
else
echo
echo
echo "Please check Pypy compatibility!"
exit $code
fi;
########################################################################
# Run code style check
echo "Running code style check..."
modified_files=$(git diff --cached --name-status | grep -v ^D | awk '$1 $2 { print $2}' | grep -e .py$)
if [ -n "$modified_files" ]; then
pep8 -r $modified_files
fi
if [ "$?" -eq "0" ]; then
echo
echo
echo "Code style is okay. Continuing..."
else
echo
echo
echo "Please consider cleaning up your code!"
sleep 5
fi;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment