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