Skip to content

Commit f8c8582

Browse files
kbleesdscho
authored andcommitted
config: factor out repeated code
Factor out near identical per-file logic. Signed-off-by: Karsten Blees <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 3ca546e commit f8c8582

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

config.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,33 +1207,32 @@ int git_config_system(void)
12071207
return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
12081208
}
12091209

1210+
static inline void config_from_file_gently(config_fn_t fn, const char *filename,
1211+
void *data, unsigned access_flags, int *ret, int *found) {
1212+
if (!filename || access_or_die(filename, R_OK, access_flags))
1213+
return;
1214+
1215+
*ret += git_config_from_file(fn, filename, data);
1216+
(*found)++;
1217+
}
1218+
12101219
static int do_git_config_sequence(config_fn_t fn, void *data)
12111220
{
12121221
int ret = 0, found = 0;
12131222
char *xdg_config = xdg_config_home("config");
12141223
char *user_config = expand_user_path("~/.gitconfig");
12151224
char *repo_config = git_pathdup("config");
12161225

1217-
if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK, 0)) {
1218-
ret += git_config_from_file(fn, git_etc_gitconfig(),
1219-
data);
1220-
found += 1;
1221-
}
1226+
if (git_config_system())
1227+
config_from_file_gently(fn, git_etc_gitconfig(), data, 0,
1228+
&ret, &found);
12221229

1223-
if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK)) {
1224-
ret += git_config_from_file(fn, xdg_config, data);
1225-
found += 1;
1226-
}
1227-
1228-
if (user_config && !access_or_die(user_config, R_OK, ACCESS_EACCES_OK)) {
1229-
ret += git_config_from_file(fn, user_config, data);
1230-
found += 1;
1231-
}
1230+
config_from_file_gently(fn, xdg_config, data, ACCESS_EACCES_OK,
1231+
&ret, &found);
1232+
config_from_file_gently(fn, user_config, data, ACCESS_EACCES_OK,
1233+
&ret, &found);
12321234

1233-
if (repo_config && !access_or_die(repo_config, R_OK, 0)) {
1234-
ret += git_config_from_file(fn, repo_config, data);
1235-
found += 1;
1236-
}
1235+
config_from_file_gently(fn, repo_config, data, 0, &ret, &found);
12371236

12381237
switch (git_config_from_parameters(fn, data)) {
12391238
case -1: /* error */

0 commit comments

Comments
 (0)