Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

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