简体   繁体   English

如果条件在SVN预提交挂钩中不起作用,则使用Shell脚本

[英]Shell script if condition not working in SVN pre-commit hook

need some help 需要一些帮助

I was trying to enhance the pre-commit hook to see if I can ignore parsing the commit message if there is a certain key ("!ignore") in the commit message 我正在尝试增强预提交挂钩,以查看如果提交消息中存在某个键(“!ignore”),是否可以忽略对提交消息的解析。

The if conditions before this one , like to check if there are empty commit messages works. 在此条件之前的if条件,例如检查是否有空的提交消息有效。 But this if condition somehow is not working. 但这如果条件以某种方式不起作用。

Ok when I do a commit with message "SMARTCOMMITTEST" which does not contain my check key "!ignore", the commit succeeds which means the If condition below never executed or did not execute as expected. 好的,当我使用消息“ SMARTCOMMITTEST”执行提交,但不包含我的检查键“!ignore”时,提交成功,这意味着以下If条件从未执行或未按预期执行。 So trying to understand what is wrong wit it. 因此,尝试了解它的错误所在。

SMARTCOMMIT=1 
$SVNLOOK log -t "$TXN" "$REPOS" | grep "!ignore" | wc -c || SMARTCOMMIT=0
if [ $SMARTCOMMIT = 0];      
then
echo "Please use !ignore if you dont want to use smart commits in your commit message." 1>&2
exit 1
fi

Thanks a lot to Etan for some tips... 非常感谢Etan提供的一些提示...

I changed the condition like the other if condition in the comments and then it worked 我像注释中的其他if条件一样更改了条件,然后它起作用了

 SMARTCOMMIT=$($SVNLOOK log -t "$TXN" "$REPOS" | grep "!ignore" | wc -c) if [ "$SMARTCOMMIT" = "0" ]; then echo "Please use !ignore if you dont want to use smart commits in your commit message." 1>&2 exit 1 fi 

This one worked fine.. 这个很好。

@David W .. I now have a situation to check multiple conditions in the same if @David W ..我现在有一个情况要检查多个条件,如果

 SMARTCOMMIT=$($SVNLOOK log -t "$TXN" "$REPOS" | grep '!ignore' | wc -c) COMMITMESSAGENOREVIEW=$($SVNLOOK log -t "$TXN" "$REPOS" | grep '#comment' | wc -c) COMMITMESSAGEWITHREVIEW=$($SVNLOOK log -t "$TXN" "$REPOS" | grep '+review' | wc -c) if [ "$SMARTCOMMIT" = "0" -a "$COMMITMESSAGENOREVIEW" = "0" -a "COMMITMESSAGEWITHREVIEW" = "0" ]; then echo "Please use #comment or +review to enable smart commits or !ignore to not use smart commits." 1>&2 exit 1 fi 

I tried as given in the link here but still I dont see the if condition getting executed at all. 我尝试按此处链接中的说明进行操作,但仍然看不到if条件完全执行。 Can you help me with this now? 您现在可以帮我吗?

The if statement can look at the exit output of a command, so you don't need to set an environment variable depending upon the output: if语句可以查看命令的退出输出,因此您无需根据输出设置环境变量:

shopt extglob > /dev/null  && extglob=1
if ! $SVNLOOK log -t "$TXN" "$REPOS" | grep -q '!ignore'
then
    echo 'Please use "!ignore" if you dont want to use smart commits in your commit message.' 1>&2
    exit 1
fi

This runs $SVNLOOK log -t "$TXN" "$REPOS" | grep -q '!ignore' 这将运行$SVNLOOK log -t "$TXN" "$REPOS" | grep -q '!ignore' $SVNLOOK log -t "$TXN" "$REPOS" | grep -q '!ignore'

The -q is quiet mode. -q是安静模式。 grep either exits zero if the string exists or non-zero if it doesn't exist. 如果字符串存在,则grep要么退出零,否则不退出。 By putting this in the if ! 通过将它放在if ! , the then clause executes only if !ignore was found. then只有找到!ignore时, then子句才会执行。

You have to make sure that !ignore is surrounded by single quotes, or you put a \\ in front of the ! 您必须确保!ignore用单引号引起来,或者在\\前面加上\\ ! . Bash has a csh type history mechanism in there, and there's no way to turn it off. Bash中有一个csh类型历史记录机制,无法关闭它。 Anytime the shell sees ! 任何时候壳看到! , it assumes it has to do with the process ID number. ,它假定它与进程ID号有关。

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

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