Created
May 3, 2012 18:24
-
-
Save brentsowers1/2587900 to your computer and use it in GitHub Desktop.
Git pre-commit hook to prevent non-ascii characters
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/bash | |
# Save these contents to .git/hooks/pre-commit in your project | |
# folder, and give it executable permissions with | |
# "chmod u+x .git/hooks/pre-commit" | |
# Git will abort a commit if you have non ASCII characters in | |
# the commit, and output the non ASCII characters. | |
output=`git diff HEAD | tr -d "\000-\011\013-\177" | tr -d '\n'` | |
cnt=${#output} | |
if [ -n "$output" ]; then | |
echo "Aborting commit! Non ascii characters found in change set:" | |
for ((i=0; i < cnt; i++)) | |
do | |
char=${output:$i:1} | |
echo "" | |
echo "Found character '$char':" | |
git diff HEAD -S$char | |
done | |
exit 1 | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's a nice directioh, But this approach will work only when adding new non ascii char. However, it will not work when you delete an old non-ascii and the commit will fail..