Episode #19 —modifying git commit messages with Git hooks and Golang

Stephens Xu
Fullstack Network
Published in
3 min readJun 10, 2017

--

In this session we built a new Gong functionality that will automatically append a string at each of our commit messages for the current branch.

The string is dynamic and based on current branch name.

Here is the code:

Git has a nice feature called Git Hooks that allows custom logic to fire off when certain actions occur. If you haven’t done this before, go to the .git/hooks folder of any root directory of git repo and you can see some sample shell scripts, mine look like this:

This is where the magic happens. Basically the file name correspond to the specific git action that triggers this script once you remove the .sample extension.

First in our Gong CLI, we implement an action that prepares the string we wanted to append to each commit message, based on current branch name:

{
Name: "prepare-commit-message",
Usage: "This is a prepare-commit-message hook for git",
Action: func(c *cli.Context) error {
client, err := gong.NewAuthenticatedClient()

if err != nil {
color.Red("Problem with starting the issue")
return err
}

cmd := "git"
args := []string{"rev-parse", "--abbrev-ref", "HEAD"}

out, err := exec.Command(cmd, args...).Output()

if err != nil {
return err
}

branchName := string(out)
bytes, err := ioutil.ReadAll(os.Stdin)

if err != nil {
color.Red("Could not read stdin")
return err
}

commitMessage := string(bytes)
newCommitMessge := gong.PrepareCommitMessage(client, branchName, commitMessage)

fmt.Println(newCommitMessge)

return nil
},
}

When we do gong prepare-commit-message it will output a string looking like this[GLOB-1111](http://fake-url-website/GLOB-111)

(gong.PrepareCommitMessage() has its own custom implementation that output this string)

Having the string is only half way, we still need to automatically append this string to each of our commit messages. This is where git hooks come into play.

We would create a shell script and place it inside of our .git/hooks folder and name it according to the trigger action. Since we are manipulating the commit messages, the action to trigger is prepare-commit-message and commit-msg. Place the files like so:

In the commit-msg file

#!/bin/sh
#
# Automatically adds branch name and branch description to every commit message.
#
echo "$1\n$(cat $1 | gong prepare-commit-message)"%

In the prepare-commit-msg file

#!/bin/sh
#
# Automatically adds branch name and branch description to every commit message.
#
echo "$(cat $1)\n$(cat $1 | gong prepare-commit-message)" > $1

As you can see, the shell script will trigger gong prepare-commit-message, which creates our custom string, and then append it to the original commit message that the user inputs.

Now when we do git commit -m <message> it will automatically trigger our commit-msg script and git commit action will trigger prepare-commit-msg script.

Hope this helps you. Git hooks is something new to me and I think it’s a really nice tool.

We’ll be back streaming next week, you’re welcomed to join us!

--

--