clang_format.sh 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. # Loops through all code files tracked by Git.
  6. git ls-files -- '*.c' '*.h' '*.cpp' '*.hpp' '*.cc' '*.hh' '*.cxx' '*.m' '*.mm' '*.inc' '*.java' '*.glsl' \
  7. ':!:.git/*' ':!:thirdparty/*' ':!:*/thirdparty/*' ':!:platform/android/java/lib/src/com/google/*' \
  8. ':!:*-so_wrap.*' ':!:tests/python_build/*' |
  9. while read -r f; do
  10. # Run clang-format.
  11. clang-format --Wno-error=unknown -i "$f"
  12. # Fix copyright headers, but not all files get them.
  13. if [[ "$f" == *"inc" ]]; then
  14. continue
  15. elif [[ "$f" == *"glsl" ]]; then
  16. continue
  17. elif [[ "$f" == "platform/android/java/lib/src/org/godotengine/godot/gl/GLSurfaceView"* ]]; then
  18. continue
  19. elif [[ "$f" == "platform/android/java/lib/src/org/godotengine/godot/gl/EGLLogWrapper"* ]]; then
  20. continue
  21. elif [[ "$f" == "platform/android/java/lib/src/org/godotengine/godot/utils/ProcessPhoenix"* ]]; then
  22. continue
  23. fi
  24. python misc/scripts/copyright_headers.py "$f"
  25. done
  26. diff=$(git diff --color)
  27. # If no diff has been generated all is OK, clean up, and exit.
  28. if [ -z "$diff" ] ; then
  29. printf "\e[1;32m*** Files in this commit comply with the clang-format style rules.\e[0m\n"
  30. exit 0
  31. fi
  32. # A diff has been created, notify the user, clean up, and exit.
  33. printf "\n\e[1;33m*** The following changes must be made to comply with the formatting rules:\e[0m\n\n"
  34. # Perl commands replace trailing spaces with `·` and tabs with `<TAB>`.
  35. printf "$diff\n" | perl -pe 's/(.*[^ ])( +)(\e\[m)$/my $spaces="·" x length($2); sprintf("$1$spaces$3")/ge' | perl -pe 's/(.*[^\t])(\t+)(\e\[m)$/my $tabs="<TAB>" x length($2); sprintf("$1$tabs$3")/ge'
  36. printf "\n\e[1;91m*** Please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\e[0m\n"
  37. exit 1