config: factor out repeated code

Factor out near identical per-file logic.

Signed-off-by: Karsten Blees <blees@dcon.de>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
This commit is contained in:
Karsten Blees
2015-05-23 02:19:50 +02:00
committed by Johannes Schindelin
parent 044c84a7d3
commit 036fa8a34f

View File

@@ -1205,6 +1205,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)++;
}
static int do_git_config_sequence(config_fn_t fn, void *data)
{
int ret = 0, found = 0;
@@ -1212,26 +1221,16 @@ static int do_git_config_sequence(config_fn_t fn, void *data)
char *user_config = expand_user_path("~/.gitconfig");
char *repo_config = git_pathdup("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 */