Rupert Swarbrick | 105df01 | 2021-01-29 11:33:13 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # Copyright lowRISC contributors. |
| 3 | # Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | |
| 6 | # A wrapper around clang-format, used for CI. |
| 7 | # |
| 8 | # Expects a single argument, which is the pull request's target branch |
| 9 | # (usually "master"). |
| 10 | |
| 11 | set -e |
| 12 | |
| 13 | if [ $# != 1 ]; then |
| 14 | echo >&2 "Usage: clang-format.sh <tgt-branch>" |
| 15 | exit 1 |
| 16 | fi |
| 17 | tgt_branch="$1" |
| 18 | |
Rupert Swarbrick | 81ada4f | 2021-02-16 15:59:39 +0000 | [diff] [blame] | 19 | merge_base="$(git merge-base origin/$tgt_branch HEAD)" || { |
Rupert Swarbrick | 105df01 | 2021-01-29 11:33:13 +0000 | [diff] [blame] | 20 | echo >&2 "Failed to find fork point for origin/$tgt_branch." |
| 21 | exit 1 |
| 22 | } |
| 23 | echo "Running C/C++ lint checks on files changed since $merge_base" |
| 24 | |
| 25 | TMPFILE="$(mktemp)" || { |
| 26 | echo >&2 "Failed to create temporary file" |
| 27 | exit 1 |
| 28 | } |
| 29 | trap 'rm -f "$TMPFILE"' EXIT |
| 30 | |
| 31 | set -o pipefail |
| 32 | git diff -U0 "$merge_base" -- "*.cpp" "*.cc" "*.c" "*.h" ':!*/vendor/*' | \ |
| 33 | clang-format-diff -p1 | \ |
| 34 | tee "$TMPFILE" |
| 35 | if [ -s "$TMPFILE" ]; then |
| 36 | echo -n "##vso[task.logissue type=error]" |
| 37 | echo "C/C++ lint failed. Use 'git clang-format' with appropriate options to reformat the changed code." |
| 38 | exit 1 |
| 39 | fi |