blob: 75cc6a878c6174bd028d09db6428e76ce3b555a8 [file] [log] [blame]
Shawn Bohrer113f10f2007-11-11 19:48:47 -06001/*
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"
Zoltan Klingerf538a912013-01-11 09:53:46 +110013#include "refs.h"
Jared Hance07de4eb2010-07-20 15:35:56 -040014#include "string-list.h"
Dmitry Potapov1fb32892008-03-07 04:13:17 +030015#include "quote.h"
Jiang Xin1b8fd462013-06-25 23:53:49 +080016#include "column.h"
Shawn Bohrer113f10f2007-11-11 19:48:47 -060017
Junio C Hamano625db1b2007-11-12 21:13:05 -080018static int force = -1; /* unset */
Jiang Xin17696002013-06-25 23:53:48 +080019static int interactive;
Jiang Xin396049e2013-06-25 23:53:47 +080020static struct string_list del_list = STRING_LIST_INIT_DUP;
Jiang Xin1b8fd462013-06-25 23:53:49 +080021static unsigned int colopts;
Shawn Bohrer113f10f2007-11-11 19:48:47 -060022
23static const char *const builtin_clean_usage[] = {
Jiang Xin17696002013-06-25 23:53:48 +080024 N_("git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>..."),
Shawn Bohrer113f10f2007-11-11 19:48:47 -060025 NULL
26};
27
Zoltan Klingerf538a912013-01-11 09:53:46 +110028static const char *msg_remove = N_("Removing %s\n");
29static const char *msg_would_remove = N_("Would remove %s\n");
30static const char *msg_skip_git_dir = N_("Skipping repository %s\n");
31static const char *msg_would_skip_git_dir = N_("Would skip repository %s\n");
32static const char *msg_warn_remove_failed = N_("failed to remove %s");
33
Johannes Schindelinef90d6d2008-05-14 18:46:53 +010034static int git_clean_config(const char *var, const char *value, void *cb)
Shawn Bohrer113f10f2007-11-11 19:48:47 -060035{
Jiang Xin1b8fd462013-06-25 23:53:49 +080036 if (!prefixcmp(var, "column."))
37 return git_column_config(var, value, "clean", &colopts);
38
39 if (!strcmp(var, "clean.requireforce")) {
Shawn Bohrer113f10f2007-11-11 19:48:47 -060040 force = !git_config_bool(var, value);
Jiang Xin1b8fd462013-06-25 23:53:49 +080041 return 0;
42 }
Johannes Schindelinef90d6d2008-05-14 18:46:53 +010043 return git_default_config(var, value, cb);
Shawn Bohrer113f10f2007-11-11 19:48:47 -060044}
45
Jared Hance07de4eb2010-07-20 15:35:56 -040046static int exclude_cb(const struct option *opt, const char *arg, int unset)
47{
48 struct string_list *exclude_list = opt->value;
49 string_list_append(exclude_list, arg);
50 return 0;
51}
52
Zoltan Klingerf538a912013-01-11 09:53:46 +110053static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
54 int dry_run, int quiet, int *dir_gone)
55{
56 DIR *dir;
57 struct strbuf quoted = STRBUF_INIT;
58 struct dirent *e;
59 int res = 0, ret = 0, gone = 1, original_len = path->len, len, i;
60 unsigned char submodule_head[20];
61 struct string_list dels = STRING_LIST_INIT_DUP;
62
63 *dir_gone = 1;
64
65 if ((force_flag & REMOVE_DIR_KEEP_NESTED_GIT) &&
66 !resolve_gitlink_ref(path->buf, "HEAD", submodule_head)) {
67 if (!quiet) {
Jiang Xin39598f92013-06-25 23:53:45 +080068 quote_path_relative(path->buf, prefix, &quoted);
Zoltan Klingerf538a912013-01-11 09:53:46 +110069 printf(dry_run ? _(msg_would_skip_git_dir) : _(msg_skip_git_dir),
70 quoted.buf);
71 }
72
73 *dir_gone = 0;
74 return 0;
75 }
76
77 dir = opendir(path->buf);
78 if (!dir) {
79 /* an empty dir could be removed even if it is unreadble */
80 res = dry_run ? 0 : rmdir(path->buf);
81 if (res) {
Jiang Xin39598f92013-06-25 23:53:45 +080082 quote_path_relative(path->buf, prefix, &quoted);
Zoltan Klingerf538a912013-01-11 09:53:46 +110083 warning(_(msg_warn_remove_failed), quoted.buf);
84 *dir_gone = 0;
85 }
86 return res;
87 }
88
89 if (path->buf[original_len - 1] != '/')
90 strbuf_addch(path, '/');
91
92 len = path->len;
93 while ((e = readdir(dir)) != NULL) {
94 struct stat st;
95 if (is_dot_or_dotdot(e->d_name))
96 continue;
97
98 strbuf_setlen(path, len);
99 strbuf_addstr(path, e->d_name);
100 if (lstat(path->buf, &st))
101 ; /* fall thru */
102 else if (S_ISDIR(st.st_mode)) {
103 if (remove_dirs(path, prefix, force_flag, dry_run, quiet, &gone))
104 ret = 1;
105 if (gone) {
Jiang Xin39598f92013-06-25 23:53:45 +0800106 quote_path_relative(path->buf, prefix, &quoted);
Zoltan Klingerf538a912013-01-11 09:53:46 +1100107 string_list_append(&dels, quoted.buf);
108 } else
109 *dir_gone = 0;
110 continue;
111 } else {
112 res = dry_run ? 0 : unlink(path->buf);
113 if (!res) {
Jiang Xin39598f92013-06-25 23:53:45 +0800114 quote_path_relative(path->buf, prefix, &quoted);
Zoltan Klingerf538a912013-01-11 09:53:46 +1100115 string_list_append(&dels, quoted.buf);
116 } else {
Jiang Xin39598f92013-06-25 23:53:45 +0800117 quote_path_relative(path->buf, prefix, &quoted);
Zoltan Klingerf538a912013-01-11 09:53:46 +1100118 warning(_(msg_warn_remove_failed), quoted.buf);
119 *dir_gone = 0;
120 ret = 1;
121 }
122 continue;
123 }
124
125 /* path too long, stat fails, or non-directory still exists */
126 *dir_gone = 0;
127 ret = 1;
128 break;
129 }
130 closedir(dir);
131
132 strbuf_setlen(path, original_len);
133
134 if (*dir_gone) {
135 res = dry_run ? 0 : rmdir(path->buf);
136 if (!res)
137 *dir_gone = 1;
138 else {
Jiang Xin39598f92013-06-25 23:53:45 +0800139 quote_path_relative(path->buf, prefix, &quoted);
Zoltan Klingerf538a912013-01-11 09:53:46 +1100140 warning(_(msg_warn_remove_failed), quoted.buf);
141 *dir_gone = 0;
142 ret = 1;
143 }
144 }
145
146 if (!*dir_gone && !quiet) {
147 for (i = 0; i < dels.nr; i++)
148 printf(dry_run ? _(msg_would_remove) : _(msg_remove), dels.items[i].string);
149 }
150 string_list_clear(&dels, 0);
151 return ret;
152}
153
Jiang Xin1b8fd462013-06-25 23:53:49 +0800154static void pretty_print_dels(void)
155{
156 struct string_list list = STRING_LIST_INIT_DUP;
157 struct string_list_item *item;
158 struct strbuf buf = STRBUF_INIT;
159 const char *qname;
160 struct column_options copts;
161
162 for_each_string_list_item(item, &del_list) {
163 qname = quote_path_relative(item->string, NULL, &buf);
164 string_list_append(&list, qname);
165 }
166
167 /*
168 * always enable column display, we only consult column.*
169 * about layout strategy and stuff
170 */
171 colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
172 memset(&copts, 0, sizeof(copts));
173 copts.indent = " ";
174 copts.padding = 2;
175 print_columns(&list, colopts, &copts);
176 putchar('\n');
177 strbuf_release(&buf);
178 string_list_clear(&list, 0);
179}
180
Jiang Xin17696002013-06-25 23:53:48 +0800181static void interactive_main_loop(void)
182{
183 struct strbuf confirm = STRBUF_INIT;
Jiang Xin17696002013-06-25 23:53:48 +0800184
185 while (del_list.nr) {
186 putchar('\n');
Jiang Xin1b8fd462013-06-25 23:53:49 +0800187 printf_ln(Q_("Would remove the following item:",
188 "Would remove the following items:",
189 del_list.nr));
Jiang Xin17696002013-06-25 23:53:48 +0800190 putchar('\n');
191
Jiang Xin1b8fd462013-06-25 23:53:49 +0800192 pretty_print_dels();
193
Jiang Xin17696002013-06-25 23:53:48 +0800194 printf(_("Remove [y/n]? "));
195 if (strbuf_getline(&confirm, stdin, '\n') != EOF) {
196 strbuf_trim(&confirm);
197 } else {
198 /* Ctrl-D is the same as "quit" */
199 string_list_clear(&del_list, 0);
200 putchar('\n');
201 printf_ln("Bye.");
202 break;
203 }
204
205 if (confirm.len) {
206 if (!strncasecmp(confirm.buf, "yes", confirm.len)) {
207 break;
208 } else if (!strncasecmp(confirm.buf, "no", confirm.len) ||
209 !strncasecmp(confirm.buf, "quit", confirm.len)) {
210 string_list_clear(&del_list, 0);
211 printf_ln("Bye.");
212 break;
213 } else {
214 continue;
215 }
216 }
217 }
218
Jiang Xin17696002013-06-25 23:53:48 +0800219 strbuf_release(&confirm);
220}
221
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600222int cmd_clean(int argc, const char **argv, const char *prefix)
223{
Zoltan Klingerf538a912013-01-11 09:53:46 +1100224 int i, res;
225 int dry_run = 0, remove_directories = 0, quiet = 0, ignored = 0;
226 int ignored_only = 0, config_set = 0, errors = 0, gone = 1;
Junio C Hamanoa0f4afb2009-06-30 15:33:45 -0700227 int rm_flags = REMOVE_DIR_KEEP_NESTED_GIT;
Jiang Xin396049e2013-06-25 23:53:47 +0800228 struct strbuf abs_path = STRBUF_INIT;
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600229 struct dir_struct dir;
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600230 static const char **pathspec;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500231 struct strbuf buf = STRBUF_INIT;
Thiago Farinabdab6a52010-09-06 20:32:55 -0300232 struct string_list exclude_list = STRING_LIST_INIT_NODUP;
Adam Spiers72aeb182013-01-16 13:25:58 +0000233 struct exclude_list *el;
Jiang Xin396049e2013-06-25 23:53:47 +0800234 struct string_list_item *item;
Dmitry Potapov1fb32892008-03-07 04:13:17 +0300235 const char *qname;
Junio C Hamanod871c862007-12-04 23:55:41 -0800236 char *seen = NULL;
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600237 struct option options[] = {
Nguyễn Thái Ngọc Duy145f9c82012-08-20 19:32:01 +0700238 OPT__QUIET(&quiet, N_("do not print names of files removed")),
Zoltan Klingerf538a912013-01-11 09:53:46 +1100239 OPT__DRY_RUN(&dry_run, N_("dry run")),
Nguyễn Thái Ngọc Duy145f9c82012-08-20 19:32:01 +0700240 OPT__FORCE(&force, N_("force")),
Jiang Xin17696002013-06-25 23:53:48 +0800241 OPT_BOOL('i', "interactive", &interactive, N_("interactive cleaning")),
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600242 OPT_BOOLEAN('d', NULL, &remove_directories,
Nguyễn Thái Ngọc Duy145f9c82012-08-20 19:32:01 +0700243 N_("remove whole directories")),
244 { OPTION_CALLBACK, 'e', "exclude", &exclude_list, N_("pattern"),
245 N_("add <pattern> to ignore rules"), PARSE_OPT_NONEG, exclude_cb },
246 OPT_BOOLEAN('x', NULL, &ignored, N_("remove ignored files, too")),
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600247 OPT_BOOLEAN('X', NULL, &ignored_only,
Nguyễn Thái Ngọc Duy145f9c82012-08-20 19:32:01 +0700248 N_("remove only ignored files")),
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600249 OPT_END()
250 };
251
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100252 git_config(git_clean_config, NULL);
Junio C Hamano625db1b2007-11-12 21:13:05 -0800253 if (force < 0)
254 force = 0;
255 else
256 config_set = 1;
257
Stephen Boyd37782922009-05-23 11:53:12 -0700258 argc = parse_options(argc, argv, prefix, options, builtin_clean_usage,
259 0);
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600260
261 memset(&dir, 0, sizeof(dir));
Shawn Bohrer1617adc2007-11-14 23:00:54 -0600262 if (ignored_only)
Johannes Schindelin7c4c97c2009-02-16 13:20:25 +0100263 dir.flags |= DIR_SHOW_IGNORED;
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600264
265 if (ignored && ignored_only)
Ævar Arnfjörð Bjarmason2da57ad2011-02-22 23:42:21 +0000266 die(_("-x and -X cannot be used together"));
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600267
Jiang Xin17696002013-06-25 23:53:48 +0800268 if (!interactive && !dry_run && !force) {
Ævar Arnfjörð Bjarmasona66f9b22011-02-22 23:42:22 +0000269 if (config_set)
Jiang Xin17696002013-06-25 23:53:48 +0800270 die(_("clean.requireForce set to true and neither -i, -n nor -f given; "
Ævar Arnfjörð Bjarmasona66f9b22011-02-22 23:42:22 +0000271 "refusing to clean"));
272 else
Jiang Xin17696002013-06-25 23:53:48 +0800273 die(_("clean.requireForce defaults to true and neither -i, -n nor -f given; "
Ævar Arnfjörð Bjarmasona66f9b22011-02-22 23:42:22 +0000274 "refusing to clean"));
275 }
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600276
Junio C Hamanoa0f4afb2009-06-30 15:33:45 -0700277 if (force > 1)
278 rm_flags = 0;
279
Johannes Schindelin7c4c97c2009-02-16 13:20:25 +0100280 dir.flags |= DIR_SHOW_OTHER_DIRECTORIES;
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600281
Nguyễn Thái Ngọc Duyc28b3d62009-08-20 20:47:01 +0700282 if (read_cache() < 0)
Ævar Arnfjörð Bjarmason2da57ad2011-02-22 23:42:21 +0000283 die(_("index file corrupt"));
Nguyễn Thái Ngọc Duyc28b3d62009-08-20 20:47:01 +0700284
Shawn Bohrer1617adc2007-11-14 23:00:54 -0600285 if (!ignored)
286 setup_standard_excludes(&dir);
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600287
Adam Spiers72aeb182013-01-16 13:25:58 +0000288 el = add_exclude_list(&dir, EXC_CMDL, "--exclude option");
Jared Hance07de4eb2010-07-20 15:35:56 -0400289 for (i = 0; i < exclude_list.nr; i++)
Adam Spiers72aeb182013-01-16 13:25:58 +0000290 add_exclude(exclude_list.items[i].string, "", 0, el, -(i+1));
Jared Hance07de4eb2010-07-20 15:35:56 -0400291
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600292 pathspec = get_pathspec(prefix, argv);
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600293
Linus Torvalds1d8842d2009-05-14 13:22:36 -0700294 fill_directory(&dir, pathspec);
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600295
Junio C Hamanod871c862007-12-04 23:55:41 -0800296 if (pathspec)
Jeff Kinga8db80c2008-01-12 04:04:32 -0500297 seen = xmalloc(argc > 0 ? argc : 1);
Junio C Hamanod871c862007-12-04 23:55:41 -0800298
299 for (i = 0; i < dir.nr; i++) {
300 struct dir_entry *ent = dir.entries[i];
Shawn Bohrerf2d0df72008-04-14 22:14:09 -0500301 int len, pos;
302 int matches = 0;
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600303 struct cache_entry *ce;
304 struct stat st;
Jiang Xin396049e2013-06-25 23:53:47 +0800305 const char *rel;
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600306
307 /*
308 * Remove the '/' at the end that directory
309 * walking adds for directory entries.
310 */
311 len = ent->len;
312 if (len && ent->name[len-1] == '/')
313 len--;
314 pos = cache_name_pos(ent->name, len);
315 if (0 <= pos)
316 continue; /* exact match */
317 pos = -pos - 1;
318 if (pos < active_nr) {
319 ce = active_cache[pos];
320 if (ce_namelen(ce) == len &&
321 !memcmp(ce->name, ent->name, len))
322 continue; /* Yup, this one exists unmerged */
323 }
324
Junio C Hamanod871c862007-12-04 23:55:41 -0800325 if (lstat(ent->name, &st))
Jiang Xin396049e2013-06-25 23:53:47 +0800326 die_errno("Cannot lstat '%s'", ent->name);
Junio C Hamanod871c862007-12-04 23:55:41 -0800327
328 if (pathspec) {
Jeff Kinga8db80c2008-01-12 04:04:32 -0500329 memset(seen, 0, argc > 0 ? argc : 1);
Shawn Bohrerf2d0df72008-04-14 22:14:09 -0500330 matches = match_pathspec(pathspec, ent->name, len,
Nguyễn Thái Ngọc Duy1c7d4022010-11-15 13:42:44 +0700331 0, seen);
Junio C Hamanod871c862007-12-04 23:55:41 -0800332 }
333
334 if (S_ISDIR(st.st_mode)) {
Zoltan Klingerf538a912013-01-11 09:53:46 +1100335 if (remove_directories || (matches == MATCHED_EXACTLY)) {
Jiang Xin396049e2013-06-25 23:53:47 +0800336 rel = relative_path(ent->name, prefix, &buf);
337 string_list_append(&del_list, rel);
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600338 }
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600339 } else {
Junio C Hamanod871c862007-12-04 23:55:41 -0800340 if (pathspec && !matches)
341 continue;
Jiang Xin396049e2013-06-25 23:53:47 +0800342 rel = relative_path(ent->name, prefix, &buf);
343 string_list_append(&del_list, rel);
344 }
345 }
346
Jiang Xin17696002013-06-25 23:53:48 +0800347 if (interactive && del_list.nr > 0)
348 interactive_main_loop();
Jiang Xin396049e2013-06-25 23:53:47 +0800349
350 for_each_string_list_item(item, &del_list) {
351 struct stat st;
352
353 if (prefix)
354 strbuf_addstr(&abs_path, prefix);
355
356 strbuf_addstr(&abs_path, item->string);
357
358 /*
359 * we might have removed this as part of earlier
360 * recursive directory removal, so lstat() here could
361 * fail with ENOENT.
362 */
363 if (lstat(abs_path.buf, &st))
364 continue;
365
366 if (S_ISDIR(st.st_mode)) {
367 if (remove_dirs(&abs_path, prefix, rm_flags, dry_run, quiet, &gone))
368 errors++;
369 if (gone && !quiet) {
370 qname = quote_path_relative(item->string, NULL, &buf);
371 printf(dry_run ? _(msg_would_remove) : _(msg_remove), qname);
372 }
373 } else {
374 res = dry_run ? 0 : unlink(abs_path.buf);
Zoltan Klingerf538a912013-01-11 09:53:46 +1100375 if (res) {
Jiang Xin396049e2013-06-25 23:53:47 +0800376 qname = quote_path_relative(item->string, NULL, &buf);
Zoltan Klingerf538a912013-01-11 09:53:46 +1100377 warning(_(msg_warn_remove_failed), qname);
Miklos Vajnaaa9c83c2008-02-21 02:44:46 +0100378 errors++;
Zoltan Klingerf538a912013-01-11 09:53:46 +1100379 } else if (!quiet) {
Jiang Xin396049e2013-06-25 23:53:47 +0800380 qname = quote_path_relative(item->string, NULL, &buf);
Zoltan Klingerf538a912013-01-11 09:53:46 +1100381 printf(dry_run ? _(msg_would_remove) : _(msg_remove), qname);
Miklos Vajnaaa9c83c2008-02-21 02:44:46 +0100382 }
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600383 }
Jiang Xin396049e2013-06-25 23:53:47 +0800384 strbuf_reset(&abs_path);
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600385 }
Junio C Hamanod871c862007-12-04 23:55:41 -0800386 free(seen);
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600387
Jiang Xin396049e2013-06-25 23:53:47 +0800388 strbuf_release(&abs_path);
389 strbuf_release(&buf);
390 string_list_clear(&del_list, 0);
Jared Hance07de4eb2010-07-20 15:35:56 -0400391 string_list_clear(&exclude_list, 0);
Miklos Vajnaaa9c83c2008-02-21 02:44:46 +0100392 return (errors != 0);
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600393}