Checking for specific string in commit message - S

2020-06-29 10:35发布

问题:

I am expecting the following format in the svn commit message.

Description: (some description of the change)
Entity: (change request number)

If the comment while committing doesn't follow the above format an error message should be thrown. The idea is to check for the key strings "Description" and "Entity" in the commit message. I am also checking the presence of a comment in the message.

I am using the following code but I am not able to get that check for the string "Description" working. (The check for null comment is working fine though.) Could you please tell what I might be doing wrong?

REPOS=$1
TXN=$2
LOG="/home/svn/testSVN/logs/precommithook.log"

GREP=/bin/grep
ECHO=/bin/echo
SVN="/usr/bin/svn";
SVNLOOK="/usr/bin/svnlook";

#Logs the current Transaction ID
"${ECHO}" "Transcation Id ${TXN}" >> "$LOG"

$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

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

GREP_DESCRIPTION_STATUS=$?
if [ $GREP_DESCRIPTION_STATUS –ne 0 ]
then
  "${ECHO}" "Description not found in the comment" >> "${LOG}"
   echo "Your commit has been blocked because you didn't give the Description in  your     commit 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
exit 0

I tried with simple grep "Description" as well as grep -w "Description" too. Still couldnt get it.

回答1:

Got a minor workaround. Used

LOGMSG=$($SVNLOOK log -t "$TXN" "$REPOS" | grep -w "Description" | wc -c)
if [ "$LOGMSG" -le 0 ]; then echo -e "Please provide a description when committing changes." 1>&2
exit 1
fi