Skip to content

Commit eca30e6

Browse files
committed
Restore file format.
1 parent 78dbf97 commit eca30e6

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

misc/scripts/file_format.sh

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

0 commit comments

Comments
 (0)