2 #include "add-interactive.h"
8 #include "string-list.h"
15 char header_color[COLOR_MAXLEN];
16 char help_color[COLOR_MAXLEN];
17 char prompt_color[COLOR_MAXLEN];
18 char error_color[COLOR_MAXLEN];
19 char reset_color[COLOR_MAXLEN];
22 static void init_color(struct repository *r, struct add_i_state *s,
23 const char *slot_name, char *dst,
24 const char *default_color)
26 char *key = xstrfmt("color.interactive.%s", slot_name);
31 else if (repo_config_get_value(r, key, &value) ||
32 color_parse(value, dst))
33 strlcpy(dst, default_color, COLOR_MAXLEN);
38 static void init_add_i_state(struct add_i_state *s, struct repository *r)
44 if (repo_config_get_value(r, "color.interactive", &value))
48 git_config_colorbool("color.interactive", value);
49 s->use_color = want_color(s->use_color);
51 init_color(r, s, "header", s->header_color, GIT_COLOR_BOLD);
52 init_color(r, s, "help", s->help_color, GIT_COLOR_BOLD_RED);
53 init_color(r, s, "prompt", s->prompt_color, GIT_COLOR_BOLD_BLUE);
54 init_color(r, s, "error", s->error_color, GIT_COLOR_BOLD_RED);
55 init_color(r, s, "reset", s->reset_color, GIT_COLOR_RESET);
59 * A "prefix item list" is a list of items that are identified by a string, and
60 * a unique prefix (if any) is determined for each item.
62 * It is implemented in the form of a pair of `string_list`s, the first one
63 * duplicating the strings, with the `util` field pointing at a structure whose
64 * first field must be `size_t prefix_length`.
66 * That `prefix_length` field will be computed by `find_unique_prefixes()`; It
67 * will be set to zero if no valid, unique prefix could be found.
69 * The second `string_list` is called `sorted` and does _not_ duplicate the
70 * strings but simply reuses the first one's, with the `util` field pointing at
71 * the `string_item_list` of the first `string_list`. It will be populated and
72 * sorted by `find_unique_prefixes()`.
74 struct prefix_item_list {
75 struct string_list items;
76 struct string_list sorted;
77 int *selected; /* for multi-selections */
78 size_t min_length, max_length;
80 #define PREFIX_ITEM_LIST_INIT \
81 { STRING_LIST_INIT_DUP, STRING_LIST_INIT_NODUP, NULL, 1, 4 }
83 static void prefix_item_list_clear(struct prefix_item_list *list)
85 string_list_clear(&list->items, 1);
86 string_list_clear(&list->sorted, 0);
87 FREE_AND_NULL(list->selected);
90 static void extend_prefix_length(struct string_list_item *p,
91 const char *other_string, size_t max_length)
93 size_t *len = p->util;
95 if (!*len || memcmp(p->string, other_string, *len))
99 char c = p->string[*len];
102 * Is `p` a strict prefix of `other`? Or have we exhausted the
103 * maximal length of the prefix? Or is the current character a
104 * multi-byte UTF-8 one? If so, there is no valid, unique
107 if (!c || ++*len > max_length || !isascii(c)) {
112 if (c != other_string[*len - 1])
117 static void find_unique_prefixes(struct prefix_item_list *list)
121 if (list->sorted.nr == list->items.nr)
124 string_list_clear(&list->sorted, 0);
125 /* Avoid reallocating incrementally */
126 list->sorted.items = xmalloc(st_mult(sizeof(*list->sorted.items),
128 list->sorted.nr = list->sorted.alloc = list->items.nr;
130 for (i = 0; i < list->items.nr; i++) {
131 list->sorted.items[i].string = list->items.items[i].string;
132 list->sorted.items[i].util = list->items.items + i;
135 string_list_sort(&list->sorted);
137 for (i = 0; i < list->sorted.nr; i++) {
138 struct string_list_item *sorted_item = list->sorted.items + i;
139 struct string_list_item *item = sorted_item->util;
140 size_t *len = item->util;
143 while (*len < list->min_length) {
144 char c = item->string[(*len)++];
146 if (!c || !isascii(c)) {
153 extend_prefix_length(item, sorted_item[-1].string,
155 if (i + 1 < list->sorted.nr)
156 extend_prefix_length(item, sorted_item[1].string,
161 static ssize_t find_unique(const char *string, struct prefix_item_list *list)
163 int index = string_list_find_insert_index(&list->sorted, string, 1);
164 struct string_list_item *item;
166 if (list->items.nr != list->sorted.nr)
167 BUG("prefix_item_list in inconsistent state (%"PRIuMAX
169 (uintmax_t)list->items.nr, (uintmax_t)list->sorted.nr);
172 item = list->sorted.items[-1 - index].util;
173 else if (index > 0 &&
174 starts_with(list->sorted.items[index - 1].string, string))
176 else if (index + 1 < list->sorted.nr &&
177 starts_with(list->sorted.items[index + 1].string, string))
179 else if (index < list->sorted.nr)
180 item = list->sorted.items[index].util;
183 return item - list->items.items;
186 struct list_options {
189 void (*print_item)(int i, int selected, struct string_list_item *item,
190 void *print_item_data);
191 void *print_item_data;
194 static void list(struct add_i_state *s, struct string_list *list, int *selected,
195 struct list_options *opts)
203 color_fprintf_ln(stdout, s->header_color,
206 for (i = 0; i < list->nr; i++) {
207 opts->print_item(i, selected ? selected[i] : 0, list->items + i,
208 opts->print_item_data);
210 if ((opts->columns) && ((i + 1) % (opts->columns))) {
223 struct list_and_choose_options {
224 struct list_options list_opts;
231 void (*print_help)(struct add_i_state *s);
234 #define LIST_AND_CHOOSE_ERROR (-1)
235 #define LIST_AND_CHOOSE_QUIT (-2)
238 * Returns the selected index in singleton mode, the number of selected items
241 * If an error occurred, returns `LIST_AND_CHOOSE_ERROR`. Upon EOF,
242 * `LIST_AND_CHOOSE_QUIT` is returned.
244 static ssize_t list_and_choose(struct add_i_state *s,
245 struct prefix_item_list *items,
246 struct list_and_choose_options *opts)
248 int singleton = opts->flags & SINGLETON;
249 int immediate = opts->flags & IMMEDIATE;
251 struct strbuf input = STRBUF_INIT;
252 ssize_t res = singleton ? LIST_AND_CHOOSE_ERROR : 0;
255 free(items->selected);
256 CALLOC_ARRAY(items->selected, items->items.nr);
259 if (singleton && !immediate)
260 BUG("singleton requires immediate");
262 find_unique_prefixes(items);
267 strbuf_reset(&input);
269 list(s, &items->items, items->selected, &opts->list_opts);
271 color_fprintf(stdout, s->prompt_color, "%s", opts->prompt);
272 fputs(singleton ? "> " : ">> ", stdout);
275 if (strbuf_getline(&input, stdin) == EOF) {
278 res = LIST_AND_CHOOSE_QUIT;
286 if (!strcmp(input.buf, "?")) {
293 size_t sep = strcspn(p, " \t\r\n,");
295 /* `from` is inclusive, `to` is exclusive */
296 ssize_t from = -1, to = -1;
305 /* Input that begins with '-'; de-select */
312 if (sep == 1 && *p == '*') {
314 to = items->items.nr;
315 } else if (isdigit(*p)) {
318 * A range can be specified like 5-7 or 5-.
320 * Note: `from` is 0-based while the user input
321 * is 1-based, hence we have to decrement by
322 * one. We do not have to decrement `to` even
323 * if it is 0-based because it is an exclusive
326 from = strtoul(p, &endp, 10) - 1;
329 else if (*endp == '-') {
330 to = strtoul(++endp, &endp, 10);
331 /* extra characters after the range? */
340 from = find_unique(p, items);
345 if (from < 0 || from >= items->items.nr ||
346 (singleton && from + 1 != to)) {
347 color_fprintf_ln(stdout, s->error_color,
350 } else if (singleton) {
355 if (to > items->items.nr)
356 to = items->items.nr;
358 for (; from < to; from++)
359 if (items->selected[from] != choose) {
360 items->selected[from] = choose;
361 res += choose ? +1 : -1;
367 if ((immediate && res != LIST_AND_CHOOSE_ERROR) ||
368 !strcmp(input.buf, "*"))
372 strbuf_release(&input);
378 unsigned seen:1, binary:1;
382 size_t prefix_length;
383 struct adddel index, worktree;
386 static void add_file_item(struct string_list *files, const char *name)
388 struct file_item *item = xcalloc(sizeof(*item), 1);
390 string_list_append(files, name)->util = item;
393 struct pathname_entry {
394 struct hashmap_entry ent;
396 struct file_item *item;
399 static int pathname_entry_cmp(const void *unused_cmp_data,
400 const struct hashmap_entry *he1,
401 const struct hashmap_entry *he2,
404 const struct pathname_entry *e1 =
405 container_of(he1, const struct pathname_entry, ent);
406 const struct pathname_entry *e2 =
407 container_of(he2, const struct pathname_entry, ent);
409 return strcmp(e1->name, name ? (const char *)name : e2->name);
412 struct collection_status {
413 enum { FROM_WORKTREE = 0, FROM_INDEX = 1 } mode;
415 const char *reference;
417 unsigned skip_unseen:1;
418 struct string_list *files;
419 struct hashmap file_map;
422 static void collect_changes_cb(struct diff_queue_struct *q,
423 struct diff_options *options,
426 struct collection_status *s = data;
427 struct diffstat_t stat = { 0 };
433 compute_diffstat(options, &stat, q);
435 for (i = 0; i < stat.nr; i++) {
436 const char *name = stat.files[i]->name;
437 int hash = strhash(name);
438 struct pathname_entry *entry;
439 struct file_item *file_item;
440 struct adddel *adddel;
442 entry = hashmap_get_entry_from_hash(&s->file_map, hash, name,
443 struct pathname_entry, ent);
448 add_file_item(s->files, name);
450 entry = xcalloc(sizeof(*entry), 1);
451 hashmap_entry_init(&entry->ent, hash);
452 entry->name = s->files->items[s->files->nr - 1].string;
453 entry->item = s->files->items[s->files->nr - 1].util;
454 hashmap_add(&s->file_map, &entry->ent);
457 file_item = entry->item;
458 adddel = s->mode == FROM_INDEX ?
459 &file_item->index : &file_item->worktree;
461 adddel->add = stat.files[i]->added;
462 adddel->del = stat.files[i]->deleted;
463 if (stat.files[i]->is_binary)
466 free_diffstat_info(&stat);
469 enum modified_files_filter {
475 static int get_modified_files(struct repository *r,
476 enum modified_files_filter filter,
477 struct prefix_item_list *files,
478 const struct pathspec *ps)
480 struct object_id head_oid;
481 int is_initial = !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
483 struct collection_status s = { 0 };
486 if (discard_index(r->index) < 0 ||
487 repo_read_index_preload(r, ps, 0) < 0)
488 return error(_("could not read index"));
490 prefix_item_list_clear(files);
491 s.files = &files->items;
492 hashmap_init(&s.file_map, pathname_entry_cmp, NULL, 0);
494 for (i = 0; i < 2; i++) {
496 struct setup_revision_opt opt = { 0 };
498 if (filter == INDEX_ONLY)
499 s.mode = (i == 0) ? FROM_INDEX : FROM_WORKTREE;
501 s.mode = (i == 0) ? FROM_WORKTREE : FROM_INDEX;
502 s.skip_unseen = filter && i;
504 opt.def = is_initial ?
505 empty_tree_oid_hex() : oid_to_hex(&head_oid);
507 init_revisions(&rev, NULL);
508 setup_revisions(0, NULL, &rev, &opt);
510 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
511 rev.diffopt.format_callback = collect_changes_cb;
512 rev.diffopt.format_callback_data = &s;
515 copy_pathspec(&rev.prune_data, ps);
517 if (s.mode == FROM_INDEX)
518 run_diff_index(&rev, 1);
520 rev.diffopt.flags.ignore_dirty_submodules = 1;
521 run_diff_files(&rev, 0);
525 clear_pathspec(&rev.prune_data);
527 hashmap_free_entries(&s.file_map, struct pathname_entry, ent);
529 /* While the diffs are ordered already, we ran *two* diffs... */
530 string_list_sort(&files->items);
535 static void render_adddel(struct strbuf *buf,
536 struct adddel *ad, const char *no_changes)
539 strbuf_addstr(buf, _("binary"));
541 strbuf_addf(buf, "+%"PRIuMAX"/-%"PRIuMAX,
542 (uintmax_t)ad->add, (uintmax_t)ad->del);
544 strbuf_addstr(buf, no_changes);
547 /* filters out prefixes which have special meaning to list_and_choose() */
548 static int is_valid_prefix(const char *prefix, size_t prefix_len)
550 return prefix_len && prefix &&
552 * We expect `prefix` to be NUL terminated, therefore this
553 * `strcspn()` call is okay, even if it might do much more
554 * work than strictly necessary.
556 strcspn(prefix, " \t\r\n,") >= prefix_len && /* separators */
557 *prefix != '-' && /* deselection */
558 !isdigit(*prefix) && /* selection */
560 (*prefix != '*' && /* "all" wildcard */
561 *prefix != '?')); /* prompt help */
564 struct print_file_item_data {
565 const char *modified_fmt, *color, *reset;
566 struct strbuf buf, name, index, worktree;
567 unsigned only_names:1;
570 static void print_file_item(int i, int selected, struct string_list_item *item,
571 void *print_file_item_data)
573 struct file_item *c = item->util;
574 struct print_file_item_data *d = print_file_item_data;
575 const char *highlighted = NULL;
577 strbuf_reset(&d->index);
578 strbuf_reset(&d->worktree);
579 strbuf_reset(&d->buf);
581 /* Format the item with the prefix highlighted. */
582 if (c->prefix_length > 0 &&
583 is_valid_prefix(item->string, c->prefix_length)) {
584 strbuf_reset(&d->name);
585 strbuf_addf(&d->name, "%s%.*s%s%s", d->color,
586 (int)c->prefix_length, item->string, d->reset,
587 item->string + c->prefix_length);
588 highlighted = d->name.buf;
592 printf("%c%2d: %s", selected ? '*' : ' ', i + 1,
593 highlighted ? highlighted : item->string);
597 render_adddel(&d->worktree, &c->worktree, _("nothing"));
598 render_adddel(&d->index, &c->index, _("unchanged"));
600 strbuf_addf(&d->buf, d->modified_fmt, d->index.buf, d->worktree.buf,
601 highlighted ? highlighted : item->string);
603 printf("%c%2d: %s", selected ? '*' : ' ', i + 1, d->buf.buf);
606 static int run_status(struct add_i_state *s, const struct pathspec *ps,
607 struct prefix_item_list *files,
608 struct list_and_choose_options *opts)
610 if (get_modified_files(s->r, NO_FILTER, files, ps) < 0)
613 list(s, &files->items, NULL, &opts->list_opts);
619 static int run_update(struct add_i_state *s, const struct pathspec *ps,
620 struct prefix_item_list *files,
621 struct list_and_choose_options *opts)
625 struct lock_file index_lock;
627 if (get_modified_files(s->r, WORKTREE_ONLY, files, ps) < 0)
630 if (!files->items.nr) {
635 opts->prompt = N_("Update");
636 count = list_and_choose(s, files, opts);
642 fd = repo_hold_locked_index(s->r, &index_lock, LOCK_REPORT_ON_ERROR);
648 for (i = 0; i < files->items.nr; i++) {
649 const char *name = files->items.items[i].string;
650 if (files->selected[i] &&
651 add_file_to_index(s->r->index, name, 0) < 0) {
652 res = error(_("could not stage '%s'"), name);
657 if (!res && write_locked_index(s->r->index, &index_lock, COMMIT_LOCK) < 0)
658 res = error(_("could not write index"));
661 printf(Q_("updated %d path\n",
662 "updated %d paths\n", count), (int)count);
668 static void revert_from_diff(struct diff_queue_struct *q,
669 struct diff_options *opt, void *data)
671 int i, add_flags = ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE;
673 for (i = 0; i < q->nr; i++) {
674 struct diff_filespec *one = q->queue[i]->one;
675 struct cache_entry *ce;
677 if (!(one->mode && !is_null_oid(&one->oid))) {
678 remove_file_from_index(opt->repo->index, one->path);
679 printf(_("note: %s is untracked now.\n"), one->path);
681 ce = make_cache_entry(opt->repo->index, one->mode,
682 &one->oid, one->path, 0, 0);
684 die(_("make_cache_entry failed for path '%s'"),
686 add_index_entry(opt->repo->index, ce, add_flags);
691 static int run_revert(struct add_i_state *s, const struct pathspec *ps,
692 struct prefix_item_list *files,
693 struct list_and_choose_options *opts)
698 struct object_id oid;
699 int is_initial = !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &oid,
701 struct lock_file index_lock;
704 struct diff_options diffopt = { NULL };
706 if (get_modified_files(s->r, INDEX_ONLY, files, ps) < 0)
709 if (!files->items.nr) {
714 opts->prompt = N_("Revert");
715 count = list_and_choose(s, files, opts);
719 fd = repo_hold_locked_index(s->r, &index_lock, LOCK_REPORT_ON_ERROR);
726 oidcpy(&oid, s->r->hash_algo->empty_tree);
728 tree = parse_tree_indirect(&oid);
730 res = error(_("Could not parse HEAD^{tree}"));
733 oidcpy(&oid, &tree->object.oid);
736 ALLOC_ARRAY(paths, count + 1);
737 for (i = j = 0; i < files->items.nr; i++)
738 if (files->selected[i])
739 paths[j++] = files->items.items[i].string;
742 parse_pathspec(&diffopt.pathspec, 0,
743 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH,
746 diffopt.output_format = DIFF_FORMAT_CALLBACK;
747 diffopt.format_callback = revert_from_diff;
748 diffopt.flags.override_submodule_config = 1;
751 if (do_diff_cache(&oid, &diffopt))
754 diffcore_std(&diffopt);
755 diff_flush(&diffopt);
758 clear_pathspec(&diffopt.pathspec);
760 if (!res && write_locked_index(s->r->index, &index_lock,
764 res = repo_refresh_and_write_index(s->r, REFRESH_QUIET, 0, 1,
768 printf(Q_("reverted %d path\n",
769 "reverted %d paths\n", count), (int)count);
776 static int get_untracked_files(struct repository *r,
777 struct prefix_item_list *files,
778 const struct pathspec *ps)
780 struct dir_struct dir = { 0 };
782 struct strbuf buf = STRBUF_INIT;
784 if (repo_read_index(r) < 0)
785 return error(_("could not read index"));
787 prefix_item_list_clear(files);
788 setup_standard_excludes(&dir);
789 add_pattern_list(&dir, EXC_CMDL, "--exclude option");
790 fill_directory(&dir, r->index, ps);
792 for (i = 0; i < dir.nr; i++) {
793 struct dir_entry *ent = dir.entries[i];
795 if (index_name_is_other(r->index, ent->name, ent->len)) {
797 strbuf_add(&buf, ent->name, ent->len);
798 add_file_item(&files->items, buf.buf);
802 strbuf_release(&buf);
806 static int run_add_untracked(struct add_i_state *s, const struct pathspec *ps,
807 struct prefix_item_list *files,
808 struct list_and_choose_options *opts)
810 struct print_file_item_data *d = opts->list_opts.print_item_data;
813 struct lock_file index_lock;
815 if (get_untracked_files(s->r, files, ps) < 0)
818 if (!files->items.nr) {
819 printf(_("No untracked files.\n"));
820 goto finish_add_untracked;
823 opts->prompt = N_("Add untracked");
825 count = list_and_choose(s, files, opts);
828 goto finish_add_untracked;
830 fd = repo_hold_locked_index(s->r, &index_lock, LOCK_REPORT_ON_ERROR);
833 goto finish_add_untracked;
836 for (i = 0; i < files->items.nr; i++) {
837 const char *name = files->items.items[i].string;
838 if (files->selected[i] &&
839 add_file_to_index(s->r->index, name, 0) < 0) {
840 res = error(_("could not stage '%s'"), name);
846 write_locked_index(s->r->index, &index_lock, COMMIT_LOCK) < 0)
847 res = error(_("could not write index"));
850 printf(Q_("added %d path\n",
851 "added %d paths\n", count), (int)count);
853 finish_add_untracked:
858 static int run_help(struct add_i_state *s, const struct pathspec *unused_ps,
859 struct prefix_item_list *unused_files,
860 struct list_and_choose_options *unused_opts)
862 color_fprintf_ln(stdout, s->help_color, "status - %s",
863 _("show paths with changes"));
864 color_fprintf_ln(stdout, s->help_color, "update - %s",
865 _("add working tree state to the staged set of changes"));
866 color_fprintf_ln(stdout, s->help_color, "revert - %s",
867 _("revert staged set of changes back to the HEAD version"));
868 color_fprintf_ln(stdout, s->help_color, "patch - %s",
869 _("pick hunks and update selectively"));
870 color_fprintf_ln(stdout, s->help_color, "diff - %s",
871 _("view diff between HEAD and index"));
872 color_fprintf_ln(stdout, s->help_color, "add untracked - %s",
873 _("add contents of untracked files to the staged set of changes"));
878 static void choose_prompt_help(struct add_i_state *s)
880 color_fprintf_ln(stdout, s->help_color, "%s",
882 color_fprintf_ln(stdout, s->help_color, "1 - %s",
883 _("select a single item"));
884 color_fprintf_ln(stdout, s->help_color, "3-5 - %s",
885 _("select a range of items"));
886 color_fprintf_ln(stdout, s->help_color, "2-3,6-9 - %s",
887 _("select multiple ranges"));
888 color_fprintf_ln(stdout, s->help_color, "foo - %s",
889 _("select item based on unique prefix"));
890 color_fprintf_ln(stdout, s->help_color, "-... - %s",
891 _("unselect specified items"));
892 color_fprintf_ln(stdout, s->help_color, "* - %s",
893 _("choose all items"));
894 color_fprintf_ln(stdout, s->help_color, " - %s",
895 _("(empty) finish selecting"));
898 typedef int (*command_t)(struct add_i_state *s, const struct pathspec *ps,
899 struct prefix_item_list *files,
900 struct list_and_choose_options *opts);
902 struct command_item {
903 size_t prefix_length;
907 struct print_command_item_data {
908 const char *color, *reset;
911 static void print_command_item(int i, int selected,
912 struct string_list_item *item,
913 void *print_command_item_data)
915 struct print_command_item_data *d = print_command_item_data;
916 struct command_item *util = item->util;
918 if (!util->prefix_length ||
919 !is_valid_prefix(item->string, util->prefix_length))
920 printf(" %2d: %s", i + 1, item->string);
922 printf(" %2d: %s%.*s%s%s", i + 1,
923 d->color, (int)util->prefix_length, item->string,
924 d->reset, item->string + util->prefix_length);
927 static void command_prompt_help(struct add_i_state *s)
929 const char *help_color = s->help_color;
930 color_fprintf_ln(stdout, help_color, "%s", _("Prompt help:"));
931 color_fprintf_ln(stdout, help_color, "1 - %s",
932 _("select a numbered item"));
933 color_fprintf_ln(stdout, help_color, "foo - %s",
934 _("select item based on unique prefix"));
935 color_fprintf_ln(stdout, help_color, " - %s",
936 _("(empty) select nothing"));
939 int run_add_i(struct repository *r, const struct pathspec *ps)
941 struct add_i_state s = { NULL };
942 struct print_command_item_data data = { "[", "]" };
943 struct list_and_choose_options main_loop_opts = {
944 { 4, N_("*** Commands ***"), print_command_item, &data },
945 N_("What now"), SINGLETON | IMMEDIATE, command_prompt_help
951 { "status", run_status },
952 { "update", run_update },
953 { "revert", run_revert },
954 { "add untracked", run_add_untracked },
955 { "help", run_help },
957 struct prefix_item_list commands = PREFIX_ITEM_LIST_INIT;
959 struct print_file_item_data print_file_item_data = {
960 "%12s %12s %s", NULL, NULL,
961 STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
963 struct list_and_choose_options opts = {
964 { 0, NULL, print_file_item, &print_file_item_data },
965 NULL, 0, choose_prompt_help
967 struct strbuf header = STRBUF_INIT;
968 struct prefix_item_list files = PREFIX_ITEM_LIST_INIT;
972 for (i = 0; i < ARRAY_SIZE(command_list); i++) {
973 struct command_item *util = xcalloc(sizeof(*util), 1);
974 util->command = command_list[i].command;
975 string_list_append(&commands.items, command_list[i].string)
979 init_add_i_state(&s, r);
982 * When color was asked for, use the prompt color for
983 * highlighting, otherwise use square brackets.
986 data.color = s.prompt_color;
987 data.reset = s.reset_color;
989 print_file_item_data.color = data.color;
990 print_file_item_data.reset = data.reset;
992 strbuf_addstr(&header, " ");
993 strbuf_addf(&header, print_file_item_data.modified_fmt,
994 _("staged"), _("unstaged"), _("path"));
995 opts.list_opts.header = header.buf;
997 if (discard_index(r->index) < 0 ||
998 repo_read_index(r) < 0 ||
999 repo_refresh_and_write_index(r, REFRESH_QUIET, 0, 1,
1000 NULL, NULL, NULL) < 0)
1001 warning(_("could not refresh index"));
1003 res = run_status(&s, ps, &files, &opts);
1006 i = list_and_choose(&s, &commands, &main_loop_opts);
1007 if (i == LIST_AND_CHOOSE_QUIT) {
1008 printf(_("Bye.\n"));
1012 if (i != LIST_AND_CHOOSE_ERROR) {
1013 struct command_item *util =
1014 commands.items.items[i].util;
1015 res = util->command(&s, ps, &files, &opts);
1019 prefix_item_list_clear(&files);
1020 strbuf_release(&print_file_item_data.buf);
1021 strbuf_release(&print_file_item_data.name);
1022 strbuf_release(&print_file_item_data.index);
1023 strbuf_release(&print_file_item_data.worktree);
1024 strbuf_release(&header);
1025 prefix_item_list_clear(&commands);