|
4 | 4 |
|
5 | 5 | set -u -o pipefail |
6 | 6 |
|
7 | | -set -x |
8 | | - |
9 | 7 | REPO_ROOT="$(git rev-parse --show-toplevel)" |
10 | 8 |
|
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 |
32 | 59 | fi |
33 | 60 |
|
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