Skip to content

Commit 8cdfe60

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 6efcf8b commit 8cdfe60

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
@@ -1201,32 +1201,31 @@ int git_config_system(void)
12011201
return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
12021202
}
12031203

1204+
static inline void config_from_file_gently(config_fn_t fn, const char *filename,
1205+
void *data, unsigned access_flags, int *ret, int *found) {
1206+
if (!filename || access_or_die(filename, R_OK, access_flags))
1207+
return;
1208+
1209+
*ret += git_config_from_file(fn, filename, data);
1210+
(*found)++;
1211+
}
1212+
12041213
int git_config_early(config_fn_t fn, void *data, const char *repo_config)
12051214
{
12061215
int ret = 0, found = 0;
12071216
char *xdg_config = xdg_config_home("config");
12081217
char *user_config = expand_user_path("~/.gitconfig");
12091218

1210-
if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK, 0)) {
1211-
ret += git_config_from_file(fn, git_etc_gitconfig(),
1212-
data);
1213-
found += 1;
1214-
}
1219+
if (git_config_system())
1220+
config_from_file_gently(fn, git_etc_gitconfig(), data, 0,
1221+
&ret, &found);
12151222

1216-
if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK)) {
1217-
ret += git_config_from_file(fn, xdg_config, data);
1218-
found += 1;
1219-
}
1220-
1221-
if (user_config && !access_or_die(user_config, R_OK, ACCESS_EACCES_OK)) {
1222-
ret += git_config_from_file(fn, user_config, data);
1223-
found += 1;
1224-
}
1223+
config_from_file_gently(fn, xdg_config, data, ACCESS_EACCES_OK,
1224+
&ret, &found);
1225+
config_from_file_gently(fn, user_config, data, ACCESS_EACCES_OK,
1226+
&ret, &found);
12251227

1226-
if (repo_config && !access_or_die(repo_config, R_OK, 0)) {
1227-
ret += git_config_from_file(fn, repo_config, data);
1228-
found += 1;
1229-
}
1228+
config_from_file_gently(fn, repo_config, data, 0, &ret, &found);
12301229

12311230
switch (git_config_from_parameters(fn, data)) {
12321231
case -1: /* error */

0 commit comments

Comments
 (0)