1+ name : Code Format with Clang-Format
2+
3+ on :
4+ workflow_dispatch :
5+ inputs :
6+ exclude_patterns :
7+ description : " 排除文件/目录 (以逗号间隔)\n Files/Directories to exclude(comma-separated)"
8+ required : false
9+ default : ' '
10+ branch :
11+ description : " 要格式化的分支 | Branch to format"
12+ required : true
13+ default : ' '
14+
15+ permissions :
16+ contents : write
17+ pull-requests : read
18+
19+ jobs :
20+ format-code :
21+ if : |
22+ github.repository_owner != 'RT-Thread'
23+ runs-on : ubuntu-latest
24+
25+ steps :
26+ - name : Checkout code
27+ uses : actions/checkout@v4
28+ with :
29+ ref : ${{ github.event.inputs.branch }}
30+ fetch-depth : 0
31+ token : ${{ secrets.GITHUB_TOKEN }}
32+ # 添加这行确保使用LF行尾符
33+ lfs : false
34+
35+ - name : Install clang-format
36+ run : sudo apt-get update && sudo apt-get install -y clang-format
37+
38+ - name : Check clang-format version
39+ run : |
40+ echo "📋 clang-format version information:"
41+ clang-format --version
42+ echo "📋 Detailed version info:"
43+ clang-format -version
44+ # 检查支持的功能
45+ echo "📋 Checking supported features..."
46+ clang-format --help | grep -i "align\|consecutive" || echo "No align/consecutive options found"
47+
48+ - name : Get changed files from PR
49+ id : changed-files
50+ uses : tj-actions/changed-files@v44
51+ with :
52+ since_last_remote_commit : true
53+ separator : " ,"
54+
55+ - name : Find source files to format
56+ id : find-files
57+ run : |
58+ # 获取PR中修改的文件
59+ CHANGED_FILES="${{ steps.changed-files.outputs.all_changed_files }}"
60+ echo "PR中修改的文件: $CHANGED_FILES"
61+
62+ # 如果没有修改的文件,退出
63+ if [ -z "$CHANGED_FILES" ]; then
64+ echo "❌ PR中没有修改的文件"
65+ echo "files_count=0" >> $GITHUB_OUTPUT
66+ exit 0
67+ fi
68+
69+ # 过滤出需要格式化的源文件(扩展clang-format支持的文件类型)
70+ FILES=""
71+ IFS=',' read -ra FILE_ARRAY <<< "$CHANGED_FILES"
72+ for file in "${FILE_ARRAY[@]}"; do
73+ if [[ "$file" =~ \.(cpp|h|c|hpp|cc|hh|C|H|cp|cxx|hxx|inc|inl|ipp|tpp|txx)$ ]]; then
74+ FILES="$FILES$file"$'\n'
75+ fi
76+ done
77+
78+ FILES=$(echo "$FILES" | sort | uniq)
79+
80+ # 处理排除模式
81+ EXCLUDE_PATTERNS="${{ github.event.inputs.exclude_patterns }}"
82+ if [ -n "$EXCLUDE_PATTERNS" ] && [ -n "$FILES" ]; then
83+ IFS=',' read -ra PATTERNS <<< "$EXCLUDE_PATTERNS"
84+ for pattern in "${PATTERNS[@]}"; do
85+ pattern=$(echo "$pattern" | xargs) # 去除空格
86+ if [ -n "$pattern" ]; then
87+ # 去除末尾的斜杠(如果有)
88+ pattern=${pattern%/}
89+ echo "排除模式: $pattern"
90+ # 使用 grep 过滤排除模式
91+ FILES=$(echo "$FILES" | grep -v "$pattern" || echo "$FILES")
92+ fi
93+ done
94+ fi
95+
96+ if [ -z "$FILES" ]; then
97+ echo "❌ 没有需要格式化的文件(可能都被排除了)"
98+ echo "files_count=0" >> $GITHUB_OUTPUT
99+ exit 0
100+ fi
101+
102+ # 显示找到的文件用于调试
103+ echo "需要格式化的文件:"
104+ echo "$FILES"
105+
106+ FILE_COUNT=$(echo "$FILES" | wc -l)
107+ echo "找到 $FILE_COUNT 个需要格式化的文件"
108+ echo "files_count=$FILE_COUNT" >> $GITHUB_OUTPUT
109+
110+ # 将文件列表保存为多行输出
111+ echo "files_list<<EOF" >> $GITHUB_OUTPUT
112+ echo "$FILES" >> $GITHUB_OUTPUT
113+ echo "EOF" >> $GITHUB_OUTPUT
114+
115+ - name : Format code with clang-format
116+ if : steps.find-files.outputs.files_count != '0'
117+ run : |
118+ echo "开始格式化代码..."
119+ FILES="${{ steps.find-files.outputs.files_list }}"
120+
121+ # 使用clang-format批量格式化文件
122+ echo "$FILES" | xargs -I {} sh -c '
123+ file="{}"
124+ if [ -f "$file" ]; then
125+ echo "📝 格式化: $file"
126+ clang-format -style=file -i "$file"
127+ if [ $? -eq 0 ]; then
128+ echo "✅ 格式化成功: $file"
129+ else
130+ echo "❌ 格式化失败: $file"
131+ exit 1
132+ fi
133+ else
134+ echo "⚠️ 文件不存在: $file"
135+ fi
136+ '
137+
138+ echo "✅ 代码格式化完成"
139+
140+ - name : Check for changes
141+ id : check-changes
142+ run : |
143+ if git diff --quiet; then
144+ echo "✅ 代码无需格式化"
145+ echo "has_changes=false" >> $GITHUB_OUTPUT
146+ else
147+ echo "📋 检测到格式化更改:"
148+ git diff --name-only
149+ echo "has_changes=true" >> $GITHUB_OUTPUT
150+ fi
151+
152+ - name : Commit and push changes
153+ if : steps.check-changes.outputs.has_changes == 'true'
154+ run : |
155+ git config --local user.email "github-actions[bot]@users.noreply.github.com"
156+ git config --local user.name "github-actions[bot]"
157+
158+ git add -A
159+ git commit -m "style: format code with clang-format [skip ci]"
160+ git push origin HEAD:${{ github.event.inputs.branch }}
161+
162+ echo "✅ 代码格式化完成并已推送到分支 ${{ github.event.inputs.branch }}"
163+
164+ - name : Summary
165+ run : |
166+ echo "=== 格式化总结 ==="
167+ echo "分支: ${{ github.event.inputs.branch }}"
168+ echo "排除模式: ${{ github.event.inputs.exclude_patterns || '无' }}"
169+ echo "处理文件数: ${{ steps.find-files.outputs.files_count }}"
170+ echo "有更改: ${{ steps.check-changes.outputs.has_changes }}"
171+ echo "clang-format 版本: $(clang-format --version | head -1)"
0 commit comments