Skip to content

Commit 3a15b17

Browse files
committed
git_config_early: factor out duplicate code and clarify return value
Factor out near identical per-file logic. Its also unclear that 'ret += git_config_from_file()' actually decrements ret on error. It adds the return value of error() (or const_error() for GCC), which is five levels down the call hierarchy. Signed-off-by: Karsten Blees <[email protected]>
1 parent aaa7e0d commit 3a15b17

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

config.c

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,34 +1182,36 @@ int git_config_system(void)
11821182
return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
11831183
}
11841184

1185+
static inline int config_early_helper(config_fn_t fn, const char *filename,
1186+
void *data, unsigned access_flags, int count) {
1187+
if (!filename || access_or_die(filename, R_OK, access_flags))
1188+
/* no file: return unchanged */
1189+
return count;
1190+
1191+
if (git_config_from_file(fn, filename, data))
1192+
/* error: decrement or start counting errors at -1 */
1193+
return count < 0 ? count - 1 : -1;
1194+
else
1195+
/* ok: increment unless we had errors before */
1196+
return count < 0 ? count : count + 1;
1197+
}
1198+
11851199
int git_config_early(config_fn_t fn, void *data, const char *repo_config)
11861200
{
1187-
int ret = 0, found = 0;
1201+
/* count loaded files (> 0) or errors (< 0) */
1202+
int cnt = 0;
11881203
char *xdg_config = NULL;
11891204
char *user_config = NULL;
11901205

11911206
home_config_paths(&user_config, &xdg_config, "config");
11921207

1193-
if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK, 0)) {
1194-
ret += git_config_from_file(fn, git_etc_gitconfig(),
1195-
data);
1196-
found += 1;
1197-
}
1198-
1199-
if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK)) {
1200-
ret += git_config_from_file(fn, xdg_config, data);
1201-
found += 1;
1202-
}
1208+
if (git_config_system())
1209+
cnt = config_early_helper(fn, git_etc_gitconfig(), data, 0, cnt);
12031210

1204-
if (user_config && !access_or_die(user_config, R_OK, ACCESS_EACCES_OK)) {
1205-
ret += git_config_from_file(fn, user_config, data);
1206-
found += 1;
1207-
}
1211+
cnt = config_early_helper(fn, xdg_config, data, ACCESS_EACCES_OK, cnt);
1212+
cnt = config_early_helper(fn, user_config, data, ACCESS_EACCES_OK, cnt);
12081213

1209-
if (repo_config && !access_or_die(repo_config, R_OK, 0)) {
1210-
ret += git_config_from_file(fn, repo_config, data);
1211-
found += 1;
1212-
}
1214+
cnt = config_early_helper(fn, repo_config, data, 0, cnt);
12131215

12141216
switch (git_config_from_parameters(fn, data)) {
12151217
case -1: /* error */
@@ -1218,13 +1220,14 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config)
12181220
case 0: /* found nothing */
12191221
break;
12201222
default: /* found at least one item */
1221-
found++;
1223+
if (cnt >= 0)
1224+
cnt++;
12221225
break;
12231226
}
12241227

12251228
free(xdg_config);
12261229
free(user_config);
1227-
return ret == 0 ? found : ret;
1230+
return cnt;
12281231
}
12291232

12301233
int git_config_with_options(config_fn_t fn, void *data,

0 commit comments

Comments
 (0)