clang_format.sh 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/env bash
  2. # This script runs clang-format 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. fi
  14. for extension in ${CLANG_FORMAT_FILE_EXTS[@]}; do
  15. if [[ "$f" == *"$extension" ]]; then
  16. # Run clang-format.
  17. clang-format -i "$f"
  18. continue 2
  19. fi
  20. done
  21. done
  22. git diff > patch.patch
  23. # If no patch has been generated all is OK, clean up, and exit.
  24. if [ ! -s patch.patch ] ; then
  25. printf "Files in this commit comply with the clang-format style rules.\n"
  26. rm -f patch.patch
  27. exit 0
  28. fi
  29. # A patch has been created, notify the user, clean up, and exit.
  30. printf "\n*** The following differences were found between the code "
  31. printf "and the formatting rules:\n\n"
  32. cat patch.patch
  33. printf "\n*** Aborting, please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\n"
  34. rm -f patch.patch
  35. exit 1