Skip to content

Instantly share code, notes, and snippets.

@pboling
Last active Sep 19, 2015
Embed
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 = IO.read(ARGV[0])
unless commit_msg.include?(match_data[:story_id])
commit_msg = <<EOS
#{commit_msg.strip}
[#{match_data[:story_type]}][#{match_data[:story_id]}]
EOS
File.open(ARGV[0], 'w') do |file|
file.print commit_msg
end
end
end
@pboling
Copy link
Author

pboling commented Sep 19, 2015

Turned this into a blog post: www.railsbling.com/posts/my-git-commit-hooks/

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