diff --git a/format.bat b/format.bat index d9f5f51..54d904f 100644 --- a/format.bat +++ b/format.bat @@ -18,6 +18,7 @@ IF "%~1"=="--check" ( ) ELSE IF "%~1"=="--fix" ( SET MODE=fix ) ELSE ( + REM Accumulate directories SET DIRS=!DIRS! "%~1" ) SHIFT @@ -54,14 +55,10 @@ FOR %%D IN (%DIRS%) DO ( ECHO Formatting !FILE! %FMT% -i "!FILE!" ) ELSE ( - REM Check mode: run clang-format --dry-run --Werror + ECHO Checking !FILE! %FMT% --dry-run --Werror "!FILE!" 2>nul IF ERRORLEVEL 1 ( - REM clang-format will already print diagnostic - ECHO --- Diff for !FILE! --- - REM Generate diff using fc - %FMT% "!FILE!" > "%TEMP%\fmt.tmp" - fc "!FILE!" "%TEMP%\fmt.tmp" + REM clang-format will already print diagnostics SET EXIT_CODE=1 ) ) diff --git a/format.sh b/format.sh index ff50922..eb8f665 100644 --- a/format.sh +++ b/format.sh @@ -4,8 +4,11 @@ set -euo pipefail FMT="clang-format" MODE="" DIRS=() +EXIT_CODE=0 +# ========================= # Parse arguments +# ========================= for arg in "$@"; do case "$arg" in --check) @@ -20,18 +23,21 @@ for arg in "$@"; do esac done +# Ensure mode is set if [[ -z "$MODE" ]]; then echo "Usage: $0 --check|--fix [ ...]" exit 1 fi +# Ensure at least one directory if [[ ${#DIRS[@]} -eq 0 ]]; then echo "Please provide at least one directory." exit 1 fi -EXIT_CODE=0 - +# ========================= +# Process directories +# ========================= for DIR in "${DIRS[@]}"; do if [[ ! -d "$DIR" ]]; then echo "$DIR is not a valid directory." @@ -39,25 +45,24 @@ for DIR in "${DIRS[@]}"; do continue fi - while IFS= read -r -d '' file; do - [[ "$file" == *_generated* ]] && continue + # Find all source files safely (null-separated) + while IFS= read -r -d '' FILE; do + [[ "$FILE" == *_generated* ]] && continue if [[ "$MODE" == "fix" ]]; then - echo "Formatting $file" - $FMT -i "$file" + echo "Formatting $FILE" + "$FMT" -i "$FILE" else - # Check mode: run clang-format with dry-run and Werror - # Capture stdout/stderr for diagnostic - if ! $FMT --dry-run --Werror "$file"; then - # clang-format will already print: - # Bigfoot/Tests/System/Time.cpp:21:46: error: code should be clang-formatted [-Wclang-format-violations] - # Now additionally print the diff for clarity - diff_output=$(diff -u "$file" <($FMT -style=file "$file") || true) - echo "$diff_output" + echo "Checking $FILE" + # Check mode: clang-format diagnostic only + if ! "$FMT" --dry-run --Werror "$FILE"; then + # clang-format prints: file:line:col: error ... EXIT_CODE=1 fi fi - done < <(find "$DIR" \( -name '*.h' -o -name '*.hpp' -o -name '*.c' -o -name '*.cpp' -o -name '*.m' -o -name '*.mm' \) -print0) + done < <( + find "$DIR" \( -name '*.h' -o -name '*.hpp' -o -name '*.c' -o -name '*.cpp' -o -name '*.m' -o -name '*.mm' \) -print0 + ) done exit $EXIT_CODE