1 / Git itself has a heuristic to determine if a file is binary or text (similar to istext )
2/gergap weblog ( 2010) .
. ( ), :
, , , push, () eol.
Git LF->CRLF Windows.
CRLF, Git , CRCRLF, . CRLF, , , CRLF LF. Git .
, , , .
.
#!/bin/sh
refname="$1"
oldrev="$2"
newrev="$3"
if [ -z "$GIT_DIR" ]; then
echo "Don't run this script from the command line." >&2
echo " (if you want, you could supply GIT_DIR then run" >&2
echo " $0 <ref> <oldrev> <newrev>)" >&2
exit 1
fi
if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then
echo "Usage: $0 <ref> <oldrev> <newrev>" >&2
exit 1
fi
BINARAY_EXT="pdb dll exe png gif jpg"
function IsBinary()
{
result=0
for ext in $BINARAY_EXT; do
if [ "$ext" = "${1#*.}" ]; then
result=1
break
fi
done
return $result
}
tmp=$(mktemp /tmp/git.update.XXXXXX)
log=$(mktemp /tmp/git.update.log.XXXXXX)
tree=$(mktemp /tmp/git.diff-tree.XXXXXX)
ret=0
git diff-tree -r "$oldrev" "$newrev" > $tree
exec 3<&0
exec 0<$tree
while read old_mode new_mode old_sha1 new_sha1 status name
do
test -z "$new_sha1" && continue
[ "$new_sha1" = "0000000000000000000000000000000000000000" ] && continue
IsBinary $tmp
if [ $? -eq 1 ]; then
continue
fi
git cat-file blob $new_sha1 > $tmp
RESULT=`grep -Pl '\r\n' $tmp`
echo $RESULT
if [ "$RESULT" = "$tmp" ]; then
echo "###################################################################################################"
echo "# '$name' contains CRLF! Dear Windows developer, please activate the GIT core.autocrlf feature,"
echo "# or change the line endings to LF before trying to push."
echo "# Use 'git config core.autocrlf true' to activate CRLF conversion."
echo "# OR use 'git reset HEAD~1' to undo your last commit and fix the line endings."
echo "###################################################################################################"
ret=1
fi
done
exec 0<&3
exit $ret