Last active
October 2, 2015 04:37
-
-
Save dulyanov/2173284 to your computer and use it in GitHub Desktop.
Prepending branch name and task number before commit in GIT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 and give it executable permissions | |
# cd .git/hooks | |
# wget https://gist.github.com/dulyanov/2173284/raw/prepare-commit-msg | |
# chmod 755 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 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment