As a beginner in Ruby, I am trying to optimize this script that is a client side pre-commit hook for git.
Was hoping I could get a review to ensure I am following all ruby idioms, and to ensure I have the most elegant solution.
#!/usr/bin/env ruby
#
# Pre-commit hook created to help ensure dns changes are being
# committed to the correct branch.
#
# Hash of zone files to look for when a commit is attempted.
zone_files = {
development: ‘some_zone_file1’,
prod: ‘some_zone_file2’
}
# Execute git diff to determine files includes as part of commit.
out_data = %x{git diff --cached --name-only}
pri_branch = "master"
# Iterate through the zone files, and check if any are found in the output.
# Only one needs to be found. No need to find all.
zone_files.keys.each do |key|
if out_data.include?zone_files[key]
current_branch = %x{git branch}.match(/\* (.+?)\n/m)
if current_branch[1].include?pri_branch
break
else
puts "Warning: You have triggered a git pre-commit hook."
puts "Warning: Your current branch is: #{current_branch[1]}."
puts "Warning: For dns, ensure you commit on branch: #{pri_branch}"
puts "Warning: Include '-n' to bypass this check."
puts "Exiting..."
exit 1
end
end
end