Ben Clayton | 750660e | 2019-12-18 15:09:46 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | ROOT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}")"/.. >/dev/null 2>&1 && pwd )" |
| 4 | SRC_DIR=${ROOT_DIR}/src |
| 5 | TESTS_DIR=${ROOT_DIR}/tests |
| 6 | |
| 7 | # Presubmit Checks Script. |
| 8 | CLANG_FORMAT=${CLANG_FORMAT:-clang-format} |
| 9 | GOFMT=${GOFMT:-gofmt} |
| 10 | |
| 11 | if 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 |
| 18 | fi |
| 19 | |
| 20 | function 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 |
| 42 | function run_bug_in_commit_msg() { |
Nicolas Capens | d79c734 | 2020-01-07 23:09:37 -0500 | [diff] [blame^] | 43 | git log -1 --pretty=%B | grep -E '^(Bug|Issue|Fixes):(\s?)((b\/)|(\w+:))([0-9]+)$|(^Regres:)' |
Ben Clayton | 750660e | 2019-12-18 15:09:46 +0000 | [diff] [blame] | 44 | |
| 45 | if [ $? -ne 0 ] |
| 46 | then |
Nicolas Capens | d79c734 | 2020-01-07 23:09:37 -0500 | [diff] [blame^] | 47 | 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 Clayton | 750660e | 2019-12-18 15:09:46 +0000 | [diff] [blame] | 50 | return 1 |
| 51 | fi |
| 52 | } |
| 53 | |
| 54 | function 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 | |
| 73 | function run_clang_format() { |
| 74 | ${SRC_DIR}/clang-format-all.sh |
| 75 | } |
| 76 | |
| 77 | function run_gofmt() { |
| 78 | find ${SRC_DIR} ${TESTS_DIR} -name "*.go" | xargs $GOFMT -w |
| 79 | } |
| 80 | |
| 81 | # Ensure we are clean to start out with. |
| 82 | check "git workspace must be clean" true |
| 83 | |
| 84 | # Check for 'Bug: ' line in commit |
| 85 | check bug-in-commi-msg run_bug_in_commit_msg |
| 86 | |
| 87 | # Check copyright headers |
| 88 | check copyright-headers run_copyright_headers |
| 89 | |
| 90 | # Check clang-format. |
| 91 | check clang-format run_clang_format |
| 92 | |
| 93 | # Check gofmt. |
| 94 | check gofmt run_gofmt |
| 95 | |
| 96 | echo |
Nicolas Capens | d79c734 | 2020-01-07 23:09:37 -0500 | [diff] [blame^] | 97 | echo "${green}All check completed successfully.$normal" |