@@ -138,9 +138,15 @@ struct list_and_choose_options {
138
138
void (* print_help )(struct add_i_state * s );
139
139
};
140
140
141
+ #define LIST_AND_CHOOSE_ERROR (-1)
142
+ #define LIST_AND_CHOOSE_QUIT (-2)
143
+
141
144
/*
142
145
* Returns the selected index in singleton mode, the number of selected items
143
146
* otherwise.
147
+ *
148
+ * If an error occurred, returns `LIST_AND_CHOOSE_ERROR`. Upon EOF,
149
+ * `LIST_AND_CHOOSE_QUIT` is returned.
144
150
*/
145
151
static ssize_t list_and_choose (struct prefix_item * * items , int * selected ,
146
152
size_t nr , struct add_i_state * s ,
@@ -150,7 +156,7 @@ static ssize_t list_and_choose(struct prefix_item **items, int *selected,
150
156
int immediate = opts -> flags & IMMEDIATE ;
151
157
152
158
struct strbuf input = STRBUF_INIT ;
153
- ssize_t res = singleton ? -1 : 0 ;
159
+ ssize_t res = singleton ? LIST_AND_CHOOSE_ERROR : 0 ;
154
160
155
161
if (!selected && !singleton )
156
162
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,
174
180
if (strbuf_getline (& input , stdin ) == EOF ) {
175
181
putchar ('\n' );
176
182
if (immediate )
177
- res = -2 ;
183
+ res = LIST_AND_CHOOSE_QUIT ;
178
184
break ;
179
185
}
180
186
strbuf_trim (& input );
@@ -253,7 +259,8 @@ static ssize_t list_and_choose(struct prefix_item **items, int *selected,
253
259
p += sep + 1 ;
254
260
}
255
261
256
- if ((immediate && res >= 0 ) || !strcmp (input .buf , "*" ))
262
+ if ((immediate && res != LIST_AND_CHOOSE_ERROR ) ||
263
+ !strcmp (input .buf , "*" ))
257
264
break ;
258
265
}
259
266
@@ -1050,12 +1057,13 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
1050
1057
for (;;) {
1051
1058
i = list_and_choose ((struct prefix_item * * )commands , NULL ,
1052
1059
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 )) {
1054
1062
printf (_ ("Bye.\n" ));
1055
1063
res = 0 ;
1056
1064
break ;
1057
1065
}
1058
- if (i >= 0 )
1066
+ if (i != LIST_AND_CHOOSE_ERROR )
1059
1067
res = commands [i ]-> command (& s , ps , & files , & opts );
1060
1068
}
1061
1069
0 commit comments