clang_format.sh 2.3 KB

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