midwire
7/10/2014 - 1:09 PM

Create a commit message, with link to a Pivotal story, based on the branch name and put it all on the clipboard.

Create a commit message, with link to a Pivotal story, based on the branch name and put it all on the clipboard.

#!/usr/bin/env ruby
require 'pry-nav'

def pbcopy(input)
  str = input.to_s
  IO.popen('pbcopy', 'w') { |f| f << str }
  str
end

branch_name = `git rev-parse --abbrev-ref HEAD`.chomp

DIVIDERS = %r{[-|_|/|\/]}

parts = branch_name.match(/(?<initials>.+)#{DIVIDERS}(?<number>\d{5,})#{DIVIDERS}(?<description>.*)/)

if parts.nil?
  # Initials missing?
  parts = branch_name.match(/(?<number>\d{5,})#{DIVIDERS}(?<description>.*)/)
  INITIALS = 'CB'
end

begin
  INITIALS       ||= parts[:initials].split(DIVIDERS).join("/").upcase
  PIVOTAL_NUMBER = parts[:number]
  DESCRIPTION    = parts[:description].gsub(/#{DIVIDERS}/, ' ')
rescue
  exit(0)
end

commit_message = <<MSG
[#{INITIALS}][#{PIVOTAL_NUMBER}] #{DESCRIPTION}

Pivotal Story: https://www.pivotaltracker.com/story/show/#{PIVOTAL_NUMBER}
MSG

STDOUT << commit_message
pbcopy(commit_message)
puts(">>> Commit message is on the clipboard!")