builtin-clean.con commit dir.c: export excluded_1() and add_excludes_from_file_1() (cb09753)
   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 "quote.h"
  14
  15static int force = -1; /* unset */
  16
  17static const char *const builtin_clean_usage[] = {
  18        "git clean [-d] [-f] [-n] [-q] [-x | -X] [--] <paths>...",
  19        NULL
  20};
  21
  22static int git_clean_config(const char *var, const char *value, void *cb)
  23{
  24        if (!strcmp(var, "clean.requireforce"))
  25                force = !git_config_bool(var, value);
  26        return git_default_config(var, value, cb);
  27}
  28
  29int cmd_clean(int argc, const char **argv, const char *prefix)
  30{
  31        int i;
  32        int show_only = 0, remove_directories = 0, quiet = 0, ignored = 0;
  33        int ignored_only = 0, baselen = 0, config_set = 0, errors = 0;
  34        struct strbuf directory = STRBUF_INIT;
  35        struct dir_struct dir;
  36        static const char **pathspec;
  37        struct strbuf buf = STRBUF_INIT;
  38        const char *qname;
  39        char *seen = NULL;
  40        struct option options[] = {
  41                OPT__QUIET(&quiet),
  42                OPT__DRY_RUN(&show_only),
  43                OPT_BOOLEAN('f', NULL, &force, "force"),
  44                OPT_BOOLEAN('d', NULL, &remove_directories,
  45                                "remove whole directories"),
  46                OPT_BOOLEAN('x', NULL, &ignored, "remove ignored files, too"),
  47                OPT_BOOLEAN('X', NULL, &ignored_only,
  48                                "remove only ignored files"),
  49                OPT_END()
  50        };
  51
  52        git_config(git_clean_config, NULL);
  53        if (force < 0)
  54                force = 0;
  55        else
  56                config_set = 1;
  57
  58        argc = parse_options(argc, argv, prefix, options, builtin_clean_usage,
  59                             0);
  60
  61        memset(&dir, 0, sizeof(dir));
  62        if (ignored_only)
  63                dir.flags |= DIR_SHOW_IGNORED;
  64
  65        if (ignored && ignored_only)
  66                die("-x and -X cannot be used together");
  67
  68        if (!show_only && !force)
  69                die("clean.requireForce%s set and -n or -f not given; "
  70                    "refusing to clean", config_set ? "" : " not");
  71
  72        dir.flags |= DIR_SHOW_OTHER_DIRECTORIES;
  73
  74        if (read_cache() < 0)
  75                die("index file corrupt");
  76
  77        if (!ignored)
  78                setup_standard_excludes(&dir);
  79
  80        pathspec = get_pathspec(prefix, argv);
  81
  82        fill_directory(&dir, pathspec);
  83
  84        if (pathspec)
  85                seen = xmalloc(argc > 0 ? argc : 1);
  86
  87        for (i = 0; i < dir.nr; i++) {
  88                struct dir_entry *ent = dir.entries[i];
  89                int len, pos;
  90                int matches = 0;
  91                struct cache_entry *ce;
  92                struct stat st;
  93
  94                /*
  95                 * Remove the '/' at the end that directory
  96                 * walking adds for directory entries.
  97                 */
  98                len = ent->len;
  99                if (len && ent->name[len-1] == '/')
 100                        len--;
 101                pos = cache_name_pos(ent->name, len);
 102                if (0 <= pos)
 103                        continue;       /* exact match */
 104                pos = -pos - 1;
 105                if (pos < active_nr) {
 106                        ce = active_cache[pos];
 107                        if (ce_namelen(ce) == len &&
 108                            !memcmp(ce->name, ent->name, len))
 109                                continue; /* Yup, this one exists unmerged */
 110                }
 111
 112                /*
 113                 * we might have removed this as part of earlier
 114                 * recursive directory removal, so lstat() here could
 115                 * fail with ENOENT.
 116                 */
 117                if (lstat(ent->name, &st))
 118                        continue;
 119
 120                if (pathspec) {
 121                        memset(seen, 0, argc > 0 ? argc : 1);
 122                        matches = match_pathspec(pathspec, ent->name, len,
 123                                                 baselen, seen);
 124                }
 125
 126                if (S_ISDIR(st.st_mode)) {
 127                        strbuf_addstr(&directory, ent->name);
 128                        qname = quote_path_relative(directory.buf, directory.len, &buf, prefix);
 129                        if (show_only && (remove_directories ||
 130                            (matches == MATCHED_EXACTLY))) {
 131                                printf("Would remove %s\n", qname);
 132                        } else if (remove_directories ||
 133                                   (matches == MATCHED_EXACTLY)) {
 134                                if (!quiet)
 135                                        printf("Removing %s\n", qname);
 136                                if (remove_dir_recursively(&directory, 0) != 0) {
 137                                        warning("failed to remove '%s'", qname);
 138                                        errors++;
 139                                }
 140                        } else if (show_only) {
 141                                printf("Would not remove %s\n", qname);
 142                        } else {
 143                                printf("Not removing %s\n", qname);
 144                        }
 145                        strbuf_reset(&directory);
 146                } else {
 147                        if (pathspec && !matches)
 148                                continue;
 149                        qname = quote_path_relative(ent->name, -1, &buf, prefix);
 150                        if (show_only) {
 151                                printf("Would remove %s\n", qname);
 152                                continue;
 153                        } else if (!quiet) {
 154                                printf("Removing %s\n", qname);
 155                        }
 156                        if (unlink(ent->name) != 0) {
 157                                warning("failed to remove '%s'", qname);
 158                                errors++;
 159                        }
 160                }
 161        }
 162        free(seen);
 163
 164        strbuf_release(&directory);
 165        return (errors != 0);
 166}