【问题标题】:Assistance required for Pre-Commit hook script for SVN to check for TaskIDSVN 的 Pre-Commit 挂钩脚本检查 TaskID 所需的帮助
【发布时间】:2017-12-15 09:13:14
【问题描述】:

我有一个预提交挂钩脚本来检查日志消息中的 TaskID。我无法弄清楚相同的逻辑。在 Highlighted **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

【问题讨论】:

  • 我也在寻找一种仅在 svn 主干上而不是在分支/标签上实现此功能的方法。如果没有关联的TaskID,我还想要一个出路,那么它也应该接受[TaskID:0000(四个零)-提交消息]

标签: shell svn pre-commit-hook


【解决方案1】:

我猜您的问题涉及在 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

【讨论】:

    【解决方案2】:

    另外,我在同事的帮助下修改了我的脚本。

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-11
      • 1970-01-01
      • 2011-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-01
      • 2020-06-24
      相关资源
      最近更新 更多