Skip to content
This repository has been archived by the owner on Mar 31, 2022. It is now read-only.

Commit

Permalink
Add ability to use a custom email address per user
Browse files Browse the repository at this point in the history
  • Loading branch information
zrob committed Jul 19, 2014
1 parent 98ed480 commit 3668fc7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
pivotal_git_scripts (1.2.0)
pivotal_git_scripts (1.3.0)

GEM
remote: http://rubygems.org/
Expand Down
40 changes: 35 additions & 5 deletions lib/pivotal_git_scripts/git_pair.rb
Expand Up @@ -58,9 +58,10 @@ def commit(argv)
end

config = read_pairs_config
author_names, email_ids = extract_author_names_and_email_ids_from_config(config, current_pair_initials)
author_details = extract_author_details_from_config(config, current_pair_initials)
author_names = author_details.keys.map { |i| author_details[i][:name] }
authors = pair_names(author_names)
author_email = random_author_email(email_ids, config['email'])
author_email = random_author_email(author_details)
puts "Committing under #{author_email}"
passthrough_args = argv.map{|arg| "'#{arg}'"}.join(' ')
env_variables = "GIT_AUTHOR_NAME='#{authors}' GIT_AUTHOR_EMAIL='#{author_email}' GIT_COMMITTER_NAME='#{authors}' GIT_COMMITTER_EMAIL='#{author_email}'"
Expand Down Expand Up @@ -103,6 +104,9 @@ def parse_cli_options(argv)
domain: pivotallabs.com
# no_solo_prefix: true
#global: true
# include the following section to set custom email addresses for users
#email_addresses:
# zr: zach.robinson@example.com
By default this affects the current project (.git/config).<br/>
Expand Down Expand Up @@ -168,9 +172,9 @@ def build_email(emails, config)
end
end

def random_author_email(email_ids, config)
author_id = email_ids.sample
"#{author_id}@#{config['domain']}"
def random_author_email(author_details)
author_id = author_details.keys.sample
author_details[author_id][:email]
end

def set_git_config(global, options)
Expand Down Expand Up @@ -205,6 +209,32 @@ def no_email(config)
!config.key? 'email'
end

def extract_author_details_from_config(config, initials)
details = {}

initials.each do |i|
info = read_author_info_from_config(config, [i]).first

full_name, email_id = info.split(";").map(&:strip)
email_id ||= full_name.split(' ').first.downcase

email = read_custom_email_address_from_config(config, i)
email ||= "#{email_id}@#{config['email']['domain']}"

details[i] = {
:name => full_name,
:email => email
}
end

details
end

def read_custom_email_address_from_config(config, initial)
return nil unless config['email_addresses']
return config['email_addresses'][initial.downcase]
end

private

def pair_names(author_names)
Expand Down
2 changes: 1 addition & 1 deletion lib/pivotal_git_scripts/version.rb
@@ -1,3 +1,3 @@
module PivotalGitScripts
VERSION = "1.2.0"
VERSION = "1.3.0"
end
17 changes: 17 additions & 0 deletions spec/cli_spec.rb
Expand Up @@ -291,6 +291,9 @@ def git_config_value(name, global = false)
email:
prefix: the-pair
domain: the-host.com
email_addresses:
bc: test@other-host.com
YAML
end

Expand Down Expand Up @@ -377,6 +380,20 @@ def committer_email_of_last_commit
%w(abb@the-host.com cdd@the-host.com).should include(committer_email_of_last_commit)
end
end

context 'when one of the pair has a custom email address' do
before do
run 'git pair ab bc'
end

it 'uses that email address' do
emails = 6.times.map do
git_pair_commit
author_email_of_last_commit
end.uniq
emails.should =~ ['abb@the-host.com', 'test@other-host.com']
end
end
end

context 'when no pair has been set' do
Expand Down

0 comments on commit 3668fc7

Please sign in to comment.