Last active
May 17, 2016 12:53
-
-
Save Lacrymology/c72cf9ffc3d9b3692522343de3b3b418 to your computer and use it in GitHub Desktop.
git hook to prefix commit messages with ticket number
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. If your branch name is in the form "#{prefix}-123", automatically | |
# adds "#{prefix}-123:" to commit messages unless they mention "#{orefix}-123" already. | |
# Include "#noref" in the message to avoid this behavior for the current commit. | |
# The "#noref" will be stripped from the commit message. | |
# | |
# Modified from script Henrik Nyh <http://henrik.nyh.se> 2009-09-10 under the MIT License. | |
# | |
# | |
# Install: | |
# | |
# cd your_project | |
# curl https://gist.githubusercontent.com/Lacrymology/c72cf9ffc3d9b3692522343de3b3b418/raw/472bf7b52b1346a6fdde5d761cf06ac7cb96f08c/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 https://gist.githubusercontent.com/Lacrymology/c72cf9ffc3d9b3692522343de3b3b418/raw/472bf7b52b1346a6fdde5d761cf06ac7cb96f08c/commit-msg -o ~/.githooks/commit-msg && chmod u+x ~/.githooks/commit-msg | |
# cd your_project | |
# ln -s ~/.githooks/commit-msg .git/hooks | |
prefix = 'DAM' | |
branchname = `git branch --no-color 2> /dev/null`[/^\* (.+)/, 1] | |
ticket_number = branchname.to_s.match(/\A#{prefix}-(\d+)/) && $1 | |
exit unless ticket_number | |
message_file = ARGV[0] | |
message = File.read(message_file) | |
exit if message.include?("#{prefix}-#{ticket_number}") | |
message.strip! | |
if message.include?("#noref") | |
message.sub!(/^\s*#noref\s*|\s*#noref/, '') | |
else | |
message.sub!(/([^[:punct:]])\z/, "\\1.") # Add trailing period if missing. | |
message = ["#{prefix}-#{ticket_number}", message,].join(": ") | |
puts 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