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

Commit 2390a1a

Browse files
author
Michael Grosser
committed
refactor git pairs
1 parent 59ff984 commit 2390a1a

File tree

2 files changed

+72
-75
lines changed

2 files changed

+72
-75
lines changed

bin/git-pair

Lines changed: 69 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
require 'yaml'
33
require 'optparse'
44

5-
git_dir = `git rev-parse --git-dir`.chomp
6-
exit 1 unless File.exists?(git_dir)
7-
8-
@options = {}
9-
OptionParser.new do |opts|
10-
# copy-paste from readme
11-
opts.banner = <<BANNER.sub('<br/>','')
5+
def parse_cli_options(argv)
6+
options = {}
7+
OptionParser.new do |opts|
8+
# copy-paste from readme
9+
opts.banner = <<BANNER.sub('<br/>','')
1210
Configures git authors when pair programming.
1311
1412
git pair sp js
@@ -35,19 +33,20 @@ Use the `--global` option or add `global: true` to your `.pairs` file to set the
3533
3634
Options are:
3735
BANNER
38-
opts.on("-g", "--global", "Modify global git options instead of local") { @options[:global] = true }
39-
opts.on("-v", "--version", "Show Version") do
40-
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
41-
require "pivotal_git_scripts/version"
42-
puts PivotalGitScripts::VERSION
43-
exit
44-
end
45-
opts.on("-h", "--help", "Show this.") { puts opts; exit }
46-
end.parse!(ARGV)
36+
opts.on("-g", "--global", "Modify global git options instead of local") { options[:global] = true }
37+
opts.on("-v", "--version", "Show Version") do
38+
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
39+
require "pivotal_git_scripts/version"
40+
puts PivotalGitScripts::VERSION
41+
exit
42+
end
43+
opts.on("-h", "--help", "Show this.") { puts opts; exit }
44+
end.parse!(argv)
4745

48-
## Configuration
46+
options
47+
end
4948

50-
def get_pairs_config
49+
def read_pairs_config
5150
pairs_file_path = nil
5251
candidate_file_path = '.pairs'
5352
until pairs_file_path || File.expand_path(candidate_file_path) == '/.pairs' do
@@ -78,74 +77,72 @@ INSTRUCTIONS
7877
pairs_file_path ? YAML.load_file(pairs_file_path) : {}
7978
end
8079

81-
def get_global_config(config)
82-
if @options[:global] || config["global"]
83-
"--global"
80+
def read_author_info_from_config(config, initials)
81+
initials.map do |initials|
82+
if full_name = config['pairs'][initials.downcase]
83+
full_name
84+
else
85+
puts "Couldn't find author name for initials: #{initials}. Add this person to the .pairs file in your project or home directory."
86+
exit 1
87+
end
8488
end
8589
end
8690

