-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcode-format.yml
More file actions
114 lines (104 loc) · 3.28 KB
/
code-format.yml
File metadata and controls
114 lines (104 loc) · 3.28 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
name: Code Format
on:
pull_request:
branches:
- main
push:
branches:
- main
workflow_dispatch:
inputs:
mode:
description: "Work mode: check only checks, fix automatically repairs and submits"
required: true
default: check
type: choice
options:
- check
- fix
permissions:
contents: read
jobs:
format-check:
name: Clang-Format Check
runs-on: ubuntu-latest
if: ${{ github.event_name != 'workflow_dispatch' || inputs.mode == 'check' }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup LLVM (clang-format)
uses: KyleMayes/install-llvm-action@v2
with:
version: "18"
- name: Collect C/C++ source files
id: collect
shell: bash
run: |
set -euo pipefail
files=$(git ls-files '*.c' '*.cc' '*.cpp' '*.cxx' '*.h' '*.hpp' '*.hh' '*.hxx' '*.cppm' '*.ixx')
if [ -z "$files" ]; then
echo "has_files=false" >> "$GITHUB_OUTPUT"
echo "No C/C++ files found to check, skipping."
exit 0
fi
echo "has_files=true" >> "$GITHUB_OUTPUT"
{
echo "files<<EOF"
echo "$files"
echo "EOF"
} >> "$GITHUB_OUTPUT"
- name: Run clang-format dry-run check
if: ${{ steps.collect.outputs.has_files == 'true' }}
shell: bash
run: |
set -euo pipefail
mapfile -t files < <(printf '%s\n' "${{ steps.collect.outputs.files }}")
clang-format --version
if ! clang-format --dry-run --Werror "${files[@]}"; then
echo "::error::Format check failed. Please run clang-format -i locally and commit the changes, or manually trigger workflow_dispatch and select fix."
exit 1
fi
echo "Format check passed."
format-fix:
name: Clang-Format Auto Fix
runs-on: ubuntu-latest
if: ${{ github.event_name == 'workflow_dispatch' && inputs.mode == 'fix' }}
permissions:
contents: write
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup LLVM (clang-format)
uses: KyleMayes/install-llvm-action@v2
with:
version: "18"
- name: Apply clang-format
shell: bash
run: |
set -euo pipefail
files=$(git ls-files '*.c' '*.cc' '*.cpp' '*.cxx' '*.h' '*.hpp' '*.hh' '*.hxx' '*.cppm' '*.ixx')
if [ -z "$files" ]; then
echo "No C/C++ files found to format."
exit 0
fi
echo "$files" | xargs clang-format -i
- name: Commit formatting changes
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: "ci: auto-format source files by clang-format"
commit_user_name: github-actions[bot]
commit_user_email: 41898282+github-actions[bot]@users.noreply.github.com
commit_author: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
file_pattern: |
**/*.c
**/*.cc
**/*.cpp
**/*.cxx
**/*.h
**/*.hpp
**/*.hh
**/*.hxx
**/*.cppm
**/*.ixx