2020-07-13 09:34:34 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# This script ensures proper POSIX text file formatting and a few other things.
|
2020-07-25 21:38:34 +02:00
|
|
|
# This is supplementary to clang_format.sh and black_format.sh, but should be
|
|
|
|
# run before them.
|
|
|
|
|
|
|
|
# We need dos2unix and recode.
|
|
|
|
if [ ! -x "$(command -v dos2unix)" -o ! -x "$(command -v recode)" ]; then
|
|
|
|
printf "Install 'dos2unix' and 'recode' to use this script.\n"
|
|
|
|
fi
|
2020-07-13 09:34:34 +02: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
|
|
|
|
elif [[ "$f" == *"patch" ]]; then
|
|
|
|
continue
|
|
|
|
elif [[ "$f" == *"pot" ]]; then
|
|
|
|
continue
|
|
|
|
elif [[ "$f" == *"po" ]]; then
|
|
|
|
continue
|
|
|
|
elif [[ "$f" == "thirdparty"* ]]; then
|
|
|
|
continue
|
|
|
|
elif [[ "$f" == "platform/android/java/lib/src/com/google"* ]]; then
|
|
|
|
continue
|
|
|
|
fi
|
2020-07-28 08:56:05 +02:00
|
|
|
# Ensure that files are UTF-8 formatted.
|
2020-07-13 09:34:34 +02:00
|
|
|
recode UTF-8 "$f" 2> /dev/null
|
2020-07-28 08:56:05 +02:00
|
|
|
# Ensure that files have LF line endings and do not contain a BOM.
|
2020-07-13 09:34:34 +02:00
|
|
|
dos2unix "$f" 2> /dev/null
|
2020-07-28 08:56:05 +02:00
|
|
|
# Remove trailing space characters and ensures that files end
|
|
|
|
# with newline characters. -l option handles newlines conveniently.
|
2020-07-25 21:38:34 +02:00
|
|
|
perl -i -ple 's/\s*$//g' "$f"
|
2020-07-13 09:34:34 +02:00
|
|
|
# Remove the character sequence "== true" if it has a leading space.
|
2020-07-25 21:38:34 +02:00
|
|
|
perl -i -pe 's/\x20== true//g' "$f"
|
2020-09-23 08:17:14 +02:00
|
|
|
|
|
|
|
if [[ $(uname) == "Linux" ]] && [[ "$f" != *"xml" ]]; then
|
|
|
|
# Remove empty lines after the opening brace of indented blocks.
|
|
|
|
sed -z -i 's/\x7B\x0A\x0A\x09/\x7B\x0A\x09/g' "$f"
|
|
|
|
# Remove empty lines before the closing brace (in some cases).
|
|
|
|
sed -z -i 's/\x0A\x0A\x7D/\x0A\x7D/g' "$f"
|
|
|
|
fi
|
2020-07-13 09:34:34 +02:00
|
|
|
done
|
|
|
|
|
|
|
|
git diff > patch.patch
|
|
|
|
|
|
|
|
# If no patch has been generated all is OK, clean up, and exit.
|
2020-07-25 21:38:34 +02:00
|
|
|
if [ ! -s patch.patch ] ; then
|
2020-07-13 09:34:34 +02:00
|
|
|
printf "Files in this commit comply with the formatting rules.\n"
|
|
|
|
rm -f patch.patch
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# A patch has been created, notify the user, clean up, and exit.
|
|
|
|
printf "\n*** The following differences were found between the code "
|
|
|
|
printf "and the formatting rules:\n\n"
|
|
|
|
cat patch.patch
|
|
|
|
printf "\n*** Aborting, please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\n"
|
|
|
|
rm -f patch.patch
|
|
|
|
exit 1
|