| Karl Schultz | a53b35f | 2017-12-18 16:16:45 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| Mark Lobodzinski | 4d9db43 | 2019-01-03 11:51:01 -0700 | [diff] [blame] | 2 | # Copyright (c) 2017-2019 Google Inc. |
| 3 | # Copyright (c) 2019 LunarG, Inc. |
| Karl Schultz | a53b35f | 2017-12-18 16:16:45 -0700 | [diff] [blame] | 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 | # Script to determine if source code in Pull Request is properly formatted. |
| 18 | # Exits with non 0 exit code if formatting is needed. |
| 19 | # |
| 20 | # This script assumes to be invoked at the project root directory. |
| 21 | |
| 22 | RED='\033[0;31m' |
| 23 | GREEN='\033[0;32m' |
| 24 | NC='\033[0m' # No Color |
| Mark Lobodzinski | 4d9db43 | 2019-01-03 11:51:01 -0700 | [diff] [blame] | 25 | FOUND_ERROR=0 |
| Karl Schultz | a53b35f | 2017-12-18 16:16:45 -0700 | [diff] [blame] | 26 | |
| Mike Schuchardt | 4f07e9e | 2018-04-05 22:44:05 -0600 | [diff] [blame] | 27 | FILES_TO_CHECK=$(git diff --name-only master | grep -v -E "^include/vulkan" | grep -E ".*\.(cpp|cc|c\+\+|cxx|c|h|hpp)$") |
| Mark Lobodzinski | 4d9db43 | 2019-01-03 11:51:01 -0700 | [diff] [blame] | 28 | COPYRIGHTED_FILES_TO_CHECK=$(git diff --name-only master | grep -v -E "^include/vulkan") |
| Karl Schultz | a53b35f | 2017-12-18 16:16:45 -0700 | [diff] [blame] | 29 | |
| 30 | if [ -z "${FILES_TO_CHECK}" ]; then |
| 31 | echo -e "${GREEN}No source code to check for formatting.${NC}" |
| Mark Lobodzinski | 4d9db43 | 2019-01-03 11:51:01 -0700 | [diff] [blame] | 32 | else |
| 33 | # Check source files in PR for clang-format errors |
| 34 | FORMAT_DIFF=$(git diff -U0 master -- ${FILES_TO_CHECK} | python ./scripts/clang-format-diff.py -p1 -style=file) |
| 35 | |
| 36 | if [ ! -z "${FORMAT_DIFF}" ]; then |
| 37 | echo -e "${RED}Found formatting errors!${NC}" |
| 38 | echo "${FORMAT_DIFF}" |
| 39 | FOUND_ERROR=1 |
| 40 | fi |
| 41 | |
| 42 | # Check files in PR out-of-date copyright notices |
| 43 | if [ -z "${COPYRIGHTED_FILES_TO_CHECK}" ]; then |
| 44 | echo -e "${GREEN}No source code to check for copyright dates.${NC}" |
| 45 | else |
| 46 | THISYEAR=$(date +"%Y") |
| 47 | # Look for current year in copyright lines |
| 48 | for AFILE in ${COPYRIGHTED_FILES_TO_CHECK} |
| 49 | do |
| Mark Lobodzinski | 5afc253 | 2019-01-09 15:45:58 -0700 | [diff] [blame] | 50 | COPYRIGHT_INFO=$(cat ${AFILE} | grep -E "Copyright (.)*LunarG") |
| Mark Lobodzinski | 72558f0 | 2019-01-07 15:08:50 -0700 | [diff] [blame] | 51 | if [ ! -z "${COPYRIGHT_INFO}" ]; then |
| 52 | if ! echo "$COPYRIGHT_INFO" | grep -q "$THISYEAR" ; then |
| 53 | echo -e "${RED} "$AFILE" has an out-of-date copyright notice.${NC}" |
| 54 | FOUND_ERROR=1 |
| 55 | fi |
| Mark Lobodzinski | 4d9db43 | 2019-01-03 11:51:01 -0700 | [diff] [blame] | 56 | fi |
| 57 | done |
| 58 | fi |
| Karl Schultz | a53b35f | 2017-12-18 16:16:45 -0700 | [diff] [blame] | 59 | fi |
| 60 | |
| Mark Lobodzinski | 4d9db43 | 2019-01-03 11:51:01 -0700 | [diff] [blame] | 61 | if [ $FOUND_ERROR -gt 0 ]; then |
| 62 | exit 1 |
| 63 | else |
| Karl Schultz | a53b35f | 2017-12-18 16:16:45 -0700 | [diff] [blame] | 64 | echo -e "${GREEN}All source code in PR properly formatted.${NC}" |
| 65 | exit 0 |
| Karl Schultz | a53b35f | 2017-12-18 16:16:45 -0700 | [diff] [blame] | 66 | fi |