Skip to content

Commit

Permalink
*: added pre-commit hook. (#3112)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhexuany authored and coocood committed Apr 24, 2017
1 parent f9a0abc commit 2fcd97f
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/bin/bash
# This file modified from k8s
# https://github.com/kubernetes/kubernetes/blob/master/hooks/pre-commit

# 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)

exit_code=0

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 ${files_need_goword[@]}
echo "make goword for more details"
exit_code=1
else
echo "${green}OK"
fi
echo "${reset}"

if [[ "${exit_code}" != 0 ]]; then
echo "${red}Aborting commit${reset}"
fi
exit ${exit_code}

0 comments on commit 2fcd97f

Please sign in to comment.