yams/.githooks/pre-commit
2025-09-06 00:11:43 -04:00

31 lines
676 B
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
# Find staged C/C++ files
files=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(c|cc|cpp|cxx|h|hh|hpp)$' || true)
if [ -z "$files" ]; then
exit 0
fi
# Run clang-format check
fail=0
for f in $files; do
if [ -f "$f" ]; then
clang-format --dry-run --Werror "$f" || fail=1
fi
done
if [ "$fail" -ne 0 ]; then
echo "clang-format check failed. Run 'clang-format -i <files>' or use your IDE formatter." >&2
exit 1
fi
# Optional clang-tidy (set YAMS_LINT_TIDY=1)
if [ "${YAMS_LINT_TIDY:-}" = "1" ]; then
for f in $files; do
if [ -f "$f" ]; then
clang-tidy "$f" --quiet || true
fi
done
fi
exit 0