git-pre-commit-hook 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/bin/sh
  2. if git rev-parse --verify HEAD >/dev/null 2>&1 ; then
  3. against=HEAD
  4. else
  5. # Initial commit: diff against an empty tree object
  6. against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
  7. fi
  8. # Redirect output to stderr.
  9. exec 1>&2
  10. #-------------------------------------------------------------------------------
  11. # Prevent files with non-ascii filenames from being committed.
  12. if test $(git diff --cached --name-only --diff-filter=A -z $against | LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0 ; then
  13. echo "Error: Attempt to add a non-ascii file name."
  14. echo
  15. echo "This can cause problems if you want to work"
  16. echo "with people on other platforms."
  17. echo
  18. echo "To be portable it is advisable to rename the file ..."
  19. echo
  20. echo "Commit aborted."
  21. exit 1
  22. fi
  23. #-------------------------------------------------------------------------------
  24. # Check the formatting of all C files.
  25. # http://man.openbsd.org/sed#r
  26. # http://man.openbsd.org/sed#E
  27. # http://netbsd.gw.com/cgi-bin/man-cgi?sed++NetBSD-current
  28. # https://github.com/freebsd/freebsd/blob/master/usr.bin/sed/main.c
  29. # http://git.savannah.gnu.org/gitweb/?p=sed.git;a=blob;f=sed/sed.c
  30. # GNU has -r and -E (undocumented); MacOS has -E but not -r; Sunos has neither.
  31. files=$(git diff-index --name-status --cached HEAD | grep -v ^D | sed -E "s/^[A-Z]+[A-Z0-9]*[ \t]+/ /")
  32. cfiles=""
  33. for f in $files ; do
  34. if test `dirname $f` = "src/ALAC" ; then
  35. echo "Skipping cstyle checking on $f"
  36. elif test `echo $f | grep -c "\.[ch]$"` -gt 0 ; then
  37. cfiles="$cfiles $f"
  38. fi
  39. done
  40. if test -n "$cfiles" ; then
  41. Scripts/cstyle.py $cfiles
  42. if test $? -ne 0 ; then
  43. echo
  44. echo "Commit aborted. Fix the above error before trying again."
  45. exit 1
  46. fi
  47. fi
  48. #-------------------------------------------------------------------------------
  49. # Check the copyright notice of all files to be commited.
  50. user=`git config --global user.email`
  51. year=`date +"%Y"`
  52. missing_copyright_year=""
  53. if test $user = "[email protected]" ; then
  54. for f in $files ; do
  55. if test `head -5 $f | grep -i copyright | grep -c -i $user` -gt 0 ; then
  56. user_copyright=`grep -i copyright $f | grep $user | grep -c $year`
  57. if test $user_copyright -lt 1 ; then
  58. missing_copyright_year="$missing_copyright_year $f"
  59. fi
  60. fi
  61. done
  62. fi
  63. if test -n "$missing_copyright_year" ; then
  64. echo "Missing current year in the copyright notice of the following files:"
  65. for f in $missing_copyright_year ; do
  66. echo " $f"
  67. done
  68. echo "Commit aborted."
  69. exit 1
  70. fi
  71. exit 0