verify-indentation.sh 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/bin/bash
  2. cd $(git rev-parse --show-toplevel)
  3. export PATH=$PATH:$PWD/scripts
  4. if [ -z "$TRAVIS_COMMIT_RANGE" ]; then
  5. echo "No commit range given"
  6. exit 0
  7. fi
  8. if ! type -p astyle.sh >/dev/null; then
  9. echo astyle.sh not found
  10. exit 1
  11. fi
  12. set -e
  13. ASTYLEDIFF=/tmp/astyle.diff
  14. >$ASTYLEDIFF
  15. if [[ ! -z $TRAVIS_PULL_REQUEST_BRANCH ]]; then
  16. # if on a PR, just analyse the changed files
  17. echo "TRAVIS PR BRANCH: $TRAVIS_PULL_REQUEST_BRANCH"
  18. FILES=$(git diff --diff-filter=AM --name-only $(git merge-base HEAD master) | tr '\n' ' ' )
  19. elif [[ ! -z $TRAVIS_COMMIT_RANGE ]]; then
  20. echo "TRAVIS COMMIT RANGE: $TRAVIS_COMMIT_RANGE"
  21. FILES=$(git diff --diff-filter=AM --name-only ${TRAVIS_COMMIT_RANGE/.../..} | tr '\n' ' ' )
  22. fi
  23. for f in $FILES; do
  24. if ! [ -f "$f" ]; then
  25. echo "$f was removed." >>/tmp/ctest-important.log
  26. continue
  27. fi
  28. echo "Checking $f" >>/tmp/ctest-important.log
  29. case "$f" in
  30. thirdparty*)
  31. echo "$f skipped"
  32. continue
  33. ;;
  34. *.cpp|*.c|*.h|*.cxx|*.hxx|*.c++|*.h++|*.cc|*.hh|*.C|*.H|*.sip|*.py)
  35. ;;
  36. *)
  37. continue
  38. ;;
  39. esac
  40. m="$f.prepare"
  41. cp "$f" "$m"
  42. astyle.sh "$f"
  43. if diff -u "$m" "$f" >>$ASTYLEDIFF; then
  44. rm "$m"
  45. else
  46. echo "File $f needs indentation"
  47. fi
  48. done
  49. if [ -s "$ASTYLEDIFF" ]; then
  50. echo
  51. echo "Required indentation updates:"
  52. cat "$ASTYLEDIFF"
  53. cat <<EOF
  54. Tips to prevent and resolve:
  55. * Enable WITH_ASTYLE in your cmake configuration to format C++ code
  56. * Install autopep8 (>= 1.2.1) to format python code
  57. * Use "scripts/astyle.sh file" to fix the now badly indented files
  58. * Consider using scripts/prepare-commit.sh as pre-commit hook to avoid this
  59. in the future (ln -s scripts/prepare-commit.sh .git/hooks/pre-commit) or
  60. run it manually before each commit.
  61. EOF
  62. exit 1
  63. fi