#!/usr/bin/env ruby # # Git commit-msg hook. Prepend branch name to commit tasks. # If your branch name is in the numeric form "12345", also prepend "(Task 123)" to commit messages, # unless #notask is included in the message (#notask will be stripped). # # Based on git://gist.github.com/750755.git # # Place code into .git/hooks/prepare-commit-msg # chmod 755 .git/hooks/prepare-commit-msg branchname = `git branch --no-color 2> /dev/null`[/^\* (.+)/, 1].strip message_file = ARGV[0] message = File.read(message_file).strip prepend = "[#{branchname}] " # When task branch name is numeric and not a merge, use it for task number if branchname =~ /\d+/ && (message =~ /Merge branch.*into #{branchname}/).nil? task_number = branchname.gsub(/[^\d]/, '') prepend += "(Task #{task_number}) " end # When message is blank and starts with comments, add a line break prepend += "\n" if message.chars.first == "#" if message.include?("#notask") message.sub!(/^\s*#notask\s*|\s*#notask/, '') else message = prepend + message end File.open(message_file, 'w') {|f| f.write message }