prepare-commit.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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:$PWD/scripts
  18. if ! tty -s && [[ "$0" =~ /pre-commit ]]; then
  19. exec </dev/tty
  20. fi
  21. cd "$TOPLEVEL"
  22. # GNU prefix command for mac os support (gsed, gsplit)
  23. GP=
  24. if [[ "$OSTYPE" =~ darwin* ]]; then
  25. GP=g
  26. fi
  27. if ! type -p astyle.sh >/dev/null; then
  28. echo astyle.sh not found
  29. exit 1
  30. fi
  31. if ! type -p colordiff >/dev/null; then
  32. colordiff()
  33. {
  34. cat "$@"
  35. }
  36. fi
  37. if [ "$1" = "-c" ]; then
  38. echo "Cleaning..."
  39. remove_temporary_files.sh
  40. fi
  41. set -e
  42. # determine changed files
  43. MODIFIED=$(git status --porcelain| ${GP}sed -ne "s/^ *[MA] *//p" | sort -u)
  44. if [ -z "$MODIFIED" ]; then
  45. echo nothing was modified
  46. exit 0
  47. fi
  48. if [[ -n "$QGIS_CHECK_SPELLING" && -x "${TOPLEVEL}"/scripts/spell_check/check_spelling.sh ]]; then "${TOPLEVEL}"/scripts/spell_check/check_spelling.sh "$MODIFIED"; fi
  49. FILES_CHANGED=0
  50. # save original changes
  51. REV=$(git log -n1 --pretty=%H)
  52. #git diff >sha-"$REV".diff
  53. ASTYLEDIFF=astyle.$REV.diff
  54. true > "$ASTYLEDIFF"
  55. # reformat
  56. i=0
  57. N=$(echo "$MODIFIED" | wc -w)
  58. for f in $MODIFIED; do
  59. (( i++ )) || true
  60. case "$f" in
  61. *.cpp|*.c|*.h|*.cxx|*.hxx|*.c++|*.h++|*.cc|*.hh|*.C|*.H|*.py|*.mm)
  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. rm "$ASTYLEDIFF"
  80. else
  81. echo "Files changed (see $ASTYLEDIFF)"
  82. fi
  83. FILES_CHANGED=1
  84. else
  85. rm "$ASTYLEDIFF"
  86. fi
  87. exit $FILES_CHANGED
  88. # vim: set ts=2 expandtab :