|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Copyright 2021 Google LLC |
| 4 | +# |
| 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 | +# https://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 | +# Pre-commit Git checks. |
| 18 | +# Set up: |
| 19 | +# ln -s .githooks/pre-commit .git/hooks/pre-commit |
| 20 | + |
| 21 | +# Constants. |
| 22 | +BOLD="\e[1m" |
| 23 | +UNSET="\e[0m" |
| 24 | +WHITE="\e[97m" |
| 25 | +RED="\e[91m" |
| 26 | +BACK_MAGENTA="\e[45m" |
| 27 | +BACK_BLUE="\e[44m" |
| 28 | +BACK_RED="\e[41m" |
| 29 | +BACK_GREEN="\e[42m" |
| 30 | + |
| 31 | +# Methods. |
| 32 | +function echo_error { |
| 33 | + ERR_MSG=$1 |
| 34 | + HELP_MSG=$2 |
| 35 | + echo -e "$BOLD $BACK_BLUE $WHITE Precommit:\t $BACK_RED Changes NOT committed. $UNSET" |
| 36 | + echo -e "$BOLD $BACK_BLUE $WHITE Precommit:\t $BACK_RED $WHITE $ERR_MSG $BACK_BLUE $HELP_MSG $UNSET" |
| 37 | +} |
| 38 | + |
| 39 | +function echo_status { |
| 40 | + STATUS_MSG=$1 |
| 41 | + echo -e "$BOLD $BACK_BLUE $WHITE Precommit:\t $STATUS_MSG $UNSET" |
| 42 | +} |
| 43 | + |
| 44 | +function echo_success { |
| 45 | + echo -e "$BOLD $BACK_BLUE $WHITE Precommit:\t $BACK_GREEN $WHITE SUCCESS. $UNSET All checks passed!" |
| 46 | +} |
| 47 | + |
| 48 | +function header_check_preparation { |
| 49 | + echo_status "Setting up license check environment" |
| 50 | + export GOPATH=$(go env GOPATH) |
| 51 | + if [ $? -ne 0 ]; |
| 52 | + then |
| 53 | + echo_status "Please install Go first, instructions can be found here: https://golang.org/doc/install." |
| 54 | + else |
| 55 | + export ENV_PATH=$(echo $PATH) |
| 56 | + if [[ $ENV_PATH != *$GOPATH* ]]; |
| 57 | + then |
| 58 | + echo_status "GOPATH is not in the system path, adding it now." |
| 59 | + export PATH=$GOPATH/bin:$PATH |
| 60 | + fi |
| 61 | + which addlicense |
| 62 | + if [ $? -ne 0 ]; |
| 63 | + then |
| 64 | + echo_status "addlicense tool is not yet installed, downloading it now." |
| 65 | + go get -u github.com/google/addlicense |
| 66 | + fi |
| 67 | + fi |
| 68 | +} |
| 69 | + |
| 70 | +# Disk cache. |
| 71 | +BAZEL_CACHE_DIR=/tmp/bazel_cache_gapic_generator_java |
| 72 | +if [ ! -d $BAZEL_CACHE_DIR ] |
| 73 | +then |
| 74 | + mkdir $BAZEL_CACHE_DIR |
| 75 | +fi |
| 76 | + |
| 77 | +# Check only the staged files. |
| 78 | +NUM_TOTAL_FILES_CHANGED=$(git diff --cached --name-only | wc -l) |
| 79 | +NUM_PYTHON_FILES_CHANGED=$(git diff --cached --name-only "*.py" | wc -l) |
| 80 | +NUM_UNIT_GOLDEN_FILES_CHANGED=$(git diff --cached --name-only "src/test/*/*.golden" | wc -l) |
| 81 | +NUM_INTEGRATION_GOLDEN_FILES_CHANGED=$(git diff --cached --name-only "tests/integration/goldens/*/*.golden" | wc -l) |
| 82 | +NUM_INTEGRATION_BAZEL_FILES_CHANGED=$(git diff --cached --name-only "tests/integration/*/BUILD.bazel" | wc -l) |
| 83 | +NUM_BAZEL_FILES_CHANGED=$(git diff --cached --name-only "*BUILD.bazel" | wc -l) |
| 84 | + |
| 85 | +if [ $NUM_TOTAL_FILES_CHANGED -le 0 ] |
| 86 | +then |
| 87 | + echo_error "No new files to commit." "" |
| 88 | + exit 1 |
| 89 | +fi |
| 90 | + |
| 91 | + |
| 92 | +if [ -x /usr/lib/git-core/google_hook ]; then |
| 93 | + /usr/lib/git-core/google_hook pre-commit "$@" |
| 94 | +fi |
| 95 | + |
| 96 | +# Check unit tests. |
| 97 | +if [ $NUM_PYTHON_FILES_CHANGED -gt 0 ] || [ $NUM_UNIT_GOLDEN_FILES_CHANGED -gt 0 ] |
| 98 | +then |
| 99 | + echo_status "Checking unit tests..." |
| 100 | + nox -s unit-3.9 |
| 101 | + TEST_STATUS=$? |
| 102 | + if [ $TEST_STATUS != 0 ] |
| 103 | + then |
| 104 | + echo_error "Tests failed." "Please fix them and try again." |
| 105 | + exit 1 |
| 106 | + fi |
| 107 | +fi |
| 108 | + |
| 109 | +# Check integration tests. |
| 110 | +if [ $NUM_PYTHON_FILES_CHANGED -gt 0 ] \ |
| 111 | + || [ $NUM_INTEGRATION_GOLDEN_FILES_CHANGED -gt 0 ] \ |
| 112 | + || [ $NUM_INTEGRATION_BAZEL_FILES_CHANGED -gt 0 ] |
| 113 | +then |
| 114 | + echo_status "Checking integration tests..." |
| 115 | + bazel --batch test --disk_cache="$BAZEL_CACHE_DIR" //tests/integration/... |
| 116 | + TEST_STATUS=$? |
| 117 | + if [ $TEST_STATUS != 0 ] |
| 118 | + then |
| 119 | + echo_error "Tests failed." "Please fix them and try again." |
| 120 | + exit 1 |
| 121 | + fi |
| 122 | +fi |
| 123 | + |
| 124 | +# Check and fix Bazel format. |
| 125 | +if [ $NUM_BAZEL_FILES_CHANGED -gt 0 ] |
| 126 | +then |
| 127 | + for FILE in $(find ./ -name BUILD.bazel) |
| 128 | + do |
| 129 | + buildifier --lint=fix $FILE |
| 130 | + done |
| 131 | +fi |
| 132 | + |
| 133 | +echo_success |
0 commit comments