Skip to content

Commit 4ec3211

Browse files
Add wip code for config backends.
1 parent c937735 commit 4ec3211

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

ext/rugged/rugged.h

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <assert.h>
4141
#include <git2.h>
4242
#include <git2/odb_backend.h>
43+
#include <git2/sys/config.h>
4344
#include <git2/sys/refdb_backend.h>
4445
#include <git2/sys/refs.h>
4546
#include <git2/sys/repository.h>

ext/rugged/rugged_config.c

+61
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,65 @@ static VALUE rb_git_config_transaction(VALUE self)
384384
return rb_result;
385385
}
386386

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+
387446
void Init_rugged_config(void)
388447
{
389448
/*
@@ -397,6 +456,8 @@ void Init_rugged_config(void)
397456

398457
rb_define_method(rb_cRuggedConfig, "delete", rb_git_config_delete, 1);
399458

459+
rb_define_method(rb_cRuggedConfig, "add_backend", rb_git_config_add_backend, -1);
460+
400461
rb_define_method(rb_cRuggedConfig, "store", rb_git_config_store, 2);
401462
rb_define_method(rb_cRuggedConfig, "[]=", rb_git_config_store, 2);
402463

0 commit comments

Comments
 (0)