|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +# Add files or directories to exclude when formatting (supports globs) |
| 6 | +EXCLUDED_FILES=( |
| 7 | + # The paths src/libs/* and src/FreeRTOS/* are excluded from formatting |
| 8 | + # test-format. |
| 9 | + "./src/libs/*" |
| 10 | + "./src/FreeRTOS/*" |
| 11 | + # Upstream uses test-format.sh in the pipeline, but it is only used |
| 12 | + # to check the files changed in a PR. It appears that the whole repo |
| 13 | + # has not yet been formatted completely. |
| 14 | + # These files are reformatted differently when running format.sh. |
| 15 | + # To avoid ceating large unneccessary diffs to upstream, avoid formatting. |
| 16 | + "./src/drivers/Bma421_C/bma4_defs.h" |
| 17 | + "./src/drivers/Bma421_C/bma4.h" |
| 18 | + "./src/drivers/Bma421_C/bma423.h" |
| 19 | + "./bootloader/boot_graphics.h" |
| 20 | +) |
| 21 | + |
| 22 | +SOURCE_FILES=$(find . -type f \( -iname '*.h' -o -iname '*.cpp' \) -a \( -not -path "./build*" -a -not -path "./cmake-build*" \)) |
| 23 | + |
| 24 | +formatted_count=0 |
| 25 | +skipped_count=0 |
| 26 | + |
| 27 | + |
| 28 | +for file in $SOURCE_FILES; do |
| 29 | + [ -e "$file" ] || continue |
| 30 | + |
| 31 | + for excluded in "${EXCLUDED_FILES[@]}"; do |
| 32 | + # We want to glob match here to support globs in EXCLUDED_FILES. |
| 33 | + # shellcheck disable=SC2053 |
| 34 | + if [[ "$file" == $excluded ]]; then |
| 35 | + echo "Excluding $file" |
| 36 | + skipped_count=$((skipped_count + 1)) |
| 37 | + continue 2 # Skip both inner and outer loop iteration to go to next file |
| 38 | + fi |
| 39 | + done |
| 40 | + |
| 41 | + echo Formatting "$file" |
| 42 | + formatted_count=$((formatted_count + 1)) |
| 43 | + clang-format-14 --style file -i "$file" |
| 44 | +done |
| 45 | + |
| 46 | +echo "Files formatted: $formatted_count" |
| 47 | +echo "Files skipped: $skipped_count" |
| 48 | + |
| 49 | +exit 0 |
0 commit comments