简体   繁体   English

SVN预提交钩子检查样式

[英]Svn pre-commit hook for checkstyle

Here is my current checkstyle shell script. 这是我当前的checkstyle shell脚本。 It works fine if I commit on TRUNK but not on Branches. 如果我在TRUNK上提交但不在分支上提交,则可以正常工作。 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? 如果checkstyle捕获了一些不是问题的东西,或者开发人员写的方式实际上比checkstyle坚持的应该更清楚,更容易理解,该怎么办? When you use something like checkstyle or findbugs , you have to understand you'll get a few false positives. 当您使用诸如checkstylefindbugs类的东西时,您必须了解您会得到一些误报。

A better method is to use a continuous build engine like Jenkins . 更好的方法是使用像Jenkins这样的连续构建引擎。 Jenkins can be setup to automatically kick off a build with each and every commit. 可以将Jenkins设置为在每次提交时自动启动构建。 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. 然后,您实际上可以直接从Jenkins释放代码以进行测试和为客户服务。 After all, you know the same jar/ear/war files you've tested are the same ones your customers will get. 毕竟,您知道您测试过的jar / ear / war文件与客户将获得的文件相同。
  • Automatically run various tests including: 自动运行各种测试,包括:
    • Checkstyle Checkstyle的
    • Findbugs FindBugs的
    • Corbertura Corbertura
    • PMD PMD
    • DRY
    • JUnit 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将整个构建输出,所有保存的工件以及所有测试保存在一个易于使用的易于使用的网页中,任何用户都可以使用。
  • Jenkins can integrate into a wide variety of issue tracking tools, so you can see what Jenkins build a particular issue was involved in. Jenkins可以集成到各种问题跟踪工具中,因此您可以看到Jenkins构建特定问题所涉及的内容。

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. CruiseControl也是如此,您可以在那里使用TeamCity,Bamboo和许多其他连续构建系统。 I like Jenkins because development is very active, and it's dead simple to setup. 我喜欢Jenkins,因为开发非常活跃,而且设置非常简单。 It took me about 30 minutes to download it and run my first job the very first time I heard of it. 我花了30分钟左右的时间下载并开始我的第一份工作。

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. 我知道您问过您的预提交钩子,但我不想听起来像个推销员(詹金斯是自由开放的源代码,并且我与项目没有任何联系),但是像检查checkstyle一样复杂。提交前的钩子正在请求麻烦。 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"). 尝试比较目录结构,然后创建临时目录(删除“ rm -Rf $ TMPDIR”)。

Perhaps you have diferences between trunk and branches like: 也许您在主干和分支之间有区别,例如:

Trunk: /tmp/12/code/file.java 中继线:/tmp/12/code/file.java

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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