| 1 | #!/bin/bash |
| 2 | |
| 3 | ########################################################################### |
| 4 | # fheroes2: https://github.com/ihhub/fheroes2 # |
| 5 | # Copyright (C) 2021 - 2025 # |
| 6 | # # |
| 7 | # Copyright 2018 Google LLC # |
| 8 | # # |
| 9 | # This program is free software; you can redistribute it and/or modify # |
| 10 | # it under the terms of the GNU General Public License as published by # |
| 11 | # the Free Software Foundation; either version 2 of the License, or # |
| 12 | # (at your option) any later version. # |
| 13 | # # |
| 14 | # This program is distributed in the hope that it will be useful, # |
| 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # |
| 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # |
| 17 | # GNU General Public License for more details. # |
| 18 | # # |
| 19 | # You should have received a copy of the GNU General Public License # |
| 20 | # along with this program; if not, write to the # |
| 21 | # Free Software Foundation, Inc., # |
| 22 | # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # |
| 23 | ########################################################################### |
| 24 | |
| 25 | # Script to determine whether the source code in the Pull Request is formatted correctly. |
| 26 | # Exits with a non-zero exit code if formatting is needed. |
| 27 | # |
| 28 | # It is assumed that this script is called from the project root directory. |
| 29 | |
| 30 | set -e -o pipefail |
| 31 | |
| 32 | FILES_TO_CHECK=$(git diff --name-only HEAD^ | (grep -E ".*\.(cpp|cc|c\+\+|cxx|c|h|hpp|java|js)$" || true) \ |
| 33 | | (grep -v "^ios/SDL_uikit_main\.c$" || true) \ |
| 34 | | (grep -v "^ios/ShaderTypes\.h$" || true) \ |
| 35 | | (grep -v "^src/thirdparty/.*/.*" || true)) |
| 36 | |
| 37 | if [[ -z "$FILES_TO_CHECK" ]]; then |
| 38 | echo "There is no source code to check the formatting." |
| 39 | exit 0 |
| 40 | fi |
| 41 | |
| 42 | if FORMAT_DIFF=$(git diff -U0 HEAD^ -- $FILES_TO_CHECK | clang-format-diff -p1 -style=file) && [[ -z "$FORMAT_DIFF" ]]; then |
| 43 | echo "All the source code in the PR is formatted correctly." |
| 44 | exit 0 |
| 45 | else |
| 46 | echo "Formatting errors found!" |
| 47 | echo "$FORMAT_DIFF" |
| 48 | exit 1 |
| 49 | fi |