Last Updated: February 25, 2016
·
1.632K
· rogerbraun

Add Pivotal Tracker ticket ID to git commit based on branch name

Put this in .git/hooks/prepare_commit_msg and make it executable. Now, when you commit without the -m option, the ticket id will be extracted from your branch name and added to the commit message. Your branch name has to end with pt_TICKET_ID. Example: comment_streams_pt_4354546.

#!/usr/bin/env ruby

commit_msg = File.read(ARGV[0])
branch = `git rev-parse --abbrev-ref HEAD`

# Adds the ticket id if the branch is named something like this:
# branch_name_pt_4354363
if branch[/pt_\d+$/]
  ticket_id = branch[/\d+$/]
  new_commit_msg = "Pivotal Tracker: [##{ticket_id}]\n" + commit_msg
  File.write(ARGV[0], new_commit_msg)
end