5

When adding a new tag in git, I would like to automatically modify the default (empty) tag message before my $EDITOR fires up—similar to the way that git allows to prepare commit messages via the prepare-commit-msg hook.

For example:

git tag -s v1.2.3

should open my editor with pre-filled contents like this:

Release v1.2.3:

  * Dynamically generated message 1

  * Dynamically generated message 2

Default standard text.

#
# Write a tag message
# Lines starting with '#' will be ignored

Is there any way to achieve this? Unfortunately, the prepare-commit-msg hook doesn’t work with tag messages. (Either this, or I was too dumb to find out how to do it.)

igor
  • 2,090
  • 20
  • 32
  • 1
    Note: With Git 2.17 (Q2 2018), there is also an `--edit` option to `git tag` now. See https://stackoverflow.com/a/49215146/6309 – VonC Mar 10 '18 at 23:41

2 Answers2

4

You could create an alias which would first populate a temp file with the desired content and then run git tag with the option -F <file>/--file=<file> to feed the temp file's content into the tag message. Theoretically, something like this:

[alias]
    tag-prepare = !~/bin/prepare_file.sh && git tag --file="/home/user/temp/temp.txt"

You would then call it with git tag-prepare v1.2.3.

Note that the prepare_file.sh script needs to create the entire tag message because the --file option does not open the editor to edit the content anymore, it only takes w/e is in the provided file and uses that as the message.

mart1n
  • 5,969
  • 5
  • 46
  • 83
  • I already thought about a command line solution using the `-F` parameter, but I still want to be able to modify the message in my editor. The automatically generated message fits entirely most of the time, but not always. – igor Aug 21 '13 at 09:37
  • 1
    Well, you can do it through the script. In the script, open the file with $EDITOR with the text already generated, edit it, save it, exit, let the script finish and then execute `git tag` with the `-F` option. – mart1n Aug 21 '13 at 12:17
3

You can do something like this

message="A header line

A body line...
Other lines...

Final line...
"
git tag --annotate --message "${message}" --edit 0.8.0

It will start tag creation and opens an editor. In my case vim: enter image description here

Dmytro Serdiuk
  • 859
  • 10
  • 20