|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Copyright (C) 2025 HERE Europe B.V. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | +# SPDX-License-Identifier: Apache-2.0 |
| 18 | +# License-Filename: LICENSE |
| 19 | +# |
| 20 | + |
| 21 | +# Important 2 lines |
| 22 | +set +e |
| 23 | +set -x |
| 24 | + |
| 25 | +# This script gets the changed files in the pull request, and runs |
| 26 | +# clang-format tool to verify them |
| 27 | + |
| 28 | +# Get the current branch name |
| 29 | +CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) |
| 30 | + |
| 31 | +if [[ $CURRENT_BRANCH == "master" ]]; then |
| 32 | + printf "Currently in master branch, skipping clang-format run.\n" |
| 33 | + exit 0 |
| 34 | +else |
| 35 | + printf "Currently in %s branch. Running clang-foramt.\n" "$CURRENT_BRANCH" |
| 36 | +fi |
| 37 | + |
| 38 | +git branch --all |
| 39 | +git fetch origin master |
| 40 | +git branch --all |
| 41 | +# Get affected files and filter source files |
| 42 | +FILES=$(git diff-tree --no-commit-id --name-only -r origin/master "$CURRENT_BRANCH" \ |
| 43 | + | grep '\.c\|\.cpp\|\.cxx\|\.h\|\.hpp\|\.hxx') |
| 44 | + |
| 45 | +if [ -z "$FILES" ]; then |
| 46 | + printf "No affected files, exiting.\n" |
| 47 | + exit 0 |
| 48 | +else |
| 49 | + printf "Affected files:\n %s\n" "$FILES" |
| 50 | +fi |
| 51 | + |
| 52 | +printf "\n\n### Running clang-format - START ### \n\n" |
| 53 | + |
| 54 | +clang-format-6.0 -i ${FILES} |
| 55 | +RESULT=$? |
| 56 | + |
| 57 | +printf "\n\n### Running clang-format - DONE ### \n\n" |
| 58 | + |
| 59 | +if [ "$RESULT" -ne "0" ]; then |
| 60 | + printf "\n\nClang-format failed!\n\n" |
| 61 | + exit ${RESULT} |
| 62 | +fi |
| 63 | + |
| 64 | +CLANG_FORMAT_FILE=${CLANG_FORMAT_FILE:-clang-format.diff} |
| 65 | +git diff > ${CLANG_FORMAT_FILE} |
| 66 | +if [ -s ${CLANG_FORMAT_FILE} ] ; then |
| 67 | + echo "Unformatted files are detected. " |
| 68 | + echo "You may apply provided patch. Download from the workflow summary and unpack to root of repository." |
| 69 | + echo "Then run: git apply $CLANG_FORMAT_FILE" |
| 70 | + # No error here, otherwise github is skipping further steps so artifacts are not stored |
| 71 | +fi |
0 commit comments