简体   繁体   中英

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

The if conditions before this one , like to check if there are empty commit messages works. 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. 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...

I changed the condition like the other if condition in the comments and then it worked

 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

 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. 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:

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'

The -q is quiet mode. grep either exits zero if the string exists or non-zero if it doesn't exist. By putting this in the if ! , the then clause executes only if !ignore was found.

You have to make sure that !ignore is surrounded by single quotes, or you put a \\ in front of the ! . Bash has a csh type history mechanism in there, and there's no way to turn it off. Anytime the shell sees ! , it assumes it has to do with the process ID number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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