Skip to content

Instantly share code, notes, and snippets.

@ngarneau
Created May 15, 2012 22:30
Show Gist options
  • Save ngarneau/2705633 to your computer and use it in GitHub Desktop.
Save ngarneau/2705633 to your computer and use it in GitHub Desktop.
Checks if your commit / branch name as a specific pattern
#!/usr/bin/env ruby
# Get the commit type.
# We only want this hook to take effect on message commit.
commit_type = ARGV[1]
if commit_type != "message"
exit 0
end
# The pattern we are looking
# for into the commit / branchname
$pattern = /web-/i # insensitive
# Loading the commit file
message_file = ARGV[0]
message = File.read(message_file).strip
# Finding the actual branchname
branchname = `git branch --no-color 2> /dev/null`[/^\* (.+)/, 1].to_s
# Colorizing, for a pretty output
def colorize(text, color_code)
"\e[#{color_code}m#{text}\e[0m"
end
def red(text); colorize(text, 31); end
def green(text); colorize(text, 32); end
def yellow(text); colorize(text, 33); end
# Finding the pattern within a string
def findTicket string
if m = string.match($pattern)
return true
else
return false
end
end
# If we find the issue number in the
# commit, we accept it
if findTicket message
puts green("Thanks for putting your issue number into the commit :)")
exit 0
# We could not find the issue number
# so we check for the branchname
elsif findTicket branchname
message = branchname + " :: " + message
File.open(message_file, 'w') {|f| f.write message }
puts yellow("We found the issue '#{branchname}' within your branch name and was added automatically to your commit.")
exit 0
# If none of the above matches
# We reject the commit
else
puts red("!!! Your commit was rejected !!!")
puts red("I could not find any issue # in your commit / branch. Please provide one.")
exit 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment