Class: PivotalGitScripts::GitPair::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/pivotal_git_scripts/git_pair.rb

Instance Method Summary collapse

Instance Method Details

#build_email(emails, config) ⇒ Object



184
185
186
187
188
189
190
191
# File 'lib/pivotal_git_scripts/git_pair.rb', line 184

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

#commit(argv) ⇒ Object



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
# File 'lib/pivotal_git_scripts/git_pair.rb', line 51

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

#current_pair_initialsObject

Raises:



85
86
87
88
89
# File 'lib/pivotal_git_scripts/git_pair.rb', line 85

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

#extract_author_details_from_config(config, initials) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/pivotal_git_scripts/git_pair.rb', line 230

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

#extract_author_names_and_email_ids_from_config(config, initials) ⇒ Object



216
217
218
219
220
221
222
223
224
# File 'lib/pivotal_git_scripts/git_pair.rb', line 216

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

#main(argv) ⇒ Object



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
# File 'lib/pivotal_git_scripts/git_pair.rb', line 21

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

#no_email(config) ⇒ Object



226
227
228
# File 'lib/pivotal_git_scripts/git_pair.rb', line 226

def no_email(config)
  !config.key? 'email'
end

#parse_cli_options(argv) ⇒ Object



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
# File 'lib/pivotal_git_scripts/git_pair.rb', line 91

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
    [email protected]


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: [email protected]
    #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

#random_author_details(author_details) ⇒ Object



193
194
195
196
# File 'lib/pivotal_git_scripts/git_pair.rb', line 193

def random_author_details(author_details)
  author_id = author_details.keys.sample
  author_details[author_id]
end

#read_author_info_from_config(config, initials_ary) ⇒ Object



177
178
179
180
181
182
# File 'lib/pivotal_git_scripts/git_pair.rb', line 177

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

#read_custom_email_address_from_config(config, initial) ⇒ Object



254
255
256
257
# File 'lib/pivotal_git_scripts/git_pair.rb', line 254

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

#read_gpg_keys_from_config(config, initial) ⇒ Object



259
260
261
262
# File 'lib/pivotal_git_scripts/git_pair.rb', line 259

def read_gpg_keys_from_config(config, initial)
  return nil unless config['gpg_keys']
  return config['gpg_keys'][initial.downcase]
end

#read_pairs_configObject



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
# File 'lib/pivotal_git_scripts/git_pair.rb', line 141

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

#report_git_settings(git_dir, key) ⇒ Object



206
207
208
209
210
211
212
213
214
# File 'lib/pivotal_git_scripts/git_pair.rb', line 206

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

#set_git_config(global, options) ⇒ Object



198
199
200
201
202
203
204
# File 'lib/pivotal_git_scripts/git_pair.rb', line 198

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