Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds file option to config_set to allow adding to specific git-config files #458

Merged
merged 4 commits into from Jun 19, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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