public
Last active — forked from henrik/commit-msg

# Outdated, view updated version at https://github.com/omnikron/stagecoach/tree/master/lib/githooks # Git commit-msg hook that works in tandem with the redmine_stagecoach gem to automatically add your branch's associated github issue number to your commit

  • Download Gist
commit-msg
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#!/usr/bin/env ruby
#
# Git commit-msg hook adapted from Henrik Nyh's <http://henrik.nyh.se> original version
# <https://gist.github.com/184711>
#
# Works in tandem with the redmine_stagecoach gem to automatically add your
# branch's associated github issue number to your commit message.
#
# If you include "#noref" in the commit message, nothing will be added to
# the commit message, and the "#noref" itself will be stripped.
#
# If you want to close the issue with this commit, include "#close" in your commit message
#
# Install:
#
# cd your_project
# stick it in .git/hooks/commit-msg
# chmod u+x .git/hooks/commit-msg
 
require 'yaml'
 
# Find the stagecoach gem path. The gsub just trims /bin/stagecoach off the end.
begin
gem_path = Gem.bin_path('redmine_stagecoach', 'stagecoach').gsub!(/\/bin\/stagecoach$/, '')
rescue
exit
end
 
case gem_path
when nil
exit
else
config = YAML::load(File.open(gem_path + '/lib/stagecoach/config.yaml', 'r'))
end
 
# Find out what branch we are on
def branches
`git branch`.split("\n")
end
 
def current_branch
branches.each do |b|
if b =~ /\*/
return b[1..-1].strip
end
end
end
 
 
# And now the git hook stuff
FLAGS = [
NOREF = "noref",
CLOSE = "close",
FINISH = "finish"
]
 
CLOSING_FLAGS = [ CLOSE, FINISH ]
 
begin
ticket_number = config[current_branch][:github_issue]
rescue
exit
end
finish = "Closes #%s" % ticket_number
reference = "#%s" % ticket_number
 
message_file = ARGV[0]
message = File.read(message_file).strip
exit if message.include?("##{ticket_number}")
 
# Determine if any of the flags are included. Make a note of which and then remove it.
message.sub!(/(?:^|\s)#(#{Regexp.union(*FLAGS)})\b/, '')
flag = $1
 
message =
case flag
when NOREF
message
when *CLOSING_FLAGS
[ message, finish ].join(" ")
else
[ message, reference ].join(" ")
end
 
File.open(message_file, 'w') {|f| f.write message }

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.