Merge branch 'np/lookup-object-hashing'
[gitweb.git] / builtin / clean.c
index dba8387747b0fa5129b9270ce5140ba847a7a139..615cd57caf1d4cbeafc73e7037bae81a6915df59 100644 (file)
@@ -15,6 +15,7 @@
 #include "quote.h"
 #include "column.h"
 #include "color.h"
+#include "pathspec.h"
 
 static int force = -1; /* unset */
 static int interactive;
@@ -365,6 +366,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 +443,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 +506,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 ||
@@ -839,24 +864,23 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
        int rm_flags = REMOVE_DIR_KEEP_NESTED_GIT;
        struct strbuf abs_path = STRBUF_INIT;
        struct dir_struct dir;
-       static const char **pathspec;
+       struct pathspec pathspec;
        struct strbuf buf = STRBUF_INIT;
        struct string_list exclude_list = STRING_LIST_INIT_NODUP;
        struct exclude_list *el;
        struct string_list_item *item;
        const char *qname;
-       char *seen = NULL;
        struct option options[] = {
                OPT__QUIET(&quiet, N_("do not print names of files removed")),
                OPT__DRY_RUN(&dry_run, N_("dry run")),
                OPT__FORCE(&force, N_("force")),
                OPT_BOOL('i', "interactive", &interactive, N_("interactive cleaning")),
-               OPT_BOOLEAN('d', NULL, &remove_directories,
+               OPT_BOOL('d', NULL, &remove_directories,
                                N_("remove whole directories")),
                { OPTION_CALLBACK, 'e', "exclude", &exclude_list, N_("pattern"),
                  N_("add <pattern> to ignore rules"), PARSE_OPT_NONEG, exclude_cb },
-               OPT_BOOLEAN('x', NULL, &ignored, N_("remove ignored files, too")),
-               OPT_BOOLEAN('X', NULL, &ignored_only,
+               OPT_BOOL('x', NULL, &ignored, N_("remove ignored files, too")),
+               OPT_BOOL('X', NULL, &ignored_only,
                                N_("remove only ignored files")),
                OPT_END()
        };
@@ -901,12 +925,11 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
        for (i = 0; i < exclude_list.nr; i++)
                add_exclude(exclude_list.items[i].string, "", 0, el, -(i+1));
 
-       pathspec = get_pathspec(prefix, argv);
+       parse_pathspec(&pathspec, 0,
+                      PATHSPEC_PREFER_CWD,
+                      prefix, argv);
 
-       fill_directory(&dir, pathspec);
-
-       if (pathspec)
-               seen = xmalloc(argc > 0 ? argc : 1);
+       fill_directory(&dir, &pathspec);
 
        for (i = 0; i < dir.nr; i++) {
                struct dir_entry *ent = dir.entries[i];
@@ -937,11 +960,9 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
                if (lstat(ent->name, &st))
                        die_errno("Cannot lstat '%s'", ent->name);
 
-               if (pathspec) {
-                       memset(seen, 0, argc > 0 ? argc : 1);
-                       matches = match_pathspec(pathspec, ent->name, len,
-                                                0, seen);
-               }
+               if (pathspec.nr)
+                       matches = match_pathspec_depth(&pathspec, ent->name,
+                                                      len, 0, NULL);
 
                if (S_ISDIR(st.st_mode)) {
                        if (remove_directories || (matches == MATCHED_EXACTLY)) {
@@ -949,7 +970,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
                                string_list_append(&del_list, rel);
                        }
                } else {
-                       if (pathspec && !matches)
+                       if (pathspec.nr && !matches)
                                continue;
                        rel = relative_path(ent->name, prefix, &buf);
                        string_list_append(&del_list, rel);
@@ -995,7 +1016,6 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
                }
                strbuf_reset(&abs_path);
        }
-       free(seen);
 
        strbuf_release(&abs_path);
        strbuf_release(&buf);