file_format.sh 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env bash
  2. # This script ensures proper POSIX text file formatting and a few other things.
  3. # This is supplementary to clang_format.sh and black_format.sh, but should be
  4. # run before them.
  5. # We need dos2unix and isutf8.
  6. if [ ! -x "$(command -v dos2unix)" -o ! -x "$(command -v isutf8)" ]; then
  7. printf "Install 'dos2unix' and 'isutf8' (moreutils package) to use this script.\n"
  8. fi
  9. set -uo pipefail
  10. IFS=$'\n\t'
  11. # Loops through all text files tracked by Git.
  12. git grep -zIl '' |
  13. while IFS= read -rd '' f; do
  14. # Exclude some types of files.
  15. if [[ "$f" == *"csproj" ]]; then
  16. continue
  17. elif [[ "$f" == *"sln" ]]; then
  18. continue
  19. elif [[ "$f" == *".bat" ]]; then
  20. continue
  21. elif [[ "$f" == *".out" ]]; then
  22. # GDScript integration testing files.
  23. continue
  24. elif [[ "$f" == *"patch" ]]; then
  25. continue
  26. elif [[ "$f" == *"pot" ]]; then
  27. continue
  28. elif [[ "$f" == *"po" ]]; then
  29. continue
  30. elif [[ "$f" == "thirdparty/"* ]]; then
  31. continue
  32. elif [[ "$f" == *"/thirdparty/"* ]]; then
  33. continue
  34. elif [[ "$f" == "platform/android/java/lib/src/com/google"* ]]; then
  35. continue
  36. elif [[ "$f" == *"-so_wrap."* ]]; then
  37. continue
  38. elif [[ "$f" == *".test.txt" ]]; then
  39. continue
  40. fi
  41. # Ensure that files are UTF-8 formatted.
  42. isutf8 "$f" >> utf8-validation.txt 2>&1
  43. # Ensure that files have LF line endings and do not contain a BOM.
  44. dos2unix "$f" 2> /dev/null
  45. # Remove trailing space characters and ensures that files end
  46. # with newline characters. -l option handles newlines conveniently.
  47. perl -i -ple 's/\s*$//g' "$f"
  48. done
  49. diff=$(git diff --color)
  50. if [ ! -s utf8-validation.txt ] && [ -z "$diff" ] ; then
  51. # If no UTF-8 violations were collected (the file is empty) and
  52. # no diff has been generated all is OK, clean up, and exit.
  53. printf "\e[1;32m*** Files in this commit comply with the file formatting rules.\e[0m\n"
  54. rm -f utf8-validation.txt
  55. exit 0
  56. fi
  57. if [ -s utf8-validation.txt ]
  58. then
  59. # If the file has content and is not empty, violations
  60. # detected, notify the user, clean up, and exit.
  61. printf "\n\e[1;33m*** The following files contain invalid UTF-8 character sequences:\e[0m\n\n"
  62. cat utf8-validation.txt
  63. fi
  64. rm -f utf8-validation.txt
  65. if [ ! -z "$diff" ]
  66. then
  67. # A diff has been created, notify the user, clean up, and exit.
  68. printf "\n\e[1;33m*** The following changes must be made to comply with the formatting rules:\e[0m\n\n"
  69. # Perl commands replace trailing spaces with `·` and tabs with `<TAB>`.
  70. 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'
  71. fi
  72. printf "\n\e[1;91m*** Please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\e[0m\n"
  73. exit 1