Skip to content

Commit fcafd08

Browse files
Allow creating empty config objects.
1 parent 199e03f commit fcafd08

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

ext/rugged/rugged_config.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,21 @@ VALUE rugged_config_new(VALUE klass, VALUE owner, git_config *cfg)
4141

4242
/*
4343
* call-seq:
44-
* Config.new(path) -> new_config
44+
* Config.new([path]) -> config
4545
*
46-
* Open the file specified in +path+ as a +Rugged::Config+ file.
47-
* If +path+ cannot be found, or the file is an invalid Git config,
48-
* an exception will be raised.
46+
* Create a new config object.
47+
*
48+
* If +path+ is specified, the file at this path will be used as the backing
49+
* config store. +path+ can also be an array of file paths to be used.
50+
*
51+
* If +path+ is not specified, an empty config object will be returned.
4952
*/
50-
static VALUE rb_git_config_new(VALUE klass, VALUE rb_path)
53+
static VALUE rb_git_config_new(int argc, VALUE *argv, VALUE klass)
5154
{
52-
git_config *config = NULL;
55+
git_config *config;
56+
VALUE rb_path;
57+
58+
rb_scan_args(argc, argv, "01", &rb_path);
5359

5460
if (TYPE(rb_path) == T_ARRAY) {
5561
int error, i;
@@ -71,8 +77,11 @@ static VALUE rb_git_config_new(VALUE klass, VALUE rb_path)
7177
rugged_exception_check(
7278
git_config_open_ondisk(&config, StringValueCStr(rb_path))
7379
);
80+
} else if(NIL_P(rb_path)) {
81+
rugged_exception_check(git_config_new(&config));
7482
} else {
75-
rb_raise(rb_eTypeError, "Expecting a filename or an array of filenames");
83+
rb_raise(rb_eTypeError, "wrong argument type %s (expected an Array, String, or nil)",
84+
rb_obj_classname(rb_path));
7685
}
7786

7887
return rugged_config_new(klass, Qnil, config);
@@ -381,7 +390,7 @@ void Init_rugged_config(void)
381390
* Config
382391
*/
383392
rb_cRuggedConfig = rb_define_class_under(rb_mRugged, "Config", rb_cObject);
384-
rb_define_singleton_method(rb_cRuggedConfig, "new", rb_git_config_new, 1);
393+
rb_define_singleton_method(rb_cRuggedConfig, "new", rb_git_config_new, -1);
385394

386395
rb_define_singleton_method(rb_cRuggedConfig, "global", rb_git_config_open_default, 0);
387396
rb_define_singleton_method(rb_cRuggedConfig, "open_global", rb_git_config_open_default, 0);

test/config_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ def setup
55
@repo = FixtureRepo.from_rugged("testrepo.git")
66
end
77

8+
def test_empty_config
9+
config = Rugged::Config.new
10+
assert_nil config['not.exist']
11+
12+
config = Rugged::Config.new(nil)
13+
assert_nil config['not.exist']
14+
end
15+
816
def test_read_config_file
917
config = @repo.config
1018
assert_equal 'false', config['core.bare']

0 commit comments

Comments
 (0)