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

The default version parses a lot of files and takes ~5s on my machine.
This adds an option `-g` to run only on files modified/staged in git.
This commit is contained in:
Jonas Diemer
2025-12-30 06:05:06 +01:00
committed by GitHub
parent 85d76da967
commit d4bd119950

View File

@@ -1,3 +1,26 @@
#!/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)
# --exclude-standard: ignores files in .gitignore
git ls-files --modified --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