Skip to content

Commit abdf24d

Browse files
authored
Merge branch 'develop' into greedy_third_iteration
2 parents cbe8d05 + f541fbc commit abdf24d

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

.githooks/pre-commit

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,42 @@ readarray -d '' files < <(
1212
| grep -zE '\.py$' \
1313
| grep -zEv '^(llvm-project/|external/llvm-project/)'
1414
)
15+
1516
if (( ${#files[@]} )); then
16-
echo "Running yapf on ${#files[@]} files..."
17-
yapf -i "${files[@]}"
18-
echo "Staging changes..."
19-
git add -- "${files[@]}"
17+
if command -v yapf >/dev/null 2>&1; then
18+
echo "Running yapf on ${#files[@]} files..."
19+
yapf -i "${files[@]}"
20+
echo "Staging changes..."
21+
git add -- "${files[@]}"
22+
else
23+
echo "yapf not found, skipping Python formatting."
24+
fi
25+
fi
26+
27+
readarray -d '' cxxfiles < <(git diff --cached --name-only -z --diff-filter=ACM \
28+
| grep -zE '\.(c|cc|cxx|cpp|hpp|h|hh|hxx|hip|cl)$' \
29+
| grep -zEv '^(llvm-project/|external/llvm-project/)')
30+
31+
if (( ${#cxxfiles[@]} )); then
32+
echo "Running clang-format on ${#cxxfiles[@]} staged C/C++ files..."
33+
# Use direct clang-format to avoid re-diff logic; rely on available binary
34+
clangfmt_bin="${CLANG_FORMAT_BIN:-clang-format}"
35+
if ! command -v "$clangfmt_bin" >/dev/null 2>&1; then
36+
echo "Error: $clangfmt_bin not found, aborting commit."
37+
exit 1
38+
else
39+
for f in "${cxxfiles[@]}"; do
40+
# Only format if file still exists
41+
[[ -f "$f" ]] || continue
42+
"$clangfmt_bin" -i "$f"
43+
done
44+
# Re-add any files changed by formatting so changes land in this commit
45+
git add -- "${cxxfiles[@]}"
46+
# Optionally fail if formatting introduced changes (enforce clean tree)
47+
if ! git diff --cached --quiet -- "${cxxfiles[@]}"; then
48+
echo "Applied clang-format changes and staged them."
49+
fi
50+
fi
2051
fi
2152

2253
if (( NEED_POP )); then

0 commit comments

Comments
 (0)