13

By convention I create my story branches in git to include Jira issue ids in them, e.g. FOO-1001. I have a script to do that for me. Now, I have prepared another script that fetches the title of FOO-1001 from Jira API. I want to achieve that when I type:

$ git commit

My editor opens up prefilled with the following:

BUGFIX: FOO-1001 Some sample issue title downloaded using my script

What is the easiest way to achieve this using the scripts I described? My idea is to somehow format the commit message to a file so that git can find it and use as default. One way seems to be to use prepare-commit-msg hook, but I would prefer to achieve my goal using a standalone script, without any configuration in .git (so that my colleagues can easily reuse it).

VoY
  • 5,479
  • 2
  • 37
  • 45
  • 1
    The answer from chepner to use templates is the correct one. If you want to share it with your colleagues its best to put your `prepare-commit-msg` hook and some template files in a repo everyone in your organization has access to. You could put an install script in that repo as well. – Haralan Dobrev Mar 22 '13 at 10:56
  • I went with that solution and it works fine. The only thing that pains me is that fetching the issue id takes a while and it slows down `git commit`. If I'm bothered enough by it I might save the commit message someplace on branch creation. Thank you guys. – VoY Mar 22 '13 at 13:58

2 Answers2

17

The commit command has an option for reading a commit message from a template:

 -t <file>, --template=<file>
       When editing the commit message, start the editor with the contents
       in the given file. The commit.template configuration variable is often
       used to give this option implicitly to the command. This mechanism can
       be used by projects that want to guide participants with some hints on
       what to write in the message in what order. If the user exits the editor
       without editing the message, the commit is aborted. This has no effect
       when a message is given by other means, e.g. with the -m or -F options.
chepner
  • 497,756
  • 71
  • 530
  • 681
  • This doesn't work with `git tag --annotate`. Is there any way to overcome that limitation? – weaver Aug 11 '16 at 01:18
  • Not that I'm aware of. It seems odd that it supports `-m` but not `-t`; the assumption might be that tag commit messages generally are short enough that you wouldn't need a template. – chepner Aug 11 '16 at 01:31
  • GitHub has started to use tag annotations as release annotations, so I want to be able to say `git tag --annotate --template <(git log $(git describe --abbrev=0)..HEAD)` in my release script. Seems impossible :( – weaver Aug 11 '16 at 01:36
  • @weaver please check [this answer](https://stackoverflow.com/a/58169814/10418734) about a `tag`s message. – Dmytro Serdiuk Sep 30 '19 at 14:20
5

There's also -F:

   -F <file>, --file=<file>
       Take the commit message from the given file. Use - to read the message from the standard input.
AlexChaffee
  • 8,092
  • 2
  • 49
  • 55