-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
Copy pathpre-commit
executable file
·78 lines (68 loc) · 2.14 KB
/
pre-commit
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
#!/usr/bin/env bash
# This file modified from k8s
# https://github.com/kubernetes/kubernetes/blob/master/hooks/pre-commit
# Now It's removed, The Reason is https://github.com/kubernetes/community/issues/729
# The PR is https://github.com/kubernetes/kubernetes/pull/47673
# How to use this hook?
# ln -s hooks/pre-commit .git/hooks/
# In case hook is not executable
# chmod +x .git/hooks/pre-commit
readonly reset=$(tput sgr0)
readonly red=$(tput bold; tput setaf 1)
readonly green=$(tput bold; tput setaf 2)
readonly goword=./tools/bin/goword
exit_code=0
# comment it by default. You can uncomment it.
# echo -ne "Checking that it builds..."
# if ! OUT=$(make 2>&1); then
# echo
# echo "${red}${OUT}"
# exit_code=1
# else
# echo "${green}OK"
# fi
# echo "${reset}"
echo -ne "Checking for files that need gofmt... "
files_need_gofmt=()
files=($(git diff --cached --name-only --diff-filter ACM | grep "\.go" | grep -v -e "^_vendor"))
for file in "${files[@]}"; do
# Check for files that fail gofmt.
diff="$(git show ":${file}" | gofmt -s -d 2>&1)"
if [[ -n "$diff" ]]; then
files_need_gofmt+=("${file}")
fi
done
if [[ "${#files_need_gofmt[@]}" -ne 0 ]]; then
echo "${red}ERROR!"
echo "Some files have not been gofmt'd. To fix these errors, "
echo "copy and paste the following:"
echo " gofmt -s -w ${files_need_gofmt[@]}"
exit_code=1
else
echo "${green}OK"
fi
echo "${reset}"
echo -ne "Checking for files that need goword... "
files_need_goword=()
files=($(git diff --cached --name-only --diff-filter ACM | grep "\.go" | grep -v -e "^_vendor"))
for file in "${files[@]}"; do
# Check for files that fail goword.
diff=$(${goword} ${file})
if [[ -n "$diff" ]]; then
files_need_goword+=("${file}")
fi
done
if [[ "${#files_need_goword[@]}" -ne 0 ]]; then
echo "${red}ERROR!"
echo "Some files may have spelling errors."
echo "copy and paste the following for where fails this test:"
echo " $ ${goword} ${files_need_goword[@]}"
exit_code=1
else
echo "${green}OK"
fi
echo "${reset}"
if [[ "${exit_code}" != 0 ]]; then
echo "${red}Aborting commit${reset}"
fi
exit ${exit_code}