Skip to content

Instantly share code, notes, and snippets.

@sibljon
Created September 17, 2013 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sibljon/6601961 to your computer and use it in GitHub Desktop.
Save sibljon/6601961 to your computer and use it in GitHub Desktop.
git commit-msg hook to prepend commit messages with a JIRA ticket number
#!/usr/bin/env ruby
#
# Jon Sibley, Sept 17 2013, based on this gist: https://gist.github.com/hakanensari/4194831
#
# A minimal commit message hook that extracts a JIRA ticket number from the branch
# name and appends it to the commit message.
#
# The commit message remains unchanged if the branch name doesn't include a
# ticket number or the message already includes a reference to it.
#
# Let's assume you have a story that outlines that you want to deliver foo
# value. You start by checking out a branch:
#
# 123-deliver-foo-feature
#
# You commit a unit of work:
#
# Did bar
#
# This becomes:
#
# IOS-123 Did bar
#
branchname = `git name-rev --name-only HEAD 2>/dev/null`.strip
exit unless branchname.match(/^(\d+)/)
ticket = "#{$1}"
message_file = ARGV[0]
message = File.read(message_file)
# exit if message.include? "IOS-" + ticket
prepended_message = "IOS-#{ticket} " + message
File.open(message_file, 'w') { |f| f.write prepended_message }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment