file_format.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. exit 1
  9. fi
  10. set -uo pipefail
  11. if [ $# -eq 0 ]; then
  12. # Loop through all code files tracked by Git.
  13. mapfile -d '' files < <(git grep -zIl '')
  14. else
  15. # $1 should be a file listing file paths to process. Used in CI.
  16. mapfile -d ' ' < <(cat "$1")
  17. fi
  18. for f in "${files[@]}"; do
  19. # Exclude some types of files.
  20. if [[ "$f" == *"csproj" ]]; then
  21. continue
  22. elif [[ "$f" == *"sln" ]]; then
  23. continue
  24. elif [[ "$f" == *".bat" ]]; then
  25. continue
  26. elif [[ "$f" == *".out" ]]; then
  27. # GDScript integration testing files.
  28. continue
  29. elif [[ "$f" == *"patch" ]]; then
  30. continue
  31. elif [[ "$f" == *"pot" ]]; then
  32. continue
  33. elif [[ "$f" == *"po" ]]; then
  34. continue
  35. elif [[ "$f" == "thirdparty/"* ]]; then
  36. continue
  37. elif [[ "$f" == *"/thirdparty/"* ]]; then
  38. continue
  39. elif [[ "$f" == "platform/android/java/lib/src/com/google"* ]]; then
  40. continue
  41. elif [[ "$f" == *"-so_wrap."* ]]; then
  42. continue
  43. elif [[ "$f" == *".test.txt" ]]; then
  44. continue
  45. fi
  46. # Ensure that files are UTF-8 formatted.
  47. isutf8 "$f" >> utf8-validation.txt 2>&1
  48. # Ensure that files have LF line endings and do not contain a BOM.
  49. dos2unix "$f" 2> /dev/null
  50. # Remove trailing space characters and ensures that files end
  51. # with newline characters. -l option handles newlines conveniently.
  52. perl -i -ple 's/\s*$//g' "$f"
  53. done
  54. diff=$(git diff --color)
  55. if [ ! -s utf8-validation.txt ] && [ -z "$diff" ] ; then
  56. # If no UTF-8 violations were collected (the file is empty) and
  57. # no diff has been generated all is OK, clean up, and exit.
  58. printf "\e[1;32m*** Files in this commit comply with the file formatting rules.\e[0m\n"
  59. rm -f utf8-validation.txt
  60. exit 0
  61. fi
  62. if [ -s utf8-validation.txt ]
  63. then
  64. # If the file has content and is not empty, violations
  65. # detected, notify the user, clean up, and exit.
  66. printf "\n\e[1;33m*** The following files contain invalid UTF-8 character sequences:\e[0m\n\n"
  67. cat utf8-validation.txt
  68. fi
  69. rm -f utf8-validation.txt
  70. if [ ! -z "$diff" ]
  71. then
  72. # A diff has been created, notify the user, clean up, and exit.
  73. printf "\n\e[1;33m*** The following changes must be made to comply with the formatting rules:\e[0m\n\n"
  74. # Perl commands replace trailing spaces with `·` and tabs with `<TAB>`.
  75. 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'
  76. fi
  77. printf "\n\e[1;91m*** Please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\e[0m\n"
  78. exit 1