status: unify parsing of --untracked= and status.showUntrackedFiles

There are two code paths that take a string and parse it to enum untracked_status_type. Introduce a helper function and use it. As these two places handle an error differently, add an additional invalid value to the enum, and have the caller of the helper handle the error condition, instead of dying or emitting error message from the helper. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Mar 13, 2024 at 10:32 UTC 63acdc4827869a6cedd46aca62e16819498c53fb
2 files changed +29 -18
builtin/commit.c
+27 -17
@@ -1157,22 +1157,34 @@ static void handle_ignored_arg(struct wt_status *s)
1157 die(_("Invalid ignored mode '%s'"), ignored_arg);
1158 }
1159
1160 -static void handle_untracked_files_arg(struct wt_status *s)
1160 +static enum untracked_status_type parse_untracked_setting_name(const char *u)
1161 {
1162 - if (!untracked_files_arg)
1163 - ; /* default already initialized */
1164 - else if (!strcmp(untracked_files_arg, "no"))
1165 - s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
1166 - else if (!strcmp(untracked_files_arg, "normal"))
1167 - s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
1168 - else if (!strcmp(untracked_files_arg, "all"))
1169 - s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
1162 /*
1163 * Please update $__git_untracked_file_modes in
1164 * git-completion.bash when you add new options
1165 */
1166 + if (!strcmp(u, "no"))
1167 + return SHOW_NO_UNTRACKED_FILES;
1168 + else if (!strcmp(u, "normal"))
1169 + return SHOW_NORMAL_UNTRACKED_FILES;
1170 + else if (!strcmp(u, "all"))
1171 + return SHOW_ALL_UNTRACKED_FILES;
1172 else
1175 - die(_("Invalid untracked files mode '%s'"), untracked_files_arg);
1173 + return SHOW_UNTRACKED_FILES_ERROR;
1174 +}
1175 +
1176 +static void handle_untracked_files_arg(struct wt_status *s)
1177 +{
1178 + enum untracked_status_type u;
1179 +
1180 + if (!untracked_files_arg)
1181 + return; /* default already initialized */
1182 +
1183 + u = parse_untracked_setting_name(untracked_files_arg);
1184 + if (u == SHOW_UNTRACKED_FILES_ERROR)
1185 + die(_("Invalid untracked files mode '%s'"),
1186 + untracked_files_arg);
1187 + s->show_untracked_files = u;
1188 }
1189
1190 static const char *read_commit_message(const char *name)
@@ -1455,16 +1467,14 @@ static int git_status_config(const char *k, const char *v,
1467 return 0;
1468 }
1469 if (!strcmp(k, "status.showuntrackedfiles")) {
1470 + enum untracked_status_type u;
1471 +
1472 if (!v)
1473 return config_error_nonbool(k);
1460 - else if (!strcmp(v, "no"))
1461 - s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
1462 - else if (!strcmp(v, "normal"))
1463 - s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
1464 - else if (!strcmp(v, "all"))
1465 - s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
1466 - else
1474 + u = parse_untracked_setting_name(v);
1475 + if (u == SHOW_UNTRACKED_FILES_ERROR)
1476 return error(_("Invalid untracked files mode '%s'"), v);
1477 + s->show_untracked_files = u;
1478 return 0;
1479 }
1480 if (!strcmp(k, "diff.renamelimit")) {
wt-status.h
+2 -1
@@ -23,7 +23,8 @@ enum color_wt_status {
23 };
24
25 enum untracked_status_type {
26 - SHOW_NO_UNTRACKED_FILES,
26 + SHOW_UNTRACKED_FILES_ERROR = -1,
27 + SHOW_NO_UNTRACKED_FILES = 0,
28 SHOW_NORMAL_UNTRACKED_FILES,
29 SHOW_ALL_UNTRACKED_FILES
30 };