Skip to content

Commit 645ac97

Browse files
committed
Add clang-format-diff to github actions
Back again with another attempt at #12446 after 6 years.
1 parent 0f992a2 commit 645ac97

3 files changed

Lines changed: 52 additions & 0 deletions

File tree

.clang-format-ignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# ignore third_party code from clang-format checks
2+
third_party/*
3+
test/third_party/*
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: clang-format-diff
2+
3+
on: [pull_request]
4+
5+
jobs:
6+
clang_format_diff:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v4
10+
# Fetch all history for all tags and branches
11+
with:
12+
fetch-depth: 0
13+
- name: Install clang-format
14+
run: |
15+
sudo apt-get install clang-format-19
16+
sudo update-alternatives --install /usr/bin/git-clang-format git-clang-format /usr/bin/git-clang-format-19 100
17+
- run: tests/clang-format-diff.sh origin/$GITHUB_BASE_REF

tools/maint/clang-format-diff.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/bash
2+
# Based on binaryen/scripts/clang-format-diff.sh
3+
# TODO(sbc): Switch to pre-packaged github actions once we find one that
4+
# is mature enough for our needs.
5+
6+
set -o errexit
7+
set -o pipefail
8+
9+
if [ -n "$1" ]; then
10+
BRANCH="$1"
11+
elif [ -n "$GITHUB_BASE_REF" ]; then
12+
BRANCH="origin/$GITHUB_BASE_REF"
13+
else
14+
BRANCH="@{upstream}"
15+
fi
16+
17+
MERGE_BASE=$(git merge-base $BRANCH HEAD)
18+
FORMAT_ARGS=${MERGE_BASE}
19+
if [ -n "$LLVM_VERSION" ]; then
20+
FORMAT_ARGS="--binary=clang-format-${LLVM_VERSION} ${FORMAT_MSG}"
21+
fi
22+
23+
FORMAT_MSG=$(git clang-format ${FORMAT_ARGS} -q --diff || true)
24+
if [ -n "$FORMAT_MSG" -a "$FORMAT_MSG" != "no modified files to format" ]
25+
then
26+
echo "Please run git clang-format with clang-format-${LLVM_VERSION} before committing, or apply this diff:"
27+
echo
28+
# Run git clang-format again, this time without capruting stdout. This way
29+
# clang-format format the message nicely and add color.
30+
git clang-format ${FORMAT_ARGS} -q --diff
31+
exit 1
32+
fi

0 commit comments

Comments
 (0)