diff --git a/checkstyle/GeekCheckStyle.xml b/checkstyle/GeekCheckStyle.xml new file mode 100644 index 000000000..f7b6658e7 --- /dev/null +++ b/checkstyle/GeekCheckStyle.xml @@ -0,0 +1,137 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/checkstyle/checkstyle-8.38-all.jar b/checkstyle/checkstyle-8.38-all.jar new file mode 100644 index 000000000..52076fe4f Binary files /dev/null and b/checkstyle/checkstyle-8.38-all.jar differ diff --git a/checkstyle/pre-commit b/checkstyle/pre-commit new file mode 100644 index 000000000..274e80d25 --- /dev/null +++ b/checkstyle/pre-commit @@ -0,0 +1,62 @@ +#! /bin/bash +# @author:haloo# +#@func:pre-commit# +## cp ./checkstyle/pre-commit ./.git/hooks/ + +echo 避免NPE是程序员的基本修养 +echo 开始style checking + +wd=`pwd` +echo "当前工作目录:$wd" + +# check-style版本号 +check_style_version="checkstyle-8.38-all.jar" +check_style_xml_name="GeekCheckStyle.xml" +check_jar_path="$wd/checkstyle/$check_style_version" +check_xml_path="$wd/checkstyle/$check_style_xml_name" + +## 清空temp文件 + +rm -f temp + +is_err=0 +errorCount=0 +warnCount=0 + +## 查找add到git 缓冲区中,以.java后缀的文件 +for file in `git status --porcelain | sed s/^...// | grep '\.java$'`; do + path="$wd/$file" + echo "检查文件: $path" + re=`java -jar $check_jar_path -c $check_xml_path $path >> temp` + + warn=`cat temp | grep "WARN"` + if [[ $warn = *"WARN"* ]];then + echo "${warn}" + needle="WARN" + number_of_occurrences=$(grep -o "$needle" <<< "$warn" | wc -l) + ((warnCount = warnCount + number_of_occurrences)) + is_err=1 + fi + + err=`cat temp | grep "ERROR"` + if [[ $err = *"ERROR"* ]];then + echo "${err}" + needle="ERROR" + number_of_occurrences=$(grep -o "$needle" <<< "$err" | wc -l) + ((errorCount = errorCount + number_of_occurrences)) + is_err=1 + fi +done + +echo "检查完成,祝你好运" + +rm -f temp + +if [ $is_err -ne 0 ];then + echo "出现了 $errorCount 个error" + echo "出现了 $warnCount 个warn" + echo "请先符合style才能提交" + exit 1 +fi +echo "No Bug ,It is Good!!" +exit 0 diff --git a/init.sh b/init.sh new file mode 100644 index 000000000..42f10fb1c --- /dev/null +++ b/init.sh @@ -0,0 +1,2 @@ +cp checkstyle/pre-commit .git/hooks/ +chmod +x .git/hooks/pre-commit