FormatChecks #2
@@ -18,6 +18,7 @@ IF "%~1"=="--check" (
|
|||||||
) ELSE IF "%~1"=="--fix" (
|
) ELSE IF "%~1"=="--fix" (
|
||||||
SET MODE=fix
|
SET MODE=fix
|
||||||
) ELSE (
|
) ELSE (
|
||||||
|
REM Accumulate directories
|
||||||
SET DIRS=!DIRS! "%~1"
|
SET DIRS=!DIRS! "%~1"
|
||||||
)
|
)
|
||||||
SHIFT
|
SHIFT
|
||||||
@@ -54,14 +55,10 @@ FOR %%D IN (%DIRS%) DO (
|
|||||||
ECHO Formatting !FILE!
|
ECHO Formatting !FILE!
|
||||||
%FMT% -i "!FILE!"
|
%FMT% -i "!FILE!"
|
||||||
) ELSE (
|
) ELSE (
|
||||||
REM Check mode: run clang-format --dry-run --Werror
|
ECHO Checking !FILE!
|
||||||
%FMT% --dry-run --Werror "!FILE!" 2>nul
|
%FMT% --dry-run --Werror "!FILE!" 2>nul
|
||||||
IF ERRORLEVEL 1 (
|
IF ERRORLEVEL 1 (
|
||||||
REM clang-format will already print diagnostic
|
REM clang-format will already print diagnostics
|
||||||
ECHO --- Diff for !FILE! ---
|
|
||||||
REM Generate diff using fc
|
|
||||||
%FMT% "!FILE!" > "%TEMP%\fmt.tmp"
|
|
||||||
fc "!FILE!" "%TEMP%\fmt.tmp"
|
|
||||||
SET EXIT_CODE=1
|
SET EXIT_CODE=1
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|||||||
35
format.sh
35
format.sh
@@ -4,8 +4,11 @@ set -euo pipefail
|
|||||||
FMT="clang-format"
|
FMT="clang-format"
|
||||||
MODE=""
|
MODE=""
|
||||||
DIRS=()
|
DIRS=()
|
||||||
|
EXIT_CODE=0
|
||||||
|
|
||||||
|
# =========================
|
||||||
# Parse arguments
|
# Parse arguments
|
||||||
|
# =========================
|
||||||
for arg in "$@"; do
|
for arg in "$@"; do
|
||||||
case "$arg" in
|
case "$arg" in
|
||||||
--check)
|
--check)
|
||||||
@@ -20,18 +23,21 @@ for arg in "$@"; do
|
|||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
|
# Ensure mode is set
|
||||||
if [[ -z "$MODE" ]]; then
|
if [[ -z "$MODE" ]]; then
|
||||||
echo "Usage: $0 --check|--fix <directory> [<directory> ...]"
|
echo "Usage: $0 --check|--fix <directory> [<directory> ...]"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Ensure at least one directory
|
||||||
if [[ ${#DIRS[@]} -eq 0 ]]; then
|
if [[ ${#DIRS[@]} -eq 0 ]]; then
|
||||||
echo "Please provide at least one directory."
|
echo "Please provide at least one directory."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
EXIT_CODE=0
|
# =========================
|
||||||
|
# Process directories
|
||||||
|
# =========================
|
||||||
for DIR in "${DIRS[@]}"; do
|
for DIR in "${DIRS[@]}"; do
|
||||||
if [[ ! -d "$DIR" ]]; then
|
if [[ ! -d "$DIR" ]]; then
|
||||||
echo "$DIR is not a valid directory."
|
echo "$DIR is not a valid directory."
|
||||||
@@ -39,25 +45,24 @@ for DIR in "${DIRS[@]}"; do
|
|||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
|
|
||||||
while IFS= read -r -d '' file; do
|
# Find all source files safely (null-separated)
|
||||||
[[ "$file" == *_generated* ]] && continue
|
while IFS= read -r -d '' FILE; do
|
||||||
|
[[ "$FILE" == *_generated* ]] && continue
|
||||||
|
|
||||||
if [[ "$MODE" == "fix" ]]; then
|
if [[ "$MODE" == "fix" ]]; then
|
||||||
echo "Formatting $file"
|
echo "Formatting $FILE"
|
||||||
$FMT -i "$file"
|
"$FMT" -i "$FILE"
|
||||||
else
|
else
|
||||||
# Check mode: run clang-format with dry-run and Werror
|
echo "Checking $FILE"
|
||||||
# Capture stdout/stderr for diagnostic
|
# Check mode: clang-format diagnostic only
|
||||||
if ! $FMT --dry-run --Werror "$file"; then
|
if ! "$FMT" --dry-run --Werror "$FILE"; then
|
||||||
# clang-format will already print:
|
# clang-format prints: file:line:col: error ...
|
||||||
# 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"
|
|
||||||
EXIT_CODE=1
|
EXIT_CODE=1
|
||||||
fi
|
fi
|
||||||
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
|
done
|
||||||
|
|
||||||
exit $EXIT_CODE
|
exit $EXIT_CODE
|
||||||
|
|||||||
Reference in New Issue
Block a user