@@ -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 );
@@ -375,19 +384,80 @@ static VALUE rb_git_config_transaction(VALUE self)
375384 return rb_result ;
376385}
377386
387+ /*
388+ * call-seq:
389+ * config.add_backend(backend, level, force = false) -> config
390+ *
391+ * Add a backend to be used by the config.
392+ *
393+ * A backend can only be assigned once, and becomes unusable from that
394+ * point on. Trying to assign a backend a second time will raise an
395+ * exception.
396+ */
397+ static VALUE rb_git_config_add_backend (int argc , VALUE * argv , VALUE self )
398+ {
399+ git_config * config ;
400+ git_config_backend * backend ;
401+ ID id_level ;
402+ git_config_level_t level ;
403+ VALUE rb_backend , rb_level , rb_force ;
404+
405+ rb_scan_args (argc , argv , "21" , & rb_backend , & rb_level , & rb_force );
406+
407+ // TODO: Check rb_backend
408+
409+ Check_Type (rb_level , T_SYMBOL );
410+
411+ id_level = SYM2ID (rb_level );
412+
413+ if (id_level == rb_intern ("system" )) {
414+ level = GIT_CONFIG_LEVEL_SYSTEM ;
415+ } else if (id_level == rb_intern ("xdg" )) {
416+ level = GIT_CONFIG_LEVEL_XDG ;
417+ } else if (id_level == rb_intern ("global" )) {
418+ level = GIT_CONFIG_LEVEL_GLOBAL ;
419+ } else if (id_level == rb_intern ("local" )) {
420+ level = GIT_CONFIG_LEVEL_LOCAL ;
421+ } else if (id_level == rb_intern ("app" )) {
422+ level = GIT_CONFIG_LEVEL_APP ;
423+ } else if (id_level == rb_intern ("highest" )) {
424+ level = GIT_CONFIG_HIGHEST_LEVEL ;
425+ } else {
426+ rb_raise (rb_eArgError , "Invalid config backend level." );
427+ }
428+
429+ // TODO: if (!NIL_P(rb_force) && rb_force != Qtrue && rb_force != Qfalse)
430+
431+ Data_Get_Struct (self , git_config , config );
432+ Data_Get_Struct (rb_backend , git_config_backend , backend );
433+
434+ if (!backend )
435+ rb_exc_raise (rb_exc_new2 (rb_eRuntimeError , "Can not reuse config backend instances" ));
436+
437+ rugged_exception_check (git_config_add_backend (config , backend , level , RTEST (rb_force )));
438+
439+ // libgit2 has taken ownership of the backend, so we should make sure
440+ // we don't try to free it.
441+ ((struct RData * )rb_backend )-> data = NULL ;
442+
443+ return self ;
444+ }
445+
378446void Init_rugged_config (void )
379447{
380448 /*
381449 * Config
382450 */
383451 rb_cRuggedConfig = rb_define_class_under (rb_mRugged , "Config" , rb_cObject );
384- rb_define_singleton_method (rb_cRuggedConfig , "new" , rb_git_config_new , 1 );
452+ rb_define_singleton_method (rb_cRuggedConfig , "new" , rb_git_config_new , - 1 );
385453
386454 rb_define_singleton_method (rb_cRuggedConfig , "global" , rb_git_config_open_default , 0 );
387455 rb_define_singleton_method (rb_cRuggedConfig , "open_global" , rb_git_config_open_default , 0 );
388456
389457 rb_define_method (rb_cRuggedConfig , "delete" , rb_git_config_delete , 1 );
390458
459+ rb_define_method (rb_cRuggedConfig , "add_backend" , rb_git_config_add_backend , -1 );
460+
391461 rb_define_method (rb_cRuggedConfig , "store" , rb_git_config_store , 2 );
392462 rb_define_method (rb_cRuggedConfig , "[]=" , rb_git_config_store , 2 );
393463
0 commit comments