所以我的分支以 bugtracker 票号命名,类似于“issue-1234”,我们有一个约定,总是在提交消息中写下票号。我想知道是否可以在我处理 issue-* 分支时自动将票号附加到提交消息中而无需我明确键入它。
我查看了 git commit hooks,即 pre-commit、prepare-message 和 post-commit,它们似乎都不能做我想做的事情。提交后 Hook 接近,但您无法修改使用 -m 提交的消息。
重申一下,我想知道这是否可能:
在分支上:issue-1234
git commit -a -m"fixed this pesky issue"
提交后,在 git log 中,它显示消息为:
fixed this pesky issue. ticket number: #1234
最佳答案
你错过了一个钩子(Hook)。你想要的是commit-msg
:
This hook is invoked by git commit, and can be bypassed with --no-verify option. It takes a single parameter, the name of the file that holds the proposed commit log message. Exiting with non-zero status causes the git commit to abort.
例如:
#!/bin/sh
ticket=$(git symbolic-ref HEAD | awk -F- '/^issue-/ {print $2}')
if [ -n "$ticket" ]; then
echo "ticket #$ticket" >> $1
fi
这是对您的分支名称的非常幼稚的解析,它只是简单地附加到自己一行的提交消息中。如果这对您来说不够好,请修改它。
当然,我实际上建议在 prepare-commit-msg
中执行此操作,并使用 git commit
提交(不使用 -m
).您实际上可以在单行提交消息中写入足够的信息是非常非常罕见的。此外,这将让您在提交之前看到消息,以防您的 Hook 没有完全按照您的要求执行。
关于git - 使用 git 提交 Hook 附加票号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5823772/