Skip to content

Commit ffe1ba2

Browse files
committed
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]>
1 parent 5031686 commit ffe1ba2

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, *endp;
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);
@@ -140,7 +269,10 @@ static ssize_t list_and_choose(struct add_i_state *s, struct string_list *items,
140269
}
141270

142271
p[sep] = '\0';
143-
if (index < 0 || index >= items->nr)
272+
if (index < 0)
273+
index = find_unique(p, items);
274+
275+
if (index < 0 || index >= items->items.nr)
144276
printf(_("Huh (%s)?\n"), p);
145277
else {
146278
res = index;
@@ -306,6 +438,23 @@ static void render_adddel(struct strbuf *buf,
306438
strbuf_addstr(buf, no_changes);
307439
}
308440

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

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

354516
int run_add_i(struct repository *r, const struct pathspec *ps)
@@ -364,7 +526,7 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
364526
} command_list[] = {
365527
{ "status", run_status },
366528
};
367-
struct string_list commands = STRING_LIST_INIT_NODUP;
529+
struct prefix_item_list commands = PREFIX_ITEM_LIST_INIT;
368530

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

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

384549
init_add_i_state(&s, r);
385550

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

@@ -414,7 +580,7 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
414580
strbuf_release(&print_file_item_data.index);
415581
strbuf_release(&print_file_item_data.worktree);
416582
strbuf_release(&header);
417-
string_list_clear(&commands, 0);
583+
prefix_item_list_clear(&commands);
418584

419585
return res;
420586
}

0 commit comments

Comments
 (0)