]>
Commit | Line | Data |
---|---|---|
f83dff60 JS |
1 | #include "cache.h" |
2 | #include "add-interactive.h" | |
1daaebca SĐ |
3 | #include "color.h" |
4 | #include "config.h" | |
5e82b9e4 DF |
5 | #include "diffcore.h" |
6 | #include "revision.h" | |
7 | #include "refs.h" | |
8 | #include "string-list.h" | |
a8c45be9 | 9 | #include "lockfile.h" |
ab1e1ccc | 10 | #include "dir.h" |
8746e072 | 11 | #include "run-command.h" |
5e82b9e4 | 12 | |
1daaebca SĐ |
13 | static void init_color(struct repository *r, struct add_i_state *s, |
14 | const char *slot_name, char *dst, | |
15 | const char *default_color) | |
16 | { | |
17 | char *key = xstrfmt("color.interactive.%s", slot_name); | |
18 | const char *value; | |
19 | ||
20 | if (!s->use_color) | |
21 | dst[0] = '\0'; | |
22 | else if (repo_config_get_value(r, key, &value) || | |
23 | color_parse(value, dst)) | |
24 | strlcpy(dst, default_color, COLOR_MAXLEN); | |
25 | ||
26 | free(key); | |
27 | } | |
28 | ||
25ea47af | 29 | void init_add_i_state(struct add_i_state *s, struct repository *r) |
5e82b9e4 | 30 | { |
1daaebca SĐ |
31 | const char *value; |
32 | ||
33 | s->r = r; | |
34 | ||
35 | if (repo_config_get_value(r, "color.interactive", &value)) | |
36 | s->use_color = -1; | |
37 | else | |
38 | s->use_color = | |
39 | git_config_colorbool("color.interactive", value); | |
40 | s->use_color = want_color(s->use_color); | |
41 | ||
42 | init_color(r, s, "header", s->header_color, GIT_COLOR_BOLD); | |
68db1cbf | 43 | init_color(r, s, "help", s->help_color, GIT_COLOR_BOLD_RED); |
3d965c76 SĐ |
44 | init_color(r, s, "prompt", s->prompt_color, GIT_COLOR_BOLD_BLUE); |
45 | init_color(r, s, "error", s->error_color, GIT_COLOR_BOLD_RED); | |
46 | init_color(r, s, "reset", s->reset_color, GIT_COLOR_RESET); | |
25ea47af JS |
47 | init_color(r, s, "fraginfo", s->fraginfo_color, |
48 | diff_get_color(s->use_color, DIFF_FRAGINFO)); | |
bcdd297b JS |
49 | init_color(r, s, "context", s->context_color, |
50 | diff_get_color(s->use_color, DIFF_CONTEXT)); | |
51 | init_color(r, s, "old", s->file_old_color, | |
52 | diff_get_color(s->use_color, DIFF_FILE_OLD)); | |
53 | init_color(r, s, "new", s->file_new_color, | |
54 | diff_get_color(s->use_color, DIFF_FILE_NEW)); | |
180f48df JS |
55 | |
56 | FREE_AND_NULL(s->interactive_diff_filter); | |
57 | git_config_get_string("interactive.difffilter", | |
58 | &s->interactive_diff_filter); | |
59 | } | |
60 | ||
61 | void clear_add_i_state(struct add_i_state *s) | |
62 | { | |
63 | FREE_AND_NULL(s->interactive_diff_filter); | |
64 | memset(s, 0, sizeof(*s)); | |
65 | s->use_color = -1; | |
5e82b9e4 DF |
66 | } |
67 | ||
76b74323 JS |
68 | /* |
69 | * A "prefix item list" is a list of items that are identified by a string, and | |
70 | * a unique prefix (if any) is determined for each item. | |
71 | * | |
72 | * It is implemented in the form of a pair of `string_list`s, the first one | |
73 | * duplicating the strings, with the `util` field pointing at a structure whose | |
74 | * first field must be `size_t prefix_length`. | |
75 | * | |
76 | * That `prefix_length` field will be computed by `find_unique_prefixes()`; It | |
77 | * will be set to zero if no valid, unique prefix could be found. | |
78 | * | |
79 | * The second `string_list` is called `sorted` and does _not_ duplicate the | |
80 | * strings but simply reuses the first one's, with the `util` field pointing at | |
81 | * the `string_item_list` of the first `string_list`. It will be populated and | |
82 | * sorted by `find_unique_prefixes()`. | |
83 | */ | |
84 | struct prefix_item_list { | |
85 | struct string_list items; | |
86 | struct string_list sorted; | |
f37c2264 | 87 | int *selected; /* for multi-selections */ |
76b74323 JS |
88 | size_t min_length, max_length; |
89 | }; | |
90 | #define PREFIX_ITEM_LIST_INIT \ | |
f37c2264 | 91 | { STRING_LIST_INIT_DUP, STRING_LIST_INIT_NODUP, NULL, 1, 4 } |
76b74323 JS |
92 | |
93 | static void prefix_item_list_clear(struct prefix_item_list *list) | |
94 | { | |
95 | string_list_clear(&list->items, 1); | |
96 | string_list_clear(&list->sorted, 0); | |
f37c2264 | 97 | FREE_AND_NULL(list->selected); |
76b74323 JS |
98 | } |
99 | ||
100 | static void extend_prefix_length(struct string_list_item *p, | |
101 | const char *other_string, size_t max_length) | |
102 | { | |
103 | size_t *len = p->util; | |
104 | ||
105 | if (!*len || memcmp(p->string, other_string, *len)) | |
106 | return; | |
107 | ||
108 | for (;;) { | |
109 | char c = p->string[*len]; | |
110 | ||
111 | /* | |
112 | * Is `p` a strict prefix of `other`? Or have we exhausted the | |
113 | * maximal length of the prefix? Or is the current character a | |
114 | * multi-byte UTF-8 one? If so, there is no valid, unique | |
115 | * prefix. | |
116 | */ | |
117 | if (!c || ++*len > max_length || !isascii(c)) { | |
118 | *len = 0; | |
119 | break; | |
120 | } | |
121 | ||
122 | if (c != other_string[*len - 1]) | |
123 | break; | |
124 | } | |
125 | } | |
126 | ||
127 | static void find_unique_prefixes(struct prefix_item_list *list) | |
128 | { | |
129 | size_t i; | |
130 | ||
131 | if (list->sorted.nr == list->items.nr) | |
132 | return; | |
133 | ||
134 | string_list_clear(&list->sorted, 0); | |
135 | /* Avoid reallocating incrementally */ | |
136 | list->sorted.items = xmalloc(st_mult(sizeof(*list->sorted.items), | |
137 | list->items.nr)); | |
138 | list->sorted.nr = list->sorted.alloc = list->items.nr; | |
139 | ||
140 | for (i = 0; i < list->items.nr; i++) { | |
141 | list->sorted.items[i].string = list->items.items[i].string; | |
142 | list->sorted.items[i].util = list->items.items + i; | |
143 | } | |
144 | ||
145 | string_list_sort(&list->sorted); | |
146 | ||
147 | for (i = 0; i < list->sorted.nr; i++) { | |
148 | struct string_list_item *sorted_item = list->sorted.items + i; | |
149 | struct string_list_item *item = sorted_item->util; | |
150 | size_t *len = item->util; | |
151 | ||
152 | *len = 0; | |
153 | while (*len < list->min_length) { | |
154 | char c = item->string[(*len)++]; | |
155 | ||
156 | if (!c || !isascii(c)) { | |
157 | *len = 0; | |
158 | break; | |
159 | } | |
160 | } | |
161 | ||
162 | if (i > 0) | |
163 | extend_prefix_length(item, sorted_item[-1].string, | |
164 | list->max_length); | |
165 | if (i + 1 < list->sorted.nr) | |
166 | extend_prefix_length(item, sorted_item[1].string, | |
167 | list->max_length); | |
168 | } | |
169 | } | |
170 | ||
171 | static ssize_t find_unique(const char *string, struct prefix_item_list *list) | |
172 | { | |
173 | int index = string_list_find_insert_index(&list->sorted, string, 1); | |
174 | struct string_list_item *item; | |
175 | ||
176 | if (list->items.nr != list->sorted.nr) | |
177 | BUG("prefix_item_list in inconsistent state (%"PRIuMAX | |
178 | " vs %"PRIuMAX")", | |
179 | (uintmax_t)list->items.nr, (uintmax_t)list->sorted.nr); | |
180 | ||
181 | if (index < 0) | |
182 | item = list->sorted.items[-1 - index].util; | |
183 | else if (index > 0 && | |
184 | starts_with(list->sorted.items[index - 1].string, string)) | |
185 | return -1; | |
186 | else if (index + 1 < list->sorted.nr && | |
187 | starts_with(list->sorted.items[index + 1].string, string)) | |
188 | return -1; | |
189 | else if (index < list->sorted.nr) | |
190 | item = list->sorted.items[index].util; | |
191 | else | |
192 | return -1; | |
193 | return item - list->items.items; | |
194 | } | |
195 | ||
5e82b9e4 | 196 | struct list_options { |
6348bfba | 197 | int columns; |
5e82b9e4 | 198 | const char *header; |
f37c2264 JS |
199 | void (*print_item)(int i, int selected, struct string_list_item *item, |
200 | void *print_item_data); | |
5e82b9e4 DF |
201 | void *print_item_data; |
202 | }; | |
203 | ||
f37c2264 | 204 | static void list(struct add_i_state *s, struct string_list *list, int *selected, |
1daaebca | 205 | struct list_options *opts) |
5e82b9e4 | 206 | { |
6348bfba | 207 | int i, last_lf = 0; |
5e82b9e4 DF |
208 | |
209 | if (!list->nr) | |
210 | return; | |
211 | ||
212 | if (opts->header) | |
1daaebca SĐ |
213 | color_fprintf_ln(stdout, s->header_color, |
214 | "%s", opts->header); | |
5e82b9e4 DF |
215 | |
216 | for (i = 0; i < list->nr; i++) { | |
f37c2264 JS |
217 | opts->print_item(i, selected ? selected[i] : 0, list->items + i, |
218 | opts->print_item_data); | |
6348bfba JS |
219 | |
220 | if ((opts->columns) && ((i + 1) % (opts->columns))) { | |
221 | putchar('\t'); | |
222 | last_lf = 0; | |
223 | } | |
224 | else { | |
225 | putchar('\n'); | |
226 | last_lf = 1; | |
227 | } | |
228 | } | |
229 | ||
230 | if (!last_lf) | |
5e82b9e4 | 231 | putchar('\n'); |
6348bfba JS |
232 | } |
233 | struct list_and_choose_options { | |
234 | struct list_options list_opts; | |
235 | ||
236 | const char *prompt; | |
f37c2264 JS |
237 | enum { |
238 | SINGLETON = (1<<0), | |
239 | IMMEDIATE = (1<<1), | |
240 | } flags; | |
68db1cbf | 241 | void (*print_help)(struct add_i_state *s); |
6348bfba JS |
242 | }; |
243 | ||
244 | #define LIST_AND_CHOOSE_ERROR (-1) | |
245 | #define LIST_AND_CHOOSE_QUIT (-2) | |
246 | ||
247 | /* | |
f37c2264 JS |
248 | * Returns the selected index in singleton mode, the number of selected items |
249 | * otherwise. | |
6348bfba JS |
250 | * |
251 | * If an error occurred, returns `LIST_AND_CHOOSE_ERROR`. Upon EOF, | |
252 | * `LIST_AND_CHOOSE_QUIT` is returned. | |
253 | */ | |
76b74323 JS |
254 | static ssize_t list_and_choose(struct add_i_state *s, |
255 | struct prefix_item_list *items, | |
6348bfba JS |
256 | struct list_and_choose_options *opts) |
257 | { | |
f37c2264 JS |
258 | int singleton = opts->flags & SINGLETON; |
259 | int immediate = opts->flags & IMMEDIATE; | |
260 | ||
6348bfba | 261 | struct strbuf input = STRBUF_INIT; |
f37c2264 JS |
262 | ssize_t res = singleton ? LIST_AND_CHOOSE_ERROR : 0; |
263 | ||
264 | if (!singleton) { | |
265 | free(items->selected); | |
266 | CALLOC_ARRAY(items->selected, items->items.nr); | |
267 | } | |
268 | ||
269 | if (singleton && !immediate) | |
270 | BUG("singleton requires immediate"); | |
6348bfba | 271 | |
76b74323 JS |
272 | find_unique_prefixes(items); |
273 | ||
6348bfba JS |
274 | for (;;) { |
275 | char *p; | |
276 | ||
277 | strbuf_reset(&input); | |
278 | ||
f37c2264 | 279 | list(s, &items->items, items->selected, &opts->list_opts); |
6348bfba | 280 | |
3d965c76 | 281 | color_fprintf(stdout, s->prompt_color, "%s", opts->prompt); |
f37c2264 | 282 | fputs(singleton ? "> " : ">> ", stdout); |
6348bfba JS |
283 | fflush(stdout); |
284 | ||
285 | if (strbuf_getline(&input, stdin) == EOF) { | |
286 | putchar('\n'); | |
f37c2264 JS |
287 | if (immediate) |
288 | res = LIST_AND_CHOOSE_QUIT; | |
6348bfba JS |
289 | break; |
290 | } | |
291 | strbuf_trim(&input); | |
292 | ||
293 | if (!input.len) | |
294 | break; | |
295 | ||
68db1cbf JS |
296 | if (!strcmp(input.buf, "?")) { |
297 | opts->print_help(s); | |
298 | continue; | |
299 | } | |
300 | ||
6348bfba JS |
301 | p = input.buf; |
302 | for (;;) { | |
303 | size_t sep = strcspn(p, " \t\r\n,"); | |
f37c2264 JS |
304 | int choose = 1; |
305 | /* `from` is inclusive, `to` is exclusive */ | |
306 | ssize_t from = -1, to = -1; | |
6348bfba JS |
307 | |
308 | if (!sep) { | |
309 | if (!*p) | |
310 | break; | |
311 | p++; | |
312 | continue; | |
313 | } | |
314 | ||
f37c2264 JS |
315 | /* Input that begins with '-'; de-select */ |
316 | if (*p == '-') { | |
317 | choose = 0; | |
318 | p++; | |
319 | sep--; | |
320 | } | |
321 | ||
322 | if (sep == 1 && *p == '*') { | |
323 | from = 0; | |
324 | to = items->items.nr; | |
325 | } else if (isdigit(*p)) { | |
6348bfba | 326 | char *endp; |
f37c2264 JS |
327 | /* |
328 | * A range can be specified like 5-7 or 5-. | |
329 | * | |
330 | * Note: `from` is 0-based while the user input | |
331 | * is 1-based, hence we have to decrement by | |
332 | * one. We do not have to decrement `to` even | |
333 | * if it is 0-based because it is an exclusive | |
334 | * boundary. | |
335 | */ | |
336 | from = strtoul(p, &endp, 10) - 1; | |
337 | if (endp == p + sep) | |
338 | to = from + 1; | |
339 | else if (*endp == '-') { | |
340 | to = strtoul(++endp, &endp, 10); | |
341 | /* extra characters after the range? */ | |
342 | if (endp != p + sep) | |
343 | from = -1; | |
344 | } | |
6348bfba JS |
345 | } |
346 | ||
347 | if (p[sep]) | |
348 | p[sep++] = '\0'; | |
f37c2264 JS |
349 | if (from < 0) { |
350 | from = find_unique(p, items); | |
351 | if (from >= 0) | |
352 | to = from + 1; | |
353 | } | |
76b74323 | 354 | |
f37c2264 JS |
355 | if (from < 0 || from >= items->items.nr || |
356 | (singleton && from + 1 != to)) { | |
3d965c76 SĐ |
357 | color_fprintf_ln(stdout, s->error_color, |
358 | _("Huh (%s)?"), p); | |
f37c2264 JS |
359 | break; |
360 | } else if (singleton) { | |
361 | res = from; | |
6348bfba JS |
362 | break; |
363 | } | |
364 | ||
f37c2264 JS |
365 | if (to > items->items.nr) |
366 | to = items->items.nr; | |
367 | ||
368 | for (; from < to; from++) | |
369 | if (items->selected[from] != choose) { | |
370 | items->selected[from] = choose; | |
371 | res += choose ? +1 : -1; | |
372 | } | |
373 | ||
6348bfba JS |
374 | p += sep; |
375 | } | |
376 | ||
f37c2264 JS |
377 | if ((immediate && res != LIST_AND_CHOOSE_ERROR) || |
378 | !strcmp(input.buf, "*")) | |
6348bfba | 379 | break; |
5e82b9e4 | 380 | } |
6348bfba JS |
381 | |
382 | strbuf_release(&input); | |
383 | return res; | |
5e82b9e4 DF |
384 | } |
385 | ||
386 | struct adddel { | |
387 | uintmax_t add, del; | |
8746e072 | 388 | unsigned seen:1, unmerged:1, binary:1; |
5e82b9e4 DF |
389 | }; |
390 | ||
391 | struct file_item { | |
a8c45be9 | 392 | size_t prefix_length; |
5e82b9e4 DF |
393 | struct adddel index, worktree; |
394 | }; | |
395 | ||
396 | static void add_file_item(struct string_list *files, const char *name) | |
397 | { | |
398 | struct file_item *item = xcalloc(sizeof(*item), 1); | |
399 | ||
400 | string_list_append(files, name)->util = item; | |
401 | } | |
402 | ||
403 | struct pathname_entry { | |
404 | struct hashmap_entry ent; | |
405 | const char *name; | |
406 | struct file_item *item; | |
407 | }; | |
408 | ||
409 | static int pathname_entry_cmp(const void *unused_cmp_data, | |
410 | const struct hashmap_entry *he1, | |
411 | const struct hashmap_entry *he2, | |
412 | const void *name) | |
413 | { | |
414 | const struct pathname_entry *e1 = | |
415 | container_of(he1, const struct pathname_entry, ent); | |
416 | const struct pathname_entry *e2 = | |
417 | container_of(he2, const struct pathname_entry, ent); | |
418 | ||
419 | return strcmp(e1->name, name ? (const char *)name : e2->name); | |
420 | } | |
421 | ||
422 | struct collection_status { | |
c08171d1 | 423 | enum { FROM_WORKTREE = 0, FROM_INDEX = 1 } mode; |
5e82b9e4 DF |
424 | |
425 | const char *reference; | |
426 | ||
c08171d1 | 427 | unsigned skip_unseen:1; |
8746e072 | 428 | size_t unmerged_count, binary_count; |
5e82b9e4 DF |
429 | struct string_list *files; |
430 | struct hashmap file_map; | |
431 | }; | |
432 | ||
433 | static void collect_changes_cb(struct diff_queue_struct *q, | |
434 | struct diff_options *options, | |
435 | void *data) | |
436 | { | |
437 | struct collection_status *s = data; | |
438 | struct diffstat_t stat = { 0 }; | |
439 | int i; | |
440 | ||
441 | if (!q->nr) | |
442 | return; | |
443 | ||
444 | compute_diffstat(options, &stat, q); | |
445 | ||
446 | for (i = 0; i < stat.nr; i++) { | |
447 | const char *name = stat.files[i]->name; | |
448 | int hash = strhash(name); | |
449 | struct pathname_entry *entry; | |
450 | struct file_item *file_item; | |
8746e072 | 451 | struct adddel *adddel, *other_adddel; |
5e82b9e4 DF |
452 | |
453 | entry = hashmap_get_entry_from_hash(&s->file_map, hash, name, | |
454 | struct pathname_entry, ent); | |
455 | if (!entry) { | |
c08171d1 JS |
456 | if (s->skip_unseen) |
457 | continue; | |
458 | ||
5e82b9e4 DF |
459 | add_file_item(s->files, name); |
460 | ||
461 | entry = xcalloc(sizeof(*entry), 1); | |
462 | hashmap_entry_init(&entry->ent, hash); | |
463 | entry->name = s->files->items[s->files->nr - 1].string; | |
464 | entry->item = s->files->items[s->files->nr - 1].util; | |
465 | hashmap_add(&s->file_map, &entry->ent); | |
466 | } | |
467 | ||
468 | file_item = entry->item; | |
c08171d1 | 469 | adddel = s->mode == FROM_INDEX ? |
5e82b9e4 | 470 | &file_item->index : &file_item->worktree; |
8746e072 JS |
471 | other_adddel = s->mode == FROM_INDEX ? |
472 | &file_item->worktree : &file_item->index; | |
5e82b9e4 DF |
473 | adddel->seen = 1; |
474 | adddel->add = stat.files[i]->added; | |
475 | adddel->del = stat.files[i]->deleted; | |
8746e072 JS |
476 | if (stat.files[i]->is_binary) { |
477 | if (!other_adddel->binary) | |
478 | s->binary_count++; | |
5e82b9e4 | 479 | adddel->binary = 1; |
8746e072 JS |
480 | } |
481 | if (stat.files[i]->is_unmerged) { | |
482 | if (!other_adddel->unmerged) | |
483 | s->unmerged_count++; | |
484 | adddel->unmerged = 1; | |
485 | } | |
5e82b9e4 DF |
486 | } |
487 | free_diffstat_info(&stat); | |
488 | } | |
489 | ||
c08171d1 JS |
490 | enum modified_files_filter { |
491 | NO_FILTER = 0, | |
492 | WORKTREE_ONLY = 1, | |
493 | INDEX_ONLY = 2, | |
494 | }; | |
495 | ||
496 | static int get_modified_files(struct repository *r, | |
497 | enum modified_files_filter filter, | |
a8c45be9 | 498 | struct prefix_item_list *files, |
8746e072 JS |
499 | const struct pathspec *ps, |
500 | size_t *unmerged_count, | |
501 | size_t *binary_count) | |
5e82b9e4 DF |
502 | { |
503 | struct object_id head_oid; | |
504 | int is_initial = !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, | |
505 | &head_oid, NULL); | |
c08171d1 JS |
506 | struct collection_status s = { 0 }; |
507 | int i; | |
5e82b9e4 DF |
508 | |
509 | if (discard_index(r->index) < 0 || | |
510 | repo_read_index_preload(r, ps, 0) < 0) | |
511 | return error(_("could not read index")); | |
512 | ||
a8c45be9 JS |
513 | prefix_item_list_clear(files); |
514 | s.files = &files->items; | |
5e82b9e4 DF |
515 | hashmap_init(&s.file_map, pathname_entry_cmp, NULL, 0); |
516 | ||
c08171d1 | 517 | for (i = 0; i < 2; i++) { |
5e82b9e4 DF |
518 | struct rev_info rev; |
519 | struct setup_revision_opt opt = { 0 }; | |
520 | ||
c08171d1 JS |
521 | if (filter == INDEX_ONLY) |
522 | s.mode = (i == 0) ? FROM_INDEX : FROM_WORKTREE; | |
523 | else | |
524 | s.mode = (i == 0) ? FROM_WORKTREE : FROM_INDEX; | |
525 | s.skip_unseen = filter && i; | |
526 | ||
5e82b9e4 DF |
527 | opt.def = is_initial ? |
528 | empty_tree_oid_hex() : oid_to_hex(&head_oid); | |
529 | ||
530 | init_revisions(&rev, NULL); | |
531 | setup_revisions(0, NULL, &rev, &opt); | |
532 | ||
533 | rev.diffopt.output_format = DIFF_FORMAT_CALLBACK; | |
534 | rev.diffopt.format_callback = collect_changes_cb; | |
535 | rev.diffopt.format_callback_data = &s; | |
536 | ||
537 | if (ps) | |
538 | copy_pathspec(&rev.prune_data, ps); | |
539 | ||
c08171d1 | 540 | if (s.mode == FROM_INDEX) |
5e82b9e4 DF |
541 | run_diff_index(&rev, 1); |
542 | else { | |
543 | rev.diffopt.flags.ignore_dirty_submodules = 1; | |
544 | run_diff_files(&rev, 0); | |
545 | } | |
0c3944a6 JS |
546 | |
547 | if (ps) | |
548 | clear_pathspec(&rev.prune_data); | |
5e82b9e4 DF |
549 | } |
550 | hashmap_free_entries(&s.file_map, struct pathname_entry, ent); | |
8746e072 JS |
551 | if (unmerged_count) |
552 | *unmerged_count = s.unmerged_count; | |
553 | if (binary_count) | |
554 | *binary_count = s.binary_count; | |
5e82b9e4 DF |
555 | |
556 | /* While the diffs are ordered already, we ran *two* diffs... */ | |
a8c45be9 | 557 | string_list_sort(&files->items); |
5e82b9e4 DF |
558 | |
559 | return 0; | |
560 | } | |
561 | ||
562 | static void render_adddel(struct strbuf *buf, | |
563 | struct adddel *ad, const char *no_changes) | |
564 | { | |
565 | if (ad->binary) | |
566 | strbuf_addstr(buf, _("binary")); | |
567 | else if (ad->seen) | |
568 | strbuf_addf(buf, "+%"PRIuMAX"/-%"PRIuMAX, | |
569 | (uintmax_t)ad->add, (uintmax_t)ad->del); | |
570 | else | |
571 | strbuf_addstr(buf, no_changes); | |
572 | } | |
573 | ||
76b74323 JS |
574 | /* filters out prefixes which have special meaning to list_and_choose() */ |
575 | static int is_valid_prefix(const char *prefix, size_t prefix_len) | |
576 | { | |
577 | return prefix_len && prefix && | |
578 | /* | |
579 | * We expect `prefix` to be NUL terminated, therefore this | |
580 | * `strcspn()` call is okay, even if it might do much more | |
581 | * work than strictly necessary. | |
582 | */ | |
583 | strcspn(prefix, " \t\r\n,") >= prefix_len && /* separators */ | |
584 | *prefix != '-' && /* deselection */ | |
585 | !isdigit(*prefix) && /* selection */ | |
586 | (prefix_len != 1 || | |
587 | (*prefix != '*' && /* "all" wildcard */ | |
588 | *prefix != '?')); /* prompt help */ | |
589 | } | |
590 | ||
5e82b9e4 | 591 | struct print_file_item_data { |
a8c45be9 JS |
592 | const char *modified_fmt, *color, *reset; |
593 | struct strbuf buf, name, index, worktree; | |
ab1e1ccc | 594 | unsigned only_names:1; |
5e82b9e4 DF |
595 | }; |
596 | ||
f37c2264 | 597 | static void print_file_item(int i, int selected, struct string_list_item *item, |
5e82b9e4 DF |
598 | void *print_file_item_data) |
599 | { | |
600 | struct file_item *c = item->util; | |
601 | struct print_file_item_data *d = print_file_item_data; | |
a8c45be9 | 602 | const char *highlighted = NULL; |
5e82b9e4 DF |
603 | |
604 | strbuf_reset(&d->index); | |
605 | strbuf_reset(&d->worktree); | |
606 | strbuf_reset(&d->buf); | |
607 | ||
a8c45be9 JS |
608 | /* Format the item with the prefix highlighted. */ |
609 | if (c->prefix_length > 0 && | |
610 | is_valid_prefix(item->string, c->prefix_length)) { | |
611 | strbuf_reset(&d->name); | |
612 | strbuf_addf(&d->name, "%s%.*s%s%s", d->color, | |
613 | (int)c->prefix_length, item->string, d->reset, | |
614 | item->string + c->prefix_length); | |
615 | highlighted = d->name.buf; | |
616 | } | |
617 | ||
ab1e1ccc JS |
618 | if (d->only_names) { |
619 | printf("%c%2d: %s", selected ? '*' : ' ', i + 1, | |
620 | highlighted ? highlighted : item->string); | |
621 | return; | |
622 | } | |
623 | ||
5e82b9e4 DF |
624 | render_adddel(&d->worktree, &c->worktree, _("nothing")); |
625 | render_adddel(&d->index, &c->index, _("unchanged")); | |
a8c45be9 JS |
626 | |
627 | strbuf_addf(&d->buf, d->modified_fmt, d->index.buf, d->worktree.buf, | |
628 | highlighted ? highlighted : item->string); | |
5e82b9e4 | 629 | |
f37c2264 | 630 | printf("%c%2d: %s", selected ? '*' : ' ', i + 1, d->buf.buf); |
5e82b9e4 DF |
631 | } |
632 | ||
633 | static int run_status(struct add_i_state *s, const struct pathspec *ps, | |
a8c45be9 JS |
634 | struct prefix_item_list *files, |
635 | struct list_and_choose_options *opts) | |
5e82b9e4 | 636 | { |
8746e072 | 637 | if (get_modified_files(s->r, NO_FILTER, files, ps, NULL, NULL) < 0) |
5e82b9e4 DF |
638 | return -1; |
639 | ||
a8c45be9 | 640 | list(s, &files->items, NULL, &opts->list_opts); |
5e82b9e4 DF |
641 | putchar('\n'); |
642 | ||
643 | return 0; | |
644 | } | |
f83dff60 | 645 | |
a8c45be9 JS |
646 | static int run_update(struct add_i_state *s, const struct pathspec *ps, |
647 | struct prefix_item_list *files, | |
648 | struct list_and_choose_options *opts) | |
649 | { | |
650 | int res = 0, fd; | |
651 | size_t count, i; | |
652 | struct lock_file index_lock; | |
653 | ||
8746e072 | 654 | if (get_modified_files(s->r, WORKTREE_ONLY, files, ps, NULL, NULL) < 0) |
a8c45be9 JS |
655 | return -1; |
656 | ||
657 | if (!files->items.nr) { | |
658 | putchar('\n'); | |
659 | return 0; | |
660 | } | |
661 | ||
662 | opts->prompt = N_("Update"); | |
663 | count = list_and_choose(s, files, opts); | |
664 | if (count <= 0) { | |
665 | putchar('\n'); | |
666 | return 0; | |
667 | } | |
668 | ||
669 | fd = repo_hold_locked_index(s->r, &index_lock, LOCK_REPORT_ON_ERROR); | |
670 | if (fd < 0) { | |
671 | putchar('\n'); | |
672 | return -1; | |
673 | } | |
674 | ||
675 | for (i = 0; i < files->items.nr; i++) { | |
676 | const char *name = files->items.items[i].string; | |
677 | if (files->selected[i] && | |
678 | add_file_to_index(s->r->index, name, 0) < 0) { | |
679 | res = error(_("could not stage '%s'"), name); | |
680 | break; | |
681 | } | |
682 | } | |
683 | ||
684 | if (!res && write_locked_index(s->r->index, &index_lock, COMMIT_LOCK) < 0) | |
685 | res = error(_("could not write index")); | |
686 | ||
687 | if (!res) | |
688 | printf(Q_("updated %d path\n", | |
689 | "updated %d paths\n", count), (int)count); | |
690 | ||
691 | putchar('\n'); | |
692 | return res; | |
693 | } | |
694 | ||
c54ef5e4 JS |
695 | static void revert_from_diff(struct diff_queue_struct *q, |
696 | struct diff_options *opt, void *data) | |
697 | { | |
698 | int i, add_flags = ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE; | |
699 | ||
700 | for (i = 0; i < q->nr; i++) { | |
701 | struct diff_filespec *one = q->queue[i]->one; | |
702 | struct cache_entry *ce; | |
703 | ||
704 | if (!(one->mode && !is_null_oid(&one->oid))) { | |
705 | remove_file_from_index(opt->repo->index, one->path); | |
706 | printf(_("note: %s is untracked now.\n"), one->path); | |
707 | } else { | |
708 | ce = make_cache_entry(opt->repo->index, one->mode, | |
709 | &one->oid, one->path, 0, 0); | |
710 | if (!ce) | |
711 | die(_("make_cache_entry failed for path '%s'"), | |
712 | one->path); | |
713 | add_index_entry(opt->repo->index, ce, add_flags); | |
714 | } | |
715 | } | |
716 | } | |
717 | ||
718 | static int run_revert(struct add_i_state *s, const struct pathspec *ps, | |
719 | struct prefix_item_list *files, | |
720 | struct list_and_choose_options *opts) | |
721 | { | |
722 | int res = 0, fd; | |
723 | size_t count, i, j; | |
724 | ||
725 | struct object_id oid; | |
726 | int is_initial = !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &oid, | |
727 | NULL); | |
728 | struct lock_file index_lock; | |
729 | const char **paths; | |
730 | struct tree *tree; | |
731 | struct diff_options diffopt = { NULL }; | |
732 | ||
8746e072 | 733 | if (get_modified_files(s->r, INDEX_ONLY, files, ps, NULL, NULL) < 0) |
c54ef5e4 JS |
734 | return -1; |
735 | ||
736 | if (!files->items.nr) { | |
737 | putchar('\n'); | |
738 | return 0; | |
739 | } | |
740 | ||
741 | opts->prompt = N_("Revert"); | |
742 | count = list_and_choose(s, files, opts); | |
743 | if (count <= 0) | |
744 | goto finish_revert; | |
745 | ||
746 | fd = repo_hold_locked_index(s->r, &index_lock, LOCK_REPORT_ON_ERROR); | |
747 | if (fd < 0) { | |
748 | res = -1; | |
749 | goto finish_revert; | |
750 | } | |
751 | ||
752 | if (is_initial) | |
753 | oidcpy(&oid, s->r->hash_algo->empty_tree); | |
754 | else { | |
755 | tree = parse_tree_indirect(&oid); | |
756 | if (!tree) { | |
757 | res = error(_("Could not parse HEAD^{tree}")); | |
758 | goto finish_revert; | |
759 | } | |
760 | oidcpy(&oid, &tree->object.oid); | |
761 | } | |
762 | ||
763 | ALLOC_ARRAY(paths, count + 1); | |
764 | for (i = j = 0; i < files->items.nr; i++) | |
765 | if (files->selected[i]) | |
766 | paths[j++] = files->items.items[i].string; | |
767 | paths[j] = NULL; | |
768 | ||
769 | parse_pathspec(&diffopt.pathspec, 0, | |
770 | PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, | |
771 | NULL, paths); | |
772 | ||
773 | diffopt.output_format = DIFF_FORMAT_CALLBACK; | |
774 | diffopt.format_callback = revert_from_diff; | |
775 | diffopt.flags.override_submodule_config = 1; | |
776 | diffopt.repo = s->r; | |
777 | ||
778 | if (do_diff_cache(&oid, &diffopt)) | |
779 | res = -1; | |
780 | else { | |
781 | diffcore_std(&diffopt); | |
782 | diff_flush(&diffopt); | |
783 | } | |
784 | free(paths); | |
785 | clear_pathspec(&diffopt.pathspec); | |
786 | ||
787 | if (!res && write_locked_index(s->r->index, &index_lock, | |
788 | COMMIT_LOCK) < 0) | |
789 | res = -1; | |
790 | else | |
791 | res = repo_refresh_and_write_index(s->r, REFRESH_QUIET, 0, 1, | |
792 | NULL, NULL, NULL); | |
793 | ||
794 | if (!res) | |
795 | printf(Q_("reverted %d path\n", | |
796 | "reverted %d paths\n", count), (int)count); | |
797 | ||
798 | finish_revert: | |
799 | putchar('\n'); | |
800 | return res; | |
801 | } | |
802 | ||
ab1e1ccc JS |
803 | static int get_untracked_files(struct repository *r, |
804 | struct prefix_item_list *files, | |
805 | const struct pathspec *ps) | |
806 | { | |
807 | struct dir_struct dir = { 0 }; | |
808 | size_t i; | |
809 | struct strbuf buf = STRBUF_INIT; | |
810 | ||
811 | if (repo_read_index(r) < 0) | |
812 | return error(_("could not read index")); | |
813 | ||
814 | prefix_item_list_clear(files); | |
815 | setup_standard_excludes(&dir); | |
816 | add_pattern_list(&dir, EXC_CMDL, "--exclude option"); | |
817 | fill_directory(&dir, r->index, ps); | |
818 | ||
819 | for (i = 0; i < dir.nr; i++) { | |
820 | struct dir_entry *ent = dir.entries[i]; | |
821 | ||
822 | if (index_name_is_other(r->index, ent->name, ent->len)) { | |
823 | strbuf_reset(&buf); | |
824 | strbuf_add(&buf, ent->name, ent->len); | |
825 | add_file_item(&files->items, buf.buf); | |
826 | } | |
827 | } | |
828 | ||
829 | strbuf_release(&buf); | |
830 | return 0; | |
831 | } | |
832 | ||
833 | static int run_add_untracked(struct add_i_state *s, const struct pathspec *ps, | |
834 | struct prefix_item_list *files, | |
835 | struct list_and_choose_options *opts) | |
836 | { | |
837 | struct print_file_item_data *d = opts->list_opts.print_item_data; | |
838 | int res = 0, fd; | |
839 | size_t count, i; | |
840 | struct lock_file index_lock; | |
841 | ||
842 | if (get_untracked_files(s->r, files, ps) < 0) | |
843 | return -1; | |
844 | ||
845 | if (!files->items.nr) { | |
846 | printf(_("No untracked files.\n")); | |
847 | goto finish_add_untracked; | |
848 | } | |
849 | ||
850 | opts->prompt = N_("Add untracked"); | |
851 | d->only_names = 1; | |
852 | count = list_and_choose(s, files, opts); | |
853 | d->only_names = 0; | |
854 | if (count <= 0) | |
855 | goto finish_add_untracked; | |
856 | ||
857 | fd = repo_hold_locked_index(s->r, &index_lock, LOCK_REPORT_ON_ERROR); | |
858 | if (fd < 0) { | |
859 | res = -1; | |
860 | goto finish_add_untracked; | |
861 | } | |
862 | ||
863 | for (i = 0; i < files->items.nr; i++) { | |
864 | const char *name = files->items.items[i].string; | |
865 | if (files->selected[i] && | |
866 | add_file_to_index(s->r->index, name, 0) < 0) { | |
867 | res = error(_("could not stage '%s'"), name); | |
868 | break; | |
869 | } | |
870 | } | |
871 | ||
872 | if (!res && | |
873 | write_locked_index(s->r->index, &index_lock, COMMIT_LOCK) < 0) | |
874 | res = error(_("could not write index")); | |
875 | ||
876 | if (!res) | |
877 | printf(Q_("added %d path\n", | |
878 | "added %d paths\n", count), (int)count); | |
879 | ||
880 | finish_add_untracked: | |
881 | putchar('\n'); | |
882 | return res; | |
883 | } | |
884 | ||
8746e072 JS |
885 | static int run_patch(struct add_i_state *s, const struct pathspec *ps, |
886 | struct prefix_item_list *files, | |
887 | struct list_and_choose_options *opts) | |
888 | { | |
889 | int res = 0; | |
890 | ssize_t count, i, j; | |
891 | size_t unmerged_count = 0, binary_count = 0; | |
892 | ||
893 | if (get_modified_files(s->r, WORKTREE_ONLY, files, ps, | |
894 | &unmerged_count, &binary_count) < 0) | |
895 | return -1; | |
896 | ||
897 | if (unmerged_count || binary_count) { | |
898 | for (i = j = 0; i < files->items.nr; i++) { | |
899 | struct file_item *item = files->items.items[i].util; | |
900 | ||
901 | if (item->index.binary || item->worktree.binary) { | |
902 | free(item); | |
903 | free(files->items.items[i].string); | |
904 | } else if (item->index.unmerged || | |
905 | item->worktree.unmerged) { | |
906 | color_fprintf_ln(stderr, s->error_color, | |
907 | _("ignoring unmerged: %s"), | |
908 | files->items.items[i].string); | |
909 | free(item); | |
910 | free(files->items.items[i].string); | |
911 | } else | |
912 | files->items.items[j++] = files->items.items[i]; | |
913 | } | |
914 | files->items.nr = j; | |
915 | } | |
916 | ||
917 | if (!files->items.nr) { | |
918 | if (binary_count) | |
919 | fprintf(stderr, _("Only binary files changed.\n")); | |
920 | else | |
921 | fprintf(stderr, _("No changes.\n")); | |
922 | return 0; | |
923 | } | |
924 | ||
925 | opts->prompt = N_("Patch update"); | |
926 | count = list_and_choose(s, files, opts); | |
927 | if (count >= 0) { | |
928 | struct argv_array args = ARGV_ARRAY_INIT; | |
1942ee44 | 929 | struct pathspec ps_selected = { 0 }; |
8746e072 | 930 | |
8746e072 JS |
931 | for (i = 0; i < files->items.nr; i++) |
932 | if (files->selected[i]) | |
933 | argv_array_push(&args, | |
934 | files->items.items[i].string); | |
1942ee44 JS |
935 | parse_pathspec(&ps_selected, |
936 | PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL, | |
937 | PATHSPEC_LITERAL_PATH, "", args.argv); | |
d2a233cb | 938 | res = run_add_p(s->r, ADD_P_ADD, NULL, &ps_selected); |
8746e072 | 939 | argv_array_clear(&args); |
1942ee44 | 940 | clear_pathspec(&ps_selected); |
8746e072 JS |
941 | } |
942 | ||
943 | return res; | |
944 | } | |
945 | ||
d7633578 JS |
946 | static int run_diff(struct add_i_state *s, const struct pathspec *ps, |
947 | struct prefix_item_list *files, | |
948 | struct list_and_choose_options *opts) | |
949 | { | |
950 | int res = 0; | |
951 | ssize_t count, i; | |
952 | ||
953 | struct object_id oid; | |
954 | int is_initial = !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &oid, | |
955 | NULL); | |
956 | if (get_modified_files(s->r, INDEX_ONLY, files, ps, NULL, NULL) < 0) | |
957 | return -1; | |
958 | ||
959 | if (!files->items.nr) { | |
960 | putchar('\n'); | |
961 | return 0; | |
962 | } | |
963 | ||
964 | opts->prompt = N_("Review diff"); | |
965 | opts->flags = IMMEDIATE; | |
966 | count = list_and_choose(s, files, opts); | |
967 | opts->flags = 0; | |
968 | if (count >= 0) { | |
969 | struct argv_array args = ARGV_ARRAY_INIT; | |
970 | ||
971 | argv_array_pushl(&args, "git", "diff", "-p", "--cached", | |
972 | oid_to_hex(!is_initial ? &oid : | |
973 | s->r->hash_algo->empty_tree), | |
974 | "--", NULL); | |
975 | for (i = 0; i < files->items.nr; i++) | |
976 | if (files->selected[i]) | |
977 | argv_array_push(&args, | |
978 | files->items.items[i].string); | |
979 | res = run_command_v_opt(args.argv, 0); | |
980 | argv_array_clear(&args); | |
981 | } | |
982 | ||
983 | putchar('\n'); | |
984 | return res; | |
985 | } | |
986 | ||
8c159044 | 987 | static int run_help(struct add_i_state *s, const struct pathspec *unused_ps, |
a8c45be9 JS |
988 | struct prefix_item_list *unused_files, |
989 | struct list_and_choose_options *unused_opts) | |
8c159044 SĐ |
990 | { |
991 | color_fprintf_ln(stdout, s->help_color, "status - %s", | |
992 | _("show paths with changes")); | |
993 | color_fprintf_ln(stdout, s->help_color, "update - %s", | |
994 | _("add working tree state to the staged set of changes")); | |
995 | color_fprintf_ln(stdout, s->help_color, "revert - %s", | |
996 | _("revert staged set of changes back to the HEAD version")); | |
997 | color_fprintf_ln(stdout, s->help_color, "patch - %s", | |
998 | _("pick hunks and update selectively")); | |
999 | color_fprintf_ln(stdout, s->help_color, "diff - %s", | |
1000 | _("view diff between HEAD and index")); | |
1001 | color_fprintf_ln(stdout, s->help_color, "add untracked - %s", | |
1002 | _("add contents of untracked files to the staged set of changes")); | |
1003 | ||
1004 | return 0; | |
1005 | } | |
1006 | ||
a8c45be9 JS |
1007 | static void choose_prompt_help(struct add_i_state *s) |
1008 | { | |
1009 | color_fprintf_ln(stdout, s->help_color, "%s", | |
1010 | _("Prompt help:")); | |
1011 | color_fprintf_ln(stdout, s->help_color, "1 - %s", | |
1012 | _("select a single item")); | |
1013 | color_fprintf_ln(stdout, s->help_color, "3-5 - %s", | |
1014 | _("select a range of items")); | |
1015 | color_fprintf_ln(stdout, s->help_color, "2-3,6-9 - %s", | |
1016 | _("select multiple ranges")); | |
1017 | color_fprintf_ln(stdout, s->help_color, "foo - %s", | |
1018 | _("select item based on unique prefix")); | |
1019 | color_fprintf_ln(stdout, s->help_color, "-... - %s", | |
1020 | _("unselect specified items")); | |
1021 | color_fprintf_ln(stdout, s->help_color, "* - %s", | |
1022 | _("choose all items")); | |
1023 | color_fprintf_ln(stdout, s->help_color, " - %s", | |
1024 | _("(empty) finish selecting")); | |
1025 | } | |
1026 | ||
6348bfba | 1027 | typedef int (*command_t)(struct add_i_state *s, const struct pathspec *ps, |
a8c45be9 JS |
1028 | struct prefix_item_list *files, |
1029 | struct list_and_choose_options *opts); | |
6348bfba | 1030 | |
76b74323 JS |
1031 | struct command_item { |
1032 | size_t prefix_length; | |
1033 | command_t command; | |
1034 | }; | |
1035 | ||
3d965c76 SĐ |
1036 | struct print_command_item_data { |
1037 | const char *color, *reset; | |
1038 | }; | |
1039 | ||
f37c2264 JS |
1040 | static void print_command_item(int i, int selected, |
1041 | struct string_list_item *item, | |
6348bfba JS |
1042 | void *print_command_item_data) |
1043 | { | |
3d965c76 | 1044 | struct print_command_item_data *d = print_command_item_data; |
76b74323 JS |
1045 | struct command_item *util = item->util; |
1046 | ||
1047 | if (!util->prefix_length || | |
1048 | !is_valid_prefix(item->string, util->prefix_length)) | |
1049 | printf(" %2d: %s", i + 1, item->string); | |
1050 | else | |
3d965c76 SĐ |
1051 | printf(" %2d: %s%.*s%s%s", i + 1, |
1052 | d->color, (int)util->prefix_length, item->string, | |
1053 | d->reset, item->string + util->prefix_length); | |
6348bfba JS |
1054 | } |
1055 | ||
68db1cbf JS |
1056 | static void command_prompt_help(struct add_i_state *s) |
1057 | { | |
1058 | const char *help_color = s->help_color; | |
1059 | color_fprintf_ln(stdout, help_color, "%s", _("Prompt help:")); | |
1060 | color_fprintf_ln(stdout, help_color, "1 - %s", | |
1061 | _("select a numbered item")); | |
1062 | color_fprintf_ln(stdout, help_color, "foo - %s", | |
1063 | _("select item based on unique prefix")); | |
1064 | color_fprintf_ln(stdout, help_color, " - %s", | |
1065 | _("(empty) select nothing")); | |
1066 | } | |
1067 | ||
f83dff60 JS |
1068 | int run_add_i(struct repository *r, const struct pathspec *ps) |
1069 | { | |
5e82b9e4 | 1070 | struct add_i_state s = { NULL }; |
3d965c76 | 1071 | struct print_command_item_data data = { "[", "]" }; |
6348bfba | 1072 | struct list_and_choose_options main_loop_opts = { |
3d965c76 | 1073 | { 4, N_("*** Commands ***"), print_command_item, &data }, |
f37c2264 | 1074 | N_("What now"), SINGLETON | IMMEDIATE, command_prompt_help |
6348bfba JS |
1075 | }; |
1076 | struct { | |
1077 | const char *string; | |
1078 | command_t command; | |
1079 | } command_list[] = { | |
1080 | { "status", run_status }, | |
a8c45be9 | 1081 | { "update", run_update }, |
c54ef5e4 | 1082 | { "revert", run_revert }, |
ab1e1ccc | 1083 | { "add untracked", run_add_untracked }, |
8746e072 | 1084 | { "patch", run_patch }, |
d7633578 | 1085 | { "diff", run_diff }, |
2e697ced | 1086 | { "quit", NULL }, |
8c159044 | 1087 | { "help", run_help }, |
6348bfba | 1088 | }; |
76b74323 | 1089 | struct prefix_item_list commands = PREFIX_ITEM_LIST_INIT; |
6348bfba | 1090 | |
5e82b9e4 | 1091 | struct print_file_item_data print_file_item_data = { |
a8c45be9 JS |
1092 | "%12s %12s %s", NULL, NULL, |
1093 | STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT | |
5e82b9e4 | 1094 | }; |
a8c45be9 JS |
1095 | struct list_and_choose_options opts = { |
1096 | { 0, NULL, print_file_item, &print_file_item_data }, | |
1097 | NULL, 0, choose_prompt_help | |
5e82b9e4 DF |
1098 | }; |
1099 | struct strbuf header = STRBUF_INIT; | |
a8c45be9 | 1100 | struct prefix_item_list files = PREFIX_ITEM_LIST_INIT; |
6348bfba | 1101 | ssize_t i; |
5e82b9e4 DF |
1102 | int res = 0; |
1103 | ||
76b74323 JS |
1104 | for (i = 0; i < ARRAY_SIZE(command_list); i++) { |
1105 | struct command_item *util = xcalloc(sizeof(*util), 1); | |
1106 | util->command = command_list[i].command; | |
1107 | string_list_append(&commands.items, command_list[i].string) | |
1108 | ->util = util; | |
1109 | } | |
6348bfba | 1110 | |
5e82b9e4 | 1111 | init_add_i_state(&s, r); |
6348bfba | 1112 | |
3d965c76 SĐ |
1113 | /* |
1114 | * When color was asked for, use the prompt color for | |
1115 | * highlighting, otherwise use square brackets. | |
1116 | */ | |
1117 | if (s.use_color) { | |
1118 | data.color = s.prompt_color; | |
1119 | data.reset = s.reset_color; | |
1120 | } | |
a8c45be9 JS |
1121 | print_file_item_data.color = data.color; |
1122 | print_file_item_data.reset = data.reset; | |
3d965c76 | 1123 | |
5e82b9e4 DF |
1124 | strbuf_addstr(&header, " "); |
1125 | strbuf_addf(&header, print_file_item_data.modified_fmt, | |
1126 | _("staged"), _("unstaged"), _("path")); | |
a8c45be9 | 1127 | opts.list_opts.header = header.buf; |
5e82b9e4 DF |
1128 | |
1129 | if (discard_index(r->index) < 0 || | |
1130 | repo_read_index(r) < 0 || | |
1131 | repo_refresh_and_write_index(r, REFRESH_QUIET, 0, 1, | |
1132 | NULL, NULL, NULL) < 0) | |
1133 | warning(_("could not refresh index")); | |
1134 | ||
1135 | res = run_status(&s, ps, &files, &opts); | |
1136 | ||
6348bfba | 1137 | for (;;) { |
2e697ced JS |
1138 | struct command_item *util; |
1139 | ||
6348bfba | 1140 | i = list_and_choose(&s, &commands, &main_loop_opts); |
2e697ced JS |
1141 | if (i < 0 || i >= commands.items.nr) |
1142 | util = NULL; | |
1143 | else | |
1144 | util = commands.items.items[i].util; | |
1145 | ||
1146 | if (i == LIST_AND_CHOOSE_QUIT || (util && !util->command)) { | |
6348bfba JS |
1147 | printf(_("Bye.\n")); |
1148 | res = 0; | |
1149 | break; | |
1150 | } | |
2e697ced JS |
1151 | |
1152 | if (util) | |
76b74323 | 1153 | res = util->command(&s, ps, &files, &opts); |
6348bfba JS |
1154 | } |
1155 | ||
a8c45be9 | 1156 | prefix_item_list_clear(&files); |
5e82b9e4 | 1157 | strbuf_release(&print_file_item_data.buf); |
a8c45be9 | 1158 | strbuf_release(&print_file_item_data.name); |
5e82b9e4 DF |
1159 | strbuf_release(&print_file_item_data.index); |
1160 | strbuf_release(&print_file_item_data.worktree); | |
1161 | strbuf_release(&header); | |
76b74323 | 1162 | prefix_item_list_clear(&commands); |
180f48df | 1163 | clear_add_i_state(&s); |
5e82b9e4 DF |
1164 | |
1165 | return res; | |
f83dff60 | 1166 | } |