#!/bin/sh has_errors=0 # 获取git暂存的所有go代码 # --cached 暂存的 # --name-only 只显示名字 # --diff-filter=ACM 过滤暂存文件,A=Added C=Copied M=Modified, 即筛选出添加/复制/修改的文件 allgofiles=$(git diff --cached --name-only --diff-filter=ACM | grep '.go$') echo "\033[32m------------------------代码开始检测-------------------------------\033[0m\n" gofiles=() godirs=() for allfile in ${allgofiles[@]}; do # 过滤vendor的 # 过滤prootobuf自动生产的文件 # 过滤测试文件 if [[ $allfile == "vendor"* || $allfile == *".pb.go" || $allfile == *"_test.go" ]];then continue else gofiles+=("$allfile") # 文件夹去重 existdir=0 dir=`echo "$allfile" |xargs -n1 dirname|sort -u` for somedir in ${godirs[@]}; do if [[ $dir == $somedir ]]; then existdir=1 break fi done if [[ $existdir -eq 0 ]]; then godirs+=("$dir") fi fi done [ -z "$gofiles" ] && exit 0 # gofmt 格式化代码 unformatted=$(/data/go/bin/gofmt -l ${gofiles[@]}) if [ -n "$unformatted" ]; then echo >&2 "\033[31m gofmt 工具发现代码格式不标准,请运行以下命令,然后再git add 提交代码: \033[0m" echo "-------------------------------------------------------------------" for f in ${unformatted[@]}; do echo >&2 "gofmt -d -w $PWD/$f" # gofmt -d -w $PWD/$f done echo "-------------------------------------------------------------------\n\n" has_errors=1 fi # goimports 自动格式化,包工具 if /data/go/bin/goimports >/dev/null 2>&1; then # 检测是否安装 unimports=$(/data/go/bin/goimports -l ${gofiles[@]}) if [ -n "$unimports" ]; then echo >&2 "\033[31m goimports 工具发现import格式不标准,请运行以下命令,然后再git add 提交代码: \033[0m" echo "-------------------------------------------------------------------" for f in ${unimports[@]} ; do echo >&2 "goimports -d -w $PWD/$f" done echo "-------------------------------------------------------------------\n\n" has_errors=1 fi else echo 'Error: goimports not install. Run: "go get -u golang.org/x/tools/cmd/goimports"' >&2 exit 1 fi # golint 代码规范检测(目前不使用) # echo >&2 "\033[32m golint 工具检测代码是否标准,如果发现有不标准的,请检测后再提交代码(不要求强制优化):\033[0m\n" # echo "-------------------------------------------------------------------" # if /data/go/bin/golint >/dev/null 2>&1; then # 检测是否安装 # lint_errors=false # for file in ${gofiles[@]} ; do # lint_result="$(/data/go/bin/golint $file)" # run golint # if test -n "$lint_result" ; then # echo "golint '$file':\n$lint_result" # lint_errors=true # fi # done # # if [ $lint_errors = true ] ; then # # echo "" # # fi # else # echo 'Error: golint 工具未安装,请执行命令: "go get -u github.com/golang/lint/golint"' >&2 # exit 1 # fi # echo "-------------------------------------------------------------------\n" # go vet 静态错误检查,检测代码是否能编译 # show_vet_header=true # for dir in ${godirs[@]} ; do # vet=$(go vet $PWD/$dir 2>&1) # if [ -n "$vet" -a $show_vet_header = true ] ; then # echo "\033[31m govet 检测代码存在异常,请仔细检查以下代码,修改完成后,然后再git add 提交代码: \033[0m" # show_vet_header=false # fi # echo "-------------------------------------------------------------------\n" # if [ -n "$vet" ] ; then # echo "$vet\n" # has_errors=1 # fi # echo "-------------------------------------------------------------------\n\n" # done if [ $has_errors -eq 1 ]; then echo "\033[31m------------代码检测失败,发现异常,请及时修复后再提交-------------\033[0m\n" else echo "\033[32m----------代码检测成功,未发现异常,可以 commit 代码---------------\033[0m\n" fi; exit $has_errors