Add fall-back support to the configuration If a config has several files, we need to check all of them before we can say that a variable doesn't exist. Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
diff --git a/src/config.c b/src/config.c
index 29b9b79..39a236a 100644
--- a/src/config.c
+++ b/src/config.c
@@ -310,13 +310,19 @@ int git_config_get_string(git_config *cfg, const char *name, const char **out)
{
file_internal *internal;
git_config_file *file;
+ int i, error;
if (cfg->files.length == 0)
return git__throw(GIT_EINVALIDARGS, "Cannot get variable value; no files open in the `git_config` instance");
- internal = git_vector_get(&cfg->files, 0);
- file = internal->file;
+ for (i = 0; i < cfg->files.length; ++i) {
+ internal = git_vector_get(&cfg->files, i);
+ file = internal->file;
+ error = file->get(file, name, out);
+ if (error == GIT_SUCCESS)
+ break;
+ }
- return file->get(file, name, out);
+ return error;
}
diff --git a/tests/resources/config/.gitconfig b/tests/resources/config/.gitconfig
index 8f8075b..fa72bdd 100644
--- a/tests/resources/config/.gitconfig
+++ b/tests/resources/config/.gitconfig
@@ -1,2 +1,3 @@
[core]
repositoryformatversion = 5
+ something = 2
\ No newline at end of file
diff --git a/tests/t15-config.c b/tests/t15-config.c
index 04b2fde..23a1792 100644
--- a/tests/t15-config.c
+++ b/tests/t15-config.c
@@ -230,6 +230,26 @@ BEGIN_TEST(config10, "a repo's config overrides the global config")
git_repository_free(repo);
END_TEST
+BEGIN_TEST(config11, "fall back to the global config")
+ git_repository *repo;
+ char home_orig[GIT_PATH_MAX];
+ char *home;
+ git_config *cfg;
+ int num;
+
+ home = getenv("HOME");
+ strcpy(home_orig, home);
+ setenv("HOME", CONFIG_BASE, 1);
+
+ must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
+ must_pass(git_repository_config(&cfg, repo));
+ setenv("HOME", home_orig, 1);
+ must_pass(git_config_get_int(cfg, "core.something", &num));
+ must_be_true(num == 2);
+ git_config_free(cfg);
+ git_repository_free(repo);
+END_TEST
+
BEGIN_SUITE(config)
ADD_TEST(config0);
ADD_TEST(config1);
@@ -242,4 +262,5 @@ BEGIN_SUITE(config)
ADD_TEST(config8);
ADD_TEST(config9);
ADD_TEST(config10);
+ ADD_TEST(config11);
END_SUITE