Skip to content

Commit b301add

Browse files
feat: Add CI workflow config file
1 parent c1f9a56 commit b301add

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
name: Code Format
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
push:
8+
branches:
9+
- main
10+
workflow_dispatch:
11+
inputs:
12+
mode:
13+
description: "Work mode: check only checks, fix automatically repairs and submits"
14+
required: true
15+
default: check
16+
type: choice
17+
options:
18+
- check
19+
- fix
20+
21+
permissions:
22+
contents: read
23+
24+
jobs:
25+
format-check:
26+
name: Clang-Format Check
27+
runs-on: ubuntu-latest
28+
if: ${{ github.event_name != 'workflow_dispatch' || inputs.mode == 'check' }}
29+
steps:
30+
- name: Checkout
31+
uses: actions/checkout@v4
32+
33+
- name: Setup LLVM (clang-format)
34+
uses: KyleMayes/install-llvm-action@v2
35+
with:
36+
version: "18"
37+
38+
- name: Collect C/C++ source files
39+
id: collect
40+
shell: bash
41+
run: |
42+
set -euo pipefail
43+
files=$(git ls-files '*.c' '*.cc' '*.cpp' '*.cxx' '*.h' '*.hpp' '*.hh' '*.hxx' '*.cppm' '*.ixx')
44+
if [ -z "$files" ]; then
45+
echo "has_files=false" >> "$GITHUB_OUTPUT"
46+
echo "No C/C++ files found to check, skipping."
47+
exit 0
48+
fi
49+
echo "has_files=true" >> "$GITHUB_OUTPUT"
50+
{
51+
echo "files<<EOF"
52+
echo "$files"
53+
echo "EOF"
54+
} >> "$GITHUB_OUTPUT"
55+
56+
- name: Run clang-format dry-run check
57+
if: ${{ steps.collect.outputs.has_files == 'true' }}
58+
shell: bash
59+
run: |
60+
set -euo pipefail
61+
mapfile -t files < <(printf '%s\n' "${{ steps.collect.outputs.files }}")
62+
clang-format --version
63+
if ! clang-format --dry-run --Werror "${files[@]}"; then
64+
echo "::error::Format check failed. Please run clang-format -i locally and commit the changes, or manually trigger workflow_dispatch and select fix."
65+
exit 1
66+
fi
67+
echo "Format check passed."
68+
69+
format-fix:
70+
name: Clang-Format Auto Fix
71+
runs-on: ubuntu-latest
72+
if: ${{ github.event_name == 'workflow_dispatch' && inputs.mode == 'fix' }}
73+
permissions:
74+
contents: write
75+
steps:
76+
- name: Checkout
77+
uses: actions/checkout@v4
78+
with:
79+
fetch-depth: 0
80+
81+
- name: Setup LLVM (clang-format)
82+
uses: KyleMayes/install-llvm-action@v2
83+
with:
84+
version: "18"
85+
86+
- name: Apply clang-format
87+
shell: bash
88+
run: |
89+
set -euo pipefail
90+
files=$(git ls-files '*.c' '*.cc' '*.cpp' '*.cxx' '*.h' '*.hpp' '*.hh' '*.hxx' '*.cppm' '*.ixx')
91+
if [ -z "$files" ]; then
92+
echo "No C/C++ files found to format."
93+
exit 0
94+
fi
95+
echo "$files" | xargs clang-format -i
96+
97+
- name: Commit formatting changes
98+
uses: stefanzweifel/git-auto-commit-action@v5
99+
with:
100+
commit_message: "ci: auto-format source files by clang-format"
101+
commit_user_name: github-actions[bot]
102+
commit_user_email: 41898282+github-actions[bot]@users.noreply.github.com
103+
commit_author: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
104+
file_pattern: |
105+
**/*.c
106+
**/*.cc
107+
**/*.cpp
108+
**/*.cxx
109+
**/*.h
110+
**/*.hpp
111+
**/*.hh
112+
**/*.hxx
113+
**/*.cppm
114+
**/*.ixx

0 commit comments

Comments
 (0)