【问题标题】:Making git produce a warning if no task was mentioned如果没有提到任何任务,让 git 产生警告
【发布时间】:2023-03-03 14:47:01
【问题描述】:

我总是忘记将#ID 放在提交消息中,这对其他正在关注该问题的人来说非常不利。

是否可以强制 git 产生警告,告诉我我正在尝试提交而不在提交消息中添加 #ID

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

【问题讨论】:

标签: git


【解决方案1】:

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

我使用此脚本从分支名称中获取票号并将其添加到前面:

# 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 挂钩中的所有推送提交,以确保更加安全。

【讨论】:

    猜你喜欢
    • 2014-02-26
    • 2021-11-25
    • 1970-01-01
    • 2014-03-18
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    相关资源
    最近更新 更多