Skip to content

Instantly share code, notes, and snippets.

@freemo
Created November 1, 2014 07:10
Show Gist options
  • Save freemo/903c29cba4418af7732a to your computer and use it in GitHub Desktop.
Save freemo/903c29cba4418af7732a to your computer and use it in GitHub Desktop.
A GIT hook for enforcing EOL characters
#!/bin/sh
#echo ""
#echo "==== Remote update-hooks/file-hooks/enforce-eol.sh ===="
#Initialize
ref="$1"
old_rev="$2"
new_rev="$3"
tree_path="$4"
tree_file="$5"
if [ ! -f ${tree_path}/${tree_file} ]; then
exit 0
fi
file_path="${tree_path}/${tree_file}"
repo_path=$PWD
enforce_eol="lf"
is_auto="true"
#figure out if the file is binary or not
if [ -n "$(file ${file_path} | grep -e 'text')" ]; then
is_binary="false"
else
is_binary="true"
fi
#determine files line ending
file_eol="lf"
if [ -n "`grep -P -e '\r\n' "${file_path}"`" ]; then
if [ -n "`grep -P -e '[^\r]\n' "${file_path}"`" ]; then
file_eol="mixed"
else
file_eol="crlf"
fi
elif [ -n "`grep -P -e '\n' "${file_path}"`" ]; then
file_eol="lf"
else
#there are no line endings so none of this matters
exit 0
fi
#print some information that might be useful
#echo "ref: ${ref} - old: ${old_rev} - new: ${new_rev} - tree: ${tree_path} - file: ${tree_file}"
cd $tree_path
#get the files attributes
text_attr=`git --git-dir=${repo_path} --work-tree=${tree_path} check-attr text -- ${tree_file} | sed -e "s/^[^:]*: text: //g"`
eol_attr=`git --git-dir=${repo_path} --work-tree=${tree_path} check-attr eol -- ${tree_file} | sed -e "s/^[^:]*: eol: //g"`
#figure out what to do
enforce="true"
#evaluate the text attribute
if [ $text_attr = "set" ]; then
enforce="true"
elif [ $text_attr = "unset" ]; then
enforce="false"
else
if [ $is_binary = "true" ]; then
enforce="false"
else
enforce="true"
fi
fi
#evaluate the eol attribute
if [ $eol_attr = "set" ]; then
echo "ERROR: Invalid value in .gitattributes for file '${tree_file}' you have eol set but did not give it a value"
exit 1
elif [ $eol_attr = "unset" ]; then
echo "ERROR: Invalid value in .gitattributes for file '${tree_file}' you have eol unset but did not give it a value"
exit 1
elif [ $eol_attr = "unspecified" ]; then
enforce_eol="lf"
elif [ $eol_attr = "lf" ]; then
enforce_eol="lf"
enforce="true"
elif [ $eol_attr = "crlf" ]; then
enforce_eol="crlf"
enforce="true"
else
echo "ERROR: Invalid value in .gitattributes for file '${tree_file}' you have eol set to an unrecognized value of '${eol_attr}'"
exit1
fi
#if we arent enforcing lets just exit
if [ $enforce = "false" ]; then
# echo "DEBUG: ${tree_file} Not enforcing: $is_binary $file_eol $enforce_eol $enforce"
exit 0
fi
#the only thing left to do is to match the actual EOL against the EOL beign enforced
if [ "lf" = $file_eol ]; then
# echo "DEBUG: skipping file '${tree_file}' because it uses ${file_eol} and lf was also expected"
exit 0
else
echo "ERROR: The file '${tree_file}' is expected to have a lf EOL style yet instead it was found to have a ${file_eol} style EOL. Please adjust the .gitattributes file accordingly"
exit 1
fi
#clean up
cd ${repo_path}
#echo "==== Exited Remote update-hooks/file-hooks/enforce-eol.sh ===="
#echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment