Skip to content

Commit 5fb9e8d

Browse files
dschogitster
authored andcommitted
built-in add -i: show unique prefixes of the commands
Just like in the Perl script `git-add--interactive.perl`, for each command a unique prefix is determined (if there exists any within the given parameters), and shown in the list, and accepted as a shortcut for the command. To determine the unique prefixes, as well as to look up the command in question, we use a copy of the list and sort it. While this might seem like overkill for a single command, it will make much more sense when all the commands are implemented, and when we reuse the same logic to present a list of files to edit, with convenient unique prefixes. At the start of the development of this patch series, a dedicated data structure was introduced that imitated the Trie that the Perl version implements. However, this was deemed overkill, and we now simply sort the list before determining the length of the unique prefixes by looking at each item's neighbor. As a bonus, we now use the same sorted list to perform a binary search using the user-provided prefix as search key. Original-patch-by: Slavica Đukić <[email protected]> Helped-by: SZEDER Gábor <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d0d1938 commit 5fb9e8d

File tree

1 file changed

+177
-11
lines changed

1 file changed

+177
-11
lines changed

add-interactive.c

Lines changed: 177 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,132 @@ static void init_add_i_state(struct add_i_state *s, struct repository *r)
4545
init_color(r, s, "header", s->header_color, GIT_COLOR_BOLD);
4646
}
4747

