[ci] Move code for quick lint job into bash scripts
This shouldn't cause any changes to behaviour, but I have done a bit
of cleaning up (using the magic pathspec arguments to git diff to
simplify some things, and avoiding creating files in $PWD, to make
this nicer to run locally).
The idea is that a developer can now run
ci/jobs/quick-lint.sh
to get roughly the same tests as are run by the quick lint step for a
pull request in CI.
Signed-off-by: Rupert Swarbrick <rswarbrick@lowrisc.org>
diff --git a/ci/scripts/clang-format.sh b/ci/scripts/clang-format.sh
new file mode 100755
index 0000000..bc89fc9
--- /dev/null
+++ b/ci/scripts/clang-format.sh
@@ -0,0 +1,39 @@
+#!/bin/bash
+# Copyright lowRISC contributors.
+# Licensed under the Apache License, Version 2.0, see LICENSE for details.
+# SPDX-License-Identifier: Apache-2.0
+
+# A wrapper around clang-format, used for CI.
+#
+# Expects a single argument, which is the pull request's target branch
+# (usually "master").
+
+set -e
+
+if [ $# != 1 ]; then
+ echo >&2 "Usage: clang-format.sh <tgt-branch>"
+ exit 1
+fi
+tgt_branch="$1"
+
+merge_base="$(git merge-base --fork-point origin/$tgt_branch)" || {
+ echo >&2 "Failed to find fork point for origin/$tgt_branch."
+ exit 1
+}
+echo "Running C/C++ lint checks on files changed since $merge_base"
+
+TMPFILE="$(mktemp)" || {
+ echo >&2 "Failed to create temporary file"
+ exit 1
+}
+trap 'rm -f "$TMPFILE"' EXIT
+
+set -o pipefail
+git diff -U0 "$merge_base" -- "*.cpp" "*.cc" "*.c" "*.h" ':!*/vendor/*' | \
+ clang-format-diff -p1 | \
+ tee "$TMPFILE"
+if [ -s "$TMPFILE" ]; then
+ echo -n "##vso[task.logissue type=error]"
+ echo "C/C++ lint failed. Use 'git clang-format' with appropriate options to reformat the changed code."
+ exit 1
+fi