Skip to content

Coding - Apply .clang-format formatting #1

Coding - Apply .clang-format formatting

Coding - Apply .clang-format formatting #1

# This workflow checks the code formatting of changed files in a pull request using clang-format.
# It is triggered on pull requests to the master branch.
# The workflow verifies that the clang-format version matches 18.1.8,
# checks formatting of modified files, and if formatting issues are found,
# creates a patch file that can be applied to fix the formatting.
name: Clang-Format Check
on:
pull_request:
branches:
- '**'
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
cancel-in-progress: true
jobs:
format-check:
name: Check code formatting
runs-on: windows-2022
steps:
- name: Checkout repository
uses: actions/checkout@v4.1.7
with:
fetch-depth: 0
- name: Check clang-format version
run: |
clang-format --version | findstr "18.1.8" >nul
if ($LASTEXITCODE -ne 0) {
echo "::error::Wrong clang-format version. Expected 18.1.8"
exit 1
}
shell: pwsh
- name: Get changed files
id: changed-files
run: |
$changedFiles = git diff --name-only origin/${{ github.base_ref }} HEAD |
findstr /R "^src\\|^tools\\" |
findstr /R "\.cpp$ \.hxx$ \.cxx$ \.lxx$ \.h$ \.pxx$ \.hpp$"
echo "files=$changedFiles" >> $env:GITHUB_OUTPUT
shell: pwsh
- name: Check formatting
id: check
if: steps.changed-files.outputs.files
run: |
$files = "${{ steps.changed-files.outputs.files }}" -split ' '
foreach ($file in $files) {
clang-format --style=file $file > "$file.formatted"
if (Compare-Object (Get-Content $file) (Get-Content "$file.formatted")) {
git diff --no-index $file "$file.formatted" >> format.patch
}
Remove-Item "$file.formatted"
}
if (Test-Path format.patch) {
echo "has_changes=true" >> $env:GITHUB_OUTPUT
echo "::error::Clang-format check failed! Some files need formatting.`nTo fix:`n1. Download format.patch from workflow artifacts`n2. Apply it using: git apply format.patch`n3. Commit and push the changes"
exit 1
}
shell: pwsh
continue-on-error: true
- name: Upload format patch
if: steps.check.outputs.has_changes == 'true'
uses: actions/upload-artifact@v4
with:
name: format-patch
path: format.patch
- name: Fail if formatting errors found
if: steps.check.outputs.has_changes == 'true'
run: exit 1