Skip to content

Instantly share code, notes, and snippets.

@queso
Created September 28, 2009 18:07
Show Gist options
  • Save queso/195627 to your computer and use it in GitHub Desktop.
Save queso/195627 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# Git commit-msg hook. If your branch name is in the form "t123", automatically
# adds "Refs #123." to commit messages unless they mention "#123" already.
#
# By Henrik Nyh <http://henrik.nyh.se> 2009-09-10 under the MIT License.
#
#
# Install:
#
# cd your_project
# curl http://gist.github.com/raw/184711/b8ce3b0192c74fc33a9b796907a5d0bb46087619/commit-msg -o .git/hooks/commit-msg && chmod u+x .git/hooks/commit-msg
#
# Or store it centrally and symlink in your projects:
#
# curl --create-dirs http://gist.github.com/raw/184711/b8ce3b0192c74fc33a9b796907a5d0bb46087619/commit-msg -o ~/.githooks/commit-msg && chmod u+x ~/.githooks/commit-msg
# cd your_project
# ln -s ~/.githooks/commit-msg .git/hooks
repo_name = `git config -f .git/config --get repo.name 2> /dev/null`.strip
branchname = `git branch --no-color 2> /dev/null`[/^\* (.+)/, 1]
ticket_number = branchname.to_s.match(/\At(\d+)/) && $1
message_file = ARGV[0]
message = File.read(message_file)
message.strip!
message.sub!(/([^[:punct:]])\z/, "\\1.") # Add trailing period if missing.
new_message = []
new_message << "[#{repo_name}]" unless repo_name.nil?
new_message << message
new_message << "Refs ##{ticket_number}" unless ticket_number.nil? || message.include?("##{ticket_number}")
File.open(message_file, 'w') {|f| f.write new_message.join(" ") }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment