Skip to content

Commit cfd2b6f

Browse files
committed
Split clang-format check into a separate script
1 parent fa32964 commit cfd2b6f

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/bash
2+
3+
# Stop on errors
4+
set -e
5+
6+
# Log information about the run of this check.
7+
echo "Pull request's base branch is: ${BASE_BRANCH}"
8+
echo "Pull request's merge branch is: ${MERGE_BRANCH}"
9+
echo "Pull request's source branch is: ${GITHUB_HEAD_REF}"
10+
clang-format-7 --version
11+
12+
# The checkout action leaves us in detatched head state. The following line
13+
# names the checked out commit, for simpler reference later.
14+
git checkout -b ${MERGE_BRANCH}
15+
16+
# Build list of files to ignore
17+
while read file ; do EXCLUDES+="':(top,exclude)$file' " ; done < .clang-format-ignore
18+
19+
# Make sure we can refer to ${BASE_BRANCH} by name
20+
git checkout ${BASE_BRANCH}
21+
git checkout ${MERGE_BRANCH}
22+
23+
# Find the commit on which the PR is based.
24+
MERGE_BASE=$(git merge-base ${BASE_BRANCH} ${MERGE_BRANCH})
25+
echo "Checking for formatting errors introduced since $MERGE_BASE"
26+
27+
# Do the checking. "eval" is used so that quotes (as inserted into $EXCLUDES
28+
# above) are not interpreted as parts of file names.
29+
eval git-clang-format-7 --binary clang-format-7 $MERGE_BASE -- $EXCLUDES
30+
git diff > formatted.diff
31+
if [[ -s formatted.diff ]] ; then
32+
echo 'Formatting error! The following diff shows the required changes'
33+
echo 'Use the raw log to get a version of the diff that preserves spacing'
34+
cat formatted.diff
35+
exit 1
36+
fi
37+
echo 'No formatting errors found'
38+
exit 0

.github/workflows/pull-request-checks.yaml

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -143,25 +143,4 @@ jobs:
143143
env:
144144
BASE_BRANCH: ${{ github.base_ref }}
145145
MERGE_BRANCH: ${{ github.ref }}
146-
run: |
147-
echo "Pull request's base branch is: ${BASE_BRANCH}"
148-
echo "Pull request's merge branch is: ${MERGE_BRANCH}"
149-
echo "Pull request's source branch is: ${GITHUB_HEAD_REF}"
150-
clang-format-7 --version
151-
git checkout -b ${MERGE_BRANCH}
152-
while read file ; do EXCLUDES+="':(top,exclude)$file' " ; done < .clang-format-ignore
153-
git checkout ${BASE_BRANCH}
154-
git checkout ${MERGE_BRANCH}
155-
MERGE_BASE=$(git merge-base ${BASE_BRANCH} ${MERGE_BRANCH})
156-
echo "Checking for formatting errors introduced since $MERGE_BASE"
157-
# use eval so that quotes (as inserted into $EXCLUDES above) are not interpreted as parts of file names
158-
eval git-clang-format-7 --binary clang-format-7 $MERGE_BASE -- $EXCLUDES
159-
git diff > formatted.diff
160-
if [[ -s formatted.diff ]] ; then
161-
echo 'Formatting error! The following diff shows the required changes'
162-
echo 'Use the raw log to get a version of the diff that preserves spacing'
163-
cat formatted.diff
164-
exit 1
165-
fi
166-
echo 'No formatting errors found'
167-
exit 0
146+
run: ./.github/workflows/pull-request-check-clang-format.sh

0 commit comments

Comments
 (0)