Skip to content

Instantly share code, notes, and snippets.

@pjmartorell
Created July 21, 2014 10:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjmartorell/6456a5ffa7f7ff9d3195 to your computer and use it in GitHub Desktop.
Save pjmartorell/6456a5ffa7f7ff9d3195 to your computer and use it in GitHub Desktop.
Prepend automatically a reference to the issue number to commit tasks
#!/usr/bin/env ruby
# Git commit-msg hook. Prepend a reference to the issue number of the branch name to commit tasks.
# If your branch name is in the numeric form "issue_12345", also prepend "refs #12345" to commit messages,
# unless "refs #12345" is manually provided inline.
#
# Place code into .git/hooks/prepare-commit-msg and give it executable permissions
# cd .git/hooks
# wget https://gist.github.com/pjmartorell/6456a5ffa7f7ff9d3195
# chmod 755 prepare-commit-msg
branch = `git branch`.scan(/\* (.*)/).flatten[0]
exit 0 if branch == "master"
issue_no = branch.gsub(/[^\d]/, '')
exit 0 if issue_no.nil?
message_file = ARGV[0]
message = File.read(message_file).strip
message = "refs ##{issue_no} " + message unless message.match("refs #")
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