Skip to content

Commit f159d0d

Browse files
committed
fixup! built-in add -i: implement the main loop
Clarify the role of the "magic" `-1` and `-2` return values of the `list_and_choose()` function. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent e3538d8 commit f159d0d

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

add-interactive.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,15 @@ struct list_and_choose_options {
138138
void (*print_help)(struct add_i_state *s);
139139
};
140140

141+
#define LIST_AND_CHOOSE_ERROR (-1)
142+
#define LIST_AND_CHOOSE_QUIT (-2)
143+
141144
/*
142145
* Returns the selected index in singleton mode, the number of selected items
143146
* otherwise.
147+
*
148+
* If an error occurred, returns `LIST_AND_CHOOSE_ERROR`. Upon EOF,
149+
* `LIST_AND_CHOOSE_QUIT` is returned.
144150
*/
145151
static ssize_t list_and_choose(struct prefix_item **items, int *selected,
146152
size_t nr, struct add_i_state *s,
@@ -150,7 +156,7 @@ static ssize_t list_and_choose(struct prefix_item **items, int *selected,
150156
int immediate = opts->flags & IMMEDIATE;
151157

152158
struct strbuf input = STRBUF_INIT;
153-
ssize_t res = singleton ? -1 : 0;
159+
ssize_t res = singleton ? LIST_AND_CHOOSE_ERROR : 0;
154160

155161
if (!selected && !singleton)
156162
BUG("need a selected array in non-singleton mode");
@@ -174,7 +180,7 @@ static ssize_t list_and_choose(struct prefix_item **items, int *selected,
174180
if (strbuf_getline(&input, stdin) == EOF) {
175181
putchar('\n');
176182
if (immediate)
177-
res = -2;
183+
res = LIST_AND_CHOOSE_QUIT;
178184
break;
179185
}
180186
strbuf_trim(&input);
@@ -253,7 +259,8 @@ static ssize_t list_and_choose(struct prefix_item **items, int *selected,
253259
p += sep + 1;
254260
}
255261

256-
if ((immediate && res >= 0) || !strcmp(input.buf, "*"))
262+
if ((immediate && res != LIST_AND_CHOOSE_ERROR) ||
263+
!strcmp(input.buf, "*"))
257264
break;
258265
}
259266

@@ -1050,12 +1057,13 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
10501057
for (;;) {
10511058
i = list_and_choose((struct prefix_item **)commands, NULL,
10521059
ARRAY_SIZE(commands), &s, &main_loop_opts);
1053-
if (i < -1 || (i >= 0 && !commands[i]->command)) {
1060+
if (i == LIST_AND_CHOOSE_QUIT ||
1061+
(i != LIST_AND_CHOOSE_ERROR && !commands[i]->command)) {
10541062
printf(_("Bye.\n"));
10551063
res = 0;
10561064
break;
10571065
}
1058-
if (i >= 0)
1066+
if (i != LIST_AND_CHOOSE_ERROR)
10591067
res = commands[i]->command(&s, ps, &files, &opts);
10601068
}
10611069

0 commit comments

Comments
 (0)