-
-
Save ashishtajane/e5735c247ffe92705205 to your computer and use it in GitHub Desktop.
commit-msg hook to add a prefix to commit messages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Adapted from | |
# https://gist.github.com/EmmanuelOga/2926764 | |
# Convert a message to include branch name information. | |
class Transmogrifier < Struct.new(:message, :branchname) | |
NOREF_MATCHER = /#noref/ | |
BRANCHES_TO_SKIP = ['master', 'develop', 'staging'] | |
PREFIX_MATCHER = /\A([a-zA-Z]+[-_]\d+)[-_]/ | |
PREFIX_FORMAT = "[%s] %s" | |
def prefix | |
(branchname.to_s[PREFIX_MATCHER, 1] || "").split(/[_-]/).join("-").upcase.strip | |
end | |
def to_s | |
return message if BRANCHES_TO_SKIP.include? branchname | |
return message unless prefix =~ /\S/ | |
if message =~ NOREF_MATCHER | |
output = message.gsub(NOREF_MATCHER, "") | |
elsif message.include?(prefix) | |
output = message | |
else | |
output = PREFIX_FORMAT % [prefix, message] | |
end | |
output.squeeze(" ").strip | |
end | |
end | |
# Overwrites the file which holds the commit message with a fancier one. | |
def run! | |
branchname = `git branch --no-color 2> /dev/null`[/^\* (.+)/, 1].to_s | |
message_path = ARGV.first | |
message = File.read(message_path).strip.chomp | |
File.open(message_path, 'w') {|f| f.write Transmogrifier.new(message, branchname) } | |
end | |
run! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment