summaryrefslogtreecommitdiff
path: root/misc/scripts
diff options
context:
space:
mode:
authorNathan Franke <me@nathan.sh>2022-01-27 10:34:33 -0600
committerNathan Franke <me@nathan.sh>2022-01-29 04:41:03 -0600
commit8a0a3acceee804b91afe31022cf0310c01162f73 (patch)
tree7bd0ff57dadd6d26c5d03bd49658c6c4568788c1 /misc/scripts
parent01f5d7c616920373ff7d140673bc6f8301213713 (diff)
simplify formatting scripts, add a clang-tidy script, and run clang-tidy
Diffstat (limited to 'misc/scripts')
-rwxr-xr-xmisc/scripts/black_format.sh16
-rwxr-xr-xmisc/scripts/clang_format.sh53
-rwxr-xr-xmisc/scripts/clang_tidy.sh39
-rwxr-xr-xmisc/scripts/file_format.sh7
4 files changed, 68 insertions, 47 deletions
diff --git a/misc/scripts/black_format.sh b/misc/scripts/black_format.sh
index 2ad9a23832..45d7f9dd2b 100755
--- a/misc/scripts/black_format.sh
+++ b/misc/scripts/black_format.sh
@@ -6,28 +6,20 @@ set -uo pipefail
# Apply black.
echo -e "Formatting Python files..."
-PY_FILES=$(find \( -path "./.git" \
- -o -path "./thirdparty" \
- \) -prune \
- -o \( -name "SConstruct" \
- -o -name "SCsub" \
- -o -name "*.py" \
- \) -print)
+PY_FILES=$(git ls-files '*SConstruct' '*SCsub' '*.py' --exclude='.git/*' --exclude='thirdparty/*')
black -l 120 $PY_FILES
-git diff --color > patch.patch
+diff=$(git diff --color)
# If no patch has been generated all is OK, clean up, and exit.
-if [ ! -s patch.patch ] ; then
+if [ -z "$diff" ] ; then
printf "Files in this commit comply with the black style 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
+echo "$diff"
printf "\n*** Aborting, please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\n"
-rm -f patch.patch
exit 1
diff --git a/misc/scripts/clang_format.sh b/misc/scripts/clang_format.sh
index 0006b82280..311944d3b4 100755
--- a/misc/scripts/clang_format.sh
+++ b/misc/scripts/clang_format.sh
@@ -4,13 +4,10 @@
# This is the primary script responsible for fixing style violations.
set -uo pipefail
-IFS=$'\n\t'
-CLANG_FORMAT_FILE_EXTS=(".c" ".h" ".cpp" ".hpp" ".cc" ".hh" ".cxx" ".m" ".mm" ".inc" ".java" ".glsl")
-
-# Loops through all text files tracked by Git.
-git grep -zIl '' |
-while IFS= read -rd '' f; do
+# Loops through all code files tracked by Git.
+git ls-files '*.c' '*.h' '*.cpp' '*.hpp' '*.cc' '*.hh' '*.cxx' '*.m' '*.mm' '*.inc' '*.java' '*.glsl' |
+while read -r f; do
# Exclude some files.
if [[ "$f" == "thirdparty"* ]]; then
continue
@@ -20,37 +17,31 @@ while IFS= read -rd '' f; do
continue
fi
- for extension in ${CLANG_FORMAT_FILE_EXTS[@]}; do
- if [[ "$f" == *"$extension" ]]; then
- # Run clang-format.
- clang-format --Wno-error=unknown -i "$f"
- # Fix copyright headers, but not all files get them.
- if [[ "$f" == *"inc" ]]; then
- continue 2
- elif [[ "$f" == *"glsl" ]]; then
- continue 2
- elif [[ "$f" == "platform/android/java/lib/src/org/godotengine/godot/input/InputManager"* ]]; then
- continue 2
- fi
- python misc/scripts/copyright_headers.py "$f"
- continue 2
- fi
- done
+ # Run clang-format.
+ clang-format --Wno-error=unknown -i "$f"
+
+ # Fix copyright headers, but not all files get them.
+ if [[ "$f" == *"inc" ]]; then
+ continue
+ elif [[ "$f" == *"glsl" ]]; then
+ continue
+ elif [[ "$f" == "platform/android/java/lib/src/org/godotengine/godot/input/InputManager"* ]]; then
+ continue
+ fi
+
+ python misc/scripts/copyright_headers.py "$f"
done
-git diff --color > patch.patch
+diff=$(git diff --color)
# If no patch has been generated all is OK, clean up, and exit.
-if [ ! -s patch.patch ] ; then
- printf "Files in this commit comply with the clang-format style rules.\n"
- rm -f patch.patch
+if [ -z "$diff" ] ; then
+ printf "Files in this commit comply with the clang-tidy style rules.\n"
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
+printf "\n*** The following changes have been made to comply with the formatting rules:\n\n"
+echo "$diff"
+printf "\n*** Please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\n"
exit 1
diff --git a/misc/scripts/clang_tidy.sh b/misc/scripts/clang_tidy.sh
new file mode 100755
index 0000000000..85519c0e72
--- /dev/null
+++ b/misc/scripts/clang_tidy.sh
@@ -0,0 +1,39 @@
+#!/usr/bin/env bash
+
+# This script runs clang-tidy on all relevant files in the repo.
+# This is more thorough than clang-format and thus slower; it should only be run manually.
+
+set -uo pipefail
+
+# Loops through all code files tracked by Git.
+git ls-files '*.c' '*.h' '*.cpp' '*.hpp' '*.cc' '*.hh' '*.cxx' '*.m' '*.mm' '*.inc' '*.java' '*.glsl' |
+while read -r f; do
+ # Exclude some files.
+ if [[ "$f" == "thirdparty"* ]]; then
+ continue
+ elif [[ "$f" == "platform/android/java/lib/src/com/google"* ]]; then
+ continue
+ elif [[ "$f" == *"-so_wrap."* ]]; then
+ continue
+ fi
+
+ # Run clang-tidy.
+ clang-tidy --quiet --fix "$f" &> /dev/null
+
+ # Run clang-format. This also fixes the output of clang-tidy.
+ clang-format --Wno-error=unknown -i "$f"
+done
+
+diff=$(git diff --color)
+
+# If no patch has been generated all is OK, clean up, and exit.
+if [ -z "$diff" ] ; then
+ printf "Files in this commit comply with the clang-tidy style rules.\n"
+ exit 0
+fi
+
+# A patch has been created, notify the user, clean up, and exit.
+printf "\n*** The following changes have been made to comply with the formatting rules:\n\n"
+echo "$diff"
+printf "\n*** Please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\n"
+exit 1
diff --git a/misc/scripts/file_format.sh b/misc/scripts/file_format.sh
index e66bc88bc0..0c7235817d 100755
--- a/misc/scripts/file_format.sh
+++ b/misc/scripts/file_format.sh
@@ -47,10 +47,10 @@ while IFS= read -rd '' f; do
perl -i -ple 's/\s*$//g' "$f"
done
-git diff --color > patch.patch
+diff=$(git diff --color)
# If no patch has been generated all is OK, clean up, and exit.
-if [ ! -s patch.patch ] ; then
+if [ -z "$diff" ] ; then
printf "Files in this commit comply with the formatting rules.\n"
rm -f patch.patch
exit 0
@@ -59,7 +59,6 @@ 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
+echo "$diff"
printf "\n*** Aborting, please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\n"
-rm -f patch.patch
exit 1