检查Java类是否在SVN Precommit挂钩中被注释 [英] Check if Java Class is commented in SVN Precommit hook
问题描述
我想确保在有人将某些内容提交到SVN存储库之前,对Java类进行了注释。
所以我想实现以下工作流程:
I want to ensure that a Java class is commented, before someone commits something into the SVN repository. So I want to realize the following workflow:
- 用户在类中进行了某些更改
- 用户想要提交类
- 在执行对存储库的提交之前,SVN或其他方法检查类之前和
public $之前是否有注释。 c $ c>方法(对于Java AutoDoc)。
- 如果有注释=>提交,否则返回错误消息
- User changes something in a class
- User wants to commit class
- Before executing the commit to the repository, SVN or something else checks if there are comments before the class and before
public
methods (for Java AutoDoc). - If there are comments => Commit, Otherwise return Error message
我该如何实现?我发现了很多有关预提交挂钩的信息。但是所有事情都是关于检查是否设置了提交消息/注释。
How can I realize this? I found a lot about pre-commit hooks. But everything was about checking if the commit message/comment is set.
如果有人可以为这个问题提供解决方案,那将非常好并且很有帮助。
It would be very nice and helpful if someone could provide a solution for this problem.
推荐答案
是的,您可以使用 CheckStyle 。
首先,下载CheckStyle jar,然后将其安装在某个地方。
然后,为CheckStyle创建一个新的 xml配置文件,以检查您的公共方法/类具有一些JavaDoc(例如,您可以将其称为 javadoc_check.xml ):
First, download the CheckStyle jar, and install it somewhere. Then, create a new xml config file for CheckStyle to check if your public methods/classes have some JavaDoc (you can call it javadoc_check.xml for instance):
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
<module name="TreeWalker">
<module name="JavadocMethod">
<property name="scope" value="public"/>
</module>
<module name="JavadocType">
<property name="scope" value="public"/>
</module>
</module>
</module>
完成后,您可以像这样创建pre_commit钩子:
Once it's done, you can create you pre_commit hook, like that:
#!/bin/bash
REPOS="$1"
TXN="$2"
CHECKSTYLEJAR=/path/to/checkstyle-5.7/checkstyle-5.7-all.jar
CHECKSTYLECONFIG=/path/to/javadoc_check.xml
SVNLOOK=/usr/bin/svnlook
JAVA=/usr/bin/java
EXITSTATUS=0
ERRORMARKER=__ERROR_MARKER__
# Create temporary dir
TMPDIR=$(mktemp -d)
# Iterate on the files commited
while read changeline;
do
# Get the filename
file=${changeline:4}
# Check if it's an Updated or Added java file
if [[ $file == *.java && ($changeline == U* || $changeline == A*) ]] ; then
# Get the file content in a temporary file
$SVNLOOK cat -t "$TXN" "$REPOS" "$file" > $TMPDIR/${file##*/}
echo -e "\n=> Checking $file"
# Check the file with checkstyle
( $JAVA -jar $CHECKSTYLEJAR -c $CHECKSTYLECONFIG $TMPDIR/${file##*/} 2>&1 || echo "$ERRORMARKER" 1>&2 ) | sed -e "s{$TMPDIR/{{"
# Delete the temporary file
rm $TMPDIR/${file##*/}
fi
done < <($SVNLOOK changed -t "$TXN" "$REPOS") 3>&2 2>&1 1>&3 | grep "$ERRORMARKER" && EXITSTATUS=1 # Check for errors
# Delete temporary dir
rmdir $TMPDIR
exit $EXITSTATUS
别忘了更改CheckStyle jar和CheckStyle配置的路径。
Don't forget to change the path to the CheckStyle jar and CheckStyle config.
下次您将尝试在公共方法/类上提交不带JavaDoc注释的Java文件,提交将被取消,并且您将收到错误消息:
The next time you will try to commit a java file without JavaDoc comment on a public method/class, the commit will be cancelled, and you will have an error message:
$ svn commit
Sending test.java
Transmitting file data .svn: E165001: Commit failed (details follow):
svn: E165001: Commit blocked by pre-commit hook (exit code 1) with output:
=> Checking test.java
Starting audit...
test.java:9:1: Missing a Javadoc comment.
Audit done.
svn: E165001: Your commit message was left in a temporary file:
svn: E165001: '/path/to/project/svn-commit.1.tmp'
$
如果您提交多个文件,则将检查所有文件,并且将提交仅在所有修改/添加的Java文件都具有JavaDoc注释的情况下,才授权。
If you commit more than one file, all the files will be checked, and the commit will be authorized only if all the modified/added java files have JavaDoc comments.
这篇关于检查Java类是否在SVN Precommit挂钩中被注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!