Question

I need to check if any of the strings "Added/Changed/Fixed/Deleted" are in a commit log message. I am writing an svn precommit hook, and the expected commit comment should have one of these 4 strings in the message.

The code I am using is as below

REPOS=$1
TXN=$2

SVN="/usr/bin/svn";
SVNLOOK="/usr/bin/svnlook";

$SVNLOOK log "$REPOS" -t "$TXN" | \
grep "[a-zA-Z0-9]" > /dev/null

GREP_STATUS=$?
if [ $GREP_STATUS -ne 0 ]
then
  "${ECHO}" "No Log comments present" >> "${LOG}"
   echo "Your commit has been blocked because you didn't give any log message" 1>&2
   echo "Please write a log message describing the purpose of your changes and" 1>&2
   echo "then try committing again. -- Thank you" 1>&2
exit 1
fi

In the above code,

$SVNLOOK log "$REPOS" -t "$TXN"

will give me the commit message that the user has entered. Now I have to check for the presence of any of the strings "Added, Changed, Fixed, Deleted" in the message. That is,

if (any of the above 4 strings are not present),
 exit 1

I tried with

$($SVNLOOK log -t "$TXN" "$REPOS" | grep -q "Added\|Changed\|Fixed\|Deleted"|)

but it doesnt seem to be working.

Was it helpful?

Solution 2

To used alternation you need Extended Regexp:

grep -qE 'Added|Changed|Fixed|Deleted'

Or:

egrep -q 'Added|Changed|Fixed|Deleted'

OTHER TIPS

Use grep -e option (multiple times) like this:

grep -e Added -e Changed -e Fixed -e Deleted

otherwise go to the regex route:

grep --regexp=Added|Changed|Fixed|Deleted

Remove the backslashes and use egrep I also recommend -i for case insensitive matching:

egrep -q -i "added|changed|fixed|deleted"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top