2026-02-16 06:13:05 -05:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
2026-02-22 05:50:08 +00:00
|
|
|
# Check if clang-format is available and pick the preferred binary.
|
|
|
|
|
if command -v clang-format-21 >/dev/null 2>&1; then
|
|
|
|
|
CLANG_FORMAT_BIN="clang-format-21"
|
|
|
|
|
elif command -v clang-format >/dev/null 2>&1; then
|
|
|
|
|
CLANG_FORMAT_BIN="clang-format"
|
|
|
|
|
else
|
|
|
|
|
printf "'clang-format' not found in current environment\n"
|
|
|
|
|
printf "Install clang-format-21 (recommended), clang, clang-tools, or clang-format depending on your distro/os and tooling requirements\n"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
set -euo pipefail
|
2025-12-03 22:00:29 +11:00
|
|
|
|
2025-12-30 18:21:47 +10:00
|
|
|
GIT_LS_FILES_FLAGS=""
|
2026-04-22 23:20:03 +03:00
|
|
|
# -g scopes formatting to tracked files currently modified in git status.
|
2026-02-22 05:50:08 +00:00
|
|
|
if [[ "${1:-}" == "-g" ]]; then
|
2025-12-30 18:21:47 +10:00
|
|
|
GIT_LS_FILES_FLAGS="--modified"
|
|
|
|
|
fi
|
2025-12-30 06:05:06 +01:00
|
|
|
|
2026-02-22 05:50:08 +00:00
|
|
|
CLANG_FORMAT_VERSION_RAW="$(${CLANG_FORMAT_BIN} --version)"
|
|
|
|
|
CLANG_FORMAT_MAJOR="$(printf '%s\n' "${CLANG_FORMAT_VERSION_RAW}" | grep -oE '[0-9]+' | head -n1)"
|
|
|
|
|
|
2026-04-22 23:20:03 +03:00
|
|
|
# Guard against local binaries older than the repo formatting config.
|
2026-02-22 05:50:08 +00:00
|
|
|
if [[ -z "${CLANG_FORMAT_MAJOR}" || "${CLANG_FORMAT_MAJOR}" -lt 21 ]]; then
|
|
|
|
|
echo "Error: ${CLANG_FORMAT_BIN} is too old: ${CLANG_FORMAT_VERSION_RAW}"
|
|
|
|
|
echo "This repository's .clang-format requires clang-format 21 or newer."
|
|
|
|
|
echo "Install clang-format-21 and rerun ./bin/clang-format-fix"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
2026-02-16 06:13:05 -05:00
|
|
|
|
2025-12-30 06:05:06 +01:00
|
|
|
# --- Main Logic ---
|
|
|
|
|
|
2025-12-30 18:21:47 +10:00
|
|
|
# Format all files (or only modified files if -g is passed)
|
2025-12-30 06:05:06 +01:00
|
|
|
|
2025-12-30 18:21:47 +10:00
|
|
|
# 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.
|
2026-02-09 09:55:58 +01:00
|
|
|
# Also exclude files in 'lib/Epub/Epub/hyphenation/generated/' as they are script-generated.
|
2026-04-22 23:20:03 +03:00
|
|
|
# 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
|
2025-12-30 18:21:47 +10:00
|
|
|
git ls-files --exclude-standard ${GIT_LS_FILES_FLAGS} \
|
|
|
|
|
| grep -E '\.(c|cpp|h|hpp)$' \
|
|
|
|
|
| grep -v -E '^lib/EpdFont/builtinFonts/' \
|
2026-02-09 09:55:58 +01:00
|
|
|
| grep -v -E '^lib/Epub/Epub/hyphenation/generated/' \
|
2026-02-19 20:30:15 +11:00
|
|
|
| grep -v -E '^lib/uzlib/' \
|
2026-02-22 05:50:08 +00:00
|
|
|
| xargs -r "${CLANG_FORMAT_BIN}" -style=file -i
|
2026-04-22 23:20:03 +03:00
|
|
|
# Restore strict pipeline failure handling for the rest of the script.
|
|
|
|
|
set -o pipefail
|