-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpre-commit.sh
executable file
·48 lines (37 loc) · 1.09 KB
/
pre-commit.sh
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
#!/bin/bash
# GOAL : error (go vet) and lint (gofmt) checks using golang vanilla install
# Inspired from :
# - https://golang.org/misc/git/pre-commit?m=text
# - https://ngurajeka.com/post/using-pre-commit-on-golang-project/
STAGED_GO_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\.go$')
if [[ "$STAGED_GO_FILES" = "" ]]; then
exit 0
fi
PASS=true
# Run gofmt on all staged files and check the exit status
unformatted=$(gofmt -l $STAGED_GO_FILES)
if [ -n "$unformatted" ]; then
# Some files are not gofmt'd. Print message and fail.
echo >&2 "Go files must be formatted with gofmt. Please run:"
for fn in $unformatted; do
echo >&2 " gofmt -w $PWD/$fn"
done
fi
for FILE in $STAGED_GO_FILES
do
# Run govet on the staged file and check the exit status
go vet $FILE
if [[ $? != 0 ]]; then
printf "\t\033[31mgo vet $FILE\033[0m \033[0;30m\033[41mFAILURE!\033[0m\n"
PASS=false
else
printf "\t\033[32mgo vet $FILE\033[0m \033[0;30m\033[42mpass\033[0m\n"
fi
done
if ! $PASS; then
printf "COMMIT FAILED\n"
exit 1
else
printf "COMMIT SUCCEEDED\n"
fi
exit 0