#!/usr/bin/env ruby

# Configures the git author to a list of developers when pair programming,
# alphabetized on first name
# 
# Usage: git pair ls gw (Sets the repo specific author to 'Greg Woodward & Loren Siebert')
#        git pair       (Unsets the repo specific author so the git global config takes effect)
#        git pair --global ls gw (Sets the global author to 'Greg Woodward & Loren Siebert')
#        git pair --global (Unsets the global author)
#
# Orginal Author: Bryan Helmkamp (http://brynary.com)

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

require 'yaml'

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

## Configuration

def get_pairs_config
  pairs_file_path = nil
  candidate_file_path = '.pairs'
  until pairs_file_path || File.expand_path(candidate_file_path) == '/.pairs' do
    if File.exists?(candidate_file_path)
      pairs_file_path = candidate_file_path
    else
      candidate_file_path = File.join("..", candidate_file_path)
    end
  end
  unless pairs_file_path
    puts <<-INSTRUCTIONS
Could not find a .pairs file. Create a YAML file in your project or home directory.
Format: <initials>: <name>[; <email>]
Example:
# .pairs - configuration for 'git pair'
# place in project or home directory
pairs:
  eh: Edward Hieatt
  js: Josh Susser; jsusser
  sf: Serguei Filimonov; serguei
email:
  prefix: pair
  domain: pivotallabs.com
INSTRUCTIONS
    exit(1)
  end
  pairs_file_path ? YAML.load_file(pairs_file_path) : {}
end

def get_global_config(config)
  if ARGV.delete("--global") || config["global"]
    "--global"
  end
end

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

config               = get_pairs_config
global_config_string = get_global_config(config)
authors              = ARGV.map do |initials|
  if full_name = config['pairs'][initials.downcase]
    full_name
  else
    puts "Couldn't find author name for initials: #{initials}. Add this person to the .pairs file in your project or home directory."
    exit 1
  end
end

if authors.any?
  authors.sort!.uniq!
  names, emails = authors.collect do |a|
    both = a.split(";").collect {|s| s.strip}
    both << both[0].split(' ').first.downcase if both.length == 1  # default email to first name
    both
  end.transpose

  case authors.size
  when 1
    authors = names.first
  when 2
    authors = names.join(" & ")
  else
    authors = names[0..-2].join(", ") + " & " + names.last
  end

  email = "#{([config['email']['prefix']] + emails).compact.join('+')}@#{config['email']['domain']}"
  system(%Q{git config #{global_config_string} user.name "#{authors}"})
  system(%Q{git config #{global_config_string} user.email "#{email}"})
else
  system("git config #{global_config_string} --unset user.name")
  system("git config #{global_config_string} --unset user.email")
  puts "Unset #{global_config_string} user.name and user.email"
end

global_name_setting = `git config --global --get-regexp '^user\.name'`
local_name_setting = `git config -f .git/config --get-regexp '^user\.name'`
if global_name_setting.length > 0 && local_name_setting.length > 0
  puts "NOTE: Overriding global user.name setting with local."
end
puts "global: #{global_name_setting}" if global_name_setting.length > 0
puts "local:  #{local_name_setting}"  if local_name_setting.length > 0


global_email_setting = `git config --global --get-regexp '^user\.email'`
local_email_setting = `git config -f .git/config --get-regexp '^user\.email'`
if global_email_setting.length > 0 && local_email_setting.length > 0
  puts "NOTE: Overriding global user.email setting with local."
end
puts "global: #{global_email_setting}" if global_email_setting.length > 0
puts "local:  #{local_email_setting}"  if local_email_setting.length > 0
