Skip to content

Instantly share code, notes, and snippets.

@alanhg
Last active March 15, 2020 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanhg/baa359d064b17988bb8cd89a0bbe4c2e to your computer and use it in GitHub Desktop.
Save alanhg/baa359d064b17988bb8cd89a0bbe4c2e to your computer and use it in GitHub Desktop.
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
Copy link
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