Robby on Rails: Git commit-msg for Lighthouse ticketsthoughts.sort_by{|t| t[:topic]}.collect tag:www.robbyonrails.com,2005:TypoTypo2009-02-16T14:21:13-05:00Robby Russellurn:uuid:def4745b-9f02-4b00-a6f9-597777fb3aad2009-02-16T13:51:00-05:002009-02-16T14:21:13-05:00Git commit-msg for Lighthouse tickets<p>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 (<a href="http://www.robbyonrails.com/articles/2008/12/11/lighthouse-tickets-and-git-branching">read previous post</a>).</p>
<p>I’ve just put together a quick git hook for <code>commit-msg</code>, which will automatically amend the commit message with the current ticket number when you’re following the branch naming conventions described <a href="http://www.robbyonrails.com/articles/2008/12/11/lighthouse-tickets-and-git-branching">here</a>.</p>
<p>Just toss this <a href="http://gist.github.com/65330">gist</a> into <code>.git/hooks/commit-msg</code>.</p>
<pre><code>
#!/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
</code></pre>
<p>Then a quick example of how this works…</p>
<pre><code>
➜ 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(-)
</code></pre>
<p>Now to see this in action… (screenshot)</p>
<div class="thumbnail"><a href="http://skitch.com/robbyrussell/brh2m/git-message-hook"><img src="http://img.skitch.com/20090216-rerecdmycjpu7brfppgmmbt8tx.preview.jpg" alt="git message hook" /></a></div>
<p>Then we’ll check out the git log really quick.</p>
<pre><code>
➜ 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]
</code></pre>
<p>Thanks to <a href="http://andy.delcambre.com/">Andy</a> for helping me figure out how to read user input during a git hook.</p><p>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 (<a href="http://www.robbyonrails.com/articles/2008/12/11/lighthouse-tickets-and-git-branching">read previous post</a>).</p>
<p>I’ve just put together a quick git hook for <code>commit-msg</code>, which will automatically amend the commit message with the current ticket number when you’re following the branch naming conventions described <a href="http://www.robbyonrails.com/articles/2008/12/11/lighthouse-tickets-and-git-branching">here</a>.</p>
<p>Just toss this <a href="http://gist.github.com/65330">gist</a> into <code>.git/hooks/commit-msg</code>.</p>
<pre><code>
#!/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
</code></pre>
<p>Then a quick example of how this works…</p>
<pre><code>
➜ 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(-)
</code></pre>
<p>Now to see this in action… (screenshot)</p>
<div class="thumbnail"><a href="http://skitch.com/robbyrussell/brh2m/git-message-hook"><img src="http://img.skitch.com/20090216-rerecdmycjpu7brfppgmmbt8tx.preview.jpg" alt="git message hook" /></a></div>
<p>Then we’ll check out the git log really quick.</p>
<pre><code>
➜ 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]
</code></pre>
<p>Thanks to <a href="http://andy.delcambre.com/">Andy</a> for helping me figure out how to read user input during a git hook.</p>