Skip to content

Commit 334d744

Browse files
committed
Add GH action enforcing clang-format
1 parent 5a5f35b commit 334d744

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: Clang Format Check
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened]
6+
7+
jobs:
8+
clang-format-check:
9+
name: Check PR formatting with clang-format-15
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v4
15+
with:
16+
fetch-depth: 0 # Need full history to compare with main branch
17+
18+
- name: Install clang-format-15
19+
run: |
20+
sudo apt-get install -y clang-format-15
21+
22+
- name: Fetch main branch
23+
run: |
24+
git fetch origin main:main
25+
26+
- name: Check formatting
27+
run: |
28+
echo "Running git-clang-format-15 to check for formatting issues..."
29+
echo "Comparing against main branch..."
30+
31+
# Create a temporary file for the diff and ensure cleanup
32+
DIFF_FILE="$(mktemp)"
33+
trap 'rm -f "$DIFF_FILE"' EXIT
34+
35+
# Run git-clang-format and capture status safely under set -e
36+
if git-clang-format-15 --diff main --binary clang-format-15 > "$DIFF_FILE"; then
37+
status=0
38+
else
39+
status=$?
40+
fi
41+
if [ "$status" -eq 0 ]; then
42+
echo "✅ Code is properly formatted!"
43+
exit 0
44+
elif [ "$status" -eq 1 ]; then
45+
echo "❌ Code formatting issues detected!"
46+
echo ""
47+
echo "The following changes would be made by clang-format-15:"
48+
echo "=================================================="
49+
cat "$DIFF_FILE"
50+
echo "=================================================="
51+
echo ""
52+
echo "Please run the following command locally on your feature branch and commit the changes:"
53+
echo " git-clang-format-15 main --binary clang-format-15"
54+
exit 1
55+
else
56+
echo "❌ git-clang-format-15 failed with exit code $status"
57+
echo "Output (if any):"
58+
cat "$DIFF_FILE"
59+
exit 1
60+
fi

0 commit comments

Comments
 (0)