1
+ name : clang-format-check
2
+
3
+ on :
4
+ pull_request :
5
+ types : [opened, synchronize]
6
+ branches :
7
+ - main
8
+ workflow_dispatch :
9
+
10
+ jobs :
11
+ format-check :
12
+ runs-on : ubuntu-latest
13
+
14
+ steps :
15
+ - name : Checkout code
16
+ uses : actions/checkout@v4
17
+ with :
18
+ fetch-depth : 0 # Fetch all commits of the pull request branch
19
+
20
+
21
+ - name : Install clang-format
22
+ run : sudo apt-get install -y clang-format
23
+
24
+ - name : Download clang-format-diff.py
25
+ run : |
26
+ wget https://raw.githubusercontent.com/llvm/llvm-project/main/clang/tools/clang-format/clang-format-diff.py
27
+ chmod +x clang-format-diff.py
28
+
29
+ # get names of the base and pr branches
30
+ - name : Get the base and head branches
31
+ id : fetchstep
32
+ run : |
33
+ echo "BASE_BRANCH=${{ github.event.pull_request.base.ref }}" >> $GITHUB_ENV
34
+ echo "HEAD_BRANCH=${{ github.event.pull_request.head.ref }}" >> $GITHUB_ENV
35
+ echo "Base branch: $BASE_BRANCH"
36
+ echo "Head branch: $HEAD_BRANCH"
37
+ git fetch origin ${{ env.BASE_BRANCH }}
38
+ git fetch origin ${{ env.HEAD_BRANCH }}
39
+
40
+ # get differences on the PR branch excluding generated folder in the root
41
+ - name : git diff
42
+ id : diffstep
43
+ env :
44
+ ACTIONS_RUNNER_DEBUG : true
45
+ run : |
46
+ # Format only the changed lines using clang-format-diff.py
47
+ set -e
48
+ git diff -U0 --no-color origin/${{ env.BASE_BRANCH }}...origin/${{ env.HEAD_BRANCH }} -- . ':!generated/' > diff_output.patch
49
+ cat diff_output.patch
50
+
51
+ # run formatter on the differences if any
52
+ - name : format diff
53
+ id : formatstep
54
+ env :
55
+ ACTIONS_RUNNER_DEBUG : true
56
+ run : |
57
+ if [ -s diff_output.patch ]; then
58
+ python3 clang-format-diff.py -p1 < diff_output.patch > formatted_differences.patch 2> error.log || true
59
+ if [ -s error.log ]; then
60
+ echo "Errors from clang-format-diff.py:"
61
+ cat error.log
62
+ else
63
+ echo "No Errors from clang-format-diff.py"
64
+ fi
65
+ rm diff_output.patch
66
+ fi
67
+
68
+ # check if differences found after formatting, then exit
69
+ - name : Check formatting needed
70
+ id : validatestep
71
+ env :
72
+ ACTIONS_RUNNER_DEBUG : true
73
+ run : |
74
+ if [ -s formatted_differences.patch ]; then
75
+ echo "Formatting issues found!. Formatted changes:"
76
+ cat formatted_differences.patch
77
+ rm formatted_differences.patch
78
+ exit 1
79
+ fi
0 commit comments