From ff4ab50b1d1bd05872e8ad56efa36e4027262ee0 Mon Sep 17 00:00:00 2001 From: Peter McLean Date: Sat, 2 Aug 2025 11:40:27 -0400 Subject: [PATCH 1/2] Add clang-format workflow --- .github/workflows/clang_format.yaml | 58 ++++++++++++++++++++++ .github/workflows/cmake-multi-platform.yml | 2 + 2 files changed, 60 insertions(+) create mode 100644 .github/workflows/clang_format.yaml diff --git a/.github/workflows/clang_format.yaml b/.github/workflows/clang_format.yaml new file mode 100644 index 00000000..11967d6b --- /dev/null +++ b/.github/workflows/clang_format.yaml @@ -0,0 +1,58 @@ +name: Clang Format Check + +on: + push: + branches: [ "master" ] + paths-ignore: + - '**/*.md' + - '*.md' + pull_request: + branches: [ "master" ] + paths-ignore: + - '**/*.md' + - '*.md' + +jobs: + clang-format: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Install clang-format + shell: bash + run: | + sudo apt-get update + sudo apt-get install -y clang-format-19 + + - name: Get list of source files + id: files + shell: bash + run: | + FILES=$(git ls-files '*.cpp' '*.c' '*.h' '*.hpp' '*.cxx' '*.hxx' '*.cc' '*.hh' | tr '\n' ' ') + echo "FILES=${FILES}" >> $GITHUB_OUTPUT + + - name: Check formatting + shell: bash + run: | + FILES="${{ steps.files.outputs.FILES }}" + + if [[ -z "${FILES}" ]]; then + echo "No source files to check." + exit 0 + fi + + clang-format-19 --dry-run --Werror ${FILES} 2>&1 | tee clang-format-report.txt || true + + if [[ -s clang-format-report.txt ]]; then + echo "❌ Code formatting issues found. See clang-format-report.txt." + exit 1 + fi + + - name: Upload clang-format report + if: failure() + uses: actions/upload-artifact@v4 + with: + name: clang-format-report + path: clang-format-report.txt diff --git a/.github/workflows/cmake-multi-platform.yml b/.github/workflows/cmake-multi-platform.yml index 66d25613..c452235a 100644 --- a/.github/workflows/cmake-multi-platform.yml +++ b/.github/workflows/cmake-multi-platform.yml @@ -6,10 +6,12 @@ on: branches: [ "master" ] paths-ignore: - '**/*.md' + - '*.md' pull_request: branches: [ "master" ] paths-ignore: - '**/*.md' + - '*.md' jobs: run-matrix: From e19c4318070b15f35b6f07547a6fd2ff3562d9fe Mon Sep 17 00:00:00 2001 From: Peter McLean Date: Sat, 2 Aug 2025 20:13:49 -0400 Subject: [PATCH 2/2] Check clang-format of diff/patch instead of everything --- .github/workflows/clang_format.yaml | 41 ++++++++++++++++------------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/.github/workflows/clang_format.yaml b/.github/workflows/clang_format.yaml index 11967d6b..59c0c12b 100644 --- a/.github/workflows/clang_format.yaml +++ b/.github/workflows/clang_format.yaml @@ -19,6 +19,8 @@ jobs: steps: - name: Checkout code uses: actions/checkout@v4 + with: + fetch-depth: 0 - name: Install clang-format shell: bash @@ -26,33 +28,36 @@ jobs: sudo apt-get update sudo apt-get install -y clang-format-19 - - name: Get list of source files - id: files - shell: bash + - name: Fetch base branch + if: github.event_name == 'pull_request' run: | - FILES=$(git ls-files '*.cpp' '*.c' '*.h' '*.hpp' '*.cxx' '*.hxx' '*.cc' '*.hh' | tr '\n' ' ') - echo "FILES=${FILES}" >> $GITHUB_OUTPUT + echo "Fetching base branch ${{ github.base_ref }}" + git fetch origin ${{ github.base_ref }}:${{ github.base_ref }} - - name: Check formatting - shell: bash + - name: Get Git Diff + id: diff run: | - FILES="${{ steps.files.outputs.FILES }}" - - if [[ -z "${FILES}" ]]; then - echo "No source files to check." - exit 0 + if [[ "${{ github.event_name }}" == "pull_request" ]]; then + echo "Getting diff for pull request" + git diff origin/${{ github.base_ref }}...HEAD > diff.patch + else + echo "Getting diff for commit" + git diff ${{ github.event.before }} ${{ github.event.after }} > diff.patch fi - clang-format-19 --dry-run --Werror ${FILES} 2>&1 | tee clang-format-report.txt || true + - name: Check formatting + shell: bash + run: | + cat diff.patch | clang-format-diff-19 -p 1 > diff_format.patch - if [[ -s clang-format-report.txt ]]; then - echo "❌ Code formatting issues found. See clang-format-report.txt." + if [ -s diff_format.patch ]; then + echo "❌ Code formatting issues found. See diff_format.patch." exit 1 fi - - name: Upload clang-format report + - name: Upload clang-formatted diff if: failure() uses: actions/upload-artifact@v4 with: - name: clang-format-report - path: clang-format-report.txt + name: clang-format-diff + path: diff_format.patch