2020-07-24 08:19:19 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# This script ensures proper POSIX text file formatting and a few other things.
|
2020-07-25 19:38:34 +00:00
|
|
|
# This is supplementary to clang_format.sh and black_format.sh, but should be
|
|
|
|
# run before them.
|
|
|
|
|
|
|
|
# We need dos2unix and recode.
|
2022-09-07 21:12:00 +00:00
|
|
|
if [ ! -x "$(command -v dos2unix)" -o ! -x "$(command -v isutf8)" ]; then
|
|
|
|
printf "Install 'dos2unix' and 'isutf8' (from the moreutils package) to use this script.\n"
|
2020-07-25 19:38:34 +00:00
|
|
|
fi
|
2020-07-24 08:19:19 +00:00
|
|
|
|
|
|
|
set -uo pipefail
|
|
|
|
IFS=$'\n\t'
|
|
|
|
|
|
|
|
# Loops through all text files tracked by Git.
|
|
|
|
git grep -zIl '' |
|
|
|
|
while IFS= read -rd '' f; do
|
|
|
|
# Exclude some types of files.
|
|
|
|
if [[ "$f" == *"csproj" ]]; then
|
|
|
|
continue
|
|
|
|
elif [[ "$f" == *"sln" ]]; then
|
|
|
|
continue
|
2022-01-06 01:53:08 +00:00
|
|
|
elif [[ "$f" == *".bat" ]]; then
|
|
|
|
continue
|
2020-07-24 08:19:19 +00:00
|
|
|
elif [[ "$f" == *"patch" ]]; then
|
|
|
|
continue
|
|
|
|
elif [[ "$f" == *"pot" ]]; then
|
|
|
|
continue
|
|
|
|
elif [[ "$f" == *"po" ]]; then
|
|
|
|
continue
|
2022-11-12 23:46:13 +00:00
|
|
|
elif [[ "$f" == "thirdparty/"* ]]; then
|
|
|
|
continue
|
|
|
|
elif [[ "$f" == *"/thirdparty/"* ]]; then
|
2020-07-24 08:19:19 +00:00
|
|
|
continue
|
|
|
|
elif [[ "$f" == "platform/android/java/lib/src/com/google"* ]]; then
|
|
|
|
continue
|
2021-02-17 10:28:27 +00:00
|
|
|
elif [[ "$f" == *"-so_wrap."* ]]; then
|
|
|
|
continue
|
2020-07-24 08:19:19 +00:00
|
|
|
fi
|
2020-07-28 06:56:05 +00:00
|
|
|
# Ensure that files are UTF-8 formatted.
|
2022-09-07 21:12:00 +00:00
|
|
|
isutf8 "$f" >> utf8-validation.txt 2>&1
|
2020-07-28 06:56:05 +00:00
|
|
|
# Ensure that files have LF line endings and do not contain a BOM.
|
2020-07-24 08:19:19 +00:00
|
|
|
dos2unix "$f" 2> /dev/null
|
2020-07-28 06:56:05 +00:00
|
|
|
# Remove trailing space characters and ensures that files end
|
|
|
|
# with newline characters. -l option handles newlines conveniently.
|
2020-07-25 19:38:34 +00:00
|
|
|
perl -i -ple 's/\s*$//g' "$f"
|
2020-07-24 08:19:19 +00:00
|
|
|
# Remove the character sequence "== true" if it has a leading space.
|
2020-07-25 19:38:34 +00:00
|
|
|
perl -i -pe 's/\x20== true//g' "$f"
|
2020-07-24 08:19:19 +00:00
|
|
|
done
|
|
|
|
|
2021-08-25 15:05:16 +00:00
|
|
|
git diff --color > patch.patch
|
2020-07-24 08:19:19 +00:00
|
|
|
|
2022-09-07 21:12:00 +00:00
|
|
|
# If no UTF-8 violations were collected and no patch has been
|
|
|
|
# generated all is OK, clean up, and exit.
|
|
|
|
if [ ! -s utf8-validation.txt ] && [ ! -s patch.patch ] ; then
|
2020-07-24 08:19:19 +00:00
|
|
|
printf "Files in this commit comply with the formatting rules.\n"
|
2022-09-07 21:12:00 +00:00
|
|
|
rm -f patch.patch utf8-validation.txt
|
2020-07-24 08:19:19 +00:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2022-09-07 21:12:00 +00:00
|
|
|
# Violations detected, notify the user, clean up, and exit.
|
|
|
|
if [ -s utf8-validation.txt ]
|
|
|
|
then
|
|
|
|
printf "\n*** The following files contain invalid UTF-8 character sequences:\n\n"
|
|
|
|
cat utf8-validation.txt
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -s patch.patch ]
|
|
|
|
then
|
|
|
|
printf "\n*** The following differences were found between the code "
|
|
|
|
printf "and the formatting rules:\n\n"
|
|
|
|
cat patch.patch
|
|
|
|
fi
|
|
|
|
rm -f utf8-validation.txt patch.patch
|
2020-07-24 08:19:19 +00:00
|
|
|
printf "\n*** Aborting, please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\n"
|
|
|
|
exit 1
|