Skip to content

Instantly share code, notes, and snippets.

Last active Sep 19, 2015
What would you like to do?
commit message git hook: Adds story type and story ID to the end of each commit
#!/usr/bin/env ruby
# vim: set syntax=ruby
# branches should be named like:
# <story_type>/<story_id>-explosion-in-the-fudge-factory-spec-suite-fix
# where story type is one of "hotfix", "bug", "feature", "candy"
branch = `git branch 2> /dev/null | grep -e ^* | awk '{print $2}'`
regex = /^(?<story_type>(hotfix)|(bug)|(feature)|(candy))\/(?<story_id>\d{8,})-.+\Z/
match_data = branch.match(regex)
# NOTE: `match` will return nil if match fails, otherwise an instance of MatchData.
# If not nil then we are assured matches for both regex capture groups
# match_data_or_nil[:story_type] will be one of "hotfix", "bug", "feature", "candy"
# match_data_or_nil[:story_id] will be a numeric string
if !match_data.nil?
commit_msg =[0])
unless commit_msg.include?(match_data[:story_id])
commit_msg = <<EOS
EOS[0], 'w') do |file|
file.print commit_msg
Copy link

pboling commented Sep 19, 2015

Turned this into a blog post:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment