忽略 prepare-commit-msg git 钩子的合并提交

Ignoring merge commits for prepare-commit-msg git hook

我想到了以下 prepare-commit-msg git 钩子:

#!/bin/sh

BRANCH=`git branch | grep '^*' | sed -e 's/^\* //'`
MESSAGE=`cat ""`
JIRA_ID=$(echo "$BRANCH" | grep -Eo "[A-Z0-9]{1,10}-?[A-Z0-9]+-\d+")

if [ -z "$JIRA_ID" ]; then
    echo "$MESSAGE" > ""
else
    echo "[$JIRA_ID] $MESSAGE" > ""
fi

它通过向每个提交消息添加分支名称(以 BRANCH-123 的形式)来完成它的工作。但是,我不希望它与具有自动生成提交消息 s.a 的合并提交一起使用。 Merge branch 'master' into BRANCH-123。用我的钩子将其转换为 BRANCH-123 Merge branch 'master' into BRANCH-123

我需要编辑脚本以某种方式忽略(不应用钩子)以 Merge branch 开头的提交。

正如 prepare-commit-msg 的文档所说,

It takes one to three parameters. The first is the name of the file that contains the commit log message. The second is the source of the commit message, and can be: message (if a -m or -F option was given); template (if a -t option was given or the configuration option commit.template is set); merge (if the commit is a merge or a .git/MERGE_MSG file exists); squash (if a .git/SQUASH_MSG file exists); or commit, followed by a commit SHA-1 (if a -c, -C or --amend option was given).

我们可以测试第二个参数,如果是 merge:

则返回 0
#!/bin/bash

COMMIT_MSG_FILE=
COMMIT_SOURCE=
SHA1=

if [ "${COMMIT_SOURCE}" = merge ];then
    exit 0
fi