clang_format.sh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env bash
  2. # This script runs clang-format and fixes copyright headers on all relevant files in the repo.
  3. # This is the primary script responsible for fixing style violations.
  4. set -uo pipefail
  5. IFS=$'\n\t'
  6. CLANG_FORMAT_FILE_EXTS=(".c" ".h" ".cpp" ".hpp" ".cc" ".hh" ".cxx" ".m" ".mm" ".inc" ".java" ".glsl")
  7. # Loops through all text files tracked by Git.
  8. git grep -zIl '' |
  9. while IFS= read -rd '' f; do
  10. # Exclude some files.
  11. if [[ "$f" == "thirdparty"* ]]; then
  12. continue
  13. elif [[ "$f" == "platform/android/java/lib/src/com/google"* ]]; then
  14. continue
  15. elif [[ "$f" == *"-so_wrap."* ]]; then
  16. continue
  17. fi
  18. for extension in ${CLANG_FORMAT_FILE_EXTS[@]}; do
  19. if [[ "$f" == *"$extension" ]]; then
  20. # Run clang-format.
  21. clang-format --Wno-error=unknown -i "$f"
  22. # Fix copyright headers, but not all files get them.
  23. if [[ "$f" == *"inc" ]]; then
  24. continue 2
  25. elif [[ "$f" == *"glsl" ]]; then
  26. continue 2
  27. elif [[ "$f" == *"theme_data.h" ]]; then
  28. continue 2
  29. elif [[ "$f" == "platform/android/java/lib/src/org/godotengine/godot/gl/GLSurfaceView"* ]]; then
  30. continue 2
  31. elif [[ "$f" == "platform/android/java/lib/src/org/godotengine/godot/gl/EGLLogWrapper"* ]]; then
  32. continue 2
  33. elif [[ "$f" == "platform/android/java/lib/src/org/godotengine/godot/utils/ProcessPhoenix"* ]]; then
  34. continue 2
  35. fi
  36. python misc/scripts/copyright_headers.py "$f"
  37. continue 2
  38. fi
  39. done
  40. done
  41. git diff --color > patch.patch
  42. # If no patch has been generated all is OK, clean up, and exit.
  43. if [ ! -s patch.patch ] ; then
  44. printf "Files in this commit comply with the clang-format style rules.\n"
  45. rm -f patch.patch
  46. exit 0
  47. fi
  48. # A patch has been created, notify the user, clean up, and exit.
  49. printf "\n*** The following differences were found between the code "
  50. printf "and the formatting rules:\n\n"
  51. cat patch.patch
  52. printf "\n*** Aborting, please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\n"
  53. rm -f patch.patch
  54. exit 1