Skip to content

Commit 0277fc1

Browse files
authored
alter pre-commit to fix go install for addlicense and add step to install buildifier if not present. (#972)
- `go get -u` command used to install `addlicense` tool in L51 is deprecated since Go 1.17. Replace it with go install. - Add similar function to install `buildifier` if not already installed. This is used to fix Bazel format.
1 parent e5803e5 commit 0277fc1

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

.githooks/pre-commit

+24-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,29 @@ function header_check_preparation {
4848
if [ $? -ne 0 ];
4949
then
5050
echo_status "addlicense tool is not yet installed, downloading it now."
51-
go get -u github.com/google/addlicense
51+
go install github.com/google/addlicense@latest
52+
fi
53+
fi
54+
}
55+
56+
function 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
5274
fi
5375
fi
5476
}
@@ -123,6 +145,7 @@ fi
123145
# Check and fix Bazel format.
124146
if [ $NUM_BAZEL_FILES_CHANGED -gt 0 ]
125147
then
148+
buildifier_preparation
126149
for FILE in $(find ./ -name BUILD.bazel)
127150
do
128151
buildifier --lint=fix $FILE

0 commit comments

Comments
 (0)