Skip to content

Commit f4070d9

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 9ad2eb4 commit f4070d9

File tree

1 file changed

+51
-24
lines changed

1 file changed

+51
-24
lines changed

.ci/check-format.sh

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,58 @@
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+
C_SOURCES=()
11+
while IFS= read -r file; do
12+
[ -n "$file" ] && C_SOURCES+=("$file")
13+
done < <(git ls-files -- '*.c' '*.cxx' '*.cpp' '*.h' '*.hpp')
14+
15+
if [ ${#C_SOURCES[@]} -gt 0 ]; then
16+
if command -v clang-format-18 > /dev/null 2>&1; then
17+
echo "Checking C/C++ files..."
18+
clang-format-18 -n --Werror "${C_SOURCES[@]}"
19+
C_FORMAT_EXIT=$?
20+
else
21+
echo "Skipping C/C++ format check: clang-format-18 not found" >&2
22+
C_FORMAT_EXIT=0
23+
fi
24+
else
25+
C_FORMAT_EXIT=0
26+
fi
27+
28+
SH_SOURCES=()
29+
while IFS= read -r file; do
30+
[ -n "$file" ] && SH_SOURCES+=("$file")
31+
done < <(git ls-files -- '*.sh')
32+
33+
if [ ${#SH_SOURCES[@]} -gt 0 ]; then
34+
echo "Checking shell scripts..."
35+
MISMATCHED_SH=$(shfmt -l "${SH_SOURCES[@]}")
36+
if [ -n "$MISMATCHED_SH" ]; then
37+
echo "The following shell scripts are not formatted correctly:"
38+
printf '%s\n' "$MISMATCHED_SH"
39+
shfmt -d "${SH_SOURCES[@]}"
40+
SH_FORMAT_EXIT=1
41+
else
42+
SH_FORMAT_EXIT=0
43+
fi
44+
else
45+
SH_FORMAT_EXIT=0
46+
fi
47+
48+
PY_SOURCES=()
49+
while IFS= read -r file; do
50+
[ -n "$file" ] && PY_SOURCES+=("$file")
51+
done < <(git ls-files -- '*.py')
52+
53+
if [ ${#PY_SOURCES[@]} -gt 0 ]; then
54+
echo "Checking Python files..."
55+
black --check --diff "${PY_SOURCES[@]}"
56+
PY_FORMAT_EXIT=$?
57+
else
58+
PY_FORMAT_EXIT=0
3259
fi
3360

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

0 commit comments

Comments
 (0)