Skip to content

Instantly share code, notes, and snippets.

@sbrauer
Last active December 22, 2015 09:49
Show Gist options
  • Save sbrauer/6454801 to your computer and use it in GitHub Desktop.
Save sbrauer/6454801 to your computer and use it in GitHub Desktop.
git commit wrapper script
#!/usr/bin/env ruby
# A wrapper around "git commit" that provides a template message
# conforming to RentPath conventions.
# Assumes branches are named with the following components separated
# by underscores:
# 1. developer initials (one or more of these components are allowed)
# 2. story number
# 3. free text description (optional; may contain underscores)
# Examples:
# "sb_12345_fix_serious_bug"
# "sb_zh_67890" (two devs, no optional description)
require 'tempfile'
branch_name = `git rev-parse --abbrev-ref HEAD`.chomp
abort if branch_name == ''
initials = 'XX'
number = '00000'
components = branch_name.split('_')
number_idx = components.find_index { |x| x.match /\d/ }
if number_idx
number = components[number_idx]
initials = components[0...number_idx].join('/').upcase
end
template_string = "[#{initials}][#{number}] "
file = Tempfile.new('commit-template')
file.write(template_string)
file.close
begin
cmd = "git commit --template=#{file.path} #{ARGV.join(' ')}"
system(cmd)
ensure
file.unlink
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment