Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/git/base.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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