Last active
September 19, 2015 08:28
-
-
Save pboling/34a1d6f4e9f93927cf65 to your computer and use it in GitHub Desktop.
commit message git hook: Adds story type and story ID to the end of each commit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Turned this into a blog post: www.railsbling.com/posts/my-git-commit-hooks/