From 7e311b806df2bc8b042b01835e5f6e972c77f3a7 Mon Sep 17 00:00:00 2001 From: sirknightj Date: Sun, 26 Jan 2025 20:05:13 -0800 Subject: [PATCH] Update the clang-format job to give a patch --- .github/workflows/ci.yml | 13 ----- .github/workflows/clang-format.yml | 93 ++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 13 deletions(-) create mode 100644 .github/workflows/clang-format.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1f3af92c0..59d5e68e1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,19 +10,6 @@ on: - develop - master jobs: - clang-format-check: - runs-on: macos-latest - steps: - - name: Clone repository - uses: actions/checkout@v3 - - name: Install clang-format - run: | - brew install clang-format - clang-format --version - - name: Run clang format check - run: | - bash scripts/check-clang.sh - mac-os-build-gcc: runs-on: macos-13 permissions: diff --git a/.github/workflows/clang-format.yml b/.github/workflows/clang-format.yml new file mode 100644 index 000000000..9f1e32774 --- /dev/null +++ b/.github/workflows/clang-format.yml @@ -0,0 +1,93 @@ +name: clang-format-check +on: [ push ] +jobs: + check-format: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up clang-format + run: | + sudo apt-get update + sudo apt-get install -y clang-format-14 + sudo ln -sf /usr/bin/clang-format-14 /usr/bin/clang-format + + - name: Check clang + run: | + find . -name "*.c" -o -name "*.h" -o -name "*.cpp" | xargs clang-format -style=file --dry-run -Werror + shell: bash + + - name: Clang Format Success Summary + if: success() + run: | + echo "# 🎉 Clang Format Check Passed!" >> $GITHUB_STEP_SUMMARY + echo "All files are properly formatted." >> $GITHUB_STEP_SUMMARY + + format-and-upload: + runs-on: ubuntu-latest + needs: check-format + if: failure() # Only run this job if the check-format job fails + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up clang-format + run: | + sudo apt-get update + sudo apt-get install -y clang-format-14 + sudo ln -sf /usr/bin/clang-format-14 /usr/bin/clang-format + + - name: Find and format .c, .h, and .cpp files + run: | + git add -u + # Apply formatting + find . -name "*.c" -o -name "*.h" -o -name "*.cpp" | xargs clang-format -i -style=file + # Check which files were modified by clang-format + git diff --name-only > modified_files.txt + git diff > clang-format-fix.patch + echo "Modified files: $(cat modified_files.txt)" + shell: bash + + - name: Archive formatted files + run: | + mkdir -p formatted_files + while IFS= read -r file; do + echo "Compressing $file ..." + # Copy only modified files to the formatted_files directory, preserving structure + mkdir -p "formatted_files/$(dirname "$file")" + cp "$file" "formatted_files/$file" + done < modified_files.txt + shell: bash + + - name: Upload formatted files + uses: actions/upload-artifact@v4 + with: + name: formatted-files + path: formatted_files + + - name: Upload patch file + uses: actions/upload-artifact@v4 + with: + name: clang-format-fix.patch + path: clang-format-fix.patch + + - name: Generate Markdown Summary for Corrected Files + run: | + echo "# ❌ Clang Format Check Failed" >> $GITHUB_STEP_SUMMARY + echo "$(wc -l < modified_files.txt) file(s) are not clang-format compliant. Please review the list of affected files below:" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + while IFS= read -r file; do + echo "- \`$file\`" >> $GITHUB_STEP_SUMMARY + done < modified_files.txt + echo "" >> $GITHUB_STEP_SUMMARY + echo "## 🛠ī¸ Fixed files" >> $GITHUB_STEP_SUMMARY + echo "**Total Files Formatted**: $(wc -l < modified_files.txt)" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "ℹī¸ Check the \"Artifacts\" below for the corrected files." >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "To apply the formatting fixes locally, download the patch file below and place it in the repository root. Then run this command from the repository root:" >> $GITHUB_STEP_SUMMARY + echo "\`\`\`bash" >> $GITHUB_STEP_SUMMARY + echo "git apply clang-format-fix.patch" >> $GITHUB_STEP_SUMMARY + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY