uncrustify_precommit 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/bin/bash
  2. # use as .git/hooks/pre-commit
  3. function vercomp () {
  4. if [[ $1 == $2 ]]
  5. then
  6. return 0
  7. fi
  8. local IFS=.
  9. local i ver1=($1) ver2=($2)
  10. # fill empty fields in ver1 with zeros
  11. for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
  12. do
  13. ver1[i]=0
  14. done
  15. for ((i=0; i<${#ver1[@]}; i++))
  16. do
  17. if [[ -z ${ver2[i]} ]]
  18. then
  19. # fill empty fields in ver2 with zeros
  20. ver2[i]=0
  21. fi
  22. if ((10#${ver1[i]} > 10#${ver2[i]}))
  23. then
  24. return 1
  25. fi
  26. if ((10#${ver1[i]} < 10#${ver2[i]}))
  27. then
  28. return 2
  29. fi
  30. done
  31. return 0
  32. }
  33. exec 1>&2
  34. if ! uncrustify --version > /dev/null
  35. then
  36. echo 'uncrustify required'
  37. exit 1
  38. fi
  39. HAVE=$(uncrustify --version | sed -e 's/.*-//' -e 's/_.//')
  40. vercomp 0.78.0 "$HAVE"
  41. case $? in
  42. 0)
  43. ;;
  44. 1)
  45. echo "your uncrustify is too old";
  46. exit 1
  47. ;;
  48. 2)
  49. ;;
  50. esac
  51. RET=0
  52. changed=$(git diff --cached --name-only)
  53. crustified=""
  54. for f in $changed;
  55. do
  56. if echo $f | grep \\.[c,h]\$ > /dev/null
  57. then
  58. # compare result of uncrustify with changes
  59. #
  60. # only change any of the invocations here if
  61. # they are portable across all cmp and shell
  62. # implementations !
  63. uncrustify -q -c uncrustify.cfg -f $f | cmp -s $f -
  64. if test $? = 1 ;
  65. then
  66. crustified=" $crustified $f"
  67. RET=1
  68. fi
  69. fi
  70. done
  71. if [ $RET = 1 ];
  72. then
  73. echo "Run"
  74. echo "uncrustify --no-backup -c uncrustify.cfg ${crustified}"
  75. echo "before committing."
  76. fi
  77. exit $RET