Search code examples
bashgitgithooksticket-system

Git hook to update commit message with data from an API


I am attempting to update a git commit with details from our ticketing API. I would like to grab the title of the ticket number that the developer adds to a git commit and update the commit with that title.

e.g.

git add /some/file

git commit -am "#[id]
Did some work
Other stuff I Did"

At this point I would like to get the Id they used, call my API in bash and then append to the commit the title so the resulting commit message is actually "#[id] Ticket Title".

I am wondering what hook I can use to do this. I'm not looking to get help on how to write the hook but more so what git hook I would use and if I need to alter the commit after it's made (say I need to do it on post commit). What would the git commit --amend look like?

Thanks in advance


Solution

  • You can choose between prepare-commit-msg and commit-msg. The former is called before git commit opens an editor to edit commit message, the latter is called after. Both are supposed to edit the message file (the path to it passed as the first parameter) in place. The first one is always called, the second can be bypassed with git commit --no-verify. Error code on exit aborts commit.