如果没有提到任务,让git产生警告

时间:2018-03-27 17:59:47

标签: git

我总是忘记将#ID放在提交消息中,这对于正在观看此问题的其他人来说非常糟糕。

是否可以强制git发出警告,告诉我我正在尝试进行提交而不将#ID放入提交消息中?

如果没有,我可以在git客户端级别(如SourceTree)上执行此操作吗?

1 个答案:

答案 0 :(得分:0)

假设您也在分支名称中使用了票号,您可以在prepare-commit-msg钩子中执行此操作,并包含一些基本处理逻辑,以便在您忘记包含票证ID时尝试修复。

我使用此脚本从分支名称中获取故障单编号并添加前缀:

# Identify the task from the branch name if available
task=$(git symbolic-ref --short HEAD | grep -oP "TASK-\d+") 2>/dev/null

# If there is an error in symbolic-ref grab, you're on a detatched
# head, I've never run into this unless I was mid-rebase
if [ $? -ne 0 ]; then
    echo -e "Head detatched, skipping commit message test"
else
    # Only process if the commit source is message
    if [ -v $2 ] || [ "$2" == "message" ]; then
        # Detect the task in the provided message
        hastask=$(head -1 $1 | grep -oP "TASK-\d+" | wc -l)

        # If the task was not found
        if [ "$hastask" == "0" ]; then
            # Prepend the task if found in branch name
            echo -e "Prepending task number\n\n"
            if [ -n "$task" ]; then
                echo -e "task Number resolved to $task\n"
                echo -e "$task: `cat $1`" > $1
            # Otherwise, notify the user and hault the commit
            else
                echo -e "task number not found in branch name\n\n"
                exit 1;
            fi
        # If you remembered to include a task, notify the user and do nothing
        else
            echo -e "task number already exists in message\n\n"
        fi
    fi
fi

如果您负责服务器,可以通过检查服务器上pre-receive挂钩中的所有推送提交来进行检查,以确保安全。