Skip to content

Instantly share code, notes, and snippets.

@brentsowers1
Created May 3, 2012 18:24
Show Gist options
  • Save brentsowers1/2587900 to your computer and use it in GitHub Desktop.
Save brentsowers1/2587900 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
@AvnerCohen
Copy link

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..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment