Skip to content

Commit 0273250

Browse files
authored
Merge pull request #1310 from Yyunozor/fix/1264-hook-diff-aware-clang-tidy
fix(hooks): scope pre-commit clang-tidy to staged changes (#1264)
2 parents 33a3f03 + d899bf4 commit 0273250

3 files changed

Lines changed: 120 additions & 2 deletions

File tree

‎Makefile.cbm‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ PP_OBJ_TSAN = $(BUILD_DIR)/tsan_preprocessor.o
729729

730730
# ── Targets ──────────────────────────────────────────────────────
731731

732-
.PHONY: test test-par test-repro test-foundation test-tsan test-daemon-smoke cbm cbm-with-ui frontend embed clean-c lint lint-tidy lint-cppcheck lint-format security
732+
.PHONY: test test-par test-repro test-foundation test-tsan test-daemon-smoke cbm cbm-with-ui frontend embed clean-c lint lint-tidy lint-tidy-diff lint-cppcheck lint-format security
733733

734734
$(BUILD_DIR):
735735
mkdir -p $(BUILD_DIR)
@@ -1103,6 +1103,12 @@ lint-tidy:
11031103
@echo "=== clang-tidy ==="
11041104
@$(CLANG_TIDY) --quiet $(LINT_SRCS) -- $(CFLAGS_COMMON) $(SYSROOT_FLAG)
11051105

1106+
# clang-tidy, scoped to this commit's staged changes only (scripts/lint-tidy-diff.sh).
1107+
# The pre-commit hook runs this instead of the full lint-tidy sweep above, so a
1108+
# clean commit is not blocked by pre-existing findings elsewhere in the tree.
1109+
lint-tidy-diff:
1110+
@scripts/lint-tidy-diff.sh "$(CLANG_TIDY)" $(LINT_SRCS) -- $(CFLAGS_COMMON) $(SYSROOT_FLAG)
1111+
11061112
# cppcheck: complementary analysis (config in .cppcheck)
11071113
lint-cppcheck:
11081114
@echo "=== cppcheck ==="

‎scripts/hooks/pre-commit‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ set -euo pipefail
77
# git config core.hooksPath scripts/hooks
88

99
echo "pre-commit: running all linters in parallel..."
10-
make -j3 -f Makefile.cbm lint
10+
# clang-tidy runs diff-scoped (lint-tidy-diff, see scripts/lint-tidy-diff.sh):
11+
# the full lint-tidy sweep surfaces thousands of pre-existing findings that
12+
# have nothing to do with this commit and would block every contributor who
13+
# has clang-tidy on PATH. `make lint-tidy` remains available for a full audit.
14+
make -j3 -f Makefile.cbm lint-ci lint-tidy-diff
1115

1216
echo "pre-commit: security audit (source-level)..."
1317
scripts/security-audit.sh

‎scripts/lint-tidy-diff.sh‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/usr/bin/env bash
2+
# lint-tidy-diff.sh — clang-tidy scoped to the lines this commit changes.
3+
#
4+
# `make lint-tidy` (the full sweep) checks the entire LINT_SRCS tree, which
5+
# currently surfaces ~5,100 pre-existing findings unrelated to any given
6+
# commit (see Makefile.cbm's `lint-tidy` target). That is fine for a manual
7+
# deep audit but makes the pre-commit hook unusable: it blocks every commit,
8+
# including ones that touch files with zero findings on the touched lines.
9+
#
10+
# This script runs the same clang-tidy binary/config against the SAME staged
11+
# .c files, but passes clang-tidy's own `-line-filter` so only lines the
12+
# commit actually added/modified can produce a diagnostic. Pre-existing
13+
# findings on untouched lines — in the same file or elsewhere — are not
14+
# reported.
15+
#
16+
# Invoked by `make lint-tidy-diff` (scripts/hooks/pre-commit), which supplies
17+
# the resolved clang-tidy binary, then LINT_SRCS, then `--`, then the
18+
# compiler flags — the same argv shape as the `lint-tidy` target itself.
19+
#
20+
# Known limitation: only staged *.c files that are members of LINT_SRCS are
21+
# checked (matching the full sweep's own scope: vendored code and tests/ are
22+
# never clang-tidy'd). clang-tidy reports a header's diagnostics under
23+
# whatever path it was #include-d with, which is not reliably the same
24+
# string as its path in the working tree, so a commit that touches only a
25+
# header is not covered here even if a .c file including it also changed;
26+
# run `make lint-tidy` for a full pass in that case.
27+
28+
set -euo pipefail
29+
30+
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
31+
cd "$ROOT"
32+
33+
if [ "$#" -lt 1 ]; then
34+
echo "usage: lint-tidy-diff.sh <clang-tidy-bin> [LINT_SRCS...] -- [CFLAGS...]" >&2
35+
exit 2
36+
fi
37+
38+
CLANG_TIDY="$1"
39+
shift
40+
41+
LINT_SRCS=()
42+
while [ "$#" -gt 0 ] && [ "$1" != "--" ]; do
43+
LINT_SRCS+=("$1")
44+
shift
45+
done
46+
[ "$#" -gt 0 ] && shift # drop the `--` separator
47+
CFLAGS=("$@")
48+
49+
# bash 3.2 (macOS default, see scripts/check-lsp-originality.sh) has neither
50+
# `mapfile` nor associative arrays — read loop + linear membership check.
51+
is_lint_src() {
52+
local needle="$1" f
53+
[ "${#LINT_SRCS[@]}" -eq 0 ] && return 1
54+
for f in "${LINT_SRCS[@]}"; do
55+
[ "$f" = "$needle" ] && return 0
56+
done
57+
return 1
58+
}
59+
60+
# Staged .c files this commit actually changed (added/copied/modified/
61+
# renamed — a delete has no new lines to check) that are members of
62+
# LINT_SRCS.
63+
CHANGED=()
64+
while IFS= read -r f; do
65+
[ -n "$f" ] && is_lint_src "$f" && CHANGED+=("$f")
66+
done < <(git diff --cached --name-only --diff-filter=ACMR -- '*.c')
67+
68+
if [ "${#CHANGED[@]}" -eq 0 ]; then
69+
echo "lint-tidy-diff: no staged changes to a linted .c file, skipping clang-tidy"
70+
exit 0
71+
fi
72+
73+
# For each changed file, turn its staged hunks into a clang-tidy line-filter
74+
# range: `@@ -a,b +c,d @@` -> lines [c, c+d-1]. Zero-context (-U0) so a range
75+
# never includes untouched, pre-existing lines. Pure-deletion hunks (d == 0)
76+
# add no new lines and are skipped.
77+
LINE_FILTER='['
78+
FILTER_SEP=''
79+
TARGETS=()
80+
for f in "${CHANGED[@]}"; do
81+
[ -f "$f" ] || continue # renamed away / not present in the working tree
82+
ranges=''
83+
range_sep=''
84+
while IFS= read -r hunk; do
85+
if [[ "$hunk" =~ ^@@\ -[0-9]+(,[0-9]+)?\ \+([0-9]+)(,([0-9]+))?\ @@ ]]; then
86+
new_start="${BASH_REMATCH[2]}"
87+
new_count="${BASH_REMATCH[4]:-1}"
88+
[ "$new_count" -eq 0 ] && continue
89+
new_end=$((new_start + new_count - 1))
90+
ranges+="${range_sep}[${new_start},${new_end}]"
91+
range_sep=','
92+
fi
93+
done < <(git diff --cached -U0 --diff-filter=ACMR -- "$f" | grep -E '^@@ ')
94+
[ -z "$ranges" ] && continue
95+
96+
LINE_FILTER+="${FILTER_SEP}{\"name\":\"${f}\",\"lines\":[${ranges}]}"
97+
FILTER_SEP=','
98+
TARGETS+=("$f")
99+
done
100+
LINE_FILTER+=']'
101+
102+
if [ "${#TARGETS[@]}" -eq 0 ]; then
103+
echo "lint-tidy-diff: staged .c changes are deletion-only, skipping clang-tidy"
104+
exit 0
105+
fi
106+
107+
echo "lint-tidy-diff: ${#TARGETS[@]} changed file(s), diff-scoped clang-tidy"
108+
"$CLANG_TIDY" --quiet "-line-filter=${LINE_FILTER}" "${TARGETS[@]}" -- "${CFLAGS[@]}"

0 commit comments

Comments
 (0)