|
| 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 |
0 commit comments