diff --git a/.github/workflows/clang-format-check.yml b/.github/workflows/clang-format-check.yml new file mode 100644 index 00000000..89e1076e --- /dev/null +++ b/.github/workflows/clang-format-check.yml @@ -0,0 +1,28 @@ +name: Check Clang-Format on Diff + +on: [push, pull_request] + +jobs: + clang-format: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Import LLVM GPG Key + run: | + sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 15CF4D18AF4F7421 + + - name: Add LLVM Repository + run: | + sudo add-apt-repository "deb http://apt.llvm.org/jammy llvm-toolchain-jammy-17 main" + sudo apt-get update + + - name: Install clang-format 17 + run: | + sudo apt-get install clang-format-17 + + - name: Check Clang-Format on Diff + run: | + ./clang-format-diff.sh ${{ github.base_ref }} ${{ github.ref }} diff --git a/clang-format-diff.sh b/clang-format-diff.sh new file mode 100755 index 00000000..18e4f132 --- /dev/null +++ b/clang-format-diff.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +BASE_BRANCH=$1 +CURRENT_BRANCH=$2 + +# Save unified diff with 0 context +git diff -U0 --name-only $BASE_BRANCH...$CURRENT_BRANCH > changes.diff + +# Apply clang-format on diff +FORMAT_DIFF=$(clang-format -style=file changes.diff) + +if [ ! -z "$FORMAT_DIFF" ]; then + echo "The following formatting errors were found:" + echo "$FORMAT_DIFF" + exit 1 +else + echo "All code is properly formatted." +fi