使用git commit hooks追加票号? [英] Append ticket number using git commit hooks?
问题描述
我查看了git commit hooks,即pre-commit,prepare-message和post-commit,他们都没有能够做我想做的事。 Post-commit hook接近了,但是你不能修改用-m提交的消息。
重申,我想知道这是否可能:在分支上:issue-1234
git commit -a -mfixed this讨厌的问题
提交后,在git log中,它显示消息为:
修正了这个讨厌的问题。门票号码:#1234
你想要的是 commit-msg
:
这个钩子是由git提交,并可以绕过--no-verify选项。它采用一个参数,即包含提议的提交日志消息的文件的名称。
举个例子:
#!/ bin / sh
$ b $ ticket = $(git symbolic-ref HEAD | awk -F-'/ ^ issue- / {print $ 2}')
if [-n$ ticket];然后
回显ticket#$ ticket>> $ 1
fi
这是一个非常天真的分支名称解析,它只是附加到提交消息在它自己的行上。修改它,如果这不够好。
当然,我实际上建议在 prepare-commit-msg
,并提交 git commit
(不包括 -m
)。非常罕见的是,您实际上可以在单行提交消息中编写足够的信息。此外,如果你的钩子不能完成你想要的功能,这将让你在提交之前看到消息。
So my branch is named after bugtracker ticket number, something like "issue-1234", and we have a convention to always write down ticket number in commit message. I'm wondering if it's possible to append the ticket number in commit message automatically when I'm working on an issue-* branch without me explicitly typing it.
I looked at git commit hooks, namely pre-commit, prepare-message, and post-commit, and none of them seem to be able to do what I wanted. Post-commit hook comes close, but you cannot modify the message that's committed with -m.
To reiterate, I'm wondering if this is possible:
On branch: issue-1234
git commit -a -m"fixed this pesky issue"
After the commit, in git log, it shows the message as:
fixed this pesky issue. ticket number: #1234
You missed a hook. The one you want is 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.
So for example:
#!/bin/sh
ticket=$(git symbolic-ref HEAD | awk -F- '/^issue-/ {print $2}')
if [ -n "$ticket" ]; then
echo "ticket #$ticket" >> $1
fi
That's a very naive parsing of your branch name, and it's simply appended to the commit message on its own line. Modify it if that's not good enough for you.
Of course, I'd actually recommend doing this in prepare-commit-msg
, and committing with git commit
(without -m
). It's very, very rare that you can actually write sufficient information in a single-line commit message. Further, that will let you see the message before the commit is made, in case your hook doesn't do quite what you want.
这篇关于使用git commit hooks追加票号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!