Skip to content

Commit b4944ed

Browse files
committed
Add Rugged::Config#get_all
This method allows you to fetch all values for a particular config, similar to `git config --get-all "remote.origin.fetch"`. For example, I have a git config with two `fetch` settings in one remote: ``` $ cat ~/git/uart/.git/config [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true ignorecase = true precomposeunicode = true [remote "origin"] url = [email protected]:tenderlove/uart.git fetch = +refs/heads/*:refs/remotes/origin/* fetch = +refs/heads/*:refs/remotes/lololol/* [branch "master"] remote = origin merge = refs/heads/master ``` With `Rugged::Config#get_all` I can get the list of `fetch` options from the config: ``` $ ruby -I lib -rrugged -e'p Rugged::Repository.new("/Users/aaron/git/uart").config.get_all("remote.origin.fetch")' ["+refs/heads/*:refs/remotes/origin/*", "+refs/heads/*:refs/remotes/lololol/*"] ``` References #725
1 parent 435f5f0 commit b4944ed

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

ext/rugged/rugged_config.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,44 @@ static VALUE rb_git_config_transaction(VALUE self)
354354
return rb_result;
355355
}
356356

357+
static int each_config_value(const git_config_entry * entry, void *ctx)
358+
{
359+
VALUE list = (VALUE)ctx;
360+
rb_ary_push(list, rb_str_new_utf8(entry->value));
361+
return 0;
362+
}
363+
364+
/*
365+
* call-seq:
366+
* cfg.get_all(key) -> [value1, value2, ...]
367+
*
368+
* Get a list of values for the given config +key+. Values are always
369+
* returned as an +Array+ of +String+, or +nil+ if the given key doesn't exist
370+
* in the Config file.
371+
*
372+
* cfg['apply.whitespace'] #=> ['fix']
373+
* cfg['diff.renames'] #=> ['true']
374+
* cfg['remote.origin.fetch'] #=> ["+refs/heads/*:refs/remotes/origin/*", "+refs/heads/*:refs/lolol/origin/*"]
375+
*/
376+
static VALUE rb_git_config_get_all(VALUE self, VALUE key)
377+
{
378+
git_config *config;
379+
VALUE list;
380+
int error;
381+
382+
Data_Get_Struct(self, git_config, config);
383+
384+
list = rb_ary_new();
385+
error = git_config_get_multivar_foreach(
386+
config, StringValueCStr(key), NULL, each_config_value, (void *)list);
387+
388+
if (error == GIT_ENOTFOUND)
389+
return Qnil;
390+
391+
rugged_exception_check(error);
392+
return list;
393+
}
394+
357395
void Init_rugged_config(void)
358396
{
359397
/*
@@ -372,6 +410,7 @@ void Init_rugged_config(void)
372410

373411
rb_define_method(rb_cRuggedConfig, "get", rb_git_config_get, 1);
374412
rb_define_method(rb_cRuggedConfig, "[]", rb_git_config_get, 1);
413+
rb_define_method(rb_cRuggedConfig, "get_all", rb_git_config_get_all, 1);
375414

376415
rb_define_method(rb_cRuggedConfig, "each_key", rb_git_config_each_key, 0);
377416
rb_define_method(rb_cRuggedConfig, "each_pair", rb_git_config_each_pair, 0);

test/config_test.rb

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

8+
def test_multi_fetch
9+
config = @repo.config
10+
fetches = ["+refs/heads/*:refs/remotes/test_remote/*",
11+
"+refs/heads/*:refs/remotes/hello_remote/*"]
12+
assert_equal fetches, config.get_all("remote.test_multiple_fetches.fetch")
13+
end
14+
815
def test_read_config_file
916
config = @repo.config
1017
assert_equal 'false', config['core.bare']

test/fixtures/testrepo.git/config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@
1111
pushurl = git://github.com/libgit2/TestEmptyRepository.git
1212
fetch = +refs/heads/*:refs/remotes/test_remote/*
1313
push = refs/heads/*:refs/heads/testing/*
14+
[remote "test_multiple_fetches"]
15+
fetch = +refs/heads/*:refs/remotes/test_remote/*
16+
fetch = +refs/heads/*:refs/remotes/hello_remote/*

0 commit comments

Comments
 (0)