Skip to content

Instantly share code, notes, and snippets.

@bitwalker
Last active December 23, 2015 22:39
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 bitwalker/6704536 to your computer and use it in GitHub Desktop.
Save bitwalker/6704536 to your computer and use it in GitHub Desktop.
Update hook for git which prevents files from having their line endings set to CRLF.
#!/bin/sh
# AUTHOR: Paul Schoenfelder
#
# This post-receive hook validates that committed files use proper (LF) line endings.
verify_lf()
{
echo "Validating line endings..."
oldrev=$(git rev-parse $1)
newrev=$(git rev-parse $2)
refname="$3"
invalid_lines=$(git diff --numstat --name-only --diff-filter=AMR $newrev..$oldrev |
sed -E 's/([0-9a-zA-Z\\\/_-~]+)/'$newrev':\1/' |
xargs git cat-file blob |
grep -Iu " " |
wc -l)
if [ "$invalid_lines" != "0" ]; then
cat <<EOF
Error: Attempt to push files with invalid CRLF line-endings.
Please ensure you have the following in your .git/config:
[core]
autocrlf = input
You will need to update these files to use Unix-style line-endings (LF).
EOF
exit 1
fi
echo "Line endings good!"
}
# Allow dual mode: run from the command line or if no arguments
# are given, then run as a hook script
if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
echo "ARGS:" $1 $2 $3
verify_lf $2 $3 $1
else
while read refname oldrev newrev
do
verify_lf $oldrev $newrev $refname
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment