From bfc06811adab382d5a86315e43c4771f8974bef5 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Thu, 12 Mar 2026 10:51:33 -0700 Subject: [PATCH] Add clang-format-diff to github actions Back again with another attempt at #12446 after 6 years. --- .clang-format-ignore | 9 +++++++++ .github/workflows/ci.yml | 15 +++++++++++++++ tools/maint/clang-format-diff.sh | 22 ++++++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 .clang-format-ignore create mode 100755 tools/maint/clang-format-diff.sh diff --git a/.clang-format-ignore b/.clang-format-ignore new file mode 100644 index 0000000000000..e6fa9625f0201 --- /dev/null +++ b/.clang-format-ignore @@ -0,0 +1,9 @@ +# ignore third_party code from clang-format checks +third_party/* +test/third_party/* +system/lib/libc/musl +system/lib/libcxx +system/lib/libcxxabi +system/lib/libunwind +system/lib/mimalloc +system/lib/llvm-libc diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a044642587352..e6b56ae81140e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -98,3 +98,18 @@ jobs: echo "-- This failure is only a warning and can be ignored" exit 1 fi + + clang-format-diff: + env: + LLVM_VERSION: 19 + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + # Fetch all history for all tags and branches + with: + fetch-depth: 0 + - name: Install clang-format + run: | + sudo apt-get install clang-format-19 + sudo update-alternatives --install /usr/bin/git-clang-format git-clang-format /usr/bin/git-clang-format-19 100 + - run: tools/maint/clang-format-diff.sh origin/$GITHUB_BASE_REF diff --git a/tools/maint/clang-format-diff.sh b/tools/maint/clang-format-diff.sh new file mode 100755 index 0000000000000..0f5a937bdd631 --- /dev/null +++ b/tools/maint/clang-format-diff.sh @@ -0,0 +1,22 @@ +#!/bin/bash +# Based on binaryen/scripts/clang-format-diff.sh +# TODO(sbc): Switch to pre-packaged github actions once we find one that +# is mature enough for our needs. + +set -o errexit +set -o pipefail +set -o xtrace + +if [ -n "$1" ]; then + BRANCH="$1" +else + BRANCH="@{upstream}" +fi + +MERGE_BASE=$(git merge-base $BRANCH HEAD) +FORMAT_ARGS=${MERGE_BASE} +if [ -n "$LLVM_VERSION" ]; then + FORMAT_ARGS="--binary=clang-format-${LLVM_VERSION} ${FORMAT_ARGS}" +fi + +git clang-format ${FORMAT_ARGS} --diff