Commit 73fc5e01 by Carlos Martín Nieto

config: fix variable overriding

When two or more variables of the same name exist and the user asks
for a scalar, we must return the latest value assign to it.
parent a9fb7989
......@@ -395,6 +395,7 @@ static int config_get(const git_config_backend *cfg, const char *name, const git
char *key;
khiter_t pos;
int error;
cvar_t *var;
if ((error = git_config__normalize_name(name, &key)) < 0)
return error;
......@@ -406,7 +407,11 @@ static int config_get(const git_config_backend *cfg, const char *name, const git
if (!git_strmap_valid_index(b->values, pos))
return GIT_ENOTFOUND;
*out = ((cvar_t *)git_strmap_value_at(b->values, pos))->entry;
var = git_strmap_value_at(b->values, pos);
while (var->next)
var = var->next;
*out = var->entry;
return 0;
}
......
......@@ -71,3 +71,25 @@ void test_config_include__refresh(void)
git_config_free(cfg);
cl_fixture_cleanup("config");
}
/* We need to pretend that the variables were defined where the file was included */
void test_config_include__ordering(void)
{
git_config *cfg;
const char *str;
cl_git_mkfile("included", "[foo \"bar\"]\nbaz = hurrah\nfrotz = hiya");
cl_git_mkfile("including",
"[foo \"bar\"]\nfrotz = hello\n"
"[include]\npath = included\n"
"[foo \"bar\"]\nbaz = huzzah\n");
cl_git_pass(git_config_open_ondisk(&cfg, "including"));
cl_git_pass(git_config_get_string(&str, cfg, "foo.bar.frotz"));
cl_assert_equal_s(str, "hiya");
cl_git_pass(git_config_get_string(&str, cfg, "foo.bar.baz"));
cl_assert_equal_s(str, "huzzah");
git_config_free(cfg);
}
......@@ -523,3 +523,18 @@ void test_config_read__corrupt_header(void)
git_config_free(cfg);
}
void test_config_read__override_variable(void)
{
git_config *cfg;
const char *str;
cl_set_cleanup(&clean_test_config, NULL);
cl_git_mkfile("./testconfig", "[some] var = one\nvar = two");
cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));
cl_git_pass(git_config_get_string(&str, cfg, "some.var"));
cl_assert_equal_s(str, "two");
git_config_free(cfg);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment