mirror of
https://git.proxmox.com/git/libgit2
synced 2025-05-28 19:23:05 +00:00
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.
This commit is contained in:
parent
a9fb79896e
commit
73fc5e01c2
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user