Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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
# The pattern we are looking
# for into the commit / branchname
$pattern = /web-/i # insensitive
# Loading the commit file
message_file = ARGV[0]
message =
# 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)
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
return false
# 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, '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
puts red("!!! Your commit was rejected !!!")
puts red("I could not find any issue # in your commit / branch. Please provide one.")
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.