Skip to content

Commit 76b7432

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 6348bfb commit 76b7432

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);
@@ -142,7 +271,10 @@ static ssize_t list_and_choose(struct add_i_state *s, struct string_list *items,
142271

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

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

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

356518
int run_add_i(struct repository *r, const struct pathspec *ps)
@@ -366,7 +528,7 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
366528
} command_list[] = {
367529
{ "status", run_status },
368530
};
369-
struct string_list commands = STRING_LIST_INIT_NODUP;
531+
struct prefix_item_list commands = PREFIX_ITEM_LIST_INIT;
370532

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

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

386551
init_add_i_state(&s, r);
387552

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

@@ -416,7 +582,7 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
416582
strbuf_release(&print_file_item_data.index);
417583
strbuf_release(&print_file_item_data.worktree);
418584
strbuf_release(&header);
419-
string_list_clear(&commands, 0);
585+
prefix_item_list_clear(&commands);
420586

421587
return res;
422588
}

0 commit comments

Comments
 (0)