Skip to content

Instantly share code, notes, and snippets.

@tombentley
Created June 11, 2014 09:02
Show Gist options
  • Save tombentley/c0eb2768e1718eefd195 to your computer and use it in GitHub Desktop.
Save tombentley/c0eb2768e1718eefd195 to your computer and use it in GitHub Desktop.
# If you want to allow filename that differ only by case set this variable to true.
allowcasecollision=$(git config hooks.allowcasecollision)
if [ "$allowcasecollision" != "true" ] &&
test "$(git diff --cached --name-only |
LC_ALL=C tr [A-Z] [a-z] |
sort |
uniq --repeated )"
then
echo "Error: Attempt to add a file name which differs only by case from "
echo "another tracked file"
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.allowcasecollision true"
echo
exit 1
fi
@tombentley
Copy link
Author

This is a fragment of shell script for a git pre-commit hook to prevent adding files which differ only by case. To use it:

  • rename .git/hooks/pre-commit.sample to .git/hooks/pre-commit
  • add the above fragment to the file just above the final exec line (after the similar check for ASCII-only filenames).

@lucaswerkmeister
Copy link

You could print to stderr instead, like this:

cat >&2 << EOF
Error: Attempt to add a file name which differs only by case from
another tracked file

This can cause problems if you want to work
with people on other platforms.

To be portable it is advisable to rename the file ...

If you know what you are doing you can disable this
check using:

  git config hooks.allowcasecollision true

EOF

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