From 7612a76e7c446f880a4d64c1cf78e2220a74489e Mon Sep 17 00:00:00 2001 From: Karsten Blees Date: Sat, 23 May 2015 02:19:50 +0200 Subject: [PATCH] config: factor out repeated code Factor out near identical per-file logic. Signed-off-by: Karsten Blees Signed-off-by: Johannes Schindelin --- config.c | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/config.c b/config.c index 19c0c76f67..556ff0c716 100644 --- a/config.c +++ b/config.c @@ -1201,6 +1201,15 @@ int git_config_system(void) return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0); } +static inline void config_from_file_gently(config_fn_t fn, const char *filename, + void *data, unsigned access_flags, int *ret, int *found) { + if (!filename || access_or_die(filename, R_OK, access_flags)) + return; + + *ret += git_config_from_file(fn, filename, data); + (*found)++; +} + int git_config_early(config_fn_t fn, void *data, const char *repo_config) { int ret = 0, found = 0; @@ -1209,26 +1218,16 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config) home_config_paths(&user_config, &xdg_config, "config"); - if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK, 0)) { - ret += git_config_from_file(fn, git_etc_gitconfig(), - data); - found += 1; - } + if (git_config_system()) + config_from_file_gently(fn, git_etc_gitconfig(), data, 0, + &ret, &found); - if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK)) { - ret += git_config_from_file(fn, xdg_config, data); - found += 1; - } + config_from_file_gently(fn, xdg_config, data, ACCESS_EACCES_OK, + &ret, &found); + config_from_file_gently(fn, user_config, data, ACCESS_EACCES_OK, + &ret, &found); - if (user_config && !access_or_die(user_config, R_OK, ACCESS_EACCES_OK)) { - ret += git_config_from_file(fn, user_config, data); - found += 1; - } - - if (repo_config && !access_or_die(repo_config, R_OK, 0)) { - ret += git_config_from_file(fn, repo_config, data); - found += 1; - } + config_from_file_gently(fn, repo_config, data, 0, &ret, &found); switch (git_config_from_parameters(fn, data)) { case -1: /* error */