使用 git commit 挂钩附加票号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5823772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Append ticket number using git commit hooks?
提问by EnToutCas
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.
所以我的分支以 bugtracker 票号命名,类似于“issue-1234”,我们有一个约定,总是在提交消息中写下票号。我想知道是否可以在我处理 issue-* 分支时自动在提交消息中附加票号,而无需我明确输入它。
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.
我查看了 git commit hooks,即 pre-commit、prepare-message 和 post-commit,它们似乎都不能做我想做的事。提交后钩子接近了,但您不能修改使用 -m 提交的消息。
To reiterate, I'm wondering if this is possible:
重申一下,我想知道这是否可能:
On branch: issue-1234
在分支上:issue-1234
git commit -a -m"fixed this pesky issue"
After the commit, in git log, it shows the message as:
提交后,在 git log 中,它显示的消息为:
fixed this pesky issue. ticket number: #1234
采纳答案by Cascabel
You missed a hook. The one you want is commit-msg
:
你错过了一个钩子。你想要的是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.
这个钩子由 git commit 调用,可以通过 --no-verify 选项绕过。它接受一个参数,即保存建议提交日志消息的文件的名称。以非零状态退出会导致 git commit 中止。
So for example:
例如:
#!/bin/sh
ticket=$(git symbolic-ref HEAD | awk -F- '/^issue-/ {print }')
if [ -n "$ticket" ]; then
echo "ticket #$ticket" >>
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.
当然,我实际上建议在 中执行此操作prepare-commit-msg
,并使用git commit
(不使用-m
)进行提交。您实际上可以在单行提交消息中写入足够的信息是非常非常罕见的。此外,这将让您在提交之前看到消息,以防您的钩子没有完全按照您的意愿行事。
回答by Waiting for Dev...
You can as well use prepare-commit-msg
hook, which accepts more parameters than commit-msg
. Then you can check if the message is coming from a file, a template, etc to avoid appending the issue numbers when you don't want it.
您也可以使用prepare-commit-msg
钩子,它接受的参数比commit-msg
. 然后,您可以检查消息是否来自文件、模板等,以避免在您不想要时附加问题编号。
With the following script in .git/hooks/prepare-commit-msg
when you are working in a feature branch named foo-123
, then [#123]
will be added to the third line of every commit you make.
.git/hooks/prepare-commit-msg
当您在名为 的功能分支中工作时,使用以下脚本foo-123
,然后[#123]
将添加到您所做的每个提交的第三行。
More information in this post I wrote
#!/bin/sh
if [ x = x ]; then
BRANCH_NAME=$(git symbolic-ref --short HEAD)
STORY_NUMBER=$(echo $BRANCH_NAME | sed -n 's/.*-\([0-9]\)//p')
if [ x != x${STORY_NUMBER} ]; then
sed -i.back "1s/^/\n\n[#$STORY_NUMBER]/" ""
fi
fi
回答by offmind
This way you can add branch name to the start of commit message. It's prepare-commit-msg hook. Work both for "git commit -m" and "git commit" commands. The option is file .git/hooks/pre-commit.skip which contains a list of branches you don't want to auto-prepend.
通过这种方式,您可以将分支名称添加到提交消息的开头。这是prepare-commit-msg 钩子。适用于“git commit -m”和“git commit”命令。该选项是文件 .git/hooks/pre-commit.skip,其中包含您不想自动添加的分支列表。
BRANCH="$(git rev-parse --abbrev-ref HEAD)"
FILE_CONTENT="$(cat )"
skip_list=`git rev-parse --git-dir`"/hooks/pre-commit.skip"
if grep -E "^$BRANCH$" $skip_list; then
exit
fi
if [ = "message" ]; then
echo $BRANCH: $FILE_CONTENT >
else
echo $BRANCH: >
echo $FILE_CONTENT >>
fi
回答by VonC
Another option would be to use git notes
to add the ticket number information to the commit, using one of the hooks you mention.
(See "Notes to self"blog post entry for more on the notes mechanism)
另一种选择是使用git notes
您提到的一个钩子将票号信息添加到提交中。
(有关注释机制的更多信息,请参阅“自我注释”博客文章条目)
回答by Milind
Using pre-commitalong with the gitickethook, works pretty well to have the ticket number in the commit automatically.
回答by bimlas
Here's a complete solution for any kind of issue/ticket numbering commit messages:
这是针对任何类型的问题/工单编号提交消息的完整解决方案:
prepare-commit-msg
准备提交消息
#!/bin/bash
# Append issue number / bug tracking URL to commit.
#
# If the branch name contains the issue number, it will append it to the
# commit message. Example:
#
# BRANCH NAME LINE TO APPEND
# feature/GH-123-emoji GitHub: #123
# WRIKE-123-add-payment Wrike: https://www.wrike.com/open.htm?id=123
# UNKNOWN-123 Issue: #123
branchName=`git rev-parse --abbrev-ref HEAD`
IFS=- read issueTracker issueNumber <<< $(echo $branchName | sed -nr 's,([a-z-]+/)?([A-Z]+-[0-9]+)-.+,,p')
if [[ -z $issueNumber ]]; then
exit 0
fi
case "$issueTracker" in
WRIKE)
line="Wrike: https://www.wrike.com/open.htm?id=$issueNumber"
;;
GH)
line="GitHub: #$issueNumber"
;;
GL)
line="GitLab: #$issueNumber"
;;
*)
line="Issue: #$issueNumber"
;;
esac
# If the commit message already contains the line (`--amend`), then do
# not add it again.
if ! ( grep "$line" "" > /dev/null ); then
sed -i.bak -e "/# Please enter the commit message for your changes./ s,^,$line\n\n,"
fi
Put it into repository's .git/hooks
directory to apply only to the repo, or set up core.hooksPathin ~/.gitconfig
and copy to that directory to apply to all of your repositories.
把它放进仓库的.git/hooks
目录仅适用于回购,或设置core.hooksPath中~/.gitconfig
,并复制到该目录适用于所有的存储库。
See in my config files repositorybesides other usefull scripts.
除了其他有用的脚本外,请参阅我的配置文件存储库。