Add option to apply format fix only on changed files (much faster)

This commit is contained in:
Jonas Diemer 2025-12-29 13:43:35 +01:00
parent 140d8749a6
commit a6e5dbb32c

View File

@ -1,3 +1,27 @@
#!/bin/bash
find src lib \( -name "*.c" -o -name "*.cpp" -o -name "*.h" -o -name "*.hpp" \) -exec clang-format -style=file -i {} +
# Configuration: Standard arguments for clang-format
STYLE_ARGS="-style=file -i"
# --- Main Logic ---
if [[ "$1" == "-g" ]]; then
# Mode: Format all modified files (staged and unstaged)
# Use 'git ls-files' to get a list of all files with pending changes:
# --modified: files tracked by git that have been modified (staged or unstaged)
# --others: untracked files
# --exclude-standard: ignores files in .gitignore
git ls-files --modified --others --exclude-standard \
| grep -E '\.(c|cpp|h|hpp)$' \
| xargs -r clang-format $STYLE_ARGS
# NOTE: We skip the 'git add' step from before.
# When running on unstaged files, 'clang-format -i' modifies them
# in the working directory, where they remain unstaged (M).
else
# Executes original working command directly.
find src lib \( -name "*.c" -o -name "*.cpp" -o -name "*.h" -o -name "*.hpp" \) -exec clang-format $STYLE_ARGS {} +
fi