#!/bin/sh # Pre-commit Git checks. # Set up: # ln -s .githooks/pre-commit .git/hooks/pre-commit # Constants. BOLD="\e[1m" UNSET="\e[0m" WHITE="\e[97m" RED="\e[91m" BACK_MAGENTA="\e[45m" BACK_BLUE="\e[44m" BACK_RED="\e[41m" BACK_GREEN="\e[42m" # Methods. function echo_error { ERR_MSG=$1 HELP_MSG=$2 echo -e "$BOLD $BACK_BLUE $WHITE Precommit:\t $BACK_RED Changes NOT committed. $UNSET" echo -e "$BOLD $BACK_BLUE $WHITE Precommit:\t $BACK_RED $WHITE $ERR_MSG $BACK_BLUE $HELP_MSG $UNSET" } function echo_status { STATUS_MSG=$1 echo -e "$BOLD $BACK_BLUE $WHITE Precommit:\t $STATUS_MSG $UNSET" } function echo_success { echo -e "$BOLD $BACK_BLUE $WHITE Precommit:\t $BACK_GREEN $WHITE SUCCESS. $UNSET All checks passed!" } function header_check_preparation { echo_status "Setting up license check environment" export GOPATH=$(go env GOPATH) if [ $? -ne 0 ]; then echo_status "Please install Go first, instructions can be found here: https://golang.org/doc/install." else export ENV_PATH=$(echo $PATH) if [[ $ENV_PATH != *$GOPATH* ]]; then echo_status "GOPATH is not in the system path, adding it now." export PATH=$GOPATH/bin:$PATH fi which addlicense if [ $? -ne 0 ]; then echo_status "addlicense tool is not yet installed, downloading it now." go install github.com/google/addlicense@latest fi fi } function buildifier_preparation { echo_status "Setting up tool to fix Bazel format" export GOPATH=$(go env GOPATH) if [ $? -ne 0 ]; then echo_status "Please install Go first, instructions can be found here: https://golang.org/doc/install." else export ENV_PATH=$(echo $PATH) if [[ $ENV_PATH != *$GOPATH* ]]; then echo_status "GOPATH is not in the system path, adding it now." export PATH=$GOPATH/bin:$PATH fi which addlicense if [ $? -ne 0 ]; then echo_status "buildifier tool is not yet installed, downloading it now." go install github.com/bazelbuild/buildtools/buildifier@latest fi fi } # Disk cache. BAZEL_CACHE_DIR=/tmp/bazel_cache_gapic_generator_java if [ ! -d $BAZEL_CACHE_DIR ] then mkdir $BAZEL_CACHE_DIR fi # Check only the staged files. NUM_TOTAL_FILES_CHANGED=$(git diff --cached --name-only | wc -l) NUM_JAVA_FILES_CHANGED=$(git diff --cached --name-only "*.java" | wc -l) NUM_UNIT_GOLDEN_FILES_CHANGED=$(git diff --cached --name-only "src/test/*/*.golden" | wc -l) NUM_INTEGRATION_GOLDEN_FILES_CHANGED=$(git diff --cached --name-only "test/integration/goldens/*/*.golden" | wc -l) NUM_INTEGRATION_BAZEL_FILES_CHANGED=$(git diff --cached --name-only "test/integration/*/BUILD.bazel" | wc -l) NUM_BAZEL_FILES_CHANGED=$(git diff --cached --name-only "*BUILD.bazel" | wc -l) if [ $NUM_TOTAL_FILES_CHANGED -le 0 ] then echo_error "No new files to commit." "" exit 1 fi if [ -x /usr/lib/git-core/google_hook ]; then /usr/lib/git-core/google_hook pre-commit "$@" fi # Check Java format. if [ $NUM_JAVA_FILES_CHANGED -gt 0 ] then echo_status "Running Java linter..." mvn fmt:check FORMAT_STATUS=$? if [ $FORMAT_STATUS != 0 ] then echo_error "Linting failed." "Please run mvn fmt:format and try again." exit 1 fi fi # Check unit tests. if [ $NUM_JAVA_FILES_CHANGED -gt 0 ] || [ $NUM_UNIT_GOLDEN_FILES_CHANGED -gt 0 ] then echo_status "Checking unit tests..." mvn install --batch-mode --no-transfer-progress -Dcheckstyle.skip -Dfmt.skip TEST_STATUS=$? if [ $TEST_STATUS != 0 ] then echo_error "Tests failed." "Please fix them and try again." exit 1 fi fi # Check integration tests. if [ $NUM_JAVA_FILES_CHANGED -gt 0 ] \ || [ $NUM_INTEGRATION_GOLDEN_FILES_CHANGED -gt 0 ] \ || [ $NUM_INTEGRATION_BAZEL_FILES_CHANGED -gt 0 ] then echo_status "Checking integration tests..." bazelisk --batch test --disk_cache="$BAZEL_CACHE_DIR" //test/integration/... TEST_STATUS=$? if [ $TEST_STATUS != 0 ] then echo_error "Tests failed." "Please fix them and try again." exit 1 fi fi # Check and fix Bazel format. if [ $NUM_BAZEL_FILES_CHANGED -gt 0 ] then buildifier_preparation for FILE in $(find ./ -name BUILD.bazel) do buildifier --lint=fix $FILE done fi # Check Apache License header for source files. if [ $NUM_JAVA_FILES_CHANGED -gt 0 ] then echo_status "Checking Apache License Header ..." header_check_preparation addlicense -c "Google LLC" -l apache -check $(find $PWD/src -type f -name '*.java' ! -iname '*PlaceholderFile.java') CHECK_STATUS=$? if [ $CHECK_STATUS != 0 ] then echo_error "License header check failed." "Please ensure that all source files have a license header." exit 1 fi fi echo_success