-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·169 lines (151 loc) · 4.63 KB
/
pre-commit
File metadata and controls
executable file
·169 lines (151 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/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