1#!/bin/sh 2 3# Pre-commit Git checks. 4# Set up: 5# ln -s .githooks/pre-commit .git/hooks/pre-commit 6 7# Constants. 8BOLD="\e[1m" 9UNSET="\e[0m" 10WHITE="\e[97m" 11RED="\e[91m" 12BACK_MAGENTA="\e[45m" 13BACK_BLUE="\e[44m" 14BACK_RED="\e[41m" 15BACK_GREEN="\e[42m" 16 17# Methods. 18function echo_error { 19 ERR_MSG=$1 20 HELP_MSG=$2 21 echo -e "$BOLD $BACK_BLUE $WHITE Precommit:\t $BACK_RED Changes NOT committed. $UNSET" 22 echo -e "$BOLD $BACK_BLUE $WHITE Precommit:\t $BACK_RED $WHITE $ERR_MSG $BACK_BLUE $HELP_MSG $UNSET" 23} 24 25function echo_status { 26 STATUS_MSG=$1 27 echo -e "$BOLD $BACK_BLUE $WHITE Precommit:\t $STATUS_MSG $UNSET" 28} 29 30function echo_success { 31 echo -e "$BOLD $BACK_BLUE $WHITE Precommit:\t $BACK_GREEN $WHITE SUCCESS. $UNSET All checks passed!" 32} 33 34function header_check_preparation { 35 echo_status "Setting up license check environment" 36 export GOPATH=$(go env GOPATH) 37 if [ $? -ne 0 ]; 38 then 39 echo_status "Please install Go first, instructions can be found here: https://golang.org/doc/install." 40 else 41 export ENV_PATH=$(echo $PATH) 42 if [[ $ENV_PATH != *$GOPATH* ]]; 43 then 44 echo_status "GOPATH is not in the system path, adding it now." 45 export PATH=$GOPATH/bin:$PATH 46 fi 47 which addlicense 48 if [ $? -ne 0 ]; 49 then 50 echo_status "addlicense tool is not yet installed, downloading it now." 51 go install github.com/google/addlicense@latest 52 fi 53 fi 54} 55 56function buildifier_preparation { 57 echo_status "Setting up tool to fix Bazel format" 58 export GOPATH=$(go env GOPATH) 59 if [ $? -ne 0 ]; 60 then 61 echo_status "Please install Go first, instructions can be found here: https://golang.org/doc/install." 62 else 63 export ENV_PATH=$(echo $PATH) 64 if [[ $ENV_PATH != *$GOPATH* ]]; 65 then 66 echo_status "GOPATH is not in the system path, adding it now." 67 export PATH=$GOPATH/bin:$PATH 68 fi 69 which addlicense 70 if [ $? -ne 0 ]; 71 then 72 echo_status "buildifier tool is not yet installed, downloading it now." 73 go install github.com/bazelbuild/buildtools/buildifier@latest 74 fi 75 fi 76} 77 78# Disk cache. 79BAZEL_CACHE_DIR=/tmp/bazel_cache_gapic_generator_java 80if [ ! -d $BAZEL_CACHE_DIR ] 81then 82 mkdir $BAZEL_CACHE_DIR 83fi 84 85# Check only the staged files. 86NUM_TOTAL_FILES_CHANGED=$(git diff --cached --name-only | wc -l) 87NUM_JAVA_FILES_CHANGED=$(git diff --cached --name-only "*.java" | wc -l) 88NUM_UNIT_GOLDEN_FILES_CHANGED=$(git diff --cached --name-only "src/test/*/*.golden" | wc -l) 89NUM_INTEGRATION_GOLDEN_FILES_CHANGED=$(git diff --cached --name-only "test/integration/goldens/*/*.golden" | wc -l) 90NUM_INTEGRATION_BAZEL_FILES_CHANGED=$(git diff --cached --name-only "test/integration/*/BUILD.bazel" | wc -l) 91NUM_BAZEL_FILES_CHANGED=$(git diff --cached --name-only "*BUILD.bazel" | wc -l) 92 93if [ $NUM_TOTAL_FILES_CHANGED -le 0 ] 94then 95 echo_error "No new files to commit." "" 96 exit 1 97fi 98 99 100if [ -x /usr/lib/git-core/google_hook ]; then 101 /usr/lib/git-core/google_hook pre-commit "$@" 102fi 103 104# Check Java format. 105if [ $NUM_JAVA_FILES_CHANGED -gt 0 ] 106then 107 echo_status "Running Java linter..." 108 mvn fmt:check 109 FORMAT_STATUS=$? 110 if [ $FORMAT_STATUS != 0 ] 111 then 112 echo_error "Linting failed." "Please run mvn fmt:format and try again." 113 exit 1 114 fi 115fi 116 117# Check unit tests. 118if [ $NUM_JAVA_FILES_CHANGED -gt 0 ] || [ $NUM_UNIT_GOLDEN_FILES_CHANGED -gt 0 ] 119then 120 echo_status "Checking unit tests..." 121 mvn install --batch-mode --no-transfer-progress -Dcheckstyle.skip -Dfmt.skip 122 TEST_STATUS=$? 123 if [ $TEST_STATUS != 0 ] 124 then 125 echo_error "Tests failed." "Please fix them and try again." 126 exit 1 127 fi 128fi 129 130# Check integration tests. 131if [ $NUM_JAVA_FILES_CHANGED -gt 0 ] \ 132 || [ $NUM_INTEGRATION_GOLDEN_FILES_CHANGED -gt 0 ] \ 133 || [ $NUM_INTEGRATION_BAZEL_FILES_CHANGED -gt 0 ] 134then 135 echo_status "Checking integration tests..." 136 bazelisk --batch test --disk_cache="$BAZEL_CACHE_DIR" //test/integration/... 137 TEST_STATUS=$? 138 if [ $TEST_STATUS != 0 ] 139 then 140 echo_error "Tests failed." "Please fix them and try again." 141 exit 1 142 fi 143fi 144 145# Check and fix Bazel format. 146if [ $NUM_BAZEL_FILES_CHANGED -gt 0 ] 147then 148 buildifier_preparation 149 for FILE in $(find ./ -name BUILD.bazel) 150 do 151 buildifier --lint=fix $FILE 152 done 153fi 154 155# Check Apache License header for source files. 156if [ $NUM_JAVA_FILES_CHANGED -gt 0 ] 157then 158 echo_status "Checking Apache License Header ..." 159 header_check_preparation 160 addlicense -c "Google LLC" -l apache -check $(find $PWD/src -type f -name '*.java' ! -iname '*PlaceholderFile.java') 161 CHECK_STATUS=$? 162 if [ $CHECK_STATUS != 0 ] 163 then 164 echo_error "License header check failed." "Please ensure that all source files have a license header." 165 exit 1 166 fi 167fi 168 169echo_success 170