Setting the Commit Author to Pair Programmers’ Names in Git

[Authored by Sam]

In beezwax’s webdev division we generally work in pairs, but our commit logs didn’t used to show this. We wouldn’t bother to reconfigure the git author every time we sat down with a new pair so our git log only recorded one of the programmers’ names. Bryan Helmcamp has a nice script for setting your git commit author in pair programming situations. Here’s another one which works interactively.

Interactively set the git commit author to pair programmers’ names:

#!/usr/bin/env ruby
#
# Git pre-commit hook:
# Install at .git/hooks/pre-commit and set as executable
#
# Interactively sets git commit author for pair programming
#
###################################################
# Configuration

me = "Sam Goldstein"
my_email = 'sam_g@beezwax.net'
pair_email = 'webdev@beezwax.net'

developers = [
  "Ian Smith-Heisters",
  "Jesse Sanford",
  "Noah Thorp"
]

###################################################

puts "Pairing? (enter number):"
developers.each_with_index do |name, index|
  puts "  #{index + 1}: #{name}"
end

choice = File.new("/dev/tty").readline.chomp
unless choice =~ /Ad*Z/
  puts "Bad input `#{choice}'"
  exit 1
end

commit_name = me
commit_name += ' & ' + developers[choice.to_i - 1] unless choice == ''
commit_email = choice == '' ? my_email : pair_email

puts "...setting *commit author* to `#{commit_name}'"
puts "...setting *commit email* to `#{commit_email}'"

`git config user.name '#{commit_name}'`
`git config user.email '#{commit_email}'`

Now when you’re ready to commit you’ll be asked if this is a paired programming session.

$ git commit
Pairing? (enter number):
  1: Ian Smith-Heisters
  2: Jesse Sanford
  3: Noah Thorp
1
...setting *commit author* to `Sam Goldstein & Ian Smith-Heisters'
...setting *commit email* to `webdev@beezwax.net'

Hitting return will run the commit as you alone, but if you enter a number your commits can look like this:

commit c0228797a81efb6e89a61f5c5e96856ff527e217
Author: Sam Goldstein & Ian Smith-Heisters <webdev@beezwax.net>

It requires no extra effort (ok 1 keystroke), and works great if you don’t want to run a bunch of git config commands each time you’re ready to commit.

Leave a Reply