Skip to content

Commit

Permalink
add formatting check only on changed lines (#3169)
Browse files Browse the repository at this point in the history
* add google style formatting check only on changed lines
  • Loading branch information
sbera87 authored Nov 3, 2024
1 parent 4d7df80 commit 977690c
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions .github/workflows/clang-format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
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
# get names of the base and pr branches
- 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 }}
#get differences on the PR branch excluding generated folder in the root
- 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 }} -- . ':!generated/' > diff_output.patch
cat diff_output.patch
# run formatter on the differences if any
- name: format diff
id: formatstep
env:
ACTIONS_RUNNER_DEBUG: true
run: |
if [ -s diff_output.patch ]; then
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 from clang-format-diff.py"
fi
rm diff_output.patch
fi
# check if differences found after formatting, then exit
- 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

0 comments on commit 977690c

Please sign in to comment.