summaryrefslogtreecommitdiff
path: root/builtin
diff options
context:
space:
mode:
authorJiang Xin <worldhello.net@gmail.com>2013-07-24 10:22:04 +0800
committerJunio C Hamano <gitster@pobox.com>2013-07-24 19:16:51 -0700
commit60838613054f43d63086b0f4a7596baaee9e20c5 (patch)
treeca9eee2aafdbd902d54ec15b769d99c173630084 /builtin
parent309422e033bc2e25a3a80d1336993a75b7daaf33 (diff)
downloadgit-60838613054f43d63086b0f4a7596baaee9e20c5.tar.gz
git-clean: implement partial matching for selection
Document for interactive git-clean says: "You also could say `c` or `clean` above as long as the choice is unique". But it's not true, because only hotkey `c` and full match (`clean`) could work. Implement partial matching via find_unique function to make the document right. Signed-off-by: Jiang Xin <worldhello.net@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin')
-rw-r--r--builtin/clean.c80
1 files changed, 52 insertions, 28 deletions
diff --git a/builtin/clean.c b/builtin/clean.c
index bf03acfe88..203e9b7ea0 100644
--- a/builtin/clean.c
+++ b/builtin/clean.c
@@ -365,6 +365,56 @@ static void print_highlight_menu_stuff(struct menu_stuff *stuff, int **chosen)
string_list_clear(&menu_list, 0);
}
+static int find_unique(const char *choice, struct menu_stuff *menu_stuff)
+{
+ struct menu_item *menu_item;
+ struct string_list_item *string_list_item;
+ int i, len, found = 0;
+
+ len = strlen(choice);
+ switch (menu_stuff->type) {
+ default:
+ die("Bad type of menu_stuff when parse choice");
+ case MENU_STUFF_TYPE_MENU_ITEM:
+
+ menu_item = (struct menu_item *)menu_stuff->stuff;
+ for (i = 0; i < menu_stuff->nr; i++, menu_item++) {
+ if (len == 1 && *choice == menu_item->hotkey) {
+ found = i + 1;
+ break;
+ }
+ if (!strncasecmp(choice, menu_item->title, len)) {
+ if (found) {
+ if (len == 1) {
+ /* continue for hotkey matching */
+ found = -1;
+ } else {
+ found = 0;
+ break;
+ }
+ } else {
+ found = i + 1;
+ }
+ }
+ }
+ break;
+ case MENU_STUFF_TYPE_STRING_LIST:
+ string_list_item = ((struct string_list *)menu_stuff->stuff)->items;
+ for (i = 0; i < menu_stuff->nr; i++, string_list_item++) {
+ if (!strncasecmp(choice, string_list_item->string, len)) {
+ if (found) {
+ found = 0;
+ break;
+ }
+ found = i + 1;
+ }
+ }
+ break;
+ }
+ return found;
+}
+
+
/*
* Parse user input, and return choice(s) for menu (menu_stuff).
*
@@ -392,8 +442,6 @@ static int parse_choice(struct menu_stuff *menu_stuff,
int **chosen)
{
struct strbuf **choice_list, **ptr;
- struct menu_item *menu_item;
- struct string_list_item *string_list_item;
int nr = 0;
int i;
@@ -457,32 +505,8 @@ static int parse_choice(struct menu_stuff *menu_stuff,
bottom = 1;
top = menu_stuff->nr;
} else {
- switch (menu_stuff->type) {
- default:
- die("Bad type of menu_stuff when parse choice");
- case MENU_STUFF_TYPE_MENU_ITEM:
- menu_item = (struct menu_item *)menu_stuff->stuff;
- for (i = 0; i < menu_stuff->nr; i++, menu_item++) {
- if (((*ptr)->len == 1 &&
- *(*ptr)->buf == menu_item->hotkey) ||
- !strcasecmp((*ptr)->buf, menu_item->title)) {
- bottom = i + 1;
- top = bottom;
- break;
- }
- }
- break;
- case MENU_STUFF_TYPE_STRING_LIST:
- string_list_item = ((struct string_list *)menu_stuff->stuff)->items;
- for (i = 0; i < menu_stuff->nr; i++, string_list_item++) {
- if (!strcasecmp((*ptr)->buf, string_list_item->string)) {
- bottom = i + 1;
- top = bottom;
- break;
- }
- }
- break;
- }
+ bottom = find_unique((*ptr)->buf, menu_stuff);
+ top = bottom;
}
if (top <= 0 || bottom <= 0 || top > menu_stuff->nr || bottom > top ||