From d4bd119950041a1d461748c710ea6b749feb8c5b Mon Sep 17 00:00:00 2001 From: Jonas Diemer Date: Tue, 30 Dec 2025 06:05:06 +0100 Subject: [PATCH] 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. --- bin/clang-format-fix | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/bin/clang-format-fix b/bin/clang-format-fix index ba16ec98..92a40511 100755 --- a/bin/clang-format-fix +++ b/bin/clang-format-fix @@ -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