From a9fb79896e59f4e58cd7d174e7835e8a9c850a33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Fri, 6 Sep 2013 20:51:35 +0200 Subject: [PATCH] config: refresh included files We need to refresh the variables from the included files if they are changed, so loop over all included files and re-parse the files if any of them has changed. --- src/config_file.c | 21 ++++++++++++++++----- tests-clar/config/include.c | 23 +++++++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/config_file.c b/src/config_file.c index 3732fd15c..c83576524 100644 --- a/src/config_file.c +++ b/src/config_file.c @@ -197,14 +197,25 @@ static int config_open(git_config_backend *cfg, git_config_level_t level) static int config_refresh(git_config_backend *cfg) { - int res, updated = 0; + int res = 0, updated = 0, any_updated = 0; diskfile_backend *b = (diskfile_backend *)cfg; git_strmap *old_values; - struct reader *reader = git_array_get(b->readers, 0); + struct reader *reader; + uint32_t i; - res = git_futils_readbuffer_updated( - &reader->buffer, b->file_path, &reader->file_mtime, &reader->file_size, &updated); - if (res < 0 || !updated) + for (i = 0; i < git_array_size(b->readers); i++) { + reader = git_array_get(b->readers, i); + res = git_futils_readbuffer_updated( + &reader->buffer, reader->file_path, &reader->file_mtime, &reader->file_size, &updated); + + if (res < 0) + return (res == GIT_ENOTFOUND) ? 0 : res; + + if (updated) + any_updated = 1; + } + + if (!any_updated) return (res == GIT_ENOTFOUND) ? 0 : res; /* need to reload - store old values and prep for reload */ diff --git a/tests-clar/config/include.c b/tests-clar/config/include.c index b88a7b2d1..94669a57c 100644 --- a/tests-clar/config/include.c +++ b/tests-clar/config/include.c @@ -48,3 +48,26 @@ void test_config_include__homedir(void) git_config_free(cfg); } + +void test_config_include__refresh(void) +{ + git_config *cfg; + const char *str; + + cl_fixture_sandbox("config"); + + cl_git_pass(git_config_open_ondisk(&cfg, "config/config-include")); + + cl_git_pass(git_config_get_string(&str, cfg, "foo.bar.baz")); + cl_assert_equal_s(str, "huzzah"); + + /* Change the included file and see if we refresh */ + cl_git_mkfile("config/config-included", "[foo \"bar\"]\nbaz = hurrah"); + cl_git_pass(git_config_refresh(cfg)); + + cl_git_pass(git_config_get_string(&str, cfg, "foo.bar.baz")); + cl_assert_equal_s(str, "hurrah"); + + git_config_free(cfg); + cl_fixture_cleanup("config"); +}