Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@omnikron
Forked from henrik/commit-msg
Created January 18, 2012 10:39
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 omnikron/1632375 to your computer and use it in GitHub Desktop.
Save omnikron/1632375 to your computer and use it in GitHub Desktop.
# 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
#!/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 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment