2020-07-13 15:34:34 +08:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# This script ensures proper POSIX text file formatting and a few other things.
|
2020-07-26 03:38:34 +08:00
|
|
|
# This is supplementary to clang_format.sh and black_format.sh, but should be
|
|
|
|
# run before them.
|
|
|
|
|
2023-01-13 06:16:02 +08:00
|
|
|
# We need dos2unix and isutf8.
|
|
|
|
if [ ! -x "$(command -v dos2unix)" -o ! -x "$(command -v isutf8)" ]; then
|
|
|
|
printf "Install 'dos2unix' and 'isutf8' (moreutils package) to use this script.\n"
|
2023-04-19 21:10:36 +08:00
|
|
|
exit 1
|
2020-07-26 03:38:34 +08:00
|
|
|
fi
|
2020-07-13 15:34:34 +08:00
|
|
|
|
|
|
|
set -uo pipefail
|
|
|
|
|
2023-04-19 21:10:36 +08:00
|
|
|
if [ $# -eq 0 ]; then
|
|
|
|
# Loop through all code files tracked by Git.
|
|
|
|
mapfile -d '' files < <(git grep -zIl '')
|
|
|
|
else
|
|
|
|
# $1 should be a file listing file paths to process. Used in CI.
|
|
|
|
mapfile -d ' ' < <(cat "$1")
|
|
|
|
fi
|
|
|
|
|
|
|
|
for f in "${files[@]}"; do
|
2020-07-13 15:34:34 +08:00
|
|
|
# Exclude some types of files.
|
|
|
|
if [[ "$f" == *"csproj" ]]; then
|
|
|
|
continue
|
|
|
|
elif [[ "$f" == *"sln" ]]; then
|
|
|
|
continue
|
2022-01-06 09:53:08 +08:00
|
|
|
elif [[ "$f" == *".bat" ]]; then
|
|
|
|
continue
|
2021-04-20 02:50:52 +08:00
|
|
|
elif [[ "$f" == *".out" ]]; then
|
|
|
|
# GDScript integration testing files.
|
|
|
|
continue
|
2020-07-13 15:34:34 +08:00
|
|
|
elif [[ "$f" == *"patch" ]]; then
|
|
|
|
continue
|
|
|
|
elif [[ "$f" == *"pot" ]]; then
|
|
|
|
continue
|
|
|
|
elif [[ "$f" == *"po" ]]; then
|
|
|
|
continue
|
2022-11-13 07:46:13 +08:00
|
|
|
elif [[ "$f" == "thirdparty/"* ]]; then
|
|
|
|
continue
|
|
|
|
elif [[ "$f" == *"/thirdparty/"* ]]; then
|
2020-07-13 15:34:34 +08:00
|
|
|
continue
|
|
|
|
elif [[ "$f" == "platform/android/java/lib/src/com/google"* ]]; then
|
|
|
|
continue
|
2021-02-17 18:28:27 +08:00
|
|
|
elif [[ "$f" == *"-so_wrap."* ]]; then
|
|
|
|
continue
|
2022-08-01 00:46:53 +08:00
|
|
|
elif [[ "$f" == *".test.txt" ]]; then
|
|
|
|
continue
|
2020-07-13 15:34:34 +08:00
|
|
|
fi
|
2020-07-28 14:56:05 +08:00
|
|
|
# Ensure that files are UTF-8 formatted.
|
2022-09-05 17:55:51 +08:00
|
|
|
isutf8 "$f" >> utf8-validation.txt 2>&1
|
2020-07-28 14:56:05 +08:00
|
|
|
# Ensure that files have LF line endings and do not contain a BOM.
|
2020-07-13 15:34:34 +08:00
|
|
|
dos2unix "$f" 2> /dev/null
|
2020-07-28 14:56:05 +08:00
|
|
|
# Remove trailing space characters and ensures that files end
|
|
|
|
# with newline characters. -l option handles newlines conveniently.
|
2020-07-26 03:38:34 +08:00
|
|
|
perl -i -ple 's/\s*$//g' "$f"
|
2020-07-13 15:34:34 +08:00
|
|
|
done
|
|
|
|
|
2022-01-28 00:34:33 +08:00
|
|
|
diff=$(git diff --color)
|
2020-07-13 15:34:34 +08:00
|
|
|
|
2022-09-05 17:55:51 +08:00
|
|
|
if [ ! -s utf8-validation.txt ] && [ -z "$diff" ] ; then
|
2023-01-13 06:16:02 +08:00
|
|
|
# If no UTF-8 violations were collected (the file is empty) and
|
|
|
|
# no diff has been generated all is OK, clean up, and exit.
|
2023-04-05 19:51:38 +08:00
|
|
|
printf "\e[1;32m*** Files in this commit comply with the file formatting rules.\e[0m\n"
|
2023-01-13 06:16:02 +08:00
|
|
|
rm -f utf8-validation.txt
|
2020-07-13 15:34:34 +08:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2022-09-05 17:55:51 +08:00
|
|
|
if [ -s utf8-validation.txt ]
|
|
|
|
then
|
2023-01-13 06:16:02 +08:00
|
|
|
# If the file has content and is not empty, violations
|
|
|
|
# detected, notify the user, clean up, and exit.
|
2023-04-05 19:51:38 +08:00
|
|
|
printf "\n\e[1;33m*** The following files contain invalid UTF-8 character sequences:\e[0m\n\n"
|
2022-09-05 17:55:51 +08:00
|
|
|
cat utf8-validation.txt
|
|
|
|
fi
|
|
|
|
|
2023-01-13 06:16:02 +08:00
|
|
|
rm -f utf8-validation.txt
|
|
|
|
|
2022-09-05 17:55:51 +08:00
|
|
|
if [ ! -z "$diff" ]
|
|
|
|
then
|
2023-04-05 19:51:38 +08:00
|
|
|
# A diff has been created, notify the user, clean up, and exit.
|
|
|
|
printf "\n\e[1;33m*** The following changes must be made to comply with the formatting rules:\e[0m\n\n"
|
|
|
|
# Perl commands replace trailing spaces with `·` and tabs with `<TAB>`.
|
2024-02-02 19:10:02 +08:00
|
|
|
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'
|
2022-09-05 17:55:51 +08:00
|
|
|
fi
|
|
|
|
|
2023-04-05 19:51:38 +08:00
|
|
|
printf "\n\e[1;91m*** Please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\e[0m\n"
|
2020-07-13 15:34:34 +08:00
|
|
|
exit 1
|