diff --git a/lib/git/base.rb b/lib/git/base.rb index 50459215..6e7c7a10 100644 --- a/lib/git/base.rb +++ b/lib/git/base.rb @@ -137,13 +137,14 @@ def chdir # :yields: the Git::Path #g.config('user.name', 'Scott Chacon') # sets value #g.config('user.email', 'email@email.com') # sets value + #g.config('user.email', 'email@email.com', file: 'path/to/custom/config) # sets value in file #g.config('user.name') # returns 'Scott Chacon' #g.config # returns whole config hash - def config(name = nil, value = nil) - if(name && value) + def config(name = nil, value = nil, options = {}) + if name && value # set value - lib.config_set(name, value) - elsif (name) + lib.config_set(name, value, options) + elsif name # return value lib.config_get(name) else diff --git a/lib/git/lib.rb b/lib/git/lib.rb index 191b8a74..ce8c141b 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -583,8 +583,12 @@ def show(objectish=nil, path=nil) ## WRITE COMMANDS ## - def config_set(name, value) - command('config', name, value) + def config_set(name, value, options = {}) + if options[:file].to_s.empty? + command('config', name, value) + else + command('config', '--file', options[:file], name, value) + end end def global_config_set(name, value) diff --git a/tests/units/test_config.rb b/tests/units/test_config.rb index a1753831..c04c8530 100644 --- a/tests/units/test_config.rb +++ b/tests/units/test_config.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -require File.dirname(__FILE__) + '/../test_helper' +require_relative '../test_helper' class TestConfig < Test::Unit::TestCase def setup @@ -26,7 +26,20 @@ def test_set_config g.config('user.name', 'bully') assert_equal('bully', g.config('user.name')) end - end + end + + def test_set_config_with_custom_file + in_temp_dir do |_path| + custom_config_path = "#{Dir.pwd}/bare/.git/custom-config" + g = Git.clone(@wbare, 'bare') + assert_not_equal('bully', g.config('user.name')) + g.config('user.name', 'bully', file: custom_config_path) + assert_not_equal('bully', g.config('user.name')) + g.config('include.path', custom_config_path) + assert_equal('bully', g.config('user.name')) + assert_equal("[user]\n\tname = bully\n", File.read(custom_config_path)) + end + end def test_env_config with_custom_env_variables do