/ .github / workflows / format.yml
format.yml
 1  name: Format
 2  
 3  on:
 4    push:
 5      branches: [ master ]
 6    pull_request:
 7      branches: [ master ]
 8  
 9  jobs:
10    ClangFormatDiff:
11      name: ClangFormat
12      if: github.event_name == 'pull_request'
13      strategy:
14        fail-fast: false
15      env:
16        CLANG_FORMAT_DIFF: /usr/share/clang/clang-format-15/clang-format-diff.py
17        BRANCH_POINT: ${{ github.base_ref }}
18      runs-on: ubuntu-latest
19      steps:
20        # https://github.com/actions/checkout/issues/416
21        - name: Checkout
22          uses: actions/checkout@v4
23          with:
24            fetch-depth: 0
25            ref: ${{ github.event.pull_request.head.sha }}
26  
27        - name: Setup PR Branch
28          run: git branch ${{ github.base_ref }} origin/${{ github.base_ref }}
29  
30        - name: Install LLVM 15
31          run: |
32            sudo apt-get install -y clang-format-15 clang-15
33  
34        - name: Run ClangFormatDiff Script
35          run: contrib/clang-format-diff.sh
36  
37        - name: Detect ClangFormat Changes
38          run: |
39            if [ -s clang_format.patch ]; then
40              echo "changes_detected=true" >> $GITHUB_ENV
41            else
42              echo "changes_detected=false" >> $GITHUB_ENV
43            fi
44  
45        - name: Upload Artifacts
46          if: fromJSON(env.changes_detected)
47          uses: actions/upload-artifact@v4
48          with:
49            name: ClangFormat Patch
50            path: clang_format.patch
51  
52        - name: Patch File
53          run: |
54            if [ -s clang_format.patch ]; then
55              cat clang_format.patch
56            fi
57  
58        - name: Check Validity
59          run: |
60            if [ -s clang_format.patch ]; then
61              echo "---------------------------------------------------------------------------"
62              echo "|                          CLANG FORMAT FAILURE                           |"
63              echo "---------------------------------------------------------------------------"
64              echo "|                                                                         |"
65              echo "| Your changes are not clang-format compliant!                            |"
66              echo "|                                                                         |"
67              echo "| An Artifact 'ClangFormat Patch' was uploaded.                           |"
68              echo "|                                                                         |"
69              echo "| Consider inspecting and applying 'clang_format.patch':                  |"
70              echo "|   git apply clang_format.patch                                          |"
71              echo "|                                                                         |"
72              echo "| The contents of 'clang_format.patch' is printed below.                  |"
73              echo "|                                                                         |"
74              echo "---------------------------------------------------------------------------"
75              cat clang_format.patch
76              echo "---------------------------------------------------------------------------"
77              exit 1
78            fi