dotfiles/dot_config/git/hooks/executable_prepare-commit-msg

33 lines
828 B
Ruby

#!/usr/bin/env ruby
abort "No tempfile given" if ARGV.empty?
commit_file = ARGV[0]
commit_msg = File.read(commit_file)
if commit_msg.nil?
$stderr.puts "Nothing given, aborting"
exit 0
end
unless commit_msg.lines.first.strip.empty?
$stderr.puts "Commit message not empty, aborting"
exit 0
end
jira_match = commit_msg.match(%r{# On branch (?<ticket>[A-Z]{1,10}-\d+)})
gitlab_match = commit_msg.match(%r{# On branch (?<ticket>\d+)-.*+})
if jira_match && branch_match["ticket"]
ticket = jira_match["ticket"]
new_commit = "#{ticket}: \n" + commit_msg
elsif gitlab_match && gitlab_match["ticket"]
ticket = gitlab_match["ticket"]
new_commit = "\n# Refs: ##{ticket}\n" + commit_msg
else
$stderr.puts "Could not extract a ticket from the commit message"
exit
end
File.write(commit_file, new_commit, mode: "w")