| 1 | #!/bin/bash |
| 2 | |
| 3 | ########################################################################### |
| 4 | # fheroes2: https://github.com/ihhub/fheroes2 # |
| 5 | # Copyright (C) 2022 - 2025 # |
| 6 | # # |
| 7 | # This program is free software; you can redistribute it and/or modify # |
| 8 | # it under the terms of the GNU General Public License as published by # |
| 9 | # the Free Software Foundation; either version 2 of the License, or # |
| 10 | # (at your option) any later version. # |
| 11 | # # |
| 12 | # This program is distributed in the hope that it will be useful, # |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # |
| 15 | # GNU General Public License for more details. # |
| 16 | # # |
| 17 | # You should have received a copy of the GNU General Public License # |
| 18 | # along with this program; if not, write to the # |
| 19 | # Free Software Foundation, Inc., # |
| 20 | # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # |
| 21 | ########################################################################### |
| 22 | |
| 23 | # Script to determine whether the source code in the Pull Request has correct copyright headers. |
| 24 | # Exits with a non-zero exit code if copyright headers need to be updated. |
| 25 | # |
| 26 | # It is assumed that this script is called from the project root directory. |
| 27 | |
| 28 | set -e -o pipefail |
| 29 | |
| 30 | SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}") |
| 31 | HEADERS_DIR="$SCRIPT_DIR/copyright_headers" |
| 32 | |
| 33 | C_LIKE_FILES_TO_CHECK=$(git diff --name-only HEAD^ | (grep -E ".*\.(cpp|cc|c\+\+|cxx|c|h|hpp|java|rc|js)$" || true) \ |
| 34 | | (grep -v "^ios/SDL_uikit_main\.c$" || true) \ |
| 35 | | (grep -v "^ios/ShaderTypes\.h$" || true) \ |
| 36 | | (grep -v "^src/thirdparty/.*/.*" || true)) |
| 37 | SCRIPT_FILES_TO_CHECK=$(git diff --name-only HEAD^ | (grep -E ".*(\.(sh|py|ps1)|Android\.mk|Application\.mk|CMakeLists\.txt|Makefile[^/]*)$" || true) \ |
| 38 | | (grep -v "^src/thirdparty/.*/.*" || true)) |
| 39 | WINBAT_FILES_TO_CHECK=$(git diff --name-only HEAD^ | (grep -E ".*\.bat$" || true) \ |
| 40 | | (grep -v "^android/gradlew\.bat$" || true) \ |
| 41 | | (grep -v "^src/thirdparty/.*/.*" || true)) |
| 42 | |
| 43 | if [[ -z "$C_LIKE_FILES_TO_CHECK" ]] && [[ -z "$SCRIPT_FILES_TO_CHECK" ]] && [[ -z "$WINBAT_FILES_TO_CHECK" ]]; then |
| 44 | echo "There is no source code to check the correctness of the copyright headers." |
| 45 | exit 0 |
| 46 | fi |
| 47 | |
| 48 | if [[ -n "$C_LIKE_FILES_TO_CHECK" ]]; then |
| 49 | C_LIKE_FORMAT_DIFF=$(python3 "$SCRIPT_DIR/check_copyright_headers.py" "$HEADERS_DIR/full_header_c_like.txt" \ |
| 50 | "$HEADERS_DIR/header_template_c_like.txt" \ |
| 51 | $C_LIKE_FILES_TO_CHECK) |
| 52 | fi |
| 53 | if [[ -n "$SCRIPT_FILES_TO_CHECK" ]]; then |
| 54 | SCRIPT_FORMAT_DIFF=$(python3 "$SCRIPT_DIR/check_copyright_headers.py" --handle-shebang \ |
| 55 | "$HEADERS_DIR/full_header_script.txt" \ |
| 56 | "$HEADERS_DIR/header_template_script.txt" \ |
| 57 | $SCRIPT_FILES_TO_CHECK) |
| 58 | fi |
| 59 | if [[ -n "$WINBAT_FILES_TO_CHECK" ]]; then |
| 60 | WINBAT_FORMAT_DIFF=$(python3 "$SCRIPT_DIR/check_copyright_headers.py" "$HEADERS_DIR/full_header_winbat.txt" \ |
| 61 | "$HEADERS_DIR/header_template_winbat.txt" \ |
| 62 | $WINBAT_FILES_TO_CHECK) |
| 63 | fi |
| 64 | |
| 65 | if [[ -z "$C_LIKE_FORMAT_DIFF" ]] && [[ -z "$SCRIPT_FORMAT_DIFF" ]] && [[ -z "$WINBAT_FORMAT_DIFF" ]]; then |
| 66 | echo "All the source code in the PR has the correct copyright headers." |
| 67 | exit 0 |
| 68 | else |
| 69 | echo "Invalid copyright headers found!" |
| 70 | [[ -n "$C_LIKE_FORMAT_DIFF" ]] && echo "$C_LIKE_FORMAT_DIFF" |
| 71 | [[ -n "$SCRIPT_FORMAT_DIFF" ]] && echo "$SCRIPT_FORMAT_DIFF" |
| 72 | [[ -n "$WINBAT_FORMAT_DIFF" ]] && echo "$WINBAT_FORMAT_DIFF" |
| 73 | exit 1 |
| 74 | fi |