You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.5 KiB

4 years ago
#! /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