-
Notifications
You must be signed in to change notification settings - Fork 33
67 lines (53 loc) · 2 KB
/
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
name: Run Clang-Format Check
on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
jobs:
clang-format-check:
runs-on: ubuntu-latest
steps:
- name: Install Python and clang-format to get newer version
run: |
python3 -m pip install --upgrade pip
pip install clang-format==19.1.3
- name: Checkout the code
uses: actions/checkout@v4
with:
fetch-depth: 0 #ensure fetch of pr branch before commit
- name: Fetch latest master branch
run: git fetch origin master
- name: Set up Clang-Format
run: sudo apt-get install -y clang-format
- name: Check formatting with Clang-Format
run: |
set +e # this turn off exit-on-error
# searches for all src files that are modified in comparison to origin/master
MODIFIED_FILES=$(git diff --name-only origin/master...HEAD | grep -E '\.(c|h|cpp|hpp)$')
# track format errors
FORMAT_ERRORS=0
if [[ -n "$MODIFIED_FILES" ]]; then
echo "modified files found!"
clang-format --version
# check if formatted correctly
for file in $MODIFIED_FILES; do
echo "Checking file: $file"
# Capture the output of clang-format check
OUTPUT=$(clang-format --dry-run -Werror "$file" 2>&1)
# Check if there was an error
if [[ $? -ne 0 ]]; then
echo "Formatting issue found in $file:"
echo "$OUTPUT" # print clang-format error message
FORMAT_ERRORS=1
fi
done
# Exit with an error if any formatting issues were found
if [[ $FORMAT_ERRORS -ne 0 ]]; then
echo "Formatting issues detected. Please run clang-format $(clang-format --version) to fix the issues."
exit 1
fi
else
echo "no modified files found!"
fi
exit 0