Checking for specific string in commit message - SVN Precommit Hook

ぐ巨炮叔叔 提交于 2019-12-05 07:50:21

问题


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


来源:https://stackoverflow.com/questions/15386934/checking-for-specific-string-in-commit-message-svn-precommit-hook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!