繁体   English   中英

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

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

需要一些帮助

我正在尝试增强预提交挂钩,以查看如果提交消息中存在某个键(“!ignore”),是否可以忽略对提交消息的解析。

在此条件之前的if条件,例如检查是否有空的提交消息有效。 但这如果条件以某种方式不起作用。

好的,当我使用消息“ SMARTCOMMITTEST”执行提交,但不包含我的检查键“!ignore”时,提交成功,这意味着以下If条件从未执行或未按预期执行。 因此,尝试了解它的错误所在。

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

非常感谢Etan提供的一些提示...

我像注释中的其他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 

这个很好。

@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 

我尝试按此处链接中的说明进行操作,但仍然看不到if条件完全执行。 您现在可以帮我吗?

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

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

-q是安静模式。 如果字符串存在,则grep要么退出零,否则不退出。 通过将它放在if ! then只有找到!ignore时, then子句才会执行。

您必须确保!ignore用单引号引起来,或者在\\前面加上\\ ! Bash中有一个csh类型历史记录机制,无法关闭它。 任何时候壳看到! ,它假定它与进程ID号有关。

暂无
暂无

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

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