Merge branch 'jk/gitweb-with-newer-cgi-multi-param' into maint
[gitweb.git] / builtin / clean.c
index ceab18257e8bd412c87a4b2ed5404d548ca72ea0..7e7fdcfe54a547c90a5a6911eb59b0ec22151ac6 100644 (file)
@@ -67,7 +67,7 @@ struct menu_item {
        char hotkey;
        const char *title;
        int selected;
-       int (*fn)();
+       int (*fn)(void);
 };
 
 enum menu_stuff_type {
@@ -100,7 +100,9 @@ static int parse_clean_color_slot(const char *var)
 
 static int git_clean_config(const char *var, const char *value, void *cb)
 {
-       if (!prefixcmp(var, "column."))
+       const char *slot_name;
+
+       if (starts_with(var, "column."))
                return git_column_config(var, value, "clean", &colopts);
 
        /* honors the color.interactive* config variables which also
@@ -109,15 +111,13 @@ static int git_clean_config(const char *var, const char *value, void *cb)
                clean_use_color = git_config_colorbool(var, value);
                return 0;
        }
-       if (!prefixcmp(var, "color.interactive.")) {
-               int slot = parse_clean_color_slot(var +
-                                                 strlen("color.interactive."));
+       if (skip_prefix(var, "color.interactive.", &slot_name)) {
+               int slot = parse_clean_color_slot(slot_name);
                if (slot < 0)
                        return 0;
                if (!value)
                        return config_error_nonbool(var);
-               color_parse(value, var, clean_colors[slot]);
-               return 0;
+               return color_parse(value, clean_colors[slot]);
        }
 
        if (!strcmp(var, "clean.requireforce")) {
@@ -154,7 +154,7 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
        DIR *dir;
        struct strbuf quoted = STRBUF_INIT;
        struct dirent *e;
-       int res = 0, ret = 0, gone = 1, original_len = path->len, len, i;
+       int res = 0, ret = 0, gone = 1, original_len = path->len, len;
        unsigned char submodule_head[20];
        struct string_list dels = STRING_LIST_INIT_DUP;
 
@@ -242,6 +242,7 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
        }
 
        if (!*dir_gone && !quiet) {
+               int i;
                for (i = 0; i < dels.nr; i++)
                        printf(dry_run ?  _(msg_would_remove) : _(msg_remove), dels.items[i].string);
        }
@@ -320,7 +321,7 @@ static void print_highlight_menu_stuff(struct menu_stuff *stuff, int **chosen)
 
        switch (stuff->type) {
        default:
-               die("Bad type of menu_staff when print menu");
+               die("Bad type of menu_stuff when print menu");
        case MENU_STUFF_TYPE_MENU_ITEM:
                menu_item = (struct menu_item *)stuff->stuff;
                for (i = 0; i < stuff->nr; i++, menu_item++) {
@@ -620,8 +621,7 @@ static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff)
                                nr += chosen[i];
                }
 
-               result = xmalloc(sizeof(int) * (nr + 1));
-               memset(result, 0, sizeof(int) * (nr + 1));
+               result = xcalloc(nr + 1, sizeof(int));
                for (i = 0; i < stuff->nr && j < nr; i++) {
                        if (chosen[i])
                                result[j++] = i;
@@ -903,11 +903,11 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
 
        if (!interactive && !dry_run && !force) {
                if (config_set)
-                       die(_("clean.requireForce set to true and neither -i, -n nor -f given; "
+                       die(_("clean.requireForce set to true and neither -i, -n, nor -f given; "
                                  "refusing to clean"));
                else
-                       die(_("clean.requireForce defaults to true and neither -i, -n nor -f given; "
-                                 "refusing to clean"));
+                       die(_("clean.requireForce defaults to true and neither -i, -n, nor -f given;"
+                                 " refusing to clean"));
        }
 
        if (force > 1)
@@ -933,48 +933,28 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
 
        for (i = 0; i < dir.nr; i++) {
                struct dir_entry *ent = dir.entries[i];
-               int len, pos;
                int matches = 0;
-               const struct cache_entry *ce;
                struct stat st;
                const char *rel;
 
-               /*
-                * Remove the '/' at the end that directory
-                * walking adds for directory entries.
-                */
-               len = ent->len;
-               if (len && ent->name[len-1] == '/')
-                       len--;
-               pos = cache_name_pos(ent->name, len);
-               if (0 <= pos)
-                       continue;       /* exact match */
-               pos = -pos - 1;
-               if (pos < active_nr) {
-                       ce = active_cache[pos];
-                       if (ce_namelen(ce) == len &&
-                           !memcmp(ce->name, ent->name, len))
-                               continue; /* Yup, this one exists unmerged */
-               }
+               if (!cache_name_is_other(ent->name, ent->len))
+                       continue;
 
                if (lstat(ent->name, &st))
                        die_errno("Cannot lstat '%s'", ent->name);
 
                if (pathspec.nr)
-                       matches = match_pathspec_depth(&pathspec, ent->name,
-                                                      len, 0, NULL);
+                       matches = dir_path_match(ent, &pathspec, 0, NULL);
 
-               if (S_ISDIR(st.st_mode)) {
-                       if (remove_directories || (matches == MATCHED_EXACTLY)) {
-                               rel = relative_path(ent->name, prefix, &buf);
-                               string_list_append(&del_list, rel);
-                       }
-               } else {
-                       if (pathspec.nr && !matches)
-                               continue;
-                       rel = relative_path(ent->name, prefix, &buf);
-                       string_list_append(&del_list, rel);
-               }
+               if (pathspec.nr && !matches)
+                       continue;
+
+               if (S_ISDIR(st.st_mode) && !remove_directories &&
+                   matches != MATCHED_EXACTLY)
+                       continue;
+
+               rel = relative_path(ent->name, prefix, &buf);
+               string_list_append(&del_list, rel);
        }
 
        if (interactive && del_list.nr > 0)