Skip to content

Commit 765df7c

Browse files
codenamevjcouball
andauthored
Adds file option to config_set to allow adding to specific git-config files (#458)
Signed-off-by: Valentino Stoll <v@codenamev.com> Co-authored-by: James Couball <jcouball@yahoo.com>
1 parent 8cd523a commit 765df7c

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

lib/git/base.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,14 @@ def chdir # :yields: the Git::Path
137137

138138
#g.config('user.name', 'Scott Chacon') # sets value
139139
#g.config('user.email', 'email@email.com') # sets value
140+
#g.config('user.email', 'email@email.com', file: 'path/to/custom/config) # sets value in file
140141
#g.config('user.name') # returns 'Scott Chacon'
141142
#g.config # returns whole config hash
142-
def config(name = nil, value = nil)
143-
if(name && value)
143+
def config(name = nil, value = nil, options = {})
144+
if name && value
144145
# set value
145-
lib.config_set(name, value)
146-
elsif (name)
146+
lib.config_set(name, value, options)
147+
elsif name
147148
# return value
148149
lib.config_get(name)
149150
else

lib/git/lib.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,8 +583,12 @@ def show(objectish=nil, path=nil)
583583

584584
## WRITE COMMANDS ##
585585

586-
def config_set(name, value)
587-
command('config', name, value)
586+
def config_set(name, value, options = {})
587+
if options[:file].to_s.empty?
588+
command('config', name, value)
589+
else
590+
command('config', '--file', options[:file], name, value)
591+
end
588592
end
589593

590594
def global_config_set(name, value)

tests/units/test_config.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env ruby
22

3-
require File.dirname(__FILE__) + '/../test_helper'
3+
require_relative '../test_helper'
44

55
class TestConfig < Test::Unit::TestCase
66
def setup
@@ -26,7 +26,20 @@ def test_set_config
2626
g.config('user.name', 'bully')
2727
assert_equal('bully', g.config('user.name'))
2828
end
29-
end
29+
end
30+
31+
def test_set_config_with_custom_file
32+
in_temp_dir do |_path|
33+
custom_config_path = "#{Dir.pwd}/bare/.git/custom-config"
34+
g = Git.clone(@wbare, 'bare')
35+
assert_not_equal('bully', g.config('user.name'))
36+
g.config('user.name', 'bully', file: custom_config_path)
37+
assert_not_equal('bully', g.config('user.name'))
38+
g.config('include.path', custom_config_path)
39+
assert_equal('bully', g.config('user.name'))
40+
assert_equal("[user]\n\tname = bully\n", File.read(custom_config_path))
41+
end
42+
end
3043

3144
def test_env_config
3245
with_custom_env_variables do

0 commit comments

Comments
 (0)