24

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?

Robert Munteanu
  • 67,031
  • 36
  • 206
  • 278
  • Oh, misread it sry :( You could set an alias for git commit, which first finds the current branch, writes it to a temporary file and calls git commit with the --template parameter. – ZeissS Nov 03 '10 at 13:26

3 Answers3

22

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.

Community
  • 1
  • 1
Cascabel
  • 479,068
  • 72
  • 370
  • 318
  • Thanks for the answer, it's spot on. For reference, I managed to prepend the line with instructions from http://stackoverflow.com/questions/54365/prepend-to-a-file-one-liner-shell – Robert Munteanu Nov 05 '10 at 15:34
  • @Robert Munteanu any chance you want to put your whole solution here in an answer, for the lazy? I'd upvote it for sure :) Thanks... – Dan Rosenstark Nov 29 '10 at 19:20
  • 1
    @Yar: Personally, I'd rather have something readable with a temporary file than some file descriptor magic that I have to try to decipher later. `mv "$1" "$1.tmp"; git symbolic-ref 2> /dev/null > "$1"; cat "$1.tmp" >> "$1"; rm "$1.tmp"` – Cascabel Nov 29 '10 at 21:54
  • 1
    @Yar: try `branch=$(git symbolic-ref HEAD|sed s#refs/heads/##) exec 3<> "$1" && awk -v TEXT="[$branch]" 'BEGIN {print TEXT}{print}' "$1" >&3` – Robert Munteanu Nov 30 '10 at 07:59
  • Thanks @Robert Munteanu and @Jefromi: I've posted an answer below just to store it somewhere. It seems to work... – Dan Rosenstark Nov 30 '10 at 17:02
  • 2
    using `git rev-parse --abbrev-ref HEAD` would avoid the need for sed above. – andygavin Jan 04 '13 at 14:10
  • @andygavin Did you mean to comment on sschmeck's or Yar's answer? My answer doesn't use sed to strip refs/heads/ off symbolic-ref's output. – Cascabel Jan 04 '13 at 17:52
5

A solution using git alias but no template:

$ git config --global alias.com '!sh -c "bug=`git symbolic-ref HEAD|sed s#refs/heads/##`; git commit -em \"BUG: \${bug}\""'
$ git com
sschmeck
  • 7,233
  • 4
  • 40
  • 67
1

Just cobbling together the comments from Jefromi's answer, I end up with something like this. Surely it could be tighter (if I knew what I was doing):

tempFile='/tmp/git-commit-template'
git config commit.template "$tempFile"
rm $tempFile
branch=$(git symbolic-ref HEAD|sed s#refs/heads/##) exec 3<> "$tempFile" && awk -v TEXT="[$branch]" 'BEGIN {print TEXT}{print}' "$tempFile" >&3
git add .
git commit -a
Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421
  • Hi Dan, I tried command `branch=$(git symbolic-ref HEAD|sed s#refs/heads/##) exec 3<> "$tempFile"` and try to `echo $branch`, I dont see the value of branch name being printed? – Isaac Sep 11 '20 at 01:01
  • I don’t remember but I think you have to create the tempfile directory by hand – Dan Rosenstark Sep 11 '20 at 01:11