git-使用分支名称的一部分扩展提交钩子

git-使用分支名称的一部分扩展提交钩子,git,jira,bitbucket,githooks,Git,Jira,Bitbucket,Githooks,我想使用prepare-commit-msg钩子。我正在使用功能和错误修复分支(feature/ISSUEKEY-23123-some-feature),我想将ISSUEKEY-23123预先添加到提交消息中: #!/bin/bash BRANCH_NAME=$(git branch | grep '*' | sed 's/* //') STR=`echo $BRANCH_NAME | grep -E 'ISSUEKEY-[0-9]*' -o` if [[ $BRANCH_NAME == *"f

我想使用prepare-commit-msg钩子。我正在使用功能和错误修复分支(feature/ISSUEKEY-23123-some-feature),我想将ISSUEKEY-23123预先添加到提交消息中:

#!/bin/bash
BRANCH_NAME=$(git branch | grep '*' | sed 's/* //')
STR=`echo $BRANCH_NAME | grep -E 'ISSUEKEY-[0-9]*' -o`
if [[ $BRANCH_NAME == *"feature"* ]] || [[ $BRANCH_NAME == *"bugfix"* ]]
then
    echo $STR > $1
fi
这是可行的,但它放弃了vi为提交向我显示的标准消息,例如:

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# Explicit paths specified without -i or -o; assuming --only paths...
# On branch feature/ISSUEKEY-1716-model-implement
# Your branch is based on 'origin/feature/ISSUEKEY-1716-model-implement', but the upstream is gone.
#   (use "git branch --unset-upstream" to fixup)
#
# Changes to be committed:
#       new file:   asd
#
# Untracked files:
#       FSTD-1716
#       TMP
#
有没有一种方法可以将
STR
前置到输出,或者调用一个git命令来打印所提到的标准提交消息

打开的提交消息应为:

ISSUEKEY-1716 
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# Explicit paths specified without -i or -o; assuming --only paths...
# On branch feature/ISSUEKEY-1716-model-implement
# Your branch is based on 'origin/feature/ISSUEKEY-1716-model-implement', but the upstream is gone.
#   (use "git branch --unset-upstream" to fixup)
#
# Changes to be committed:
#       new file:   asd
#
# Untracked files:
#       FSTD-1716
#       TMP
#
我修正了它:

#!/bin/bash

BRANCH_NAME=$(git branch | grep '*' | sed 's/* //')
COMMIT_MSG=`echo $BRANCH_NAME | grep -E 'ISSUEKEY-[0-9]*' -o`
if [[ $BRANCH_NAME == *"feature/"* ]] || [[ $BRANCH_NAME == *"bugfix/"* ]]
then
    PRE_COMMIT_MSG=$(cat $1)
    if [ -z "$COMMIT_MSG" ] ; then
        exit 0
    else
        COMMIT_MSG="$COMMIT_MSG - $PRE_COMMIT_MSG"
        echo "$COMMIT_MSG" > $1
    fi
fi
引述自:

git commit在准备默认日志消息之后,在编辑器启动之前调用这个钩子。。。钩子的作用是在适当的位置编辑消息文件,并且

(我的黑体字)。这意味着您必须执行它所说的操作:就地编辑文件。不要只是覆盖它!因此,不是:

echo $STR > $1
你可以这样做:

ed - "$1" << end
0a
$STR
.
w
q
end
因为它过度依赖于各种“瓷器”命令输出样式。相反,使用
git symbolic ref
(通常是脚本的首选方法)或
git rev parse
。这两者之间的区别有点哲学性:

  • git symbolic ref HEAD
    获取您当前所在分支的名称,如果您使用的是特殊的匿名“分离头”案例,则会失败。(您希望在这里使用的
    只是
    --short
    来省略
    refs/heads/
    前缀。)

  • git rev parse HEAD
    通常保证生成某种类型的成功名称,因为
    HEAD
    始终是一个有效名称1,因此将与某个名称相对应。这意味着将
    HEAD
    转换为修订哈希ID,但使用
    --symbolic
    选项,它将保留一个“尽可能符号化”的名称,使用
    --abbrev ref
    时,它也将省略
    refs/heads/

关键的区别在于
revparse
方法在头部“分离”时不会失败,而
symbolic ref
方法会失败。失败可以让您区分这种情况,这有时很重要。出于您的目的,这并不重要,当使用
symbolic ref
时,您需要为“当前分支无名称”分离的头壳提供备用

因此,您需要:

BRANCH_NAME=$(git rev-parse --symbolic --abbrev-ref HEAD)
或:

(奇怪的是,这些命令的字节数完全相同)


这里有一个角落的案子。虽然
HEAD
在无效时始终有效,但您没有Git存储库(Git会给您一条“致命的:”消息),如果您在“未出生的分支”上,
HEAD
是对不存在的分支名称的符号引用。在这种特殊情况下,
git symbolic ref
将成功,
git rev parse HEAD
将失败,这与更常见的头分离情况相反

BRANCH_NAME=$(git rev-parse --symbolic --abbrev-ref HEAD)
BRANCH_NAME=$(git symbolic-ref --short HEAD || echo HEAD)