This repository was archived by the owner on Mar 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathgit_pair.rb
More file actions
271 lines (225 loc) · 8.94 KB
/
git_pair.rb
File metadata and controls
271 lines (225 loc) · 8.94 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
require "pivotal_git_scripts/version"
require 'yaml'
require 'optparse'
require 'pathname'
module PivotalGitScripts
module GitPair
def self.main(argv)
runner = Runner.new
runner.main(argv)
end
def self.commit(argv)
runner = Runner.new
runner.commit(argv)
end
class GitPairException < Exception; end
class Runner
def main(argv)
git_dir = `git rev-parse --git-dir`.chomp
exit 1 if git_dir.empty?
options = parse_cli_options(argv)
initials = argv
config = read_pairs_config
global = !!(options[:global] || config["global"])
if initials.any?
author_names, email_ids = extract_author_names_and_email_ids_from_config(config, initials)
authors = pair_names(author_names)
git_config = {:name => authors, :initials => initials.sort.join(" ")}
git_config[:email] = build_email(email_ids, config["email"]) unless no_email(config)
set_git_config global, git_config
else
git_config = {:name => nil, :initials => nil}
git_config[:email] = nil unless no_email(config)
set_git_config global, git_config
puts "Unset#{' global' if global} user.name, #{'user.email, ' unless no_email(config)}user.initials"
end
[:name, :email, :initials].each do |key|
report_git_settings(git_dir, key)
end
rescue GitPairException => e
puts e.message
exit 1
end
def commit(argv)
if argv[0] == '-h'
puts 'Usage: git pair-commit [options_for_git_commit]'
puts ''
puts 'Commits changes to the repository using `git commit`, but randomly chooses the author email from the'
puts 'members of the pair. In order for GitHub to assign credit for the commit activity, the user\'s email'
puts 'must be linked in their GitHub account.'
exit 0
end
config = read_pairs_config
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_details = random_author_details(author_details)
author_email = author_details[:email]
author_key = author_details[:key]
env_variables = "GIT_AUTHOR_NAME='#{authors}' GIT_AUTHOR_EMAIL='#{author_email}' GIT_COMMITTER_NAME='#{authors}' GIT_COMMITTER_EMAIL='#{author_email}'"
puts "Committing under #{author_email}"
unless author_key.nil?
argv << "--gpg-sign=#{author_key}"
puts "Signing with key #{author_key}"
end
args = argv.map{|arg| "'#{arg}'"}.join(' ')
system "#{env_variables} git commit #{args}"
rescue GitPairException => e
puts e.message
exit 1
end
def current_pair_initials
initials = `git config user.initials`.strip.split(' ')
raise GitPairException, 'Error: No pair set. Please set your pair with `git pair ...`' if initials.empty?
initials
end
def parse_cli_options(argv)
options = {}
OptionParser.new do |opts|
# copy-paste from readme
opts.banner = <<BANNER.sub('<br/>','')
Configures git authors when pair programming.
git pair sp js
user.name=Josh Susser and Sam Pierson
user.email=pair+jsusser+sam@pivotallabs.com
Create a `.pairs` config file in project root or your home folder.
# .pairs - configuration for 'git pair'
pairs:
# <initials>: <Firstname> <Lastname>[; <email-id>]
eh: Edward Hieatt
js: Josh Susser; jsusser
sf: Serguei Filimonov; serguei
# if email section is present, email will be set
# if you leave out the email config section, email will not be set
email:
prefix: pair
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
#gpg_keys:
# zr: 1A2B3C4D
By default this affects the current project (.git/config).<br/>
Use the `--global` option or add `global: true` to your `.pairs` file to set the global git configuration for all projects (~/.gitconfig).
Options are:
BANNER
opts.on("-g", "--global", "Modify global git options instead of local") { options[:global] = true }
opts.on("-v", "--version", "Show Version") do
puts PivotalGitScripts::VERSION
exit
end
opts.on("-h", "--help", "Show this.") { puts opts; exit }
end.parse!(argv)
options
end
def read_pairs_config
pairs_file_name = '.pairs'
directory = File.absolute_path(Dir.pwd)
candidate_directories = [directory]
while ! Pathname.new(directory).root? do
directory = File.absolute_path(File.join(directory, ".."))
candidate_directories << directory
end
home = File.absolute_path(ENV["HOME"])
candidate_directories << home unless candidate_directories.include? home
pairs_file_path = candidate_directories.
map { |d| File.join(d, ".pairs") }.
find { |f| File.exists? f }
unless pairs_file_path
raise GitPairException, <<-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
end
YAML.load_file(pairs_file_path)
end
def read_author_info_from_config(config, initials_ary)
initials_ary.map do |initials|
config['pairs'][initials.downcase] or
raise GitPairException, "Couldn't find author name for initials: #{initials}. Add this person to the .pairs file in your project or home directory."
end
end
def build_email(emails, config)
if config.is_a?(Hash)
prefix = config['prefix'] if !config['no_solo_prefix'] or emails.size > 1
"#{([prefix] + emails).compact.join('+')}@#{config['domain']}"
else
config
end
end
def random_author_details(author_details)
author_id = author_details.keys.sample
author_details[author_id]
end
def set_git_config(global, options)
options.each do |key,value|
config_key = "user.#{key}"
arg = value ? %Q{#{config_key} "#{value}"} : "--unset #{config_key}"
system(%Q{git config#{' --global' if global} #{arg}})
end
end
def report_git_settings(git_dir, key)
global = `git config --global --get-regexp '^user\.#{key}'`
local = `git config -f #{git_dir}/config --get-regexp '^user\.#{key}'`
if global.length > 0 && local.length > 0
puts "NOTE: Overriding global user.#{key} setting with local."
end
puts "global: #{global}" if global.length > 0
puts "local: #{local}" if local.length > 0
end
def extract_author_names_and_email_ids_from_config(config, initials)
authors = read_author_info_from_config(config, initials)
authors.sort!.uniq! # FIXME
authors.map do |a|
full_name, email_id = a.split(";").map(&:strip)
email_id ||= full_name.split(' ').first.downcase
[full_name, email_id]
end.transpose
end
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']}"
key = read_gpg_keys_from_config(config, i)
details[i] = {
:name => full_name,
:email => email,
:key => key
}
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
def read_gpg_keys_from_config(config, initial)
return nil unless config['gpg_keys']
return config['gpg_keys'][initial.downcase]
end
private
def pair_names(author_names)
[author_names[0..-2].join(", "), author_names.last].reject(&:empty?).join(" and ")
end
end
end
end