0

I have following Git hook in Ruby, which uses a regular expression to check for a Jira ticket like IPS-56981 in the commit message. If not found, then the commit is aborted.

#!/usr/bin/env ruby
message_file = ARGV[0]
message = File.read(message_file)

$regex = /\A\IPS-(\d+)\s\w+/

if !$regex.match(message)
  puts "[POLICY] Your message is not formatted correctly. You need to specify a Jira ticket."
  exit 1
end

In some cases, I want to ignore the check and commit anyway. How can I realize that in Ruby?

My ideas were to pass an additional argument to Git commit and to check in the Ruby script for that or directly to wait in the Ruby script for ignore message to ignore the Jira ticket check. But my tries are failed. Maybe I need to use another Git hook?

I tried:

#!/usr/bin/env ruby
message_file = ARGV[0]
message = File.read(message_file)

$regex = /\A\IPS-(\d+)\s\w+/

if !$regex.match(message)
  puts "[POLICY] Your message is not formatted correctly. You need to specify a Jira ticket."
  ignore=$stdin.gets.chomp
  if ignore == "ignore"
    exit 0
  end
  exit 1
end

but I get the error message:

.git/hooks/commit-msg:9:in `<main>': undefined method `chomp' for nil:NilClass (NoMethodError)

It also does not wait to give me the chance to enter ignore.

BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
  • Related question: http://stackoverflow.com/questions/3417896/how-do-i-prompt-the-user-from-within-a-commit-msg-hook – Jordan Running Dec 07 '15 at 18:01
  • The above link answers your question (there's a note about Ruby in one of the comments). Another approach, though, would be to check an environment variable, e.g. `ENV['IGNORE']`, and then you could call `IGNORE=1 git commit ...`. – Jordan Running Dec 07 '15 at 18:03
  • As a coding tip, don't use globals like `$regex`. That hints very strongly that you don't understand variable scope or the problems that can occur when using global variables. A constant would be more acceptable. – the Tin Man Dec 07 '15 at 19:24

0 Answers0