SVN 的预提交钩子脚本需要帮助来检查任务 ID



我有一个预提交钩子脚本来检查日志消息中的 TaskID。我无法弄清楚相同的逻辑。在突出显示的**if**语句中,我需要一个逻辑来检查第一行的第一个字母是否是 TaskID:(多位数(-(空格((日志消息(

预提交钩子:

REPOS="$1"
TXN="$2"
# Check log message for proper task/bug identification
if [ -x ${REPOS}/hooks/check_log_message.sh ]; then
${REPOS}/hooks/check_log_message.sh "${REPOS}" "${TXN}" 1>&2 || exit 1
fi
exit 0

=======>>>>>check_log_message.sh

#!/bin/bash
REPOS="${1}"
TXN="${2}"
SVNLOOK=/usr/bin/svnlook
LOG_MSG_LINE1=`${SVNLOOK} log -t "${TXN}" "${REPOS}" | head -n1`
**if (echo "${LOG_MSG_LINE1}" | egrep '^[T][a][s][k][I][D][:]?[-][1-9]
[s]*.*$' > /dev/null;) 
|| (echo "${LOG_MSG_LINE1}" | egrep '^[a-zA-Z]+[-][1-9][0-9]*[:]?[s]*.*$' 
> /dev/null;)**
then
exit 0
else
echo ""
echo "Your log message does not contain a TaskID(or bad format used)"
echo "The TaskID must be the first item on the first line of the log 
message."
echo ""
echo "Proper TaskID format--> TaskID:xxx- 'Your commit message'  "
exit 1
fi

我想你的问题涉及在 Bash 中使用条件。 您可以直接使用程序的退出代码。 例如,如果egrep匹配某些内容,则以代码 0 退出,这意味着成功, 否则,它将以非零值退出, 这意味着失败。 您可以在条件下使用它,例如:

if command; then
echo success
else
echo failure
fi

其中command可以是管道,例如:

if ${SVNLOOK} log -t "${TXN}" "${REPOS}" | head -n1 | egrep -q ^TaskID:
then
exit 0
fi

这意味着如果日志的第一行以TaskID:开头,则以 0 退出。除了if语句之外,您还可以使用较短的形式,如下所示&&

${SVNLOOK} log -t "${TXN}" "${REPOS}" | head -n1 | egrep -q ^TaskID: && exit 0

在这两个示例中,我都使用-qegrep来抑制输出(匹配的行(,因为我想您可能不需要它。

具有更完整模式的完整脚本:

#!/bin/bash
REPOS="${1}"
TXN="${2}"
SVNLOOK=/usr/bin/svnlook
${SVNLOOK} log -t "${TXN}" "${REPOS}" | head -n1 | egrep -q '^TaskID:[0-9][0-9]*- ' && exit 0
echo ""
echo "Your log message does not contain a TaskID(or bad format used)"
echo "The TaskID must be the first item on the first line of the log 
message."
echo ""
echo "Proper TaskID format--> TaskID:xxx- 'Your commit message'  "
exit 1

或者,我在同事的帮助下修改了我的脚本。

REPOS="$1"
TXN="$2"
# Make sure that the log message contains some text.
SVNLOOK=/usr/bin/svnlook
LOGMSG=$($SVNLOOK log -t "$TXN" "$REPOS")
# check if any comment has supplied by the commiter
if [ -z "$LOGMSG" ]; then
echo "Your commit was blocked because it have no comments." 1>&2
exit 1
fi
#check minimum size of text
if [ ${#LOGMSG} -lt 15 ]; then
echo "Your Commit was blocked because the comments does not meet minimum length requirements (15 letters)." 1>&2
exit 1
fi
# get TaskID by regex
TaskID=$(expr "$LOGMSG" : '([#][0-9]{1,9}[:][" "])[A-Za-z0-9]*')
# Check if task id was found. 
if [ -z "$TaskID" ]; then
echo ""  1>&2
echo "No Task id found in log message "$LOGMSG"" 1>&2
echo ""  1>&2
echo "The TaskID must be the first item on the first line of the log message."  1>&2
echo ""  1>&2
echo "Proper TaskID format--> #123- 'Your commit message'  " 1>&2
exit 1
fi

最新更新