Skip to content

Commit 63acdc4

Browse files
committed
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 <[email protected]>
1 parent 9451150 commit 63acdc4

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

builtin/commit.c

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,22 +1157,34 @@ static void handle_ignored_arg(struct wt_status *s)
11571157
die(_("Invalid ignored mode '%s'"), ignored_arg);
11581158
}
11591159

1160-
static void handle_untracked_files_arg(struct wt_status *s)
1160+
static enum untracked_status_type parse_untracked_setting_name(const char *u)
11611161
{
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;
11701162
/*
11711163
* Please update $__git_untracked_file_modes in
11721164
* git-completion.bash when you add new options
11731165
*/
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;
11741172
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;
11761188
}
11771189

11781190
static const char *read_commit_message(const char *name)
@@ -1455,16 +1467,14 @@ static int git_status_config(const char *k, const char *v,
14551467
return 0;
14561468
}
14571469
if (!strcmp(k, "status.showuntrackedfiles")) {
1470+
enum untracked_status_type u;
1471+
14581472
if (!v)
14591473
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)
14671476
return error(_("Invalid untracked files mode '%s'"), v);
1477+
s->show_untracked_files = u;
14681478
return 0;
14691479
}
14701480
if (!strcmp(k, "diff.renamelimit")) {

wt-status.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ enum color_wt_status {
2323
};
2424

2525
enum untracked_status_type {
26-
SHOW_NO_UNTRACKED_FILES,
26+
SHOW_UNTRACKED_FILES_ERROR = -1,
27+
SHOW_NO_UNTRACKED_FILES = 0,
2728
SHOW_NORMAL_UNTRACKED_FILES,
2829
SHOW_ALL_UNTRACKED_FILES
2930
};

0 commit comments

Comments
 (0)