blob: 0b5b01c10cd3a7c84754d4cd5eedf47f634f9ff2 [file] [log] [blame]
Karl Schultza53b35f2017-12-18 16:16:45 -07001#!/bin/bash
Mark Lobodzinski4d9db432019-01-03 11:51:01 -07002# Copyright (c) 2017-2019 Google Inc.
3# Copyright (c) 2019 LunarG, Inc.
Karl Schultza53b35f2017-12-18 16:16:45 -07004
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
Mark Lobodzinski4d9db432019-01-03 11:51:01 -070025FOUND_ERROR=0
Karl Schultza53b35f2017-12-18 16:16:45 -070026
Mike Schuchardt4f07e9e2018-04-05 22:44:05 -060027FILES_TO_CHECK=$(git diff --name-only master | grep -v -E "^include/vulkan" | grep -E ".*\.(cpp|cc|c\+\+|cxx|c|h|hpp)$")
Mark Lobodzinski4d9db432019-01-03 11:51:01 -070028COPYRIGHTED_FILES_TO_CHECK=$(git diff --name-only master | grep -v -E "^include/vulkan")
Karl Schultza53b35f2017-12-18 16:16:45 -070029
30if [ -z "${FILES_TO_CHECK}" ]; then
31 echo -e "${GREEN}No source code to check for formatting.${NC}"
Mark Lobodzinski4d9db432019-01-03 11:51:01 -070032else
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 Lobodzinski5afc2532019-01-09 15:45:58 -070050 COPYRIGHT_INFO=$(cat ${AFILE} | grep -E "Copyright (.)*LunarG")
Mark Lobodzinski72558f02019-01-07 15:08:50 -070051 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 Lobodzinski4d9db432019-01-03 11:51:01 -070056 fi
57 done
58 fi
Karl Schultza53b35f2017-12-18 16:16:45 -070059fi
60
Mark Lobodzinski4d9db432019-01-03 11:51:01 -070061if [ $FOUND_ERROR -gt 0 ]; then
62 exit 1
63else
Karl Schultza53b35f2017-12-18 16:16:45 -070064 echo -e "${GREEN}All source code in PR properly formatted.${NC}"
65 exit 0
Karl Schultza53b35f2017-12-18 16:16:45 -070066fi