-
Notifications
You must be signed in to change notification settings - Fork 1.1k
80 lines (70 loc) · 2.59 KB
/
clang-format.yml
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
76
77
78
79
80
name: clang-format-check
on:
pull_request:
types: [opened, synchronize]
branches:
- main
workflow_dispatch:
jobs:
format-check:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all commits of the pull request branch
- name: Install clang-format
run: sudo apt-get install -y clang-format
- name: Download clang-format-diff.py
run: |
wget https://raw.githubusercontent.com/llvm/llvm-project/main/clang/tools/clang-format/clang-format-diff.py
chmod +x clang-format-diff.py
# get names of the base and pr branches
- name: Get the base and head branches
id: fetchstep
run: |
echo "BASE_BRANCH=${{ github.event.pull_request.base.ref }}" >> $GITHUB_ENV
echo "HEAD_BRANCH=${{ github.event.pull_request.head.ref }}" >> $GITHUB_ENV
echo "Base branch: $BASE_BRANCH"
echo "Head branch: $HEAD_BRANCH"
git fetch origin ${{ env.BASE_BRANCH }}
git fetch origin ${{ env.HEAD_BRANCH }}
#get differences on the PR branch excluding generated folder in the root
- name: git diff
id: diffstep
env:
ACTIONS_RUNNER_DEBUG: true
run: |
# Format only the changed lines using clang-format-diff.py
set -e
git diff -U0 --no-color origin/${{ env.BASE_BRANCH }}...origin/${{ env.HEAD_BRANCH }} -- . ':!generated/' > diff_output.patch
cat diff_output.patch
# run formatter on the differences if any
- name: format diff
id: formatstep
env:
ACTIONS_RUNNER_DEBUG: true
run: |
clang-format --version
if [ -s diff_output.patch ]; then
python3 clang-format-diff.py -p1 -style=file:.clang-format < diff_output.patch > formatted_differences.patch 2> error.log || true
if [ -s error.log ]; then
echo "Errors from clang-format-diff.py:"
cat error.log
else
echo "No Errors from clang-format-diff.py"
fi
rm diff_output.patch
fi
# check if differences found after formatting, then exit
- name: Check formatting needed
id: validatestep
env:
ACTIONS_RUNNER_DEBUG: true
run: |
if [ -s formatted_differences.patch ]; then
echo "Formatting issues found!. Formatted changes:"
cat formatted_differences.patch
rm formatted_differences.patch
exit 1
fi