git-clean: refactor git-clean into two phases
[git/git.git] / builtin / clean.c
CommitLineData
113f10f2
SB
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"
f538a91e 13#include "refs.h"
07de4eba 14#include "string-list.h"
1fb32894 15#include "quote.h"
113f10f2 16
625db1b7 17static int force = -1; /* unset */
396049e5 18static struct string_list del_list = STRING_LIST_INIT_DUP;
113f10f2
SB
19
20static const char *const builtin_clean_usage[] = {
145f9c81 21 N_("git clean [-d] [-f] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>..."),
113f10f2
SB
22 NULL
23};
24
f538a91e
ZK
25static const char *msg_remove = N_("Removing %s\n");
26static const char *msg_would_remove = N_("Would remove %s\n");
27static const char *msg_skip_git_dir = N_("Skipping repository %s\n");
28static const char *msg_would_skip_git_dir = N_("Would skip repository %s\n");
29static const char *msg_warn_remove_failed = N_("failed to remove %s");
30
ef90d6d4 31static int git_clean_config(const char *var, const char *value, void *cb)
113f10f2
SB
32{
33 if (!strcmp(var, "clean.requireforce"))
34 force = !git_config_bool(var, value);
ef90d6d4 35 return git_default_config(var, value, cb);
113f10f2
SB
36}
37
07de4eba
JH
38static int exclude_cb(const struct option *opt, const char *arg, int unset)
39{
40 struct string_list *exclude_list = opt->value;
41 string_list_append(exclude_list, arg);
42 return 0;
43}
44
f538a91e
ZK
45static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
46 int dry_run, int quiet, int *dir_gone)
47{
48 DIR *dir;
49 struct strbuf quoted = STRBUF_INIT;
50 struct dirent *e;
51 int res = 0, ret = 0, gone = 1, original_len = path->len, len, i;
52 unsigned char submodule_head[20];
53 struct string_list dels = STRING_LIST_INIT_DUP;
54
55 *dir_gone = 1;
56
57 if ((force_flag & REMOVE_DIR_KEEP_NESTED_GIT) &&
58 !resolve_gitlink_ref(path->buf, "HEAD", submodule_head)) {
59 if (!quiet) {
39598f99 60 quote_path_relative(path->buf, prefix, &quoted);
f538a91e
ZK
61 printf(dry_run ? _(msg_would_skip_git_dir) : _(msg_skip_git_dir),
62 quoted.buf);
63 }
64
65 *dir_gone = 0;
66 return 0;
67 }
68
69 dir = opendir(path->buf);
70 if (!dir) {
71 /* an empty dir could be removed even if it is unreadble */
72 res = dry_run ? 0 : rmdir(path->buf);
73 if (res) {
39598f99 74 quote_path_relative(path->buf, prefix, &quoted);
f538a91e
ZK
75 warning(_(msg_warn_remove_failed), quoted.buf);
76 *dir_gone = 0;
77 }
78 return res;
79 }
80
81 if (path->buf[original_len - 1] != '/')
82 strbuf_addch(path, '/');
83
84 len = path->len;
85 while ((e = readdir(dir)) != NULL) {
86 struct stat st;
87 if (is_dot_or_dotdot(e->d_name))
88 continue;
89
90 strbuf_setlen(path, len);
91 strbuf_addstr(path, e->d_name);
92 if (lstat(path->buf, &st))
93 ; /* fall thru */
94 else if (S_ISDIR(st.st_mode)) {
95 if (remove_dirs(path, prefix, force_flag, dry_run, quiet, &gone))
96 ret = 1;
97 if (gone) {
39598f99 98 quote_path_relative(path->buf, prefix, &quoted);
f538a91e
ZK
99 string_list_append(&dels, quoted.buf);
100 } else
101 *dir_gone = 0;
102 continue;
103 } else {
104 res = dry_run ? 0 : unlink(path->buf);
105 if (!res) {
39598f99 106 quote_path_relative(path->buf, prefix, &quoted);
f538a91e
ZK
107 string_list_append(&dels, quoted.buf);
108 } else {
39598f99 109 quote_path_relative(path->buf, prefix, &quoted);
f538a91e
ZK
110 warning(_(msg_warn_remove_failed), quoted.buf);
111 *dir_gone = 0;
112 ret = 1;
113 }
114 continue;
115 }
116
117 /* path too long, stat fails, or non-directory still exists */
118 *dir_gone = 0;
119 ret = 1;
120 break;
121 }
122 closedir(dir);
123
124 strbuf_setlen(path, original_len);
125
126 if (*dir_gone) {
127 res = dry_run ? 0 : rmdir(path->buf);
128 if (!res)
129 *dir_gone = 1;
130 else {
39598f99 131 quote_path_relative(path->buf, prefix, &quoted);
f538a91e
ZK
132 warning(_(msg_warn_remove_failed), quoted.buf);
133 *dir_gone = 0;
134 ret = 1;
135 }
136 }
137
138 if (!*dir_gone && !quiet) {
139 for (i = 0; i < dels.nr; i++)
140 printf(dry_run ? _(msg_would_remove) : _(msg_remove), dels.items[i].string);
141 }
142 string_list_clear(&dels, 0);
143 return ret;
144}
145
113f10f2
SB
146int cmd_clean(int argc, const char **argv, const char *prefix)
147{
f538a91e
ZK
148 int i, res;
149 int dry_run = 0, remove_directories = 0, quiet = 0, ignored = 0;
150 int ignored_only = 0, config_set = 0, errors = 0, gone = 1;
a0f4afbe 151 int rm_flags = REMOVE_DIR_KEEP_NESTED_GIT;
396049e5 152 struct strbuf abs_path = STRBUF_INIT;
113f10f2 153 struct dir_struct dir;
113f10f2 154 static const char **pathspec;
f285a2d7 155 struct strbuf buf = STRBUF_INIT;
bdab6a59 156 struct string_list exclude_list = STRING_LIST_INIT_NODUP;
72aeb187 157 struct exclude_list *el;
396049e5 158 struct string_list_item *item;
1fb32894 159 const char *qname;
d871c869 160 char *seen = NULL;
113f10f2 161 struct option options[] = {
145f9c81 162 OPT__QUIET(&quiet, N_("do not print names of files removed")),
f538a91e 163 OPT__DRY_RUN(&dry_run, N_("dry run")),
145f9c81 164 OPT__FORCE(&force, N_("force")),
113f10f2 165 OPT_BOOLEAN('d', NULL, &remove_directories,
145f9c81
NTND
166 N_("remove whole directories")),
167 { OPTION_CALLBACK, 'e', "exclude", &exclude_list, N_("pattern"),
168 N_("add <pattern> to ignore rules"), PARSE_OPT_NONEG, exclude_cb },
169 OPT_BOOLEAN('x', NULL, &ignored, N_("remove ignored files, too")),
113f10f2 170 OPT_BOOLEAN('X', NULL, &ignored_only,
145f9c81 171 N_("remove only ignored files")),
113f10f2
SB
172 OPT_END()
173 };
174
ef90d6d4 175 git_config(git_clean_config, NULL);
625db1b7
JH
176 if (force < 0)
177 force = 0;
178 else
179 config_set = 1;
180
37782920
SB
181 argc = parse_options(argc, argv, prefix, options, builtin_clean_usage,
182 0);
113f10f2
SB
183
184 memset(&dir, 0, sizeof(dir));
1617adc7 185 if (ignored_only)
7c4c97c0 186 dir.flags |= DIR_SHOW_IGNORED;
113f10f2
SB
187
188 if (ignored && ignored_only)
2da57add 189 die(_("-x and -X cannot be used together"));
113f10f2 190
f538a91e 191 if (!dry_run && !force) {
a66f9b2a
ÆAB
192 if (config_set)
193 die(_("clean.requireForce set to true and neither -n nor -f given; "
194 "refusing to clean"));
195 else
196 die(_("clean.requireForce defaults to true and neither -n nor -f given; "
197 "refusing to clean"));
198 }
113f10f2 199
a0f4afbe
JH
200 if (force > 1)
201 rm_flags = 0;
202
7c4c97c0 203 dir.flags |= DIR_SHOW_OTHER_DIRECTORIES;
113f10f2 204
c28b3d6e 205 if (read_cache() < 0)
2da57add 206 die(_("index file corrupt"));
c28b3d6e 207
1617adc7
SB
208 if (!ignored)
209 setup_standard_excludes(&dir);
113f10f2 210
72aeb187 211 el = add_exclude_list(&dir, EXC_CMDL, "--exclude option");
07de4eba 212 for (i = 0; i < exclude_list.nr; i++)
72aeb187 213 add_exclude(exclude_list.items[i].string, "", 0, el, -(i+1));
07de4eba 214
113f10f2 215 pathspec = get_pathspec(prefix, argv);
113f10f2 216
1d8842d9 217 fill_directory(&dir, pathspec);
113f10f2 218
d871c869 219 if (pathspec)
a8db80c2 220 seen = xmalloc(argc > 0 ? argc : 1);
d871c869
JH
221
222 for (i = 0; i < dir.nr; i++) {
223 struct dir_entry *ent = dir.entries[i];
f2d0df71
SB
224 int len, pos;
225 int matches = 0;
113f10f2
SB
226 struct cache_entry *ce;
227 struct stat st;
396049e5 228 const char *rel;
113f10f2
SB
229
230 /*
231 * Remove the '/' at the end that directory
232 * walking adds for directory entries.
233 */
234 len = ent->len;
235 if (len && ent->name[len-1] == '/')
236 len--;
237 pos = cache_name_pos(ent->name, len);
238 if (0 <= pos)
239 continue; /* exact match */
240 pos = -pos - 1;
241 if (pos < active_nr) {
242 ce = active_cache[pos];
243 if (ce_namelen(ce) == len &&
244 !memcmp(ce->name, ent->name, len))
245 continue; /* Yup, this one exists unmerged */
246 }
247
d871c869 248 if (lstat(ent->name, &st))
396049e5 249 die_errno("Cannot lstat '%s'", ent->name);
d871c869
JH
250
251 if (pathspec) {
a8db80c2 252 memset(seen, 0, argc > 0 ? argc : 1);
f2d0df71 253 matches = match_pathspec(pathspec, ent->name, len,
1c7d402b 254 0, seen);
d871c869
JH
255 }
256
257 if (S_ISDIR(st.st_mode)) {
f538a91e 258 if (remove_directories || (matches == MATCHED_EXACTLY)) {
396049e5
JX
259 rel = relative_path(ent->name, prefix, &buf);
260 string_list_append(&del_list, rel);
113f10f2 261 }
113f10f2 262 } else {
d871c869
JH
263 if (pathspec && !matches)
264 continue;
396049e5
JX
265 rel = relative_path(ent->name, prefix, &buf);
266 string_list_append(&del_list, rel);
267 }
268 }
269
270 /* TODO: do interactive git-clean here, which will modify del_list */
271
272 for_each_string_list_item(item, &del_list) {
273 struct stat st;
274
275 if (prefix)
276 strbuf_addstr(&abs_path, prefix);
277
278 strbuf_addstr(&abs_path, item->string);
279
280 /*
281 * we might have removed this as part of earlier
282 * recursive directory removal, so lstat() here could
283 * fail with ENOENT.
284 */
285 if (lstat(abs_path.buf, &st))
286 continue;
287
288 if (S_ISDIR(st.st_mode)) {
289 if (remove_dirs(&abs_path, prefix, rm_flags, dry_run, quiet, &gone))
290 errors++;
291 if (gone && !quiet) {
292 qname = quote_path_relative(item->string, NULL, &buf);
293 printf(dry_run ? _(msg_would_remove) : _(msg_remove), qname);
294 }
295 } else {
296 res = dry_run ? 0 : unlink(abs_path.buf);
f538a91e 297 if (res) {
396049e5 298 qname = quote_path_relative(item->string, NULL, &buf);
f538a91e 299 warning(_(msg_warn_remove_failed), qname);
aa9c83c2 300 errors++;
f538a91e 301 } else if (!quiet) {
396049e5 302 qname = quote_path_relative(item->string, NULL, &buf);
f538a91e 303 printf(dry_run ? _(msg_would_remove) : _(msg_remove), qname);
aa9c83c2 304 }
113f10f2 305 }
396049e5 306 strbuf_reset(&abs_path);
113f10f2 307 }
d871c869 308 free(seen);
113f10f2 309
396049e5
JX
310 strbuf_release(&abs_path);
311 strbuf_release(&buf);
312 string_list_clear(&del_list, 0);
07de4eba 313 string_list_clear(&exclude_list, 0);
aa9c83c2 314 return (errors != 0);
113f10f2 315}