解析分支名称,使用提交消息中的名称启动提交 [英] Parse branch name, initiate commit with name in the commit message
问题描述
我的团队对分支名称使用通用的命名约定,在分支名称中包括 Jira 任务编号.
My team uses a common naming convention for branch names, which include the Jira task number in the branch name.
feature/ACD-1664_update-api-call
feature/VZ-1943_new-provider-template
hotfix/RV-977_fix-loading-issue
我想创建一个git alias
,它将自动存根包含 Jira 任务号的提交消息.理想情况下,一些bash脚本将解析分支名称,并在预先创建的消息的第一部分中回显commit -m
命令.
I want to create a git alias
that will automatically stub out a commit message which includes the Jira task number. Ideally some bash script that will parse the branch name and echo out the commit -m
command with the first part of the message pre-created.
- 我需要
regex
发出提交消息.
- I need to
regex
out the commmit message.
我需要从feature/ACD-1664_update-api-call
- 使用残缺的
commit
命令将该字符串回显到终端,例如:
- Echo this string out into the terminal in a stubbed-out
commit
command like:
git commit -m "ACD-1664 | <cursor>"
推荐答案
尽管这不是您要求的解决方案,但我还是想用另一种方法来解决这个问题,即使用提交钩子:
Although this is not the solution you requested, I'd like to hint at another way to cover this, with a commit hook :
您可以将.git/hooks放入具有以下内容的 commit-msg 文件:
You can put in .git/hooks a commit-msg file with these contents :
#!/bin/bash
current_branch="$(git rev-parse --abbrev-ref HEAD)"
tmp=$(mktemp) || exit
echo "$current_branch $(cat "$1")" > "$tmp"
mv "$tmp" "$1"
(感谢大家在您的帮助下对bash语法的改进此处)
(Thanks guys for the improvements in bash syntax made with your help here)
然后,它将自动在您的提交消息之前添加分支名称,这在JIRA中起到了作用.
Then it would automatically prepend your commit messages with the branch name, which does the trick in JIRA.
在极少数情况下,您不想触发该钩子,请执行以下操作:
For the rare occasions when you'd prefer NOT to trigger the hook, do this :
git commit -n -m"Your message"
这篇关于解析分支名称,使用提交消息中的名称启动提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!