48+
/*
49+
* A "prefix item list" is a list of items that are identified by a string, and
50+
* a unique prefix (if any) is determined for each item.
51+
*
52+
* It is implemented in the form of a pair of `string_list`s, the first one
53+
* duplicating the strings, with the `util` field pointing at a structure whose
54+
* first field must be `size_t prefix_length`.
55+
*
56+
* That `prefix_length` field will be computed by `find_unique_prefixes()`; It
57+
* will be set to zero if no valid, unique prefix could be found.
58+
*
59+
* The second `string_list` is called `sorted` and does _not_ duplicate the
60+
* strings but simply reuses the first one's, with the `util` field pointing at
61+
* the `string_item_list` of the first `string_list`. It will be populated and
62+
* sorted by `find_unique_prefixes()`.
63+
*/
64+
struct prefix_item_list {
65+
struct string_list items;
66+
struct string_list sorted;
67+
size_t min_length, max_length;
68+
};
69+
#define PREFIX_ITEM_LIST_INIT \
70+
{ STRING_LIST_INIT_DUP, STRING_LIST_INIT_NODUP, 1, 4 }
71+
72+
static void prefix_item_list_clear(struct prefix_item_list *list)
73+
{
74+
string_list_clear(&list->items, 1);
75+
string_list_clear(&list->sorted, 0);
76+
}
77+
78+
static void extend_prefix_length(struct string_list_item *p,
79+
const char *other_string, size_t max_length)
80+
{
81+
size_t *len = p->util;
82+
83+
if (!*len || memcmp(p->string, other_string, *len))
84+
return;
85+
86+
for (;;) {
87+
char c = p->string[*len];
88+
89+
/*
90+
* Is `p` a strict prefix of `other`? Or have we exhausted the
91+
* maximal length of the prefix? Or is the current character a
92+
* multi-byte UTF-8 one? If so, there is no valid, unique
93+
* prefix.
94+
*/
95+
if (!c || ++*len > max_length || !isascii(c)) {
96+
*len = 0;
97+
break;
98+
}
99+
100+
if (c != other_string[*len - 1])
101+
break;
102+
}
103+
}
104+
105+
static void find_unique_prefixes(struct prefix_item_list *list)
106+
{
107+
size_t i;
108+
109+
if (list->sorted.nr == list->items.nr)
110+
return;
111+
112+
string_list_clear(&list->sorted, 0);
113+
/* Avoid reallocating incrementally */
114+
list->sorted.items = xmalloc(st_mult(sizeof(*list->sorted.items),
115+
list->items.nr));
116+
list->sorted.nr = list->sorted.alloc = list->items.nr;
117+
118+
for (i = 0; i < list->items.nr; i++) {
119+
list->sorted.items[i].string = list->items.items[i].string;
120+
list->sorted.items[i].util = list->items.items + i;
121+
}
122+
123+
string_list_sort(&list->sorted);
124+
125+
for (i = 0; i < list->sorted.nr; i++) {
126+
struct string_list_item *sorted_item = list->sorted.items + i;
127+
struct string_list_item *item = sorted_item->util;
128+
size_t *len = item->util;
129+
130+
*len = 0;
131+
while (*len < list->min_length) {
132+
char c = item->string[(*len)++];
133+
134+
if (!c || !isascii(c)) {
135+
*len = 0;
136+
break;
137+
}
138+
}
139+
140+
if (i > 0)
141+
extend_prefix_length(item, sorted_item[-1].string,
142+
list->max_length);
143+
if (i + 1 < list->sorted.nr)
144+
extend_prefix_length(item, sorted_item[1].string,
145+
list->max_length);
146+
}
147+
}
148+
149+
static ssize_t find_unique(const char *string, struct prefix_item_list *list)
150+
{
151+
int index = string_list_find_insert_index(&list->sorted, string, 1);
152+
struct string_list_item *item;
153+
154+
if (list->items.nr != list->sorted.nr)
155+
BUG("prefix_item_list in inconsistent state (%"PRIuMAX
156+
" vs %"PRIuMAX")",
157+
(uintmax_t)list->items.nr, (uintmax_t)list->sorted.nr);
158+
159+
if (index < 0)
160+
item = list->sorted.items[-1 - index].util;
161+
else if (index > 0 &&
162+
starts_with(list->sorted.items[index - 1].string, string))
163+
return -1;
164+
else if (index + 1 < list->sorted.nr &&
165+
starts_with(list->sorted.items[index + 1].string, string))
166+
return -1;
167+
else if (index < list->sorted.nr)
168+
item = list->sorted.items[index].util;
169+
else
170+
return -1;
171+
return item - list->items.items;
172+
}
173+
48174
struct list_options {
49175
int columns;
50176
const char *header;
@@ -95,18 +221,21 @@ struct list_and_choose_options {
95221
* If an error occurred, returns `LIST_AND_CHOOSE_ERROR`. Upon EOF,
96222
* `LIST_AND_CHOOSE_QUIT` is returned.
97223
*/
98-
static ssize_t list_and_choose(struct add_i_state *s, struct string_list *items,
224+
static ssize_t list_and_choose(struct add_i_state *s,
225+
struct prefix_item_list *items,
99226
struct list_and_choose_options *opts)
100227
{
101228
struct strbuf input = STRBUF_INIT;
102229
ssize_t res = LIST_AND_CHOOSE_ERROR;
103230

231+
find_unique_prefixes(items);
232+
104233
for (;;) {
105234
char *p;
106235

107236
strbuf_reset(&input);
108237

109-
list(s, items, &opts->list_opts);
238+
list(s, &items->items, &opts->list_opts);
110239

111240
printf("%s%s", opts->prompt, "> ");
112241
fflush(stdout);
@@ -141,7 +270,10 @@ static ssize_t list_and_choose(struct add_i_state *s, struct string_list *items,
141270
}
142271

143272
p[sep] = '\0';
144-
if (index < 0 || index >= items->nr)
273+
if (index < 0)
274+
index = find_unique(p, items);
275+
276+
if (index < 0 || index >= items->items.nr)
145277
printf(_("Huh (%s)?\n"), p);
146278
else {
147279
res = index;
@@ -307,6 +439,23 @@ static void render_adddel(struct strbuf *buf,
307439
strbuf_addstr(buf, no_changes);
308440
}
309441

442+
/* filters out prefixes which have special meaning to list_and_choose() */
443+
static int is_valid_prefix(const char *prefix, size_t prefix_len)
444+
{
445+
return prefix_len && prefix &&
446+
/*
447+
* We expect `prefix` to be NUL terminated, therefore this
448+
* `strcspn()` call is okay, even if it might do much more
449+
* work than strictly necessary.
450+
*/
451+
strcspn(prefix, " \t\r\n,") >= prefix_len && /* separators */
452+
*prefix != '-' && /* deselection */
453+
!isdigit(*prefix) && /* selection */
454+
(prefix_len != 1 ||
455+
(*prefix != '*' && /* "all" wildcard */
456+
*prefix != '?')); /* prompt help */
457+
}
458+
310459
struct print_file_item_data {
311460
const char *modified_fmt;
312461
struct strbuf buf, index, worktree;
@@ -346,10 +495,23 @@ typedef int (*command_t)(struct add_i_state *s, const struct pathspec *ps,
346495
struct string_list *files,
347496
struct list_options *opts);
348497

498+
struct command_item {
499+
size_t prefix_length;
500+
command_t command;
501+
};
502+
349503
static void print_command_item(int i, struct string_list_item *item,
350504
void *print_command_item_data)
351505
{
352-
printf(" %2d: %s", i + 1, item->string);
506+
struct command_item *util = item->util;
507+
508+
if (!util->prefix_length ||
509+
!is_valid_prefix(item->string, util->prefix_length))
510+
printf(" %2d: %s", i + 1, item->string);
511+
else
512+
printf(" %2d: [%.*s]%s", i + 1,
513+
(int)util->prefix_length, item->string,
514+
item->string + util->prefix_length);
353515
}
354516

355517
int run_add_i(struct repository *r, const struct pathspec *ps)
@@ -365,7 +527,7 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
365527
} command_list[] = {
366528
{ "status", run_status },
367529
};
368-
struct string_list commands = STRING_LIST_INIT_NODUP;
530+
struct prefix_item_list commands = PREFIX_ITEM_LIST_INIT;
369531

370532
struct print_file_item_data print_file_item_data = {
371533
"%12s %12s %s", STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
@@ -378,9 +540,12 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
378540
ssize_t i;
379541
int res = 0;
380542

381-
for (i = 0; i < ARRAY_SIZE(command_list); i++)
382-
string_list_append(&commands, command_list[i].string)
383-
->util = command_list[i].command;
543+
for (i = 0; i < ARRAY_SIZE(command_list); i++) {
544+
struct command_item *util = xcalloc(sizeof(*util), 1);
545+
util->command = command_list[i].command;
546+
string_list_append(&commands.items, command_list[i].string)
547+
->util = util;
548+
}
384549

385550
init_add_i_state(&s, r);
386551

@@ -405,8 +570,9 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
405570
break;
406571
}
407572
if (i != LIST_AND_CHOOSE_ERROR) {
408-
command_t command = commands.items[i].util;
409-
res = command(&s, ps, &files, &opts);
573+
struct command_item *util =
574+
commands.items.items[i].util;
575+
res = util->command(&s, ps, &files, &opts);
410576
}
411577
}
412578

@@ -415,7 +581,7 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
415581
strbuf_release(&print_file_item_data.index);
416582
strbuf_release(&print_file_item_data.worktree);
417583
strbuf_release(&header);
418-
string_list_clear(&commands, 0);
584+
prefix_item_list_clear(&commands);
419585

420586
return res;
421587
}

0 commit comments

Comments
 (0)