clang_format.sh 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 -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/input/InputManager"* ]]; then
  30. continue 2
  31. fi
  32. python misc/scripts/copyright_headers.py "$f"
  33. continue 2
  34. fi
  35. done
  36. done
  37. git diff --color > patch.patch
  38. # If no patch has been generated all is OK, clean up, and exit.
  39. if [ ! -s patch.patch ] ; then
  40. printf "Files in this commit comply with the clang-format style rules.\n"
  41. rm -f patch.patch
  42. exit 0
  43. fi
  44. # A patch has been created, notify the user, clean up, and exit.
  45. printf "\n*** The following differences were found between the code "
  46. printf "and the formatting rules:\n\n"
  47. cat patch.patch
  48. printf "\n*** Aborting, please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\n"
  49. rm -f patch.patch
  50. exit 1