Skip to content

Commit f287f2b

Browse files
authored
Add GH action enforcing clang-format (#176)
1 parent 3aa6b69 commit f287f2b

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 base branch
23+
env:
24+
BASE_REF: ${{ github.event.pull_request.base.ref }}
25+
run: |
26+
git fetch origin "$BASE_REF:$BASE_REF"
27+
28+
- name: Check formatting
29+
env:
30+
BASE_SHA: ${{ github.event.pull_request.base.sha }}
31+
BASE_REF: ${{ github.event.pull_request.base.ref }}
32+
run: |
33+
echo "Running git-clang-format-15 to check for formatting issues..."
34+
echo "Comparing against base branch: $BASE_REF"
35+
36+
# Create a temporary file for the diff and ensure cleanup
37+
DIFF_FILE="$(mktemp)"
38+
trap 'rm -f "$DIFF_FILE"' EXIT
39+
40+
# Run git-clang-format against the PR base commit and capture status safely under set -e
41+
if git-clang-format-15 --diff "$BASE_REF" --binary clang-format-15 > "$DIFF_FILE"; then
42+
status=0
43+
else
44+
status=$?
45+
fi
46+
if [ "$status" -eq 0 ]; then
47+
echo "✅ Code is properly formatted!"
48+
exit 0
49+
elif [ "$status" -eq 1 ]; then
50+
echo "❌ Code formatting issues detected!"
51+
echo ""
52+
echo "The following changes would be made by clang-format-15:"
53+
echo "=================================================="
54+
cat "$DIFF_FILE"
55+
echo "=================================================="
56+
echo ""
57+
echo "Please run the following command locally on your feature branch and commit the changes:"
58+
echo " git-clang-format-15 --binary clang-format-15 $BASE_REF"
59+
exit 1
60+
else
61+
echo "❌ git-clang-format-15 failed with exit code $status"
62+
echo "Output (if any):"
63+
cat "$DIFF_FILE"
64+
exit 1
65+
fi

0 commit comments

Comments
 (0)