Skip to content

Instantly share code, notes, and snippets.

@danbeaulieu
Forked from brentsowers1/pre-commit.bash
Created May 3, 2012 18:28
Show Gist options
  • Save danbeaulieu/2587926 to your computer and use it in GitHub Desktop.
Save danbeaulieu/2587926 to your computer and use it in GitHub Desktop.
Git pre-commit hook to prevent non-ascii 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