Skip to content

Commit e92c47c

Browse files
committed
CI: Simplify format checker
This replaces temporary file generation with clang-format's built-in dry-run mode. - Use clang-format -n --Werror instead of diff with temp files - Use git ls-files to filter tracked files only - Use mapfile for bash 4+ compatibility - Capture exit codes properly for each checker
1 parent fde86ac commit e92c47c

File tree

1 file changed

+37
-24
lines changed

1 file changed

+37
-24
lines changed

.ci/check-format.sh

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,44 @@
44

55
set -u -o pipefail
66

7-
set -x
8-
97
REPO_ROOT="$(git rev-parse --show-toplevel)"
108

11-
C_SOURCES=$(find "${REPO_ROOT}" | egrep "\.(c|cxx|cpp|h|hpp)$")
12-
for file in ${C_SOURCES}; do
13-
clang-format-18 ${file} > expected-format
14-
diff -u -p --label="${file}" --label="expected coding style" ${file} expected-format
15-
done
16-
C_MISMATCH_LINE_CNT=$(clang-format-18 --output-replacements-xml ${C_SOURCES} | egrep -c "</replacement>")
17-
18-
SH_SOURCES=$(find "${REPO_ROOT}" | egrep "\.sh$")
19-
for file in ${SH_SOURCES}; do
20-
shfmt -d "${file}"
21-
done
22-
SH_MISMATCH_FILE_CNT=$(shfmt -l ${SH_SOURCES})
23-
24-
PY_SOURCES=$(find "${REPO_ROOT}" | egrep "\.py$")
25-
for file in ${PY_SOURCES}; do
26-
echo "Checking Python file: ${file}"
27-
black --diff "${file}"
28-
done
29-
PY_MISMATCH_FILE_CNT=0
30-
if [ -n "${PY_SOURCES}" ]; then
31-
PY_MISMATCH_FILE_CNT=$(echo "$(black --check ${PY_SOURCES} 2>&1)" | grep -c "^would reformat ")
9+
# Use git ls-files to exclude submodules and untracked files
10+
mapfile -t C_SOURCES < <(git ls-files | grep -E '\.(c|cxx|cpp|h|hpp)$')
11+
12+
if [ ${#C_SOURCES[@]} -gt 0 ]; then
13+
echo "Checking C/C++ files..."
14+
clang-format-18 -n --Werror "${C_SOURCES[@]}"
15+
C_FORMAT_EXIT=$?
16+
else
17+
C_FORMAT_EXIT=0
18+
fi
19+
20+
mapfile -t SH_SOURCES < <(git ls-files | grep -E '\.sh$')
21+
22+
if [ ${#SH_SOURCES[@]} -gt 0 ]; then
23+
echo "Checking shell scripts..."
24+
MISMATCHED_SH=$(shfmt -l "${SH_SOURCES[@]}")
25+
if [ -n "$MISMATCHED_SH" ]; then
26+
echo "The following shell scripts are not formatted correctly:"
27+
echo "$MISMATCHED_SH"
28+
shfmt -d "${SH_SOURCES[@]}"
29+
SH_FORMAT_EXIT=1
30+
else
31+
SH_FORMAT_EXIT=0
32+
fi
33+
else
34+
SH_FORMAT_EXIT=0
35+
fi
36+
37+
mapfile -t PY_SOURCES < <(git ls-files | grep -E '\.py$')
38+
39+
if [ ${#PY_SOURCES[@]} -gt 0 ]; then
40+
echo "Checking Python files..."
41+
black --check --diff "${PY_SOURCES[@]}"
42+
PY_FORMAT_EXIT=$?
43+
else
44+
PY_FORMAT_EXIT=0
3245
fi
3346

34-
exit $((C_MISMATCH_LINE_CNT + SH_MISMATCH_FILE_CNT + PY_MISMATCH_FILE_CNT))
47+
exit $((C_FORMAT_EXIT + SH_FORMAT_EXIT + PY_FORMAT_EXIT))

0 commit comments

Comments
 (0)