cgl: use SPDX license and copyright identifiers
[mesa-waffle.git] / .gitlab-ci / clang-format.sh
blobc0c0aa9357d28f852eec776d071f48a661f8e9df
1 #!/bin/bash
2 # vim: et sts=4 sw=4
4 set -euo pipefail
6 # The following approach was shamelessly copied from ci-fairy
7 # https://gitlab.freedesktop.org/freedesktop/ci-templates/-/blob/master/tools/ci_fairy.py
8 # CHANGES: Swap CI for CI_MERGE_REQUEST_ID, add CI_COMMIT_BRANCH.
9 if [[ ${CI_MERGE_REQUEST_ID-} ]]; then
10 upstream="${FDO_UPSTREAM_REPO-}"
11 if [[ ! "$upstream" ]]; then
12 echo "$FDO_UPSTREAM_REPO not set, using local branches to compare"
13 upstream="${CI_PROJECT_PATH}"
15 host="${CI_SERVER_HOST}"
16 token="${CI_JOB_TOKEN}"
17 url="https://gitlab-ci-token:$token@$host/$upstream"
19 remote="clang-fairy"
20 if ! git remote | grep -qw "$remote"; then
21 git remote add "$remote" "$url"
23 git fetch "$remote"
25 # There are two types of MRs apparently
26 if [[ ${CI_MERGE_REQUEST_TARGET_BRANCH_SHA-} ]]; then
27 echo "Using CI_MERGE_REQUEST_SOURCE_BRANCH_SHA"
28 commit=$CI_MERGE_REQUEST_SOURCE_BRANCH_SHA
29 elif [[ ${CI_MERGE_REQUEST_DIFF_BASE_SHA-} ]]; then
30 echo "Using CI_MERGE_REQUEST_DIFF_BASE_SHA"
31 commit=$CI_MERGE_REQUEST_DIFF_BASE_SHA
32 else
33 echo "Looking like a MR, but CI variables are not set."
34 echo "Using the default branch \"$CI_DEFAULT_BRANCH\"."
35 commit="$remote/$CI_DEFAULT_BRANCH"
37 elif [[ ${CI_COMMIT_BRANCH-} ]]; then
38 echo "Using CI_COMMIT_BEFORE_SHA"
39 commit=$CI_COMMIT_BEFORE_SHA
40 else
41 commit="master"
44 # End of the shameless copy
46 # Older versions of clang-format like 6 and 7 print an annoying message.
47 # "no modified files to format". Newer ones like 12 (earlier?), do not.
48 formatting_changes=$(git-clang-format --commit "$commit" -q --diff | grep -v "no modified files to format" || true)
49 [[ "$formatting_changes" ]] || exit 0
51 echo "ERROR: Formatting issues detected"
52 echo
53 echo "Update the codebase as indicated or disable clang-format locally."
54 echo
55 echo "Formatting suggestion:"
56 echo
57 echo -e "------------\n$formatting_changes\n------------"
58 echo
60 exit 1