简体   繁体   English

SVN预提交钩子逻辑

[英]SVN pre-commit hook logic

I'm adding a logic in my svn pre-commit hook to check if there is QA(in upper case starting with space) in commit message then commit should fail. 我在我的svn预提交钩子中添加一个逻辑来检查提交消息中是否存在QA(大写以空格开头)然后提交应该失败。 But its not working. 但它不起作用。 Kindly assist me how to write it properly. 请帮助我如何正确地写它。

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

# Make sure that the log message contains some text.
SVNLOOK=/usr/bin/svnlook

LOGMSG=$($SVNLOOK log -t "$TXN" "$REPOS")

# check if any comment has supplied by the commiter
if [ -z "$LOGMSG" ]; then
echo "Your commit was blocked because it have no comments." 1>&2
exit 1
fi

#check minimum size of text
if [ ${#LOGMSG} -lt 15 ]; then
echo "Your Commit was blocked because the comments does not meet minimum length requirements (15 letters)." 1>&2
exit 1
fi

# get TaskID by regex
TaskID=$(expr "$LOGMSG" : '\([#][0-9]\{1,9\}[:][" "]\)[A-Za-z0-9]*')

# Check if task id was found. 
if [ -z "$TaskID" ]; then

echo ""  1>&2
echo "No Task id found in log message \"$LOGMSG\"" 1>&2
echo ""  1>&2
echo "The TaskID must be the first item on the first line of the log message."  1>&2
echo ""  1>&2
echo "Proper TaskID format--> #123- 'Your commit message'  " 1>&2
exit 1
fi

#Check that QA should not be present in log message.

QA=$(expr "$LOGMSG" : '\(*[" "][QA][" "]\)')
if [ "$QA" == "QA" ]; then
echo ""  1>&2
echo "Your log message \"$LOGMSG\" must not contain QA in upper case." 1>&2
echo ""  1>&2
exit 1
fi

The regex is incorrect: 正则表达式不正确:

  • \\( starts a capturing group in expr , but you don't need a capturing group for your task \\(expr启动捕获组,但您不需要捕获组来完成任务
  • When * follows a \\( in a pattern, it tries to match a literal * *跟随\\(在模式中,它试图匹配文字*
  • [QA] matches a single character, which can be Q or A [QA]匹配单个字符,可以是QA
  • The pattern of expr must match from the start of the string expr的模式必须匹配字符串的开头

As it is, the regex doesn't correspond to your requirement. 实际上,正则表达式与您的要求不符。

Even if the above points are fixed, a pattern QA , "QA" with spaces around it, will not match commit messages like this: 即使以上几点是固定的,模式QA ,“QA”周围有空格,也不会匹配这样的提交消息:

  • "Fix the build of QA" “修复QA的构建”
  • "Broken in QA, temporarily" “在质量保证方面破裂,暂时”
  • ... and so on... ... 等等...

That is, instead of "QA" with spaces around, you probably want to match QA with word boundaries around. 也就是说,您可能希望将QA与字边界相匹配,而不是在周围有空格的“QA”。 This is easy to do using grep -w QA . 使用grep -w QA很容易做到这一点。

As you clarified in a comment, you really want a space before the "Q". 正如你在评论中澄清的那样,你真的想要一个“Q”之前的空格。 In that case the -w flag of grep is not suitable, because that requires a word boundary at both sides of patterns. 在这种情况下, grep-w标志不合适,因为这需要模式两侧的字边界。 There is another way to match word boundaries, using \\< for word start and \\> for word end. 还有另一种匹配字边界的方法,使用\\<用于单词开始,使用\\>用于单词结束。 So to have a space in front of "Q", and a word boundary after "A", you can write QA\\> , like this: 因此,要在“Q”前面有一个空格,在“A”后面有一个单词边界,你可以编写QA\\> ,如下所示:

if grep -q ' QA\>' <<< "$LOGMSG"; then
    echo
    echo "Your log message \"$LOGMSG\" must not contain QA in upper case."
    echo
    exit 1
fi 1>&2

Notice some other improvements: 注意其他一些改进:

  • Instead of redirecting to stderr every single echo , you can redirect the entire if statement 您可以重定向整个if语句,而不是将每个echo重定向到stderr
  • Instead of echo "" you can write simply echo 而不是echo ""你可以简单地写echo
  • Instead of storing the result of a command in a temporary variable, you can write conditionals on the exit code of commands 您可以在命令的退出代码上编写条件,而不是将命令的结果存储在临时变量中

This could be an error with your regex expression checking for " QA ". 这可能是您的正则表达式检查“QA”时出错。

I find using this site pretty useful for testing out regex expressions - RegExr . 我发现使用这个网站非常有用于测试正则表达式 - RegExr

I put your (*[" "][QA][" "]) expression into the site and when I looked at the details of it (a tab link towards the bottom of the page), it would break down exactly what you regular expression would match with. 我把你的(* [“”] [QA] [“”])表达式放到网站上,当我查看它的细节(页面底部的标签链接)时,它会完全分解你的常规表达式会匹配。 From this, it was saying that it was looking for the following: 据此,它说它正在寻找以下内容:

  1. 0 or more ( 0或更多(
  2. Either a " or a space 无论是“还是空间
  3. Either Q or A (not both) Q或A(不是两者)
  4. Either a " or a space 无论是“还是空间
  5. Ending with a ) 结束a)

I put the following expression into it - ( (QA) ) and it was able to find the match in a sample svn message (TEST-117 QA testing message). 我将以下表达式放入其中 - ((QA))并且它能够在示例svn消息中找到匹配(TEST-117 QA测试消息)。

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

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