clang_format.sh 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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" == "gen"* ]]; then
  14. # continue
  15. elif [[ "$f" == "platform/android/java/lib/src/com/google"* ]]; then
  16. continue
  17. elif [[ "$f" == *"-so_wrap."* ]]; then
  18. continue
  19. fi
  20. for extension in ${CLANG_FORMAT_FILE_EXTS[@]}; do
  21. if [[ "$f" == *"$extension" ]]; then
  22. # Run clang-format.
  23. clang-format -i "$f"
  24. # Fix copyright headers, but not all files get them.
  25. if [[ "$f" == *"inc" ]]; then
  26. continue 2
  27. elif [[ "$f" == *"glsl" ]]; then
  28. continue 2
  29. elif [[ "$f" == *"theme_data.h" ]]; then
  30. continue 2
  31. elif [[ "$f" == "platform/android/java/lib/src/org/godotengine/godot/input/InputManager"* ]]; then
  32. continue 2
  33. fi
  34. python misc/scripts/copyright_headers.py "$f"
  35. continue 2
  36. fi
  37. done
  38. done
  39. git diff > patch.patch
  40. # If no patch has been generated all is OK, clean up, and exit.
  41. if [ ! -s patch.patch ] ; then
  42. printf "Files in this commit comply with the clang-format style rules.\n"
  43. rm -f patch.patch
  44. exit 0
  45. fi
  46. # A patch has been created, notify the user, clean up, and exit.
  47. printf "\n*** The following differences were found between the code "
  48. printf "and the formatting rules:\n\n"
  49. cat patch.patch
  50. printf "\n*** Aborting, please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\n"
  51. rm -f patch.patch
  52. exit 1