0

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?

Community
  • 1
  • 1
Raghav Vaidhyanathan
  • 745
  • 1
  • 10
  • 25
  • 1
    `[ $SMARTCOMMIT = 0];` you missed the space you need between the `0` and the closing `]`. Also use single quotes around `!ignore` to be sure you disable history parsing (which should be disabled in scripts anyway but single quotes are safer). When does the `||` part of that command ever happen? When does `wc` ever return non-zero? – Etan Reisner Feb 03 '15 at 17:46
  • @EtanReisner it still doesnt work when i gave the space after 0 and ]. I am assuming that wc -c returns a non-zero when the commit message has the string !ignore used so it will find it and return a non-zero and Smartcommit=0 wont get set.. – Raghav Vaidhyanathan Feb 03 '15 at 17:48
  • Define "doesn't work" in that context? (You didn't explain it in the original context either for the record.) I'm assuming my secondary comments about `wc` are probably related. – Etan Reisner Feb 03 '15 at 17:49
  • i am sorry i did not follow your last comment... – Raghav Vaidhyanathan Feb 03 '15 at 17:55
  • Have you checked that `wc` returns non-zero when it finds or doesn't find any output? Not spits out a value of non-zero but returns with an *exit code* of non-zero? Because it almost certainly does not. `grep`, on the other hand, exits non-zero when no lines were printed. That's likely much more useful to you. And you still haven't explained what **exactly** isn't working here. – Etan Reisner Feb 03 '15 at 17:58
  • Remember `||` tests **exit codes** not command output. – Etan Reisner Feb 03 '15 at 17:58
  • I updated the question with what am expecting this if condition to do? A scenario per se – Raghav Vaidhyanathan Feb 03 '15 at 18:01
  • LOGMSG=$($SVNLOOK log -t "$TXN" "$REPOS" | grep [a-zA-Z0-9] | wc -c) if [ "$LOGMSG" -lt 6 ]; then echo -e "Please provide a meaningful comment when committing changes." 1>&2 exit 1 fi this if condition preceeds the one in the question. This one works to check if there are a minimum of 6 characters in the commit message and it works. – Raghav Vaidhyanathan Feb 03 '15 at 18:02
  • The answer is that `wc` and `||` aren't doing what you think they are. See my comments about exit codes and what `grep` does. – Etan Reisner Feb 03 '15 at 18:03
  • Yes, because you are checking the **output** of `wc` in that check. The one in this question is **not** doing that. – Etan Reisner Feb 03 '15 at 18:03

1 Answers1

0

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'

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.

David W.
  • 105,218
  • 39
  • 216
  • 337