prepare-commit.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env bash
  2. ###########################################################################
  3. # prepare-commit.sh
  4. # ---------------------
  5. # Date : August 2008
  6. # Copyright : (C) 2008 by Juergen E. Fischer
  7. # Email : jef at norbit dot de
  8. ###########################################################################
  9. # #
  10. # This program is free software; you can redistribute it and/or modify #
  11. # it under the terms of the GNU General Public License as published by #
  12. # the Free Software Foundation; either version 2 of the License, or #
  13. # (at your option) any later version. #
  14. # #
  15. ###########################################################################
  16. TOPLEVEL=$(git rev-parse --show-toplevel)
  17. PATH=$TOPLEVEL/scripts:$PATH
  18. cd $TOPLEVEL
  19. # GNU prefix command for mac os support (gsed, gsplit)
  20. GP=
  21. if [[ "$OSTYPE" =~ darwin* ]]; then
  22. GP=g
  23. fi
  24. if ! type -p astyle.sh >/dev/null; then
  25. echo astyle.sh not found
  26. exit 1
  27. fi
  28. if ! type -p colordiff >/dev/null; then
  29. colordiff()
  30. {
  31. cat "$@"
  32. }
  33. fi
  34. if [ "$1" = "-c" ]; then
  35. echo "Cleaning..."
  36. remove_temporary_files.sh
  37. fi
  38. set -e
  39. # determine changed files
  40. MODIFIED=$(git status --porcelain| ${GP}sed -ne "s/^ *[MA] *//p" | sort -u)
  41. #MODIFIED=$(find src -name "*.h")
  42. if [ -z "$MODIFIED" ]; then
  43. echo nothing was modified
  44. exit 0
  45. fi
  46. # save original changes
  47. REV=$(git log -n1 --pretty=%H)
  48. git diff >sha-$REV.diff
  49. ASTYLEDIFF=astyle.$REV.diff
  50. >$ASTYLEDIFF
  51. # reformat
  52. i=0
  53. N=$(echo $MODIFIED | wc -w)
  54. for f in $MODIFIED; do
  55. (( i++ )) || true
  56. case "$f" in
  57. thirdparty/*)
  58. echo $f skipped
  59. continue
  60. ;;
  61. *.cpp|*.c|*.h|*.cxx|*.hxx|*.c++|*.h++|*.cc|*.hh|*.C|*.H|*.sip|*.py)
  62. ;;
  63. *)
  64. continue
  65. ;;
  66. esac
  67. m=$f.$REV.prepare
  68. cp $f $m
  69. ASTYLEPROGRESS=" [$i/$N]" astyle.sh $f
  70. if diff -u $m $f >>$ASTYLEDIFF; then
  71. # no difference found
  72. rm $m
  73. fi
  74. done
  75. if [ -s "$ASTYLEDIFF" ]; then
  76. if tty -s; then
  77. # review astyle changes
  78. colordiff <$ASTYLEDIFF | less -r
  79. else
  80. echo "Files changed (see $ASTYLEDIFF)"
  81. fi
  82. exit 1
  83. else
  84. rm $ASTYLEDIFF
  85. fi
  86. # If there are whitespace errors, print the offending file names and fail.
  87. exec git diff-index --check --cached HEAD --
  88. exit 0
  89. # vim: set ts=8 noexpandtab :