Skip to content

Instantly share code, notes, and snippets.

@alanhg alanhg/pre-commit.sh

Last active Mar 15, 2020
Embed
What would you like to do?
checkstyle when pre commit
#!/bin/bash
wd=`pwd`
function print(){
echo "===========$*============"
}
print "Start check style for Java"
check_jar_path="$wd/config/checkstyle-8.30-all.jar"
check_xml_path="$wd/config/google_checks.xml"
rm -f temp
is_err=0
for file in `git status --porcelain | sed s/^...// | grep '\.java$'`;do
path="$wd/$file"
print "Checking file:$path"
re=`java -jar $check_jar_path -c $check_xml_path $path >> temp`
err=`cat temp | grep "ERROR"`
if [[ $err = *"ERROR"* ]];then
print "$err"
is_err=1
fi
done
print "Checking completed,God bless you!"
rm -f temp
if [ $is_err -ne 0 ]
then
print "Check failed,you should fix it!"
exit 1
fi
exit 0
@alanhg

This comment has been minimized.

Copy link
Owner Author

alanhg commented Mar 15, 2020

  1. init.sh
#!/bin/bash -e
 cp pre-commit ../../.git/hooks/
 chmod +x ../../.git/hooks/pre-commit
  1. 注意 print "$err" 要加双引号,确保输出的换行符显示正常
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.