Archive for September 2008

Setting the git commit author to pair programmers' names

Are you using git? Are you writing all your code that needs to be maintained in pairs? Great. Your commits probably look something like this:

commit a5ab4d39be608280fbea6ba9710aea4593f20bc6
Author: Bryan Helmkamp <bryan@brynary.com>
Date:   Fri Aug 29 18:47:29 2008 -0400

    Some important refactoring LM/BH

Subversion has limited support for tracking authors, so we used to manually add the initials of the programmers working on a changeset for future reference. With git, however, we can specify arbitrary author names for each commit. Now our commits look like this:

commit a5ab4d39be608280fbea6ba9710aea4593f20bc6
Author: Luke Melia and Bryan Helmkamp <developers@weplay.com>
Date:   Fri Aug 29 18:47:29 2008 -0400

    Some important refactoring

To simplify updating the git author configuration as we switch pairs, I whipped up a quick Ruby shell script called pair. Called with a list of developers' initials, it configures the git author name and email fields. Called with no arguments, it unsets the per-project git author configuration so your global settings take effect.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env ruby

# Configures the git author to a list of developers when pair programming
# 
# Usage: pair lm bh (Sets the author to 'Luke Melia and Bryan Helmkamp')
#        pair       (Unsets the author so the git global config takes effect)
# 
# Author: Bryan Helmkamp (http://brynary.com)

#######################################################################
## Configuration

PAIR_EMAIL = "developers@weplay.com"

AUTHORS = {
  "bh" => "Bryan Helmkamp",
  "lm" => "Luke Melia"
  # ...
}

## End of configuration
#######################################################################

unless File.exists?(".git")
  puts "This doesn't look like a git repository."
  exit 1
end

authors = ARGV.map do |initials|
  if AUTHORS[initials.downcase]
    AUTHORS[initials.downcase]
  else
    puts "Couldn't find author name for initials: #{initials}"
    exit 1
  end
end

if authors.any?
  if authors.size == 1
    authors = authors.first
  elsif authors.size == 2
    authors = authors.join(" and ")
  else
    authors = authors[0..-2].join(", ") + " and " + authors.last
  end
  
  `git config user.name '#{authors}'`
  `git config user.email '#{PAIR_EMAIL}'`
  
  puts "user.name = #{authors}"
  puts "user.email = #{PAIR_EMAIL}"
else
  `git config --unset user.name`
  `git config --unset user.email`
  
  puts "Unset user.name and user.email"
end

Note: If you're using GitHub, it's important to choose a PAIR_EMAIL value that does not correspond to a GitHub account. Otherwise, GitHub will resolve the email address to a GitHub user and display their username instead of the value of the author.name. We just made one up.