Skip to content

Commit 036fa8a

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 044c84a commit 036fa8a

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
@@ -1205,33 +1205,32 @@ int git_config_system(void)
12051205
return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
12061206
}
12071207

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

1215-
if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK, 0)) {
1216-
ret += git_config_from_file(fn, git_etc_gitconfig(),
1217-
data);
1218-
found += 1;
1219-
}
1224+
if (git_config_system())
1225+
config_from_file_gently(fn, git_etc_gitconfig(), data, 0,
1226+
&ret, &found);
12201227

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

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

12361235
switch (git_config_from_parameters(fn, data)) {
12371236
case -1: /* error */

0 commit comments

Comments
 (0)