Skip to content

Commit

Permalink
Adds file option to config_set to allow adding to specific git-config…
Browse files Browse the repository at this point in the history
… files (#458)

Signed-off-by: Valentino Stoll <v@codenamev.com>

Co-authored-by: James Couball <jcouball@yahoo.com>
  • Loading branch information
codenamev and jcouball committed Jun 19, 2021
1 parent 8cd523a commit 765df7c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
9 changes: 5 additions & 4 deletions lib/git/base.rb
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions lib/git/lib.rb
Expand Up @@ -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)
Expand Down
17 changes: 15 additions & 2 deletions 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
Expand All @@ -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
Expand Down

0 comments on commit 765df7c

Please sign in to comment.