Advertisement
Guest User

git crlf hook

a guest
Aug 8th, 2010
1,852
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.56 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # Author: Gerhard Gappmeier, ascolab GmbH
  4. # This script is based on the update.sample in git/contrib/hooks.
  5. # You are free to use this script for whatever you want.
  6. #
  7. # To enable this hook, rename this file to "update".
  8. #
  9.  
  10. # --- Command line
  11. refname="$1"
  12. oldrev="$2"
  13. newrev="$3"
  14. #echo "COMMANDLINE: $*"
  15.  
  16. # --- Safety check
  17. if [ -z "$GIT_DIR" ]; then
  18.     echo "Don't run this script from the command line." >&2
  19.     echo " (if you want, you could supply GIT_DIR then run" >&2
  20.     echo "  $0 <ref> <oldrev> <newrev>)" >&2
  21.     exit 1
  22. fi
  23.  
  24. if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then
  25.     echo "Usage: $0 <ref> <oldrev> <newrev>" >&2
  26.     exit 1
  27. fi
  28.  
  29. BINARAY_EXT="pdb dll exe png gif jpg"
  30.  
  31. # returns 1 if the given filename is a binary file
  32. function IsBinary()
  33. {
  34.     result=0
  35.     for ext in $BINARAY_EXT; do
  36.         if [ "$ext" = "${1#*.}" ]; then
  37.             result=1
  38.             break
  39.         fi
  40.     done
  41.  
  42.     return $result
  43. }
  44.  
  45. # make temp paths
  46. tmp=$(mktemp /tmp/git.update.XXXXXX)
  47. log=$(mktemp /tmp/git.update.log.XXXXXX)    
  48. tree=$(mktemp /tmp/git.diff-tree.XXXXXX)
  49. ret=0
  50.  
  51. git diff-tree -r "$oldrev" "$newrev" > $tree
  52. #echo
  53. #echo diff-tree:
  54. #cat $tree
  55.  
  56. # read $tree using the file descriptors
  57. exec 3<&0
  58. exec 0<$tree
  59. while read old_mode new_mode old_sha1 new_sha1 status name
  60. do
  61.     # debug output
  62.     #echo "old_mode=$old_mode new_mode=$new_mode old_sha1=$old_sha1 new_sha1=$new_sha1 status=$status name=$name"
  63.     # skip lines showing parent commit
  64.     test -z "$new_sha1" && continue
  65.     # skip deletions
  66.     [ "$new_sha1" = "0000000000000000000000000000000000000000" ] && continue
  67.    
  68.     # don't do a CRLF check for binary files
  69.     IsBinary $tmp
  70.     if [ $? -eq 1 ]; then
  71.         continue # skip binary files
  72.     fi
  73.    
  74.     # check for CRLF
  75.     git cat-file blob $new_sha1 > $tmp
  76.     RESULT=`grep -Pl '\r\n' $tmp`
  77.     echo $RESULT
  78.     if [ "$RESULT" = "$tmp" ]; then
  79.         echo "###################################################################################################"
  80.         echo "# '$name' contains CRLF! Dear Windows developer, please activate the GIT core.autocrlf feature,"
  81.         echo "# or change the line endings to LF before trying to push."
  82.         echo "# Use 'git config core.autocrlf true' to activate CRLF conversion."
  83.         echo "# OR use 'git reset HEAD~1' to undo your last commit and fix the line endings."
  84.         echo "###################################################################################################"
  85.         ret=1
  86.     fi
  87. done
  88. exec 0<&3
  89. # --- Finished
  90. exit $ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement