Skip to content

Instantly share code, notes, and snippets.

@bcardarella
Created February 6, 2009 15:27
Show Gist options
  • Save bcardarella/59440 to your computer and use it in GitHub Desktop.
Save bcardarella/59440 to your computer and use it in GitHub Desktop.
Commit message rewriter for branches to match Trac tickets
#!/usr/bin/env ruby
#
# Copy this file into .git/hooks and chmod +x
# Acceptable branch formats:
# ticket_1234 -> Refs #1234: your message
# ticket_1234_some_feature -> Refs #1234 - Some Feature: your message
# This should be easier for committing to TRAC against SVN
require 'rubygems'
require 'git'
TICKET_MATCH = /ticket_[\d]+/i
repo = Git.open(".")
File.open(ARGV[0], "r+") do |file|
if ticket = repo.current_branch.match(TICKET_MATCH)
commit_title = repo.current_branch.split(TICKET_MATCH)[1].split("_").delete_if { |e| e.empty? }.each { |word| word.capitalize! }.join(" ") rescue nil
message = file.read
file.rewind
file.puts "Refs ##{ticket[0].split("_")[1]}#{commit_title ? " - #{commit_title}" : ""}: #{message}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment