Skip to content

Commit b86cbd6

Browse files
Improved support for configuration.
- Use `git config` directly, because it will normalize extra spaces, quotations, etc. - Added support for a global configuration.
1 parent 4de1494 commit b86cbd6

File tree

2 files changed

+97
-25
lines changed

2 files changed

+97
-25
lines changed

lib/git.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,47 @@ def self.export(repository, name, options = {})
106106
repo.checkout("origin/#{options[:branch]}") if options[:branch]
107107
Dir.chdir(repo.dir.to_s) { FileUtils.rm_r '.git' }
108108
end
109+
110+
#g.config('user.name', 'Scott Chacon') # sets value
111+
#g.config('user.email', 'email@email.com') # sets value
112+
#g.config('user.name') # returns 'Scott Chacon'
113+
#g.config # returns whole config hash
114+
def config(name = nil, value = nil)
115+
lib = Git::Lib.new
116+
if(name && value)
117+
# set value
118+
lib.config_set(name, value)
119+
elsif (name)
120+
# return value
121+
lib.config_get(name)
122+
else
123+
# return hash
124+
lib.config_list
125+
end
126+
end
127+
128+
# Same as g.config, but forces it to be at the global level
129+
#
130+
#g.config('user.name', 'Scott Chacon') # sets value
131+
#g.config('user.email', 'email@email.com') # sets value
132+
#g.config('user.name') # returns 'Scott Chacon'
133+
#g.config # returns whole config hash
134+
def self.global_config(name = nil, value = nil)
135+
lib = Git::Lib.new(nil, nil)
136+
if(name && value)
137+
# set value
138+
lib.global_config_set(name, value)
139+
elsif (name)
140+
# return value
141+
lib.global_config_get(name)
142+
else
143+
# return hash
144+
lib.global_config_list
145+
end
146+
end
147+
148+
def global_config(name = nil, value = nil)
149+
self.class.global_config(name, value)
150+
end
109151

110152
end

lib/git/lib.rb

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -321,46 +321,76 @@ def config_remote(name)
321321
end
322322

323323
def config_get(name)
324-
config_list[name]
325-
#command('config', ['--get', name])
324+
do_get = lambda do
325+
command('config', ['--get', name])
326+
end
327+
328+
if @git_dir
329+
Dir.chdir(@git_dir, &do_get)
330+
else
331+
build_list.call
332+
end
333+
end
334+
335+
def global_config_get(name)
336+
command('config', ['--global', '--get', name], false)
326337
end
327338

328339
def config_list
329-
config = {}
330-
config.merge!(parse_config('~/.gitconfig'))
331-
config.merge!(parse_config(File.join(@git_dir, 'config')))
332-
#hsh = {}
333-
#command_lines('config', ['--list']).each do |line|
334-
# (key, value) = line.split('=')
335-
# hsh[key] = value
336-
#end
337-
#hsh
340+
build_list = lambda do
341+
parse_config_list command_lines('config', ['--list'])
342+
end
343+
344+
if @git_dir
345+
Dir.chdir(@git_dir, &build_list)
346+
else
347+
build_list.call
348+
end
349+
end
350+
351+
def global_config_list
352+
parse_config_list command_lines('config', ['--global', '--list'], false)
338353
end
339354

340-
def parse_config(file)
355+
def parse_config_list(lines)
341356
hsh = {}
342-
file = File.expand_path(file)
343-
if File.file?(file)
344-
current_section = nil
345-
File.readlines(file).each do |line|
346-
if m = /\[(\w+)\]/.match(line)
347-
current_section = m[1]
348-
elsif m = /\[(\w+?) "(.*?)"\]/.match(line)
349-
current_section = "#{m[1]}.#{m[2]}"
350-
elsif m = /(\w+?) = (.*)/.match(line)
351-
key = "#{current_section}.#{m[1]}"
352-
hsh[key] = m[2]
353-
end
354-
end
357+
lines.each do |line|
358+
(key, *values) = line.split('=')
359+
hsh[key] = values.join('=')
355360
end
356361
hsh
357362
end
363+
364+
def parse_config(file)
365+
hsh = {}
366+
parse_config_list command_lines('config', ['--list', '--file', file], false)
367+
#hsh = {}
368+
#file = File.expand_path(file)
369+
#if File.file?(file)
370+
# current_section = nil
371+
# File.readlines(file).each do |line|
372+
# if m = /\[(\w+)\]/.match(line)
373+
# current_section = m[1]
374+
# elsif m = /\[(\w+?) "(.*?)"\]/.match(line)
375+
# current_section = "#{m[1]}.#{m[2]}"
376+
# elsif m = /(\w+?) = (.*)/.match(line)
377+
# key = "#{current_section}.#{m[1]}"
378+
# hsh[key] = m[2]
379+
# end
380+
# end
381+
#end
382+
#hsh
383+
end
358384

359385
## WRITE COMMANDS ##
360386

361387
def config_set(name, value)
362388
command('config', [name, value])
363389
end
390+
391+
def global_config_set(name, value)
392+
command('config', ['--global', name, value], false)
393+
end
364394

365395
def add(path = '.')
366396
arr_opts = ['--']

0 commit comments

Comments
 (0)