Files
cpp-linter-template/scripts/setup-hooks.sh
MH Hung f55d10ee07 feat(linter): implement Phase 1 linter automation and docs
- Add .clang-tidy (analyzer + selected bugprone) and .clang-format (LLVM, 4-space, 100 cols)
- Enhance scripts/run-linter.sh to use compile_commands.json when available
- Add scripts/setup-hooks.sh pre-commit (format enforcement; advisory tidy)
- Update azure-pipelines.yml to export compile_commands and run clang-tidy -p build
- Fill docs/linter-setup.md and docs/coding-standards.md for Phase 1
- Add minimal tests in tests/test_main.cpp to ensure CI executes
- Rewrite README with Phase 1 workflow
2025-09-09 22:52:34 +08:00

60 lines
1.7 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 安裝 git hookspre-commit以執行 clang-format 與可選的 clang-tidy 提示
set -euo pipefail
HOOKS_DIR=".git/hooks"
PRE_COMMIT="$HOOKS_DIR/pre-commit"
if [ ! -d "$HOOKS_DIR" ]; then
echo "This script must be run inside a Git repository."
exit 1
fi
cat > "$PRE_COMMIT" <<'EOF'
#!/bin/bash
set -euo pipefail
echo "[pre-commit] Checking/formatting C++ sources..."
# Collect staged C/C++ files
STAGED=$(git diff --cached --name-only --diff-filter=ACMR | grep -E '\.(cpp|cxx|cc|c\+\+|h|hpp|hxx)$' || true)
if [ -z "$STAGED" ]; then
echo "[pre-commit] No C/C++ files staged."
exit 0
fi
# 1) clang-format in-place, then re-stage
if command -v clang-format >/dev/null 2>&1; then
echo "$STAGED" | xargs -r clang-format -i
echo "$STAGED" | xargs -r git add
else
echo "[pre-commit] clang-format not found; skipping formatting." >&2
fi
# If formatting changed files, fail once to let user review
CHANGES=$(git diff --name-only --diff-filter=ACMR | grep -E '\\.(cpp|cxx|cc|c\\+\\+|h|hpp|hxx)$' || true)
if [ -n "$CHANGES" ]; then
echo "[pre-commit] Formatting applied to:"
echo "$CHANGES"
echo "[pre-commit] Please review changes and re-commit."
exit 1
fi
# 2) Optional clang-tidy advisory (Phase 1: do not block commits)
if command -v clang-tidy >/dev/null 2>&1; then
if [ -f build/compile_commands.json ]; then
echo "[pre-commit] Running clang-tidy (advisory)..."
echo "$STAGED" | xargs -r clang-tidy -p build --config-file=.clang-tidy || true
else
echo "[pre-commit] No build/compile_commands.json; skip clang-tidy. Run CMake with -DCMAKE_EXPORT_COMPILE_COMMANDS=ON."
fi
fi
exit 0
EOF
chmod +x "$PRE_COMMIT"
echo "Installed pre-commit hook to $PRE_COMMIT"