Skip to content

Instantly share code, notes, and snippets.

@curiouslychase
Forked from anonymous/commit-msg
Created August 13, 2013 20:35
Show Gist options
  • Save curiouslychase/6225416 to your computer and use it in GitHub Desktop.
Save curiouslychase/6225416 to your computer and use it in GitHub Desktop.
Place this in the following path relative to your git repo's root: .git/hooks/commit-msg
#!/usr/bin/env ruby
# Hook into the message and append the branch name so that we don't have to
# manually do it ourselves!
# This is the message that you put when you do:
# `git commit -m "This is my message"
message_file = ARGV[0]
message = File.read(message_file).strip
branch_name = `git rev-parse --abbrev-ref HEAD`.strip
# We don't want to do this if we're on master, that'd be weird!
if branch_name != "master"
# Checks if the branch name is already in the commit message
unless message.include?branch_name
prepend = "[#{branch_name}] "
message = "#{prepend} #{message}"
end
puts message
# Rewrite the commit message with the branch name in front.
File.open(message_file, "w") { |f|
f.write message
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment