简体   繁体   中英

Svn pre-commit hook for checkstyle

Here is my current checkstyle shell script. It works fine if I commit on TRUNK but not on Branches. I don't really understand why it doesn't work. Can someone please help me?

#!/bin/sh

###################################################
#
# Verify Checkstyle
#
###################################################

REPOS="$1"
TXN="$2"

SVNLOOK=/usr/bin/svnlook
JAVA=/opt/ibm/java2-i386-50/bin/java
CHECKSTYLE=/usr/local/checkstyle/checkstyle-all-5.1.jar
TMPDIR=/tmp/$TXN
REPORT=/tmp/$TXN/report
CHECKSTYLE_CONFIG=/usr/local/checkstyle/checkstyle.xml

CHANGED=`$SVNLOOK changed -t "$TXN" "$REPOS" | grep -v "^D" | awk '{print $2}'`
mkdir -p $TMPDIR
for LINE in $CHANGED ; do
    FILE=`echo $LINE | egrep -v Test\\.java$ | egrep -v \\/src\\/test\\/ | egrep -v \\/js\\/ext`
    if [ -n "$FILE" ] ; then
        DIRNAME=`dirname $FILE`
        mkdir -p $TMPDIR/$DIRNAME
        $SVNLOOK cat $REPOS --transaction $TXN $FILE > $TMPDIR/$FILE
    fi
done
$JAVA -jar $CHECKSTYLE -c $CHECKSTYLE_CONFIG -r $TMPDIR > $TMPDIR/tmpfile.checkstyle
X=$?
if [ $X -ne 0 ] ; then
    cat $TMPDIR/tmpfile.checkstyle > /dev/stderr
    rm -Rf $TMPDIR
    exit 1
fi
rm -Rf $TMPDIR

exit 0

Thanks!

Word of advice: Don't make this a pre-commit script.

  • Any pre-commit script will hold up the commit until it completes. If I check in a dozen files, how long does it take for this script to run? When I first got into computing, a second second response time was considered acceptable. Now, if you don't get a response in a few seconds, people will complain.
  • What happens in those cases where the checkstyle catches something where it is not an issue, or the way the developer wrote it is actually clearer and easier to understand than what checkstyle insists it should be? When you use something like checkstyle or findbugs , you have to understand you'll get a few false positives.

A better method is to use a continuous build engine like Jenkins . Jenkins can be setup to automatically kick off a build with each and every commit. Jenkins can:

  • Automatically store the results of the build. Then, you can actually release the code directly from Jenkins for testing and for your clients. After all, you know the same jar/ear/war files you've tested are the same ones your customers will get.
  • Automatically run various tests including:
    • Checkstyle
    • Findbugs
    • Corbertura
    • PMD
    • DRY
    • JUnit
    • Check for built warnings
    • And dozens of others
  • Jenkins saves the entire build output, all saved artifacts, and all tests in a nice and easy to see webpage that is available to any user.
  • Jenkins can integrate into a wide variety of issue tracking tools, so you can see what Jenkins build a particular issue was involved in.

You don't have to use Jenkins. Hudson is still there. So is CruiseControl, and you can use TeamCity, Bamboo, and dozens of other continuous build systems out there. I like Jenkins because development is very active, and it's dead simple to setup. It took me about 30 minutes to download it and run my first job the very first time I heard of it.

I know you asked about your pre-commit hook, and I don't want to sound like a salesman (Jenkins is free and open source, and I have no connection to the project), but making something as complex as a checkstyle check a pre-commit hook is asking for trouble. Using a continuous build server is simply a better way of handling this issue.

A hint.

Try to compare the directory structure, whitch you create temporary (remove "rm -Rf $TMPDIR").

Perhaps you have diferences between trunk and branches like:

Trunk: /tmp/12/code/file.java

Branches: /tmp/br1/12/code/file.java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM