2 #include "add-interactive.h"
8 #include "string-list.h"
13 char header_color[COLOR_MAXLEN];
14 char help_color[COLOR_MAXLEN];
17 static void init_color(struct repository *r, struct add_i_state *s,
18 const char *slot_name, char *dst,
19 const char *default_color)
21 char *key = xstrfmt("color.interactive.%s", slot_name);
26 else if (repo_config_get_value(r, key, &value) ||
27 color_parse(value, dst))
28 strlcpy(dst, default_color, COLOR_MAXLEN);
33 static void init_add_i_state(struct add_i_state *s, struct repository *r)
39 if (repo_config_get_value(r, "color.interactive", &value))
43 git_config_colorbool("color.interactive", value);
44 s->use_color = want_color(s->use_color);
46 init_color(r, s, "header", s->header_color, GIT_COLOR_BOLD);
47 init_color(r, s, "help", s->help_color, GIT_COLOR_BOLD_RED);
51 * A "prefix item list" is a list of items that are identified by a string, and
52 * a unique prefix (if any) is determined for each item.
54 * It is implemented in the form of a pair of `string_list`s, the first one
55 * duplicating the strings, with the `util` field pointing at a structure whose
56 * first field must be `size_t prefix_length`.
58 * That `prefix_length` field will be computed by `find_unique_prefixes()`; It
59 * will be set to zero if no valid, unique prefix could be found.
61 * The second `string_list` is called `sorted` and does _not_ duplicate the
62 * strings but simply reuses the first one's, with the `util` field pointing at
63 * the `string_item_list` of the first `string_list`. It will be populated and
64 * sorted by `find_unique_prefixes()`.
66 struct prefix_item_list {
67 struct string_list items;
68 struct string_list sorted;
69 size_t min_length, max_length;
71 #define PREFIX_ITEM_LIST_INIT \
72 { STRING_LIST_INIT_DUP, STRING_LIST_INIT_NODUP, 1, 4 }
74 static void prefix_item_list_clear(struct prefix_item_list *list)
76 string_list_clear(&list->items, 1);
77 string_list_clear(&list->sorted, 0);
80 static void extend_prefix_length(struct string_list_item *p,
81 const char *other_string, size_t max_length)
83 size_t *len = p->util;
85 if (!*len || memcmp(p->string, other_string, *len))
89 char c = p->string[*len];
92 * Is `p` a strict prefix of `other`? Or have we exhausted the
93 * maximal length of the prefix? Or is the current character a
94 * multi-byte UTF-8 one? If so, there is no valid, unique
97 if (!c || ++*len > max_length || !isascii(c)) {
102 if (c != other_string[*len - 1])
107 static void find_unique_prefixes(struct prefix_item_list *list)
111 if (list->sorted.nr == list->items.nr)
114 string_list_clear(&list->sorted, 0);
115 /* Avoid reallocating incrementally */
116 list->sorted.items = xmalloc(st_mult(sizeof(*list->sorted.items),
118 list->sorted.nr = list->sorted.alloc = list->items.nr;
120 for (i = 0; i < list->items.nr; i++) {
121 list->sorted.items[i].string = list->items.items[i].string;
122 list->sorted.items[i].util = list->items.items + i;
125 string_list_sort(&list->sorted);
127 for (i = 0; i < list->sorted.nr; i++) {
128 struct string_list_item *sorted_item = list->sorted.items + i;
129 struct string_list_item *item = sorted_item->util;
130 size_t *len = item->util;
133 while (*len < list->min_length) {
134 char c = item->string[(*len)++];
136 if (!c || !isascii(c)) {
143 extend_prefix_length(item, sorted_item[-1].string,
145 if (i + 1 < list->sorted.nr)
146 extend_prefix_length(item, sorted_item[1].string,
151 static ssize_t find_unique(const char *string, struct prefix_item_list *list)
153 int index = string_list_find_insert_index(&list->sorted, string, 1);
154 struct string_list_item *item;
156 if (list->items.nr != list->sorted.nr)
157 BUG("prefix_item_list in inconsistent state (%"PRIuMAX
159 (uintmax_t)list->items.nr, (uintmax_t)list->sorted.nr);
162 item = list->sorted.items[-1 - index].util;
163 else if (index > 0 &&
164 starts_with(list->sorted.items[index - 1].string, string))
166 else if (index + 1 < list->sorted.nr &&
167 starts_with(list->sorted.items[index + 1].string, string))
169 else if (index < list->sorted.nr)
170 item = list->sorted.items[index].util;
173 return item - list->items.items;
176 struct list_options {
179 void (*print_item)(int i, struct string_list_item *item, void *print_item_data);
180 void *print_item_data;
183 static void list(struct add_i_state *s, struct string_list *list,
184 struct list_options *opts)
192 color_fprintf_ln(stdout, s->header_color,
195 for (i = 0; i < list->nr; i++) {
196 opts->print_item(i, list->items + i, opts->print_item_data);
198 if ((opts->columns) && ((i + 1) % (opts->columns))) {
211 struct list_and_choose_options {
212 struct list_options list_opts;
215 void (*print_help)(struct add_i_state *s);
218 #define LIST_AND_CHOOSE_ERROR (-1)
219 #define LIST_AND_CHOOSE_QUIT (-2)
222 * Returns the selected index.
224 * If an error occurred, returns `LIST_AND_CHOOSE_ERROR`. Upon EOF,
225 * `LIST_AND_CHOOSE_QUIT` is returned.
227 static ssize_t list_and_choose(struct add_i_state *s,
228 struct prefix_item_list *items,
229 struct list_and_choose_options *opts)
231 struct strbuf input = STRBUF_INIT;
232 ssize_t res = LIST_AND_CHOOSE_ERROR;
234 find_unique_prefixes(items);
239 strbuf_reset(&input);
241 list(s, &items->items, &opts->list_opts);
243 printf("%s%s", opts->prompt, "> ");
246 if (strbuf_getline(&input, stdin) == EOF) {
248 res = LIST_AND_CHOOSE_QUIT;
256 if (!strcmp(input.buf, "?")) {
263 size_t sep = strcspn(p, " \t\r\n,");
275 index = strtoul(p, &endp, 10) - 1;
283 index = find_unique(p, items);
285 if (index < 0 || index >= items->items.nr)
286 printf(_("Huh (%s)?\n"), p);
295 if (res != LIST_AND_CHOOSE_ERROR)
299 strbuf_release(&input);
305 unsigned seen:1, binary:1;
309 struct adddel index, worktree;
312 static void add_file_item(struct string_list *files, const char *name)
314 struct file_item *item = xcalloc(sizeof(*item), 1);
316 string_list_append(files, name)->util = item;
319 struct pathname_entry {
320 struct hashmap_entry ent;
322 struct file_item *item;
325 static int pathname_entry_cmp(const void *unused_cmp_data,
326 const struct hashmap_entry *he1,
327 const struct hashmap_entry *he2,
330 const struct pathname_entry *e1 =
331 container_of(he1, const struct pathname_entry, ent);
332 const struct pathname_entry *e2 =
333 container_of(he2, const struct pathname_entry, ent);
335 return strcmp(e1->name, name ? (const char *)name : e2->name);
338 struct collection_status {
339 enum { FROM_WORKTREE = 0, FROM_INDEX = 1 } phase;
341 const char *reference;
343 struct string_list *files;
344 struct hashmap file_map;
347 static void collect_changes_cb(struct diff_queue_struct *q,
348 struct diff_options *options,
351 struct collection_status *s = data;
352 struct diffstat_t stat = { 0 };
358 compute_diffstat(options, &stat, q);
360 for (i = 0; i < stat.nr; i++) {
361 const char *name = stat.files[i]->name;
362 int hash = strhash(name);
363 struct pathname_entry *entry;
364 struct file_item *file_item;
365 struct adddel *adddel;
367 entry = hashmap_get_entry_from_hash(&s->file_map, hash, name,
368 struct pathname_entry, ent);
370 add_file_item(s->files, name);
372 entry = xcalloc(sizeof(*entry), 1);
373 hashmap_entry_init(&entry->ent, hash);
374 entry->name = s->files->items[s->files->nr - 1].string;
375 entry->item = s->files->items[s->files->nr - 1].util;
376 hashmap_add(&s->file_map, &entry->ent);
379 file_item = entry->item;
380 adddel = s->phase == FROM_INDEX ?
381 &file_item->index : &file_item->worktree;
383 adddel->add = stat.files[i]->added;
384 adddel->del = stat.files[i]->deleted;
385 if (stat.files[i]->is_binary)
388 free_diffstat_info(&stat);
391 static int get_modified_files(struct repository *r, struct string_list *files,
392 const struct pathspec *ps)
394 struct object_id head_oid;
395 int is_initial = !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
397 struct collection_status s = { FROM_WORKTREE };
399 if (discard_index(r->index) < 0 ||
400 repo_read_index_preload(r, ps, 0) < 0)
401 return error(_("could not read index"));
403 string_list_clear(files, 1);
405 hashmap_init(&s.file_map, pathname_entry_cmp, NULL, 0);
407 for (s.phase = FROM_WORKTREE; s.phase <= FROM_INDEX; s.phase++) {
409 struct setup_revision_opt opt = { 0 };
411 opt.def = is_initial ?
412 empty_tree_oid_hex() : oid_to_hex(&head_oid);
414 init_revisions(&rev, NULL);
415 setup_revisions(0, NULL, &rev, &opt);
417 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
418 rev.diffopt.format_callback = collect_changes_cb;
419 rev.diffopt.format_callback_data = &s;
422 copy_pathspec(&rev.prune_data, ps);
424 if (s.phase == FROM_INDEX)
425 run_diff_index(&rev, 1);
427 rev.diffopt.flags.ignore_dirty_submodules = 1;
428 run_diff_files(&rev, 0);
431 hashmap_free_entries(&s.file_map, struct pathname_entry, ent);
433 /* While the diffs are ordered already, we ran *two* diffs... */
434 string_list_sort(files);
439 static void render_adddel(struct strbuf *buf,
440 struct adddel *ad, const char *no_changes)
443 strbuf_addstr(buf, _("binary"));
445 strbuf_addf(buf, "+%"PRIuMAX"/-%"PRIuMAX,
446 (uintmax_t)ad->add, (uintmax_t)ad->del);
448 strbuf_addstr(buf, no_changes);
451 /* filters out prefixes which have special meaning to list_and_choose() */
452 static int is_valid_prefix(const char *prefix, size_t prefix_len)
454 return prefix_len && prefix &&
456 * We expect `prefix` to be NUL terminated, therefore this
457 * `strcspn()` call is okay, even if it might do much more
458 * work than strictly necessary.
460 strcspn(prefix, " \t\r\n,") >= prefix_len && /* separators */
461 *prefix != '-' && /* deselection */
462 !isdigit(*prefix) && /* selection */
464 (*prefix != '*' && /* "all" wildcard */
465 *prefix != '?')); /* prompt help */
468 struct print_file_item_data {
469 const char *modified_fmt;
470 struct strbuf buf, index, worktree;
473 static void print_file_item(int i, struct string_list_item *item,
474 void *print_file_item_data)
476 struct file_item *c = item->util;
477 struct print_file_item_data *d = print_file_item_data;
479 strbuf_reset(&d->index);
480 strbuf_reset(&d->worktree);
481 strbuf_reset(&d->buf);
483 render_adddel(&d->worktree, &c->worktree, _("nothing"));
484 render_adddel(&d->index, &c->index, _("unchanged"));
485 strbuf_addf(&d->buf, d->modified_fmt,
486 d->index.buf, d->worktree.buf, item->string);
488 printf(" %2d: %s", i + 1, d->buf.buf);
491 static int run_status(struct add_i_state *s, const struct pathspec *ps,
492 struct string_list *files, struct list_options *opts)
494 if (get_modified_files(s->r, files, ps) < 0)
497 list(s, files, opts);
503 typedef int (*command_t)(struct add_i_state *s, const struct pathspec *ps,
504 struct string_list *files,
505 struct list_options *opts);
507 struct command_item {
508 size_t prefix_length;
512 static void print_command_item(int i, struct string_list_item *item,
513 void *print_command_item_data)
515 struct command_item *util = item->util;
517 if (!util->prefix_length ||
518 !is_valid_prefix(item->string, util->prefix_length))
519 printf(" %2d: %s", i + 1, item->string);
521 printf(" %2d: [%.*s]%s", i + 1,
522 (int)util->prefix_length, item->string,
523 item->string + util->prefix_length);
526 static void command_prompt_help(struct add_i_state *s)
528 const char *help_color = s->help_color;
529 color_fprintf_ln(stdout, help_color, "%s", _("Prompt help:"));
530 color_fprintf_ln(stdout, help_color, "1 - %s",
531 _("select a numbered item"));
532 color_fprintf_ln(stdout, help_color, "foo - %s",
533 _("select item based on unique prefix"));
534 color_fprintf_ln(stdout, help_color, " - %s",
535 _("(empty) select nothing"));
538 int run_add_i(struct repository *r, const struct pathspec *ps)
540 struct add_i_state s = { NULL };
541 struct list_and_choose_options main_loop_opts = {
542 { 4, N_("*** Commands ***"), print_command_item, NULL },
543 N_("What now"), command_prompt_help
549 { "status", run_status },
551 struct prefix_item_list commands = PREFIX_ITEM_LIST_INIT;
553 struct print_file_item_data print_file_item_data = {
554 "%12s %12s %s", STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
556 struct list_options opts = {
557 0, NULL, print_file_item, &print_file_item_data
559 struct strbuf header = STRBUF_INIT;
560 struct string_list files = STRING_LIST_INIT_DUP;
564 for (i = 0; i < ARRAY_SIZE(command_list); i++) {
565 struct command_item *util = xcalloc(sizeof(*util), 1);
566 util->command = command_list[i].command;
567 string_list_append(&commands.items, command_list[i].string)
571 init_add_i_state(&s, r);
573 strbuf_addstr(&header, " ");
574 strbuf_addf(&header, print_file_item_data.modified_fmt,
575 _("staged"), _("unstaged"), _("path"));
576 opts.header = header.buf;
578 if (discard_index(r->index) < 0 ||
579 repo_read_index(r) < 0 ||
580 repo_refresh_and_write_index(r, REFRESH_QUIET, 0, 1,
581 NULL, NULL, NULL) < 0)
582 warning(_("could not refresh index"));
584 res = run_status(&s, ps, &files, &opts);
587 i = list_and_choose(&s, &commands, &main_loop_opts);
588 if (i == LIST_AND_CHOOSE_QUIT) {
593 if (i != LIST_AND_CHOOSE_ERROR) {
594 struct command_item *util =
595 commands.items.items[i].util;
596 res = util->command(&s, ps, &files, &opts);
600 string_list_clear(&files, 1);
601 strbuf_release(&print_file_item_data.buf);
602 strbuf_release(&print_file_item_data.index);
603 strbuf_release(&print_file_item_data.worktree);
604 strbuf_release(&header);
605 prefix_item_list_clear(&commands);