Merge branch 'jc/rev-list-options-fix'
[git] / builtin / clean.c
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
16 static int force = -1; /* unset */
17
18 static const char *const builtin_clean_usage[] = {
19         "git clean [-d] [-f] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>...",
20         NULL
21 };
22
23 static 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
30 static 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
37 int 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         const char *qname;
49         char *seen = NULL;
50         struct option options[] = {
51                 OPT__QUIET(&quiet, "do not print names of files removed"),
52                 OPT__DRY_RUN(&show_only, "dry run"),
53                 OPT__FORCE(&force, "force"),
54                 OPT_BOOLEAN('d', NULL, &remove_directories,
55                                 "remove whole directories"),
56                 { OPTION_CALLBACK, 'e', "exclude", &exclude_list, "pattern",
57                   "exclude <pattern>", PARSE_OPT_NONEG, exclude_cb },
58                 OPT_BOOLEAN('x', NULL, &ignored, "remove ignored files, too"),
59                 OPT_BOOLEAN('X', NULL, &ignored_only,
60                                 "remove only ignored files"),
61                 OPT_END()
62         };
63
64         git_config(git_clean_config, NULL);
65         if (force < 0)
66                 force = 0;
67         else
68                 config_set = 1;
69
70         argc = parse_options(argc, argv, prefix, options, builtin_clean_usage,
71                              0);
72
73         memset(&dir, 0, sizeof(dir));
74         if (ignored_only)
75                 dir.flags |= DIR_SHOW_IGNORED;
76
77         if (ignored && ignored_only)
78                 die(_("-x and -X cannot be used together"));
79
80         if (!show_only && !force) {
81                 if (config_set)
82                         die(_("clean.requireForce set to true and neither -n nor -f given; "
83                                   "refusing to clean"));
84                 else
85                         die(_("clean.requireForce defaults to true and neither -n nor -f given; "
86                                   "refusing to clean"));
87         }
88
89         if (force > 1)
90                 rm_flags = 0;
91
92         dir.flags |= DIR_SHOW_OTHER_DIRECTORIES;
93
94         if (read_cache() < 0)
95                 die(_("index file corrupt"));
96
97         if (!ignored)
98                 setup_standard_excludes(&dir);
99
100         for (i = 0; i < exclude_list.nr; i++)
101                 add_exclude(exclude_list.items[i].string, "", 0, dir.exclude_list);
102
103         pathspec = get_pathspec(prefix, argv);
104
105         fill_directory(&dir, pathspec);
106
107         if (pathspec)
108                 seen = xmalloc(argc > 0 ? argc : 1);
109
110         for (i = 0; i < dir.nr; i++) {
111                 struct dir_entry *ent = dir.entries[i];
112                 int len, pos;
113                 int matches = 0;
114                 struct cache_entry *ce;
115                 struct stat st;
116
117                 /*
118                  * Remove the '/' at the end that directory
119                  * walking adds for directory entries.
120                  */
121                 len = ent->len;
122                 if (len && ent->name[len-1] == '/')
123                         len--;
124                 pos = cache_name_pos(ent->name, len);
125                 if (0 <= pos)
126                         continue;       /* exact match */
127                 pos = -pos - 1;
128                 if (pos < active_nr) {
129                         ce = active_cache[pos];
130                         if (ce_namelen(ce) == len &&
131                             !memcmp(ce->name, ent->name, len))
132                                 continue; /* Yup, this one exists unmerged */
133                 }
134
135                 /*
136                  * we might have removed this as part of earlier
137                  * recursive directory removal, so lstat() here could
138                  * fail with ENOENT.
139                  */
140                 if (lstat(ent->name, &st))
141                         continue;
142
143                 if (pathspec) {
144                         memset(seen, 0, argc > 0 ? argc : 1);
145                         matches = match_pathspec(pathspec, ent->name, len,
146                                                  0, seen);
147                 }
148
149                 if (S_ISDIR(st.st_mode)) {
150                         strbuf_addstr(&directory, ent->name);
151                         qname = quote_path_relative(directory.buf, directory.len, &buf, prefix);
152                         if (show_only && (remove_directories ||
153                             (matches == MATCHED_EXACTLY))) {
154                                 printf(_("Would remove %s\n"), qname);
155                         } else if (remove_directories ||
156                                    (matches == MATCHED_EXACTLY)) {
157                                 if (!quiet)
158                                         printf(_("Removing %s\n"), qname);
159                                 if (remove_dir_recursively(&directory,
160                                                            rm_flags) != 0) {
161                                         warning(_("failed to remove %s"), qname);
162                                         errors++;
163                                 }
164                         } else if (show_only) {
165                                 printf(_("Would not remove %s\n"), qname);
166                         } else {
167                                 printf(_("Not removing %s\n"), qname);
168                         }
169                         strbuf_reset(&directory);
170                 } else {
171                         if (pathspec && !matches)
172                                 continue;
173                         qname = quote_path_relative(ent->name, -1, &buf, prefix);
174                         if (show_only) {
175                                 printf(_("Would remove %s\n"), qname);
176                                 continue;
177                         } else if (!quiet) {
178                                 printf(_("Removing %s\n"), qname);
179                         }
180                         if (unlink(ent->name) != 0) {
181                                 warning(_("failed to remove %s"), qname);
182                                 errors++;
183                         }
184                 }
185         }
186         free(seen);
187
188         strbuf_release(&directory);
189         string_list_clear(&exclude_list, 0);
190         return (errors != 0);
191 }