Skip to content

Instantly share code, notes, and snippets.

@vazexqi
Created March 23, 2011 06:09
Show Gist options
  • Save vazexqi/882693 to your computer and use it in GitHub Desktop.
Save vazexqi/882693 to your computer and use it in GitHub Desktop.
Checks for non-ASCII characters or files that are too long
#!/bin/sh
#
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# Non-ASCII Character check
# If you want to allow non-ascii filenames set this variable to true.
allownonascii=$(git config hooks.allownonascii)
# Cross platform projects tend to avoid non-ascii filenames; prevent
# them from being added to the repository. We exploit the fact that the
# printable range starts at the space character and ends with tilde.
if [ "$allownonascii" != "true" ] &&
# Note that the use of brackets around a tr range is ok here, (it's
# even required, for portability to Solaris 10's /usr/bin/tr), since
# the square bracket bytes happen to fall in the designated range.
test "$(git diff --cached --name-only --diff-filter=A -z $against |
LC_ALL=C tr -d '[ -~]\0')"
then
echo "Error: Attempt to add a non-ascii file name."
echo
echo "This can cause problems if you want to work"
echo "with people on other platforms."
echo
echo "To be portable it is advisable to rename the file ..."
echo
echo "If you know what you are doing you can disable this"
echo "check using:"
echo
echo " git config hooks.allownonascii true"
echo
exit 1
fi
# File name length check
MAX_LENGTH=200
# --cached Show only the changes staged for committing
# --name-only Show only names of changed files (instead of status + name)
# --diff-filter=A Show only files that are (A)dded
# -z Leave TAB, LF, and backslash characters in the file alone
for file in $(git diff --cached --name-only --diff-filter=A -z $against)
do
length=${#file} # the ${#string} syntax provides the length of the string
if [ "$length" -gt "$MAX_LENGTH" ]
then
echo "Error: Attempting to commit a file that is longer than $MAX_LENGTH characters."
echo ""
echo "$file"
echo "has $length characters."
exit 1
fi
done
# Uncomment this if you want to be strict and check if changes introduce
# trailing whitespace or an indent that uses a space before a tab.
# exec git diff-index --check --cached $against --
@reprogrammer
Copy link

What does line 9 (against=4b825dc642cb6eb9a060e54bf8d69288fbee4904) do?

@vazexqi
Copy link
Author

vazexqi commented Mar 23, 2011

I modified this script from the initial one that was included in the .git/hooks/pre-commit.sample. That line was already there. Like the comments say, it is used to compare against an empty tree (for the first commit). 4b825dc642cb6eb9a060e54bf8d69288fbee4904 is a special SHA for that.

I am not sure why the have that special SHA instead of a constant or something but you can see the comments at http://git.661346.n2.nabble.com/Any-way-to-get-complete-diff-up-to-a-tag-td5013322.html

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