diff --git a/.githooks/pre-commit b/.githooks/pre-commit old mode 100644 new mode 100755 diff --git a/bin/clang-format-fix b/bin/clang-format-fix index 0b3d9855..70b64fec 100755 --- a/bin/clang-format-fix +++ b/bin/clang-format-fix @@ -34,19 +34,28 @@ fi # Format all files (or only modified files if -g is passed) -# Use 'git ls-files' to get a list of all files tracked by git: -# --modified: files tracked by git that have been modified (staged or unstaged) -# --exclude-standard: ignores files in .gitignore -# Additionally exclude files in 'lib/EpdFont/builtinFonts/' as they are script-generated. -# Also exclude files in 'lib/Epub/Epub/hyphenation/generated/' as they are script-generated. +# Use git diff in -g mode to capture staged and unstaged changes relative to HEAD. +# Otherwise use git ls-files to format all tracked source files. +# --exclude-standard ignores files in .gitignore. +# Also exclude generated, vendored, and build directories. # Keep the no-match case non-fatal: grep returns 1 when no files match, # which is expected when there are no modified C/C++ files. set +o pipefail -git ls-files --exclude-standard ${GIT_LS_FILES_FLAGS} \ - | grep -E '\.(c|cpp|h|hpp)$' \ - | grep -v -E '^lib/EpdFont/builtinFonts/' \ - | grep -v -E '^lib/Epub/Epub/hyphenation/generated/' \ - | grep -v -E '^lib/uzlib/' \ - | xargs -r "${CLANG_FORMAT_BIN}" -style=file -i +if [[ "${1:-}" == "-g" ]]; then + git diff --name-only HEAD -- \ + | grep -E '\.(c|cpp|h|hpp)$' \ + | grep -v -E '^(open-x4-sdk/|\.pio/|\.venv/|lib/EpdFont/builtinFonts/|lib/Epub/Epub/hyphenation/generated/|lib/uzlib/)' \ + | grep -v -E '\.generated\.h$' \ + | while IFS= read -r file; do + [[ -f "$file" ]] && printf '%s\n' "$file" + done \ + | xargs -r "${CLANG_FORMAT_BIN}" -style=file -i +else + git ls-files --exclude-standard \ + | grep -E '\.(c|cpp|h|hpp)$' \ + | grep -v -E '^(open-x4-sdk/|\.pio/|\.venv/|lib/EpdFont/builtinFonts/|lib/Epub/Epub/hyphenation/generated/|lib/uzlib/)' \ + | grep -v -E '\.generated\.h$' \ + | xargs -r "${CLANG_FORMAT_BIN}" -style=file -i +fi # Restore strict pipeline failure handling for the rest of the script. set -o pipefail \ No newline at end of file