87-
## End of configuration
88-
#######################################################################
89-
90-
config = get_pairs_config
91-
global_config_string = get_global_config(config)
92-
authors = ARGV.map do |initials|
93-
if full_name = config['pairs'][initials.downcase]
94-
full_name
91+
def build_email(emails, config)
92+
if config.is_a?(Hash)
93+
"#{([config['prefix']] + emails).compact.join('+')}@#{config['domain']}"
9594
else
96-
puts "Couldn't find author name for initials: #{initials}. Add this person to the .pairs file in your project or home directory."
97-
exit 1
95+
config
96+
end
97+
end
98+
99+
def set_git_config(global_config_string, options)
100+
options.each do |key,value|
101+
key = "user.#{key}"
102+
value = value ? %Q{#{key} "#{value}"} : "--unset #{key}"
103+
system(%Q{git config#{global_config_string} #{value}})
98104
end
99105
end
100106

101-
if authors.any?
107+
def report_git_settings(git_dir, key)
108+
global = `git config --global --get-regexp '^user\.#{key}'`
109+
local = `git config -f #{git_dir}/config --get-regexp '^user\.#{key}'`
110+
if global.length > 0 && local.length > 0
111+
puts "NOTE: Overriding global user.#{key} setting with local."
112+
end
113+
puts "global: #{global}" if global.length > 0
114+
puts "local: #{local}" if local.length > 0
115+
end
116+
117+
def extract_author_names_and_email_ids_from_config(config, initials)
118+
authors = read_author_info_from_config(config, initials)
102119
authors.sort!.uniq!
103-
names, emails = authors.collect do |a|
104-
both = a.split(";").collect {|s| s.strip}
105-
both << both[0].split(' ').first.downcase if both.length == 1 # default email to first name
106-
both
120+
authors.map do |a|
121+
full_name, email_id = a.split(";").map(&:strip)
122+
email_id ||= full_name.split(' ').first.downcase
123+
[full_name, email_id]
107124
end.transpose
125+
end
108126

109-
case authors.size
110-
when 1
111-
authors = names.first
112-
when 2
113-
authors = names.join(" & ")
114-
else
115-
authors = names[0..-2].join(", ") + " & " + names.last
116-
end
127+
git_dir = `git rev-parse --git-dir`.chomp
128+
exit 1 if git_dir.empty?
117129

118-
email = if config['email'].is_a?(Hash)
119-
"#{([config['email']['prefix']] + emails).compact.join('+')}@#{config['email']['domain']}"
120-
else
121-
config['email']
122-
end
130+
options = parse_cli_options(ARGV)
131+
initials = ARGV
132+
config = read_pairs_config
133+
global = " --global" if options[:global] or config["global"]
123134

124-
initials = ARGV.join(' ')
135+
if initials.any?
136+
author_names, email_ids = extract_author_names_and_email_ids_from_config(config, initials)
137+
authors = [author_names[0..-2].join(", "), author_names.last].reject(&:empty?).join(" & ")
138+
email = build_email(email_ids, config["email"])
125139

126-
system(%Q{git config #{global_config_string} user.name "#{authors}"})
127-
system(%Q{git config #{global_config_string} user.email "#{email}"})
128-
system(%Q{git config #{global_config_string} user.initials "#{initials}"})
140+
set_git_config global, :name => authors, :email => email, :initials => initials.join(" ")
129141
else
130-
system("git config #{global_config_string} --unset user.name")
131-
system("git config #{global_config_string} --unset user.email")
132-
system("git config #{global_config_string} --unset user.initials")
133-
puts "Unset #{global_config_string} user.name and user.email"
142+
set_git_config global, :name => nil, :email => nil, :initials => nil
143+
puts "Unset#{global} user.name, user.email, user.initials"
134144
end
135145

136-
global_name_setting = `git config --global --get-regexp '^user\.name'`
137-
local_name_setting = `git config -f #{git_dir}/config --get-regexp '^user\.name'`
138-
if global_name_setting.length > 0 && local_name_setting.length > 0
139-
puts "NOTE: Overriding global user.name setting with local."
140-
end
141-
puts "global: #{global_name_setting}" if global_name_setting.length > 0
142-
puts "local: #{local_name_setting}" if local_name_setting.length > 0
143-
144-
145-
global_email_setting = `git config --global --get-regexp '^user\.email'`
146-
local_email_setting = `git config -f #{git_dir}/config --get-regexp '^user\.email'`
147-
if global_email_setting.length > 0 && local_email_setting.length > 0
148-
puts "NOTE: Overriding global user.email setting with local."
146+
[:name, :email, :initials].each do |key|
147+
report_git_settings(git_dir, key)
149148
end
150-
puts "global: #{global_email_setting}" if global_email_setting.length > 0
151-
puts "local: #{local_email_setting}" if local_email_setting.length > 0

spec/cli_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ def expect_config(result, name, initials, email, options={})
7878

7979
prefix = (options[:global] ? "global: " : "local: ")
8080
result.should include "#{prefix}user.name #{name}"
81-
#result.should include "#{prefix}user.initials #{initials}" # TODO
81+
result.should include "#{prefix}user.initials #{initials}"
8282
result.should include "#{prefix}user.email #{email}"
8383
end
8484

8585
it "prints help" do
8686
result = run "git-pair --help"
87-
result.should include("Configures the git author")
87+
result.should include("Configures git authors when pair programming")
8888
end
8989

9090
it "prints version" do
@@ -166,7 +166,7 @@ def expect_config(result, name, initials, email, options={})
166166
run "git pair ab --global"
167167
run "git pair bc"
168168
result = run "git pair"
169-
#result.should include "Unset user.name, user.email and user.initials" #TODO
169+
result.should include "Unset user.name, user.email, user.initials"
170170
expect_config result, "Aa Bb", "ab", "[email protected]", :global => true
171171
result.should_not include("local:")
172172
end

0 commit comments

Comments
 (0)