fixes #36
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: clang-format-check | |
on: | |
pull_request: | |
types: [opened, synchronize] | |
branches: | |
- main | |
workflow_dispatch: | |
jobs: | |
format-check: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 # Fetch all commits of the pull request branch | |
- name: Install clang-format | |
run: sudo apt-get install -y clang-format | |
- name: Download clang-format-diff.py | |
run: | | |
wget https://raw.githubusercontent.com/llvm/llvm-project/main/clang/tools/clang-format/clang-format-diff.py | |
chmod +x clang-format-diff.py | |
- name: Get the base and head branches | |
id: fetchstep | |
run: | | |
echo "BASE_BRANCH=${{ github.event.pull_request.base.ref }}" >> $GITHUB_ENV | |
echo "HEAD_BRANCH=${{ github.event.pull_request.head.ref }}" >> $GITHUB_ENV | |
echo "Base branch: $BASE_BRANCH" | |
echo "Head branch: $HEAD_BRANCH" | |
git fetch origin ${{ env.BASE_BRANCH }} | |
git fetch origin ${{ env.HEAD_BRANCH }} | |
#run formatter inline | |
- name: git diff | |
id: diffstep | |
env: | |
ACTIONS_RUNNER_DEBUG: true | |
run: | | |
# Format only the changed lines using clang-format-diff.py | |
set -e | |
git diff -U0 --no-color origin/${{ env.BASE_BRANCH }}...origin/${{ env.HEAD_BRANCH }} > diff_output.patch | |
cat diff_output.patch | |
- name: format diff | |
id: formatstep | |
env: | |
ACTIONS_RUNNER_DEBUG: true | |
run: | | |
if [ -s diff_output.patch ]; then | |
echo "diff exists" | |
if [ -f clang-format-diff.py ]; then | |
echo "clang-format-diff.py exists." | |
else | |
echo "Error: clang-format-diff.py not found!" >&2 | |
exit 1 | |
fi | |
python3 clang-format-diff.py -p1 < diff_output.patch > formatted_differences.patch 2> error.log || true | |
if [ -s error.log ]; then | |
echo "Errors from clang-format-diff.py:" | |
cat error.log | |
else | |
echo "No errors." | |
fi | |
rm diff_output.patch | |
else | |
exit 1 | |
fi | |
# check if differences found after formatting | |
- name: Check formatting needed | |
id: validatestep | |
env: | |
ACTIONS_RUNNER_DEBUG: true | |
run: | | |
if [ -s formatted_differences.patch ]; then | |
echo "Formatting issues found!. Formatted changes:" | |
cat formatted_differences.patch | |
rm formatted_differences.patch | |
exit 1 | |
fi |