clang_format.sh 1.8 KB

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