1#!/bin/bash
2# Copyright (c) 2017-2019 Google Inc.
3# Copyright (c) 2019 LunarG, Inc.
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
22RED='\033[0;31m'
23GREEN='\033[0;32m'
24NC='\033[0m' # No Color
25FOUND_ERROR=0
26
27FILES_TO_CHECK=$(git diff --name-only master | grep -v -E "^include/vulkan" | grep -E ".*\.(cpp|cc|c\+\+|cxx|c|h|hpp)$")
28COPYRIGHTED_FILES_TO_CHECK=$(git diff --name-only master | grep -v -E "^include/vulkan")
29
30if [ -z "${FILES_TO_CHECK}" ]; then
31  echo -e "${GREEN}No source code to check for formatting.${NC}"
32else
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
50      COPYRIGHT_INFO=$(cat ${AFILE} | grep -E "Copyright (.)*LunarG")
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
56      fi
57    done
58  fi
59fi
60
61if [ $FOUND_ERROR  -gt 0 ]; then
62  exit 1
63else
64  echo -e "${GREEN}All source code in PR properly formatted.${NC}"
65  exit 0
66fi
67