Created
October 27, 2011 03:15
-
-
Save holysugar/1318698 to your computer and use it in GitHub Desktop.
git pre-commit warning to trailing whitespaces
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/sh | |
function error() { | |
echo "'$1' has trailing spaces.\n" >&2 | |
} | |
git diff --cached --name-only | (while read f; do | |
ERROR=0 | |
if grep -n '[[:space:]]$' "$f" ; then | |
error $f | |
ERROR=1 | |
fi | |
done; exit $ERROR) | |
if [ $? -eq 0 -o "$IGNORE_WHITESPACE" ]; then | |
exit | |
else | |
echo "if you know what you are doing, use 'IGNORE_WHITESPACE=1 git commit'" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You need to put this in the project's .git/hooks directory, make sure it's executable using
chmod a+x .git/hooks/pre-commit
/bin/sh on Ubuntu is the dash shell and the
function error()
definition doesn't work; one fix is to change the first line to /bin/bash.To automatically add this to projects, read about TEMPLATE DIRECTORY in git-init help.