Git commit-msg for Lighthouse tickets

A quick follow-up to a post from a few months ago on how our team has a naming convention for git branches when we’re working on Lighthouse tickets (read previous post).

I’ve just put together a quick git hook for commit-msg, which will automatically amend the commit message with the current ticket number when you’re following the branch naming conventions described here.

Just toss this gist into .git/hooks/commit-msg.

```
  #!/bin/sh

  #
  # Will append the current Lighthouse ticket number to the commit message automatically
  # when you use the LH_* branch naming convention.
  #
  # Drop into .git/hooks/commit-msg
  # chmod +x .git/hooks/commit-msg

  exec < /dev/tty

  commit_message=$1
  ref=$(git symbolic-ref HEAD 2> /dev/null) || return
  branch=${ref#refs/heads/}

  if [[ $branch =~ LH_(.*) ]]
  then
  lighthouse_ticket=${BASH_REMATCH[1]}

    echo "What is the state of ticket #${lighthouse_ticket}? "
    echo "(o)pen "
    echo "(h)old"
    echo "(r)esolved"
    echo "Enter the current state for #${lighthouse_ticket}: (o)"

    state="open"

    read state_selection

    case $state_selection in
      "o" )
        state="open"
        ;;
      "h" )
        state="hold"
        ;;
      "r" )
        state="resolved"
        ;;
    esac
  echo >&2 "[#${lighthouse_ticket} state:${state}]" >> "$1"
    exit 0
  fi
```

Then a quick example of how this works…

```
  ➜  bin git:(LH_9912 ♻ ) git ci -m "another test"
  What is the state of this ticket? 
  (o)pen 
  (h)old
  (r)esolved
  Enter the current state: (o)
  h
  Created commit 1ed2713: another test
   1 files changed, 3 insertions(+), 1 deletions(-)
```

Now to see this in action… (screenshot)

::: thumbnail git message
hook :::

Then we’ll check out the git log really quick.

```
➜  bin git:(LH_9912) git log
commit 1ed271323c4a054fe56e76bddc9ac81d241a1032
Author: Robby Russell <robby@planetargon.com>
Date:   Mon Feb 16 12:06:33 2009 -0800

    another test
    [#9912 state:hold]
```

Thanks to Andy for helping me figure out how to read user input during a git hook.

Hi, I'm Robby.

Robby Russell

I run Planet Argon, where we help organizations keep their Ruby on Rails apps maintainable—so they don't have to start over. I created Oh My Zsh to make developers more efficient and host the Maintainable.fm podcast to explore what it takes to build software that lasts.