-
Notifications
You must be signed in to change notification settings - Fork 3
75 lines (63 loc) · 2.1 KB
/
Copy pathformat-check.yml
File metadata and controls
75 lines (63 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
name: Format Check
on:
pull_request:
branches: [main]
jobs:
changes:
name: Detect source changes
runs-on: ubuntu-latest
outputs:
relevant: ${{ steps.filter.outputs.relevant }}
steps:
- name: Checkout repository
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Detect relevant file changes
id: filter
shell: bash
run: |
if [ "${{ github.event_name }}" != "pull_request" ]; then
echo "relevant=true" >> "$GITHUB_OUTPUT"
exit 0
fi
if git diff --name-only "${{ github.event.pull_request.base.sha }}" "${{ github.event.pull_request.head.sha }}" | grep -q '^src/'; then
echo "relevant=true" >> "$GITHUB_OUTPUT"
else
echo "relevant=false" >> "$GITHUB_OUTPUT"
fi
clang-format:
name: clang-format
needs: changes
if: needs.changes.outputs.relevant == 'true'
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v6
with:
fetch-depth: 1
- name: Install clang-format
run: sudo apt-get update && sudo apt-get install -y clang-format
- name: Check formatting
run: |
FAILED=0
FILES=$(find src/ -type f \( -name "*.cpp" -o -name "*.hpp" -o -name "*.h" \))
for file in $FILES; do
# Generate the diff between current and formatted versions
DIFF=$(clang-format "$file" | diff -u "$file" - || true)
if [ -n "$DIFF" ]; then
echo "::error file=$file::File is not formatted according to .clang-format"
echo "$DIFF"
echo ""
FAILED=1
fi
done
if [ "$FAILED" -eq 1 ]; then
echo ""
echo "Some files are not properly formatted."
echo ""
echo "Fix locally with:"
echo ' find src/ -type f \( -name "*.cpp" -o -name "*.hpp" -o -name "*.h" \) | xargs clang-format -i'
exit 1
fi
echo "All files are properly formatted."