blob: 87ac54c352c9b85a82f8cdf94faffdfc675f6044 [file] [log] [blame]
Ben Clayton750660e2019-12-18 15:09:46 +00001#!/bin/bash
2
3ROOT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}")"/.. >/dev/null 2>&1 && pwd )"
4SRC_DIR=${ROOT_DIR}/src
5TESTS_DIR=${ROOT_DIR}/tests
6
7# Presubmit Checks Script.
8CLANG_FORMAT=${CLANG_FORMAT:-clang-format}
9GOFMT=${GOFMT:-gofmt}
10
11if test -t 1; then
12 ncolors=$(tput colors)
13 if test -n "$ncolors" && test $ncolors -ge 8; then
14 normal="$(tput sgr0)"
15 red="$(tput setaf 1)"
16 green="$(tput setaf 2)"
17 fi
18fi
19
20function check() {
21 local name=$1; shift
22 echo -n "Running check $name... "
23
24 if ! "$@"; then
25 echo "${red}FAILED${normal}"
26 echo " Error executing: $@";
27 exit 1
28 fi
29
30 if ! git diff --quiet HEAD; then
31 echo "${red}FAILED${normal}"
32 echo " Git workspace not clean:"
33 git --no-pager diff -p HEAD
34 echo "${red}Check $name failed.${normal}"
35 exit 1
36 fi
37
38 echo "${green}OK${normal}"
39}
40
41# Validate commit message
42function run_bug_in_commit_msg() {
Nicolas Capensd79c7342020-01-07 23:09:37 -050043 git log -1 --pretty=%B | grep -E '^(Bug|Issue|Fixes):(\s?)((b\/)|(\w+:))([0-9]+)$|(^Regres:)'
Ben Clayton750660e2019-12-18 15:09:46 +000044
45 if [ $? -ne 0 ]
46 then
Nicolas Capensd79c7342020-01-07 23:09:37 -050047 echo "Git commit message must have a Bug: line"
48 echo "followed by a bug ID in the form b/# for Buganizer bugs or"
49 echo "project:# for Monorail bugs (e.g. 'Bug: chromium:123')."
Ben Clayton750660e2019-12-18 15:09:46 +000050 return 1
51 fi
52}
53
54function run_copyright_headers() {
55 tmpfile=`mktemp`
56 for suffix in "cpp" "hpp" "go" "h"; do
57 # Grep flag '-L' print files that DO NOT match the copyright regex
58 # Grep seems to match "(standard input)", filter this out in the for loop output
59 find ${SRC_DIR} -type f -name "*.${suffix}" | xargs grep -L "Copyright .* The SwiftShader Authors\|Microsoft Visual C++ generated\|GNU Bison"
60 done | grep -v "(standard input)" > ${tmpfile}
61 if test -s ${tmpfile}; then
62 # tempfile is NOT empty
63 echo "Copyright issue in these files:"
64 cat ${tmpfile}
65 rm ${tmpfile}
66 return 1
67 else
68 rm ${tmpfile}
69 return 0
70 fi
71}
72
73function run_clang_format() {
74 ${SRC_DIR}/clang-format-all.sh
75}
76
77function run_gofmt() {
78 find ${SRC_DIR} ${TESTS_DIR} -name "*.go" | xargs $GOFMT -w
79}
80
81# Ensure we are clean to start out with.
82check "git workspace must be clean" true
83
84# Check for 'Bug: ' line in commit
85check bug-in-commi-msg run_bug_in_commit_msg
86
87# Check copyright headers
88check copyright-headers run_copyright_headers
89
90# Check clang-format.
91check clang-format run_clang_format
92
93# Check gofmt.
94check gofmt run_gofmt
95
96echo
Nicolas Capensd79c7342020-01-07 23:09:37 -050097echo "${green}All check completed successfully.$normal"