@@ -2,9 +2,9 @@ name: Check code formatting
22
33on :
44 workflow_dispatch :
5- # pull_request:
6- # branches:
7- # - main
5+ pull_request :
6+ branches :
7+ - main
88
99env :
1010 BUILD_TYPE : Debug
@@ -17,73 +17,67 @@ jobs:
1717 uses : actions/checkout@v3
1818
1919 - name : Install clang-format
20- uses : aminya/setup-cpp@v1.1.1
20+ uses : aminya/setup-cpp@v1
2121 with :
22- clangformat : 19.1.6
22+ clang-format : true
2323
24- - name : Check formatting in dynadjust
24+ - name : Check clang-format version
25+ run : clang-format --version
26+
27+ - name : Check formatting in repo
2528 id : fmt
29+ continue-on-error : ${{ github.event_name == 'workflow_dispatch' }}
2630 run : |
27- echo "Checking formatting in dynadjust"
28- ERRORS=""
29- # Use .clang-format if present, otherwise default to LLVM style.
30- if [ -f .clang-format ]; then
31- STYLE=file
31+ echo "Checking formatting in repo"
32+ rm -f /tmp/clang_format_errors.txt
33+
34+ if [ "$GITHUB_EVENT_NAME" = "pull_request" ]; then
35+ echo "Pull request detected. Running clang-format on changed files only."
36+ base_sha="${{ github.event.pull_request.base.sha }}"
37+ head_sha="${{ github.event.pull_request.head.sha }}"
38+ changed=$(git diff --name-only "$base_sha" "$head_sha" | grep -E '\.(cpp|h|hpp)$' || true)
3239 else
33- STYLE=LLVM
40+ echo "Non-PR event. Running clang-format on all files."
41+ changed=$(find . -type f \( -name '*.cpp' -o -name '*.h' -o -name '*.hpp' \))
3442 fi
35- files=$(find dynadjust -type f \( -name '*.cpp' -o -name '*.h' \))
36- ret=0
37- for f in $files ; do
38- diff=$(clang-format -style=$STYLE "$f" | diff "$f" -)
39- if [ -n "$diff" ] ; then
40- echo "::warning file=$f::Formatting issues found "
41- ERRORS="${ERRORS}\n - $f"
42- ret=1
43+
44+ # Process each file and check formatting
45+ printf '%s\n' "$changed" | while IFS= read -r f ; do
46+ if [ -n "$f" ]; then
47+ if ! clang-format -Werror --dry-run --ferror-limit=1 "$f" 2>/dev/null ; then
48+ echo "::warning file=$f::Formatting issues in $f "
49+ echo " - $f" >> /tmp/clang_format_errors.txt
50+ fi
4351 fi
4452 done
53+
54+ ERRORS=$(cat /tmp/clang_format_errors.txt 2>/dev/null)
4555 echo "errors<<EOF" >> $GITHUB_OUTPUT
4656 echo -e "$ERRORS" >> $GITHUB_OUTPUT
4757 echo "EOF" >> $GITHUB_OUTPUT
48- #exit $ret
58+ echo "DEBUG: Contents of GITHUB_OUTPUT file:"
59+ cat "$GITHUB_OUTPUT"
60+
61+ if [ "$GITHUB_EVENT_NAME" = "workflow_dispatch" ]; then
62+ exit 0
63+ else
64+ if [ -n "$ERRORS" ]; then
65+ exit 1
66+ else
67+ exit 0
68+ fi
69+ fi
4970
5071 - name : Post comment on PR
5172 if : ${{ github.event_name == 'pull_request' && steps.fmt.outputs.errors != '' }}
5273 uses : actions/github-script@v6
5374 with :
5475 script : |
5576 const err = `${{ steps.fmt.outputs.errors }}`;
56- const msg = `### Code Formatting Issues\nThe following files do not adhere to the clang-format style:${err}`;
77+ const msg = `### Code Formatting Issues\nThe following files do not adhere to the clang-format style:\n ${err}`;
5778 github.rest.issues.createComment({
5879 issue_number: context.issue.number,
5980 owner: context.repo.owner,
6081 repo: context.repo.repo,
6182 body: msg
6283 });
63-
64- - name : Create Annotations for Formatting Issues
65- if : ${{ github.event_name == 'workflow_dispatch' && steps.fmt.outputs.errors != '' }}
66- uses : actions/github-script@v6
67- with :
68- script : |
69- const errors = `${{ steps.fmt.outputs.errors }}`.trim().split('\n').filter(e => e);
70- if (errors.length === 0) return;
71- github.rest.checks.create({
72- owner: context.repo.owner,
73- repo: context.repo.repo,
74- name: 'Check code formatting',
75- head_sha: context.sha,
76- status: 'completed',
77- conclusion: 'failure',
78- output: {
79- title: 'Formatting Issues',
80- summary: `Formatting issues detected in the following files:\n${errors.join('\n')}`,
81- annotations: errors.map(file => ({
82- path: file.replace(/^- /, ''),
83- start_line: 1,
84- end_line: 1,
85- annotation_level: 'failure',
86- message: 'Formatting issues found'
87- }))
88- }
89- });
0 commit comments