在提交模板中包含当前分支名称 [英] Including the current branch name in the commit template

查看:83
本文介绍了在提交模板中包含当前分支名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个为git设置的提交模板,我想在其中包含当前分支的名称。我通常将分支设置为bug编号,它可以帮助我填写如下样板文件:

  Bug: $ BUG 

如何使用git评论模板执行这种替换?

解决方案

我可能只是使用 prepare-commit-msg 钩子将其添加到文件中。从(链接的)手册页:


在编写默认日志消息之后,在编辑器启动之前,该钩子由git commit立即调用。



它需要一到三个参数。第一个是包含提交日志消息的文件的名称。第二个是提交消息的来源...... [消息,模板,合并,压扁或提交] ...

如果退出状态为非零,git commit会中止。

这个钩子的目的是编辑邮件文件...


您可以使用 git symbolic-ref HEAD 获取当前分支。



您可以完全绕过模板,并将挂钩前缀/插入/追加分支名称。最简单的情况下,追加,脚本只是一个shebang行,然后 git symbolic-ref HEAD>> $ 1。如果你想嵌入它,使用你最喜欢的方法 - 大多数可读性将原始文件移动,写入和追加,但在注释中链接的方法当然也可以。



如果你喜欢使用占位符模板,你可以做一些像 sed -is / Bug:\ $ BUG / BUG:$(git symbolic-ref HEAD)/ $ 1\" 。我敢肯定,你可以想象很多其他的变种。



你可能想要抑制这种行为的一些类型的提交(第二个参数),甚至只是如果第二个参数是模板,如果您使用样板替换方法,请将其打开。


I have a commit template set up for git, and I would like to include the name of the current branch in it. I usually set up the branch to be the bug id, and it would help me with filling in boilerplate such as:

Bug : $BUG

How can I perform such a substitution with the git comment template?

解决方案

I'd probably just use the prepare-commit-msg hook to add that to the file. From the (linked) manpage:

This hook is invoked by git commit right after preparing the default log message, and before the editor is started.

It takes one to three parameters. The first is the name of the file that contains the commit log message. The second is the source of the commit message ... [message, template, merge, squash, or commit] ...

If the exit status is non-zero, git commit will abort.

The purpose of the hook is to edit the message file in place ...

You can get the current branch with git symbolic-ref HEAD.

You could just bypass templates altogether, and have the hook prepend/insert/append the branch name. Simplest case, appending, the script is just a shebang line, then git symbolic-ref HEAD >> "$1". Use your favorite method if you want to embed it - most readable to move the original aside, write, and append, but the method linked in the comments certainly works too.

If you'd prefer to use a template with placeholders, you could just do something like sed -i "s/Bug : \$BUG/BUG : $(git symbolic-ref HEAD)/" "$1". I'm sure you can imagine a lot of other variations.

You might want to suppress this behavior for some of the types of commits (that second argument) or even only turn it on if the second argument is "template", if you're using the boilerplate substitution approach.

这篇关于在提交模板中包含当前分支名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