Skip to content

Instantly share code, notes, and snippets.

@theirix
Last active January 11, 2020 22:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theirix/d644acd1446b461fbf6c to your computer and use it in GitHub Desktop.
Save theirix/d644acd1446b461fbf6c to your computer and use it in GitHub Desktop.
pre-receive git hook for checking bad unicode
#!/bin/bash
# pre-receive hook to check damaged unicode encoding of received text files
# Wonderful ASCII art is borrowed from the phabricator.com project and licensed by the Apache 2.0 license
RESULT=0
while read oldrev newrev refname
do
OLDIFS=$IFS
IFS=$'\n'
for FILE in `git diff-tree -r --name-only $oldrev..$newrev`
do
#echo "Looking to $oldrev:$newrev:$FILE" >&2
git cat-file -e $newrev:$FILE 2>/dev/null
# checks only existing files
if [ "$?" = "0" ] ; then
# checks only non-binary files
if [ "`git show $newrev:$FILE | file -b --mime-encoding - 2>/dev/null`" != "binary" ] ; then
# check if a file could be converted to the cp1251 (cyrillic one-byte encoding)
git show $newrev:$FILE | iconv -f utf-8 -t cp1251 > /dev/null
if [ "$?" != "0" ] ; then
echo "File $FILE contains damaged unicode symbols" >&2
RESULT=1
fi
fi
fi
done
IFS=$OLDIFS
done
if [ "$RESULT" != "0" ] ; then
echo '' >&2
echo '+---------------------------------------------------------------+' >&2
echo '| * * * PUSH REJECTED BY EVIL DRAGON BUREAUCRATS * * * |' >&2
echo '+---------------------------------------------------------------+' >&2
echo ' \ ' >&2
echo ' \ ^ /^ ' >&2
echo ' \ / \ // \ ' >&2
echo ' \ |\___/| / \// .\ ' >&2
echo ' \ /V V \__ / // | \ \ *----* ' >&2
echo ' / / \/_/ // | \ \ \ | ' >&2
echo ' @___@` \/_ // | \ \ \/\ \ ' >&2
echo ' 0/0/| \/_ // | \ \ \ \ ' >&2
echo ' 0/0/0/0/| \/// | \ \ | | ' >&2
echo ' 0/0/0/0/0/_|_ / ( // | \ _\ | / ' >&2
echo ' 0/0/0/0/0/0/`/,_ _ _/ ) ; -. | _ _\.-~ / / ' >&2
echo ' ,-} _ *-.|.-~-. .~ ~ ' >&2
echo ' \ \__/ `/\ / ~-. _ .-~ / ' >&2
echo ' \____(Oo) *. } { / ' >&2
echo ' ( (--) .----~-.\ \-` .~ ' >&2
echo ' //__\\ \ DENIED! ///.----..< \ _ -~ ' >&2
echo ' // \\ ///-._ _ _ _ _ _ _{^ - - - - ~ ' >&2
echo ' ' >&2
echo '' >&2
fi
exit $RESULT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment