Skip to content

Commit 5ae6f8a

Browse files
committed
Improve clang tooling workflows
1 parent a5a5c90 commit 5ae6f8a

File tree

1 file changed

+48
-24
lines changed

1 file changed

+48
-24
lines changed

.github/workflows/ci_tests.yml

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -142,37 +142,58 @@ jobs:
142142
sudo apt-get update && sudo apt-get -y install clang-format
143143
144144
- name: Check code style
145+
shell: bash
145146
run: |
146-
# Find all C++ source files
147-
find . -name "*.cpp" -o -name "*.hpp" -o -name "*.c" -o -name "*.h" | \
148-
grep -v "./build/" | grep -v "./cmake-build" | grep -v "./_deps/" | \
149-
xargs clang-format --dry-run --Werror
147+
mapfile -t files < <(git ls-files '*.c' '*.cpp' '*.h' '*.hpp')
148+
149+
if [ "${#files[@]}" -eq 0 ]; then
150+
echo "No C/C++ files to check."
151+
exit 0
152+
fi
153+
154+
clang-format --dry-run --Werror "${files[@]}" 2>format_output.txt || {
155+
cat format_output.txt
156+
exit 1
157+
}
150158
151159
- name: Comment on style issues
152-
if: failure()
160+
if: failure() && github.event_name == 'pull_request'
153161
uses: actions/github-script@v7
154162
with:
155163
script: |
156164
const fs = require('fs');
157165
const { execSync } = require('child_process');
158-
166+
159167
try {
160168
// Get list of files that need formatting
161-
const files = execSync('find . -name "*.cpp" -o -name "*.hpp" -o -name "*.c" -o -name "*.h" | grep -v "./build/" | grep -v "./cmake-build" | grep -v "./_deps/"', { encoding: 'utf8' }).trim().split('\n');
162-
169+
const rawFiles = execSync('git ls-files "*.c" "*.cpp" "*.h" "*.hpp"', { encoding: 'utf8' }).trim();
170+
171+
if (!rawFiles) {
172+
console.log('No files require formatting checks.');
173+
return;
174+
}
175+
176+
const files = rawFiles.split('\n');
177+
163178
let comment = '## 🎨 Code Style Issues Found\n\n';
164179
comment += 'The following files have formatting issues:\n\n';
165-
180+
let hasIssues = false;
181+
166182
for (const file of files) {
167183
try {
168184
const result = execSync(`clang-format --dry-run --Werror "${file}" 2>&1`, { encoding: 'utf8' });
169185
} catch (error) {
170186
comment += `- \`${file}\`: Formatting issues detected\n`;
187+
hasIssues = true;
171188
}
172189
}
173-
174-
comment += '\nPlease run `clang-format -i <file>` to fix formatting issues.';
175-
190+
191+
if (!hasIssues) {
192+
comment += 'No files with formatting issues were detected.';
193+
} else {
194+
comment += '\nPlease run `clang-format -i <file>` to fix formatting issues.';
195+
}
196+
176197
github.rest.issues.createComment({
177198
issue_number: context.issue.number,
178199
owner: context.repo.owner,
@@ -198,35 +219,38 @@ jobs:
198219
cmake -S . -B cmake-build-tidy -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
199220
200221
- name: Run clang-tidy
222+
shell: bash
201223
run: |
202-
# Find all C++ source files
203-
find . -name "*.cpp" -o -name "*.hpp" | \
204-
grep -v "./build/" | grep -v "./cmake-build" | grep -v "./_deps/" | \
205-
xargs clang-tidy -p cmake-build-tidy --warnings-as-errors=* --format-style=file || true
224+
mapfile -t files < <(git ls-files '*.c' '*.cpp')
225+
226+
if [ "${#files[@]}" -eq 0 ]; then
227+
echo "No C/C++ files to analyze."
228+
touch tidy_output.txt
229+
exit 0
230+
fi
231+
232+
clang-tidy "${files[@]}" -p cmake-build-tidy --format-style=file > tidy_output.txt 2>&1 || true
233+
touch tidy_output.txt
206234
207235
- name: Count warnings and errors
208236
id: count_issues
209237
run: |
210-
# Run clang-tidy and capture output
211-
find . -name "*.cpp" -o -name "*.hpp" | \
212-
grep -v "./build/" | grep -v "./cmake-build" | grep -v "./_deps/" | \
213-
xargs clang-tidy -p cmake-build-tidy --format-style=file > tidy_output.txt 2>&1 || true
214-
215238
# Count errors and warnings
216239
errors=$(grep -c "error:" tidy_output.txt || echo "0")
217240
warnings=$(grep -c "warning:" tidy_output.txt || echo "0")
218-
241+
219242
echo "errors=$errors" >> $GITHUB_OUTPUT
220243
echo "warnings=$warnings" >> $GITHUB_OUTPUT
221-
244+
222245
# Fail if more than 3 warnings or any errors
223246
if [ "$errors" -gt 0 ] || [ "$warnings" -gt 3 ]; then
224247
echo "clang-tidy found $errors errors and $warnings warnings"
248+
cat tidy_output.txt
225249
exit 1
226250
fi
227251
228252
- name: Comment on quality issues
229-
if: failure()
253+
if: failure() && github.event_name == 'pull_request'
230254
uses: actions/github-script@v7
231255
with:
232256
script: |

0 commit comments

Comments
 (0)