builtin / clean.con commit check-ignore: move setup into cmd_check_ignore() (0006d85)
   1/*
   2 * "git clean" builtin command
   3 *
   4 * Copyright (C) 2007 Shawn Bohrer
   5 *
   6 * Based on git-clean.sh by Pavel Roskin
   7 */
   8
   9#include "builtin.h"
  10#include "cache.h"
  11#include "dir.h"
  12#include "parse-options.h"
  13#include "string-list.h"
  14#include "quote.h"
  15
  16static int force = -1; /* unset */
  17
  18static const char *const builtin_clean_usage[] = {
  19        "git clean [-d] [-f] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>...",
  20        NULL
  21};
  22
  23static int git_clean_config(const char *var, const char *value, void *cb)
  24{
  25        if (!strcmp(var, "clean.requireforce"))
  26                force = !git_config_bool(var, value);
  27        return git_default_config(var, value, cb);
  28}
  29
  30static int exclude_cb(const struct option *opt, const char *arg, int unset)
  31{
  32        struct string_list *exclude_list = opt->value;
  33        string_list_append(exclude_list, arg);
  34        return 0;
  35}
  36
  37int cmd_clean(int argc, const char **argv, const char *prefix)
  38{
  39        int i;
  40        int show_only = 0, remove_directories = 0, quiet = 0, ignored = 0;
  41        int ignored_only = 0, config_set = 0, errors = 0;
  42        int rm_flags = REMOVE_DIR_KEEP_NESTED_GIT;
  43        struct strbuf directory = STRBUF_INIT;
  44        struct dir_struct dir;
  45        static const char **pathspec;
  46        struct strbuf buf = STRBUF_INIT;
  47        struct string_list exclude_list = STRING_LIST_INIT_NODUP;
  48        struct exclude_list *el;
  49        const char *qname;
  50        char *seen = NULL;
  51        struct option options[] = {
  52                OPT__QUIET(&quiet, "do not print names of files removed"),
  53                OPT__DRY_RUN(&show_only, "dry run"),
  54                OPT__FORCE(&force, "force"),
  55                OPT_BOOLEAN('d', NULL, &remove_directories,
  56                                "remove whole directories"),
  57                { OPTION_CALLBACK, 'e', "exclude", &exclude_list, "pattern",
  58                  "add <pattern> to ignore rules", PARSE_OPT_NONEG, exclude_cb },
  59                OPT_BOOLEAN('x', NULL, &ignored, "remove ignored files, too"),
  60                OPT_BOOLEAN('X', NULL, &ignored_only,
  61                                "remove only ignored files"),
  62                OPT_END()
  63        };
  64
  65        git_config(git_clean_config, NULL);
  66        if (force < 0)
  67                force = 0;
  68        else
  69                config_set = 1;
  70
  71        argc = parse_options(argc, argv, prefix, options, builtin_clean_usage,
  72                             0);
  73
  74        memset(&dir, 0, sizeof(dir));
  75        if (ignored_only)
  76                dir.flags |= DIR_SHOW_IGNORED;
  77
  78        if (ignored && ignored_only)
  79                die(_("-x and -X cannot be used together"));
  80
  81        if (!show_only && !force) {
  82                if (config_set)
  83                        die(_("clean.requireForce set to true and neither -n nor -f given; "
  84                                  "refusing to clean"));
  85                else
  86                        die(_("clean.requireForce defaults to true and neither -n nor -f given; "
  87                                  "refusing to clean"));
  88        }
  89
  90        if (force > 1)
  91                rm_flags = 0;
  92
  93        dir.flags |= DIR_SHOW_OTHER_DIRECTORIES;
  94
  95        if (read_cache() < 0)
  96                die(_("index file corrupt"));
  97
  98        if (!ignored)
  99                setup_standard_excludes(&dir);
 100
 101        el = add_exclude_list(&dir, EXC_CMDL, "--exclude option");
 102        for (i = 0; i < exclude_list.nr; i++)
 103                add_exclude(exclude_list.items[i].string, "", 0, el, -(i+1));
 104
 105        pathspec = get_pathspec(prefix, argv);
 106
 107        fill_directory(&dir, pathspec);
 108
 109        if (pathspec)
 110                seen = xmalloc(argc > 0 ? argc : 1);
 111
 112        for (i = 0; i < dir.nr; i++) {
 113                struct dir_entry *ent = dir.entries[i];
 114                int len, pos;
 115                int matches = 0;
 116                struct cache_entry *ce;
 117                struct stat st;
 118
 119                /*
 120                 * Remove the '/' at the end that directory
 121                 * walking adds for directory entries.
 122                 */
 123                len = ent->len;
 124                if (len && ent->name[len-1] == '/')
 125                        len--;
 126                pos = cache_name_pos(ent->name, len);
 127                if (0 <= pos)
 128                        continue;       /* exact match */
 129                pos = -pos - 1;
 130                if (pos < active_nr) {
 131                        ce = active_cache[pos];
 132                        if (ce_namelen(ce) == len &&
 133                            !memcmp(ce->name, ent->name, len))
 134                                continue; /* Yup, this one exists unmerged */
 135                }
 136
 137                /*
 138                 * we might have removed this as part of earlier
 139                 * recursive directory removal, so lstat() here could
 140                 * fail with ENOENT.
 141                 */
 142                if (lstat(ent->name, &st))
 143                        continue;
 144
 145                if (pathspec) {
 146                        memset(seen, 0, argc > 0 ? argc : 1);
 147                        matches = match_pathspec(pathspec, ent->name, len,
 148                                                 0, seen);
 149                }
 150
 151                if (S_ISDIR(st.st_mode)) {
 152                        strbuf_addstr(&directory, ent->name);
 153                        qname = quote_path_relative(directory.buf, directory.len, &buf, prefix);
 154                        if (show_only && (remove_directories ||
 155                            (matches == MATCHED_EXACTLY))) {
 156                                printf(_("Would remove %s\n"), qname);
 157                        } else if (remove_directories ||
 158                                   (matches == MATCHED_EXACTLY)) {
 159                                if (!quiet)
 160                                        printf(_("Removing %s\n"), qname);
 161                                if (remove_dir_recursively(&directory,
 162                                                           rm_flags) != 0) {
 163                                        warning(_("failed to remove %s"), qname);
 164                                        errors++;
 165                                }
 166                        } else if (show_only) {
 167                                printf(_("Would not remove %s\n"), qname);
 168                        } else {
 169                                printf(_("Not removing %s\n"), qname);
 170                        }
 171                        strbuf_reset(&directory);
 172                } else {
 173                        if (pathspec && !matches)
 174                                continue;
 175                        qname = quote_path_relative(ent->name, -1, &buf, prefix);
 176                        if (show_only) {
 177                                printf(_("Would remove %s\n"), qname);
 178                                continue;
 179                        } else if (!quiet) {
 180                                printf(_("Removing %s\n"), qname);
 181                        }
 182                        if (unlink(ent->name) != 0) {
 183                                warning(_("failed to remove %s"), qname);
 184                                errors++;
 185                        }
 186                }
 187        }
 188        free(seen);
 189
 190        strbuf_release(&directory);
 191        string_list_clear(&exclude_list, 0);
 192        return (errors != 0);
 193}