]>
Commit | Line | Data |
---|---|---|
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 | ||
f8adbec9 | 9 | #define USE_THE_INDEX_COMPATIBILITY_MACROS |
113f10f2 SB |
10 | #include "builtin.h" |
11 | #include "cache.h" | |
b2141fc1 | 12 | #include "config.h" |
113f10f2 SB |
13 | #include "dir.h" |
14 | #include "parse-options.h" | |
07de4eba | 15 | #include "string-list.h" |
1fb32894 | 16 | #include "quote.h" |
1b8fd467 | 17 | #include "column.h" |
7a9b0b80 | 18 | #include "color.h" |
893d8399 | 19 | #include "pathspec.h" |
3ac68a93 | 20 | #include "help.h" |
113f10f2 | 21 | |
625db1b7 | 22 | static int force = -1; /* unset */ |
17696002 | 23 | static int interactive; |
396049e5 | 24 | static struct string_list del_list = STRING_LIST_INIT_DUP; |
1b8fd467 | 25 | static unsigned int colopts; |
113f10f2 SB |
26 | |
27 | static const char *const builtin_clean_usage[] = { | |
17696002 | 28 | N_("git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>..."), |
113f10f2 SB |
29 | NULL |
30 | }; | |
31 | ||
f538a91e ZK |
32 | static const char *msg_remove = N_("Removing %s\n"); |
33 | static const char *msg_would_remove = N_("Would remove %s\n"); | |
34 | static const char *msg_skip_git_dir = N_("Skipping repository %s\n"); | |
35 | static const char *msg_would_skip_git_dir = N_("Would skip repository %s\n"); | |
36 | static const char *msg_warn_remove_failed = N_("failed to remove %s"); | |
b09364c4 | 37 | static const char *msg_warn_lstat_failed = N_("could not lstat %s\n"); |
f538a91e | 38 | |
7a9b0b80 JX |
39 | enum color_clean { |
40 | CLEAN_COLOR_RESET = 0, | |
41 | CLEAN_COLOR_PLAIN = 1, | |
42 | CLEAN_COLOR_PROMPT = 2, | |
43 | CLEAN_COLOR_HEADER = 3, | |
44 | CLEAN_COLOR_HELP = 4, | |
78273520 | 45 | CLEAN_COLOR_ERROR = 5 |
7a9b0b80 JX |
46 | }; |
47 | ||
a73b3680 NTND |
48 | static const char *color_interactive_slots[] = { |
49 | [CLEAN_COLOR_ERROR] = "error", | |
50 | [CLEAN_COLOR_HEADER] = "header", | |
51 | [CLEAN_COLOR_HELP] = "help", | |
52 | [CLEAN_COLOR_PLAIN] = "plain", | |
53 | [CLEAN_COLOR_PROMPT] = "prompt", | |
54 | [CLEAN_COLOR_RESET] = "reset", | |
55 | }; | |
56 | ||
512f41cf JH |
57 | static int clean_use_color = -1; |
58 | static char clean_colors[][COLOR_MAXLEN] = { | |
59 | [CLEAN_COLOR_ERROR] = GIT_COLOR_BOLD_RED, | |
60 | [CLEAN_COLOR_HEADER] = GIT_COLOR_BOLD, | |
61 | [CLEAN_COLOR_HELP] = GIT_COLOR_BOLD_RED, | |
62 | [CLEAN_COLOR_PLAIN] = GIT_COLOR_NORMAL, | |
63 | [CLEAN_COLOR_PROMPT] = GIT_COLOR_BOLD_BLUE, | |
64 | [CLEAN_COLOR_RESET] = GIT_COLOR_RESET, | |
65 | }; | |
66 | ||
9f93e461 JX |
67 | #define MENU_OPTS_SINGLETON 01 |
68 | #define MENU_OPTS_IMMEDIATE 02 | |
69 | #define MENU_OPTS_LIST_ONLY 04 | |
70 | ||
71 | struct menu_opts { | |
72 | const char *header; | |
73 | const char *prompt; | |
74 | int flags; | |
75 | }; | |
76 | ||
77 | #define MENU_RETURN_NO_LOOP 10 | |
78 | ||
79 | struct menu_item { | |
80 | char hotkey; | |
81 | const char *title; | |
82 | int selected; | |
8687f777 | 83 | int (*fn)(void); |
9f93e461 JX |
84 | }; |
85 | ||
86 | enum menu_stuff_type { | |
87 | MENU_STUFF_TYPE_STRING_LIST = 1, | |
88 | MENU_STUFF_TYPE_MENU_ITEM | |
89 | }; | |
90 | ||
91 | struct menu_stuff { | |
92 | enum menu_stuff_type type; | |
93 | int nr; | |
94 | void *stuff; | |
95 | }; | |
96 | ||
3ac68a93 NTND |
97 | define_list_config_array(color_interactive_slots); |
98 | ||
ef90d6d4 | 99 | static int git_clean_config(const char *var, const char *value, void *cb) |
113f10f2 | 100 | { |
e3f1da98 RS |
101 | const char *slot_name; |
102 | ||
59556548 | 103 | if (starts_with(var, "column.")) |
1b8fd467 JX |
104 | return git_column_config(var, value, "clean", &colopts); |
105 | ||
7a9b0b80 JX |
106 | /* honors the color.interactive* config variables which also |
107 | applied in git-add--interactive and git-stash */ | |
108 | if (!strcmp(var, "color.interactive")) { | |
109 | clean_use_color = git_config_colorbool(var, value); | |
110 | return 0; | |
111 | } | |
e3f1da98 | 112 | if (skip_prefix(var, "color.interactive.", &slot_name)) { |
a73b3680 | 113 | int slot = LOOKUP_CONFIG(color_interactive_slots, slot_name); |
7a9b0b80 JX |
114 | if (slot < 0) |
115 | return 0; | |
116 | if (!value) | |
117 | return config_error_nonbool(var); | |
f6c5a296 | 118 | return color_parse(value, clean_colors[slot]); |
7a9b0b80 JX |
119 | } |
120 | ||
1b8fd467 | 121 | if (!strcmp(var, "clean.requireforce")) { |
113f10f2 | 122 | force = !git_config_bool(var, value); |
1b8fd467 JX |
123 | return 0; |
124 | } | |
7a9b0b80 | 125 | |
33c643bb JK |
126 | /* inspect the color.ui config variable and others */ |
127 | return git_color_default_config(var, value, cb); | |
7a9b0b80 JX |
128 | } |
129 | ||
130 | static const char *clean_get_color(enum color_clean ix) | |
131 | { | |
132 | if (want_color(clean_use_color)) | |
133 | return clean_colors[ix]; | |
134 | return ""; | |
135 | } | |
136 | ||
137 | static void clean_print_color(enum color_clean ix) | |
138 | { | |
139 | printf("%s", clean_get_color(ix)); | |
113f10f2 SB |
140 | } |
141 | ||
07de4eba JH |
142 | static int exclude_cb(const struct option *opt, const char *arg, int unset) |
143 | { | |
144 | struct string_list *exclude_list = opt->value; | |
517fe807 | 145 | BUG_ON_OPT_NEG(unset); |
07de4eba JH |
146 | string_list_append(exclude_list, arg); |
147 | return 0; | |
148 | } | |
149 | ||
f538a91e ZK |
150 | static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag, |
151 | int dry_run, int quiet, int *dir_gone) | |
152 | { | |
153 | DIR *dir; | |
154 | struct strbuf quoted = STRBUF_INIT; | |
155 | struct dirent *e; | |
e666b89d | 156 | int res = 0, ret = 0, gone = 1, original_len = path->len, len; |
f538a91e ZK |
157 | struct string_list dels = STRING_LIST_INIT_DUP; |
158 | ||
159 | *dir_gone = 1; | |
160 | ||
ca8b5390 EN |
161 | if ((force_flag & REMOVE_DIR_KEEP_NESTED_GIT) && |
162 | is_nonbare_repository_dir(path)) { | |
f538a91e | 163 | if (!quiet) { |
39598f99 | 164 | quote_path_relative(path->buf, prefix, "ed); |
f538a91e ZK |
165 | printf(dry_run ? _(msg_would_skip_git_dir) : _(msg_skip_git_dir), |
166 | quoted.buf); | |
167 | } | |
168 | ||
169 | *dir_gone = 0; | |
25a8f80a | 170 | goto out; |
f538a91e ZK |
171 | } |
172 | ||
173 | dir = opendir(path->buf); | |
174 | if (!dir) { | |
175 | /* an empty dir could be removed even if it is unreadble */ | |
176 | res = dry_run ? 0 : rmdir(path->buf); | |
177 | if (res) { | |
cccf97d6 | 178 | int saved_errno = errno; |
39598f99 | 179 | quote_path_relative(path->buf, prefix, "ed); |
cccf97d6 NTND |
180 | errno = saved_errno; |
181 | warning_errno(_(msg_warn_remove_failed), quoted.buf); | |
f538a91e ZK |
182 | *dir_gone = 0; |
183 | } | |
25a8f80a RS |
184 | ret = res; |
185 | goto out; | |
f538a91e ZK |
186 | } |
187 | ||
00b6c178 | 188 | strbuf_complete(path, '/'); |
f538a91e ZK |
189 | |
190 | len = path->len; | |
191 | while ((e = readdir(dir)) != NULL) { | |
192 | struct stat st; | |
193 | if (is_dot_or_dotdot(e->d_name)) | |
194 | continue; | |
195 | ||
196 | strbuf_setlen(path, len); | |
197 | strbuf_addstr(path, e->d_name); | |
198 | if (lstat(path->buf, &st)) | |
b09364c4 | 199 | warning_errno(_(msg_warn_lstat_failed), path->buf); |
f538a91e ZK |
200 | else if (S_ISDIR(st.st_mode)) { |
201 | if (remove_dirs(path, prefix, force_flag, dry_run, quiet, &gone)) | |
202 | ret = 1; | |
203 | if (gone) { | |
39598f99 | 204 | quote_path_relative(path->buf, prefix, "ed); |
f538a91e ZK |
205 | string_list_append(&dels, quoted.buf); |
206 | } else | |
207 | *dir_gone = 0; | |
208 | continue; | |
209 | } else { | |
210 | res = dry_run ? 0 : unlink(path->buf); | |
211 | if (!res) { | |
39598f99 | 212 | quote_path_relative(path->buf, prefix, "ed); |
f538a91e ZK |
213 | string_list_append(&dels, quoted.buf); |
214 | } else { | |
cccf97d6 | 215 | int saved_errno = errno; |
39598f99 | 216 | quote_path_relative(path->buf, prefix, "ed); |
cccf97d6 NTND |
217 | errno = saved_errno; |
218 | warning_errno(_(msg_warn_remove_failed), quoted.buf); | |
f538a91e ZK |
219 | *dir_gone = 0; |
220 | ret = 1; | |
221 | } | |
222 | continue; | |
223 | } | |
224 | ||
225 | /* path too long, stat fails, or non-directory still exists */ | |
226 | *dir_gone = 0; | |
227 | ret = 1; | |
228 | break; | |
229 | } | |
230 | closedir(dir); | |
231 | ||
232 | strbuf_setlen(path, original_len); | |
233 | ||
234 | if (*dir_gone) { | |
235 | res = dry_run ? 0 : rmdir(path->buf); | |
236 | if (!res) | |
237 | *dir_gone = 1; | |
238 | else { | |
cccf97d6 | 239 | int saved_errno = errno; |
39598f99 | 240 | quote_path_relative(path->buf, prefix, "ed); |
cccf97d6 NTND |
241 | errno = saved_errno; |
242 | warning_errno(_(msg_warn_remove_failed), quoted.buf); | |
f538a91e ZK |
243 | *dir_gone = 0; |
244 | ret = 1; | |
245 | } | |
246 | } | |
247 | ||
248 | if (!*dir_gone && !quiet) { | |
e666b89d | 249 | int i; |
f538a91e ZK |
250 | for (i = 0; i < dels.nr; i++) |
251 | printf(dry_run ? _(msg_would_remove) : _(msg_remove), dels.items[i].string); | |
252 | } | |
25a8f80a RS |
253 | out: |
254 | strbuf_release("ed); | |
f538a91e ZK |
255 | string_list_clear(&dels, 0); |
256 | return ret; | |
257 | } | |
258 | ||
1b8fd467 | 259 | static void pretty_print_dels(void) |
17696002 | 260 | { |
1b8fd467 | 261 | struct string_list list = STRING_LIST_INIT_DUP; |
17696002 | 262 | struct string_list_item *item; |
1b8fd467 | 263 | struct strbuf buf = STRBUF_INIT; |
17696002 | 264 | const char *qname; |
1b8fd467 JX |
265 | struct column_options copts; |
266 | ||
267 | for_each_string_list_item(item, &del_list) { | |
268 | qname = quote_path_relative(item->string, NULL, &buf); | |
269 | string_list_append(&list, qname); | |
270 | } | |
271 | ||
272 | /* | |
273 | * always enable column display, we only consult column.* | |
274 | * about layout strategy and stuff | |
275 | */ | |
276 | colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED; | |
277 | memset(&copts, 0, sizeof(copts)); | |
278 | copts.indent = " "; | |
279 | copts.padding = 2; | |
280 | print_columns(&list, colopts, &copts); | |
1b8fd467 JX |
281 | strbuf_release(&buf); |
282 | string_list_clear(&list, 0); | |
283 | } | |
284 | ||
9f93e461 JX |
285 | static void pretty_print_menus(struct string_list *menu_list) |
286 | { | |
287 | unsigned int local_colopts = 0; | |
288 | struct column_options copts; | |
289 | ||
290 | local_colopts = COL_ENABLED | COL_ROW; | |
291 | memset(&copts, 0, sizeof(copts)); | |
292 | copts.indent = " "; | |
293 | copts.padding = 2; | |
294 | print_columns(menu_list, local_colopts, &copts); | |
295 | } | |
296 | ||
297 | static void prompt_help_cmd(int singleton) | |
298 | { | |
299 | clean_print_color(CLEAN_COLOR_HELP); | |
901707ba | 300 | printf(singleton ? |
9f93e461 JX |
301 | _("Prompt help:\n" |
302 | "1 - select a numbered item\n" | |
303 | "foo - select item based on unique prefix\n" | |
901707ba | 304 | " - (empty) select nothing\n") : |
9f93e461 JX |
305 | _("Prompt help:\n" |
306 | "1 - select a single item\n" | |
307 | "3-5 - select a range of items\n" | |
308 | "2-3,6-9 - select multiple ranges\n" | |
309 | "foo - select item based on unique prefix\n" | |
310 | "-... - unselect specified items\n" | |
311 | "* - choose all items\n" | |
901707ba | 312 | " - (empty) finish selecting\n")); |
9f93e461 JX |
313 | clean_print_color(CLEAN_COLOR_RESET); |
314 | } | |
315 | ||
316 | /* | |
317 | * display menu stuff with number prefix and hotkey highlight | |
318 | */ | |
319 | static void print_highlight_menu_stuff(struct menu_stuff *stuff, int **chosen) | |
320 | { | |
321 | struct string_list menu_list = STRING_LIST_INIT_DUP; | |
322 | struct strbuf menu = STRBUF_INIT; | |
9f93e461 JX |
323 | struct menu_item *menu_item; |
324 | struct string_list_item *string_list_item; | |
325 | int i; | |
326 | ||
327 | switch (stuff->type) { | |
328 | default: | |
bef111d0 | 329 | die("Bad type of menu_stuff when print menu"); |
9f93e461 JX |
330 | case MENU_STUFF_TYPE_MENU_ITEM: |
331 | menu_item = (struct menu_item *)stuff->stuff; | |
332 | for (i = 0; i < stuff->nr; i++, menu_item++) { | |
333 | const char *p; | |
334 | int highlighted = 0; | |
335 | ||
336 | p = menu_item->title; | |
337 | if ((*chosen)[i] < 0) | |
338 | (*chosen)[i] = menu_item->selected ? 1 : 0; | |
339 | strbuf_addf(&menu, "%s%2d: ", (*chosen)[i] ? "*" : " ", i+1); | |
340 | for (; *p; p++) { | |
341 | if (!highlighted && *p == menu_item->hotkey) { | |
342 | strbuf_addstr(&menu, clean_get_color(CLEAN_COLOR_PROMPT)); | |
343 | strbuf_addch(&menu, *p); | |
344 | strbuf_addstr(&menu, clean_get_color(CLEAN_COLOR_RESET)); | |
345 | highlighted = 1; | |
346 | } else { | |
347 | strbuf_addch(&menu, *p); | |
348 | } | |
349 | } | |
350 | string_list_append(&menu_list, menu.buf); | |
351 | strbuf_reset(&menu); | |
352 | } | |
353 | break; | |
354 | case MENU_STUFF_TYPE_STRING_LIST: | |
355 | i = 0; | |
356 | for_each_string_list_item(string_list_item, (struct string_list *)stuff->stuff) { | |
357 | if ((*chosen)[i] < 0) | |
358 | (*chosen)[i] = 0; | |
359 | strbuf_addf(&menu, "%s%2d: %s", | |
360 | (*chosen)[i] ? "*" : " ", i+1, string_list_item->string); | |
361 | string_list_append(&menu_list, menu.buf); | |
362 | strbuf_reset(&menu); | |
363 | i++; | |
364 | } | |
365 | break; | |
366 | } | |
367 | ||
368 | pretty_print_menus(&menu_list); | |
369 | ||
370 | strbuf_release(&menu); | |
9f93e461 JX |
371 | string_list_clear(&menu_list, 0); |
372 | } | |
373 | ||
60838613 JX |
374 | static int find_unique(const char *choice, struct menu_stuff *menu_stuff) |
375 | { | |
376 | struct menu_item *menu_item; | |
377 | struct string_list_item *string_list_item; | |
378 | int i, len, found = 0; | |
379 | ||
380 | len = strlen(choice); | |
381 | switch (menu_stuff->type) { | |
382 | default: | |
383 | die("Bad type of menu_stuff when parse choice"); | |
384 | case MENU_STUFF_TYPE_MENU_ITEM: | |
385 | ||
386 | menu_item = (struct menu_item *)menu_stuff->stuff; | |
387 | for (i = 0; i < menu_stuff->nr; i++, menu_item++) { | |
388 | if (len == 1 && *choice == menu_item->hotkey) { | |
389 | found = i + 1; | |
390 | break; | |
391 | } | |
392 | if (!strncasecmp(choice, menu_item->title, len)) { | |
393 | if (found) { | |
394 | if (len == 1) { | |
395 | /* continue for hotkey matching */ | |
396 | found = -1; | |
397 | } else { | |
398 | found = 0; | |
399 | break; | |
400 | } | |
401 | } else { | |
402 | found = i + 1; | |
403 | } | |
404 | } | |
405 | } | |
406 | break; | |
407 | case MENU_STUFF_TYPE_STRING_LIST: | |
408 | string_list_item = ((struct string_list *)menu_stuff->stuff)->items; | |
409 | for (i = 0; i < menu_stuff->nr; i++, string_list_item++) { | |
410 | if (!strncasecmp(choice, string_list_item->string, len)) { | |
411 | if (found) { | |
412 | found = 0; | |
413 | break; | |
414 | } | |
415 | found = i + 1; | |
416 | } | |
417 | } | |
418 | break; | |
419 | } | |
420 | return found; | |
421 | } | |
422 | ||
423 | ||
9f93e461 JX |
424 | /* |
425 | * Parse user input, and return choice(s) for menu (menu_stuff). | |
426 | * | |
427 | * Input | |
428 | * (for single choice) | |
429 | * 1 - select a numbered item | |
430 | * foo - select item based on menu title | |
431 | * - (empty) select nothing | |
432 | * | |
433 | * (for multiple choice) | |
434 | * 1 - select a single item | |
435 | * 3-5 - select a range of items | |
436 | * 2-3,6-9 - select multiple ranges | |
437 | * foo - select item based on menu title | |
438 | * -... - unselect specified items | |
439 | * * - choose all items | |
440 | * - (empty) finish selecting | |
441 | * | |
442 | * The parse result will be saved in array **chosen, and | |
443 | * return number of total selections. | |
444 | */ | |
445 | static int parse_choice(struct menu_stuff *menu_stuff, | |
446 | int is_single, | |
447 | struct strbuf input, | |
448 | int **chosen) | |
449 | { | |
450 | struct strbuf **choice_list, **ptr; | |
9f93e461 JX |
451 | int nr = 0; |
452 | int i; | |
453 | ||
454 | if (is_single) { | |
455 | choice_list = strbuf_split_max(&input, '\n', 0); | |
456 | } else { | |
457 | char *p = input.buf; | |
458 | do { | |
459 | if (*p == ',') | |
460 | *p = ' '; | |
461 | } while (*p++); | |
462 | choice_list = strbuf_split_max(&input, ' ', 0); | |
463 | } | |
464 | ||
465 | for (ptr = choice_list; *ptr; ptr++) { | |
466 | char *p; | |
467 | int choose = 1; | |
468 | int bottom = 0, top = 0; | |
469 | int is_range, is_number; | |
470 | ||
471 | strbuf_trim(*ptr); | |
472 | if (!(*ptr)->len) | |
473 | continue; | |
474 | ||
475 | /* Input that begins with '-'; unchoose */ | |
476 | if (*(*ptr)->buf == '-') { | |
477 | choose = 0; | |
478 | strbuf_remove((*ptr), 0, 1); | |
479 | } | |
480 | ||
481 | is_range = 0; | |
482 | is_number = 1; | |
483 | for (p = (*ptr)->buf; *p; p++) { | |
484 | if ('-' == *p) { | |
485 | if (!is_range) { | |
486 | is_range = 1; | |
487 | is_number = 0; | |
488 | } else { | |
489 | is_number = 0; | |
490 | is_range = 0; | |
491 | break; | |
492 | } | |
493 | } else if (!isdigit(*p)) { | |
494 | is_number = 0; | |
495 | is_range = 0; | |
496 | break; | |
497 | } | |
498 | } | |
499 | ||
500 | if (is_number) { | |
501 | bottom = atoi((*ptr)->buf); | |
502 | top = bottom; | |
503 | } else if (is_range) { | |
504 | bottom = atoi((*ptr)->buf); | |
505 | /* a range can be specified like 5-7 or 5- */ | |
506 | if (!*(strchr((*ptr)->buf, '-') + 1)) | |
507 | top = menu_stuff->nr; | |
508 | else | |
509 | top = atoi(strchr((*ptr)->buf, '-') + 1); | |
510 | } else if (!strcmp((*ptr)->buf, "*")) { | |
511 | bottom = 1; | |
512 | top = menu_stuff->nr; | |
513 | } else { | |
60838613 JX |
514 | bottom = find_unique((*ptr)->buf, menu_stuff); |
515 | top = bottom; | |
9f93e461 JX |
516 | } |
517 | ||
518 | if (top <= 0 || bottom <= 0 || top > menu_stuff->nr || bottom > top || | |
519 | (is_single && bottom != top)) { | |
520 | clean_print_color(CLEAN_COLOR_ERROR); | |
901707ba | 521 | printf(_("Huh (%s)?\n"), (*ptr)->buf); |
9f93e461 JX |
522 | clean_print_color(CLEAN_COLOR_RESET); |
523 | continue; | |
524 | } | |
525 | ||
526 | for (i = bottom; i <= top; i++) | |
527 | (*chosen)[i-1] = choose; | |
528 | } | |
529 | ||
530 | strbuf_list_free(choice_list); | |
531 | ||
532 | for (i = 0; i < menu_stuff->nr; i++) | |
533 | nr += (*chosen)[i]; | |
534 | return nr; | |
535 | } | |
536 | ||
537 | /* | |
538 | * Implement a git-add-interactive compatible UI, which is borrowed | |
539 | * from git-add--interactive.perl. | |
540 | * | |
541 | * Return value: | |
542 | * | |
543 | * - Return an array of integers | |
544 | * - , and it is up to you to free the allocated memory. | |
545 | * - The array ends with EOF. | |
546 | * - If user pressed CTRL-D (i.e. EOF), no selection returned. | |
547 | */ | |
548 | static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff) | |
549 | { | |
550 | struct strbuf choice = STRBUF_INIT; | |
551 | int *chosen, *result; | |
552 | int nr = 0; | |
553 | int eof = 0; | |
554 | int i; | |
555 | ||
b32fa95f | 556 | ALLOC_ARRAY(chosen, stuff->nr); |
9f93e461 JX |
557 | /* set chosen as uninitialized */ |
558 | for (i = 0; i < stuff->nr; i++) | |
559 | chosen[i] = -1; | |
560 | ||
561 | for (;;) { | |
562 | if (opts->header) { | |
563 | printf_ln("%s%s%s", | |
564 | clean_get_color(CLEAN_COLOR_HEADER), | |
565 | _(opts->header), | |
566 | clean_get_color(CLEAN_COLOR_RESET)); | |
567 | } | |
568 | ||
569 | /* chosen will be initialized by print_highlight_menu_stuff */ | |
570 | print_highlight_menu_stuff(stuff, &chosen); | |
571 | ||
572 | if (opts->flags & MENU_OPTS_LIST_ONLY) | |
573 | break; | |
574 | ||
575 | if (opts->prompt) { | |
576 | printf("%s%s%s%s", | |
577 | clean_get_color(CLEAN_COLOR_PROMPT), | |
578 | _(opts->prompt), | |
579 | opts->flags & MENU_OPTS_SINGLETON ? "> " : ">> ", | |
580 | clean_get_color(CLEAN_COLOR_RESET)); | |
581 | } | |
582 | ||
8f309aeb | 583 | if (strbuf_getline_lf(&choice, stdin) != EOF) { |
9f93e461 JX |
584 | strbuf_trim(&choice); |
585 | } else { | |
586 | eof = 1; | |
587 | break; | |
588 | } | |
589 | ||
590 | /* help for prompt */ | |
591 | if (!strcmp(choice.buf, "?")) { | |
592 | prompt_help_cmd(opts->flags & MENU_OPTS_SINGLETON); | |
593 | continue; | |
594 | } | |
595 | ||
596 | /* for a multiple-choice menu, press ENTER (empty) will return back */ | |
597 | if (!(opts->flags & MENU_OPTS_SINGLETON) && !choice.len) | |
598 | break; | |
599 | ||
600 | nr = parse_choice(stuff, | |
601 | opts->flags & MENU_OPTS_SINGLETON, | |
602 | choice, | |
603 | &chosen); | |
604 | ||
605 | if (opts->flags & MENU_OPTS_SINGLETON) { | |
606 | if (nr) | |
607 | break; | |
608 | } else if (opts->flags & MENU_OPTS_IMMEDIATE) { | |
609 | break; | |
610 | } | |
611 | } | |
612 | ||
613 | if (eof) { | |
614 | result = xmalloc(sizeof(int)); | |
615 | *result = EOF; | |
616 | } else { | |
617 | int j = 0; | |
618 | ||
619 | /* | |
620 | * recalculate nr, if return back from menu directly with | |
621 | * default selections. | |
622 | */ | |
623 | if (!nr) { | |
624 | for (i = 0; i < stuff->nr; i++) | |
625 | nr += chosen[i]; | |
626 | } | |
627 | ||
50a6c8ef | 628 | result = xcalloc(st_add(nr, 1), sizeof(int)); |
9f93e461 JX |
629 | for (i = 0; i < stuff->nr && j < nr; i++) { |
630 | if (chosen[i]) | |
631 | result[j++] = i; | |
632 | } | |
633 | result[j] = EOF; | |
634 | } | |
635 | ||
636 | free(chosen); | |
637 | strbuf_release(&choice); | |
638 | return result; | |
639 | } | |
640 | ||
641 | static int clean_cmd(void) | |
1b8fd467 | 642 | { |
9f93e461 JX |
643 | return MENU_RETURN_NO_LOOP; |
644 | } | |
645 | ||
d1239264 JX |
646 | static int filter_by_patterns_cmd(void) |
647 | { | |
648 | struct dir_struct dir; | |
649 | struct strbuf confirm = STRBUF_INIT; | |
650 | struct strbuf **ignore_list; | |
651 | struct string_list_item *item; | |
caa3d554 | 652 | struct pattern_list *pl; |
d1239264 JX |
653 | int changed = -1, i; |
654 | ||
655 | for (;;) { | |
656 | if (!del_list.nr) | |
657 | break; | |
658 | ||
659 | if (changed) | |
660 | pretty_print_dels(); | |
661 | ||
662 | clean_print_color(CLEAN_COLOR_PROMPT); | |
663 | printf(_("Input ignore patterns>> ")); | |
664 | clean_print_color(CLEAN_COLOR_RESET); | |
8f309aeb | 665 | if (strbuf_getline_lf(&confirm, stdin) != EOF) |
d1239264 JX |
666 | strbuf_trim(&confirm); |
667 | else | |
668 | putchar('\n'); | |
669 | ||
670 | /* quit filter_by_pattern mode if press ENTER or Ctrl-D */ | |
671 | if (!confirm.len) | |
672 | break; | |
673 | ||
674 | memset(&dir, 0, sizeof(dir)); | |
65edd96a | 675 | pl = add_pattern_list(&dir, EXC_CMDL, "manual exclude"); |
d1239264 JX |
676 | ignore_list = strbuf_split_max(&confirm, ' ', 0); |
677 | ||
678 | for (i = 0; ignore_list[i]; i++) { | |
679 | strbuf_trim(ignore_list[i]); | |
680 | if (!ignore_list[i]->len) | |
681 | continue; | |
682 | ||
65edd96a | 683 | add_pattern(ignore_list[i]->buf, "", 0, pl, -(i+1)); |
d1239264 JX |
684 | } |
685 | ||
686 | changed = 0; | |
687 | for_each_string_list_item(item, &del_list) { | |
688 | int dtype = DT_UNKNOWN; | |
689 | ||
a0bba65b | 690 | if (is_excluded(&dir, &the_index, item->string, &dtype)) { |
d1239264 JX |
691 | *item->string = '\0'; |
692 | changed++; | |
693 | } | |
694 | } | |
695 | ||
696 | if (changed) { | |
697 | string_list_remove_empty_items(&del_list, 0); | |
698 | } else { | |
699 | clean_print_color(CLEAN_COLOR_ERROR); | |
700 | printf_ln(_("WARNING: Cannot find items matched by: %s"), confirm.buf); | |
701 | clean_print_color(CLEAN_COLOR_RESET); | |
702 | } | |
703 | ||
704 | strbuf_list_free(ignore_list); | |
705 | clear_directory(&dir); | |
706 | } | |
707 | ||
708 | strbuf_release(&confirm); | |
709 | return 0; | |
710 | } | |
711 | ||
c1f1d24a JX |
712 | static int select_by_numbers_cmd(void) |
713 | { | |
714 | struct menu_opts menu_opts; | |
715 | struct menu_stuff menu_stuff; | |
716 | struct string_list_item *items; | |
717 | int *chosen; | |
718 | int i, j; | |
719 | ||
720 | menu_opts.header = NULL; | |
721 | menu_opts.prompt = N_("Select items to delete"); | |
722 | menu_opts.flags = 0; | |
723 | ||
724 | menu_stuff.type = MENU_STUFF_TYPE_STRING_LIST; | |
725 | menu_stuff.stuff = &del_list; | |
726 | menu_stuff.nr = del_list.nr; | |
727 | ||
728 | chosen = list_and_choose(&menu_opts, &menu_stuff); | |
729 | items = del_list.items; | |
730 | for (i = 0, j = 0; i < del_list.nr; i++) { | |
731 | if (i < chosen[j]) { | |
732 | *(items[i].string) = '\0'; | |
733 | } else if (i == chosen[j]) { | |
734 | /* delete selected item */ | |
735 | j++; | |
736 | continue; | |
737 | } else { | |
738 | /* end of chosen (chosen[j] == EOF), won't delete */ | |
739 | *(items[i].string) = '\0'; | |
740 | } | |
741 | } | |
742 | ||
743 | string_list_remove_empty_items(&del_list, 0); | |
744 | ||
745 | free(chosen); | |
746 | return 0; | |
747 | } | |
748 | ||
96a799b6 JX |
749 | static int ask_each_cmd(void) |
750 | { | |
751 | struct strbuf confirm = STRBUF_INIT; | |
752 | struct strbuf buf = STRBUF_INIT; | |
753 | struct string_list_item *item; | |
754 | const char *qname; | |
755 | int changed = 0, eof = 0; | |
756 | ||
757 | for_each_string_list_item(item, &del_list) { | |
758 | /* Ctrl-D should stop removing files */ | |
759 | if (!eof) { | |
760 | qname = quote_path_relative(item->string, NULL, &buf); | |
d9130227 JNA |
761 | /* TRANSLATORS: Make sure to keep [y/N] as is */ |
762 | printf(_("Remove %s [y/N]? "), qname); | |
8f309aeb | 763 | if (strbuf_getline_lf(&confirm, stdin) != EOF) { |
96a799b6 JX |
764 | strbuf_trim(&confirm); |
765 | } else { | |
766 | putchar('\n'); | |
767 | eof = 1; | |
768 | } | |
769 | } | |
770 | if (!confirm.len || strncasecmp(confirm.buf, "yes", confirm.len)) { | |
771 | *item->string = '\0'; | |
772 | changed++; | |
773 | } | |
774 | } | |
775 | ||
776 | if (changed) | |
777 | string_list_remove_empty_items(&del_list, 0); | |
778 | ||
779 | strbuf_release(&buf); | |
780 | strbuf_release(&confirm); | |
781 | return MENU_RETURN_NO_LOOP; | |
782 | } | |
783 | ||
9f93e461 JX |
784 | static int quit_cmd(void) |
785 | { | |
786 | string_list_clear(&del_list, 0); | |
901707ba | 787 | printf(_("Bye.\n")); |
9f93e461 JX |
788 | return MENU_RETURN_NO_LOOP; |
789 | } | |
790 | ||
791 | static int help_cmd(void) | |
792 | { | |
793 | clean_print_color(CLEAN_COLOR_HELP); | |
794 | printf_ln(_( | |
795 | "clean - start cleaning\n" | |
d1239264 | 796 | "filter by pattern - exclude items from deletion\n" |
c1f1d24a | 797 | "select by numbers - select items to be deleted by numbers\n" |
96a799b6 | 798 | "ask each - confirm each deletion (like \"rm -i\")\n" |
9f93e461 JX |
799 | "quit - stop cleaning\n" |
800 | "help - this screen\n" | |
801 | "? - help for prompt selection" | |
802 | )); | |
803 | clean_print_color(CLEAN_COLOR_RESET); | |
804 | return 0; | |
805 | } | |
17696002 | 806 | |
9f93e461 JX |
807 | static void interactive_main_loop(void) |
808 | { | |
17696002 | 809 | while (del_list.nr) { |
9f93e461 JX |
810 | struct menu_opts menu_opts; |
811 | struct menu_stuff menu_stuff; | |
812 | struct menu_item menus[] = { | |
813 | {'c', "clean", 0, clean_cmd}, | |
d1239264 | 814 | {'f', "filter by pattern", 0, filter_by_patterns_cmd}, |
c1f1d24a | 815 | {'s', "select by numbers", 0, select_by_numbers_cmd}, |
96a799b6 | 816 | {'a', "ask each", 0, ask_each_cmd}, |
9f93e461 JX |
817 | {'q', "quit", 0, quit_cmd}, |
818 | {'h', "help", 0, help_cmd}, | |
819 | }; | |
820 | int *chosen; | |
821 | ||
822 | menu_opts.header = N_("*** Commands ***"); | |
823 | menu_opts.prompt = N_("What now"); | |
824 | menu_opts.flags = MENU_OPTS_SINGLETON; | |
825 | ||
826 | menu_stuff.type = MENU_STUFF_TYPE_MENU_ITEM; | |
827 | menu_stuff.stuff = menus; | |
828 | menu_stuff.nr = sizeof(menus) / sizeof(struct menu_item); | |
829 | ||
7a9b0b80 | 830 | clean_print_color(CLEAN_COLOR_HEADER); |
1b8fd467 JX |
831 | printf_ln(Q_("Would remove the following item:", |
832 | "Would remove the following items:", | |
833 | del_list.nr)); | |
7a9b0b80 | 834 | clean_print_color(CLEAN_COLOR_RESET); |
17696002 | 835 | |
1b8fd467 JX |
836 | pretty_print_dels(); |
837 | ||
9f93e461 JX |
838 | chosen = list_and_choose(&menu_opts, &menu_stuff); |
839 | ||
840 | if (*chosen != EOF) { | |
841 | int ret; | |
842 | ret = menus[*chosen].fn(); | |
843 | if (ret != MENU_RETURN_NO_LOOP) { | |
6a83d902 | 844 | FREE_AND_NULL(chosen); |
9f93e461 JX |
845 | if (!del_list.nr) { |
846 | clean_print_color(CLEAN_COLOR_ERROR); | |
847 | printf_ln(_("No more files to clean, exiting.")); | |
848 | clean_print_color(CLEAN_COLOR_RESET); | |
849 | break; | |
850 | } | |
17696002 JX |
851 | continue; |
852 | } | |
9f93e461 JX |
853 | } else { |
854 | quit_cmd(); | |
17696002 | 855 | } |
17696002 | 856 | |
6a83d902 | 857 | FREE_AND_NULL(chosen); |
9f93e461 JX |
858 | break; |
859 | } | |
17696002 JX |
860 | } |
861 | ||
6b1db431 SL |
862 | static void correct_untracked_entries(struct dir_struct *dir) |
863 | { | |
864 | int src, dst, ign; | |
865 | ||
866 | for (src = dst = ign = 0; src < dir->nr; src++) { | |
867 | /* skip paths in ignored[] that cannot be inside entries[src] */ | |
868 | while (ign < dir->ignored_nr && | |
869 | 0 <= cmp_dir_entry(&dir->entries[src], &dir->ignored[ign])) | |
870 | ign++; | |
871 | ||
872 | if (ign < dir->ignored_nr && | |
873 | check_dir_entry_contains(dir->entries[src], dir->ignored[ign])) { | |
874 | /* entries[src] contains an ignored path, so we drop it */ | |
875 | free(dir->entries[src]); | |
876 | } else { | |
877 | struct dir_entry *ent = dir->entries[src++]; | |
878 | ||
879 | /* entries[src] does not contain an ignored path, so we keep it */ | |
880 | dir->entries[dst++] = ent; | |
881 | ||
882 | /* then discard paths in entries[] contained inside entries[src] */ | |
883 | while (src < dir->nr && | |
884 | check_dir_entry_contains(ent, dir->entries[src])) | |
885 | free(dir->entries[src++]); | |
886 | ||
887 | /* compensate for the outer loop's loop control */ | |
888 | src--; | |
889 | } | |
890 | } | |
891 | dir->nr = dst; | |
892 | } | |
893 | ||
113f10f2 SB |
894 | int cmd_clean(int argc, const char **argv, const char *prefix) |
895 | { | |
f538a91e ZK |
896 | int i, res; |
897 | int dry_run = 0, remove_directories = 0, quiet = 0, ignored = 0; | |
898 | int ignored_only = 0, config_set = 0, errors = 0, gone = 1; | |
a0f4afbe | 899 | int rm_flags = REMOVE_DIR_KEEP_NESTED_GIT; |
396049e5 | 900 | struct strbuf abs_path = STRBUF_INIT; |
113f10f2 | 901 | struct dir_struct dir; |
893d8399 | 902 | struct pathspec pathspec; |
f285a2d7 | 903 | struct strbuf buf = STRBUF_INIT; |
bdab6a59 | 904 | struct string_list exclude_list = STRING_LIST_INIT_NODUP; |
caa3d554 | 905 | struct pattern_list *pl; |
396049e5 | 906 | struct string_list_item *item; |
1fb32894 | 907 | const char *qname; |
113f10f2 | 908 | struct option options[] = { |
145f9c81 | 909 | OPT__QUIET(&quiet, N_("do not print names of files removed")), |
f538a91e | 910 | OPT__DRY_RUN(&dry_run, N_("dry run")), |
26e90958 | 911 | OPT__FORCE(&force, N_("force"), PARSE_OPT_NOCOMPLETE), |
17696002 | 912 | OPT_BOOL('i', "interactive", &interactive, N_("interactive cleaning")), |
d5d09d47 | 913 | OPT_BOOL('d', NULL, &remove_directories, |
145f9c81 NTND |
914 | N_("remove whole directories")), |
915 | { OPTION_CALLBACK, 'e', "exclude", &exclude_list, N_("pattern"), | |
916 | N_("add <pattern> to ignore rules"), PARSE_OPT_NONEG, exclude_cb }, | |
d5d09d47 SB |
917 | OPT_BOOL('x', NULL, &ignored, N_("remove ignored files, too")), |
918 | OPT_BOOL('X', NULL, &ignored_only, | |
145f9c81 | 919 | N_("remove only ignored files")), |
113f10f2 SB |
920 | OPT_END() |
921 | }; | |
922 | ||
ef90d6d4 | 923 | git_config(git_clean_config, NULL); |
625db1b7 JH |
924 | if (force < 0) |
925 | force = 0; | |
926 | else | |
927 | config_set = 1; | |
928 | ||
37782920 SB |
929 | argc = parse_options(argc, argv, prefix, options, builtin_clean_usage, |
930 | 0); | |
113f10f2 SB |
931 | |
932 | memset(&dir, 0, sizeof(dir)); | |
1617adc7 | 933 | if (ignored_only) |
7c4c97c0 | 934 | dir.flags |= DIR_SHOW_IGNORED; |
113f10f2 SB |
935 | |
936 | if (ignored && ignored_only) | |
2da57add | 937 | die(_("-x and -X cannot be used together")); |
113f10f2 | 938 | |
17696002 | 939 | if (!interactive && !dry_run && !force) { |
a66f9b2a | 940 | if (config_set) |
235e8d59 | 941 | die(_("clean.requireForce set to true and neither -i, -n, nor -f given; " |
a66f9b2a ÆAB |
942 | "refusing to clean")); |
943 | else | |
235e8d59 JL |
944 | die(_("clean.requireForce defaults to true and neither -i, -n, nor -f given;" |
945 | " refusing to clean")); | |
a66f9b2a | 946 | } |
113f10f2 | 947 | |
a0f4afbe JH |
948 | if (force > 1) |
949 | rm_flags = 0; | |
09487f2c EN |
950 | else |
951 | dir.flags |= DIR_SKIP_NESTED_GIT; | |
a0f4afbe | 952 | |
7c4c97c0 | 953 | dir.flags |= DIR_SHOW_OTHER_DIRECTORIES; |
113f10f2 | 954 | |
e86bbcf9 EN |
955 | if (argc) { |
956 | /* | |
957 | * Remaining args implies pathspecs specified, and we should | |
958 | * recurse within those. | |
959 | */ | |
960 | remove_directories = 1; | |
961 | } | |
962 | ||
6b1db431 SL |
963 | if (remove_directories) |
964 | dir.flags |= DIR_SHOW_IGNORED_TOO | DIR_KEEP_UNTRACKED_CONTENTS; | |
965 | ||
c28b3d6e | 966 | if (read_cache() < 0) |
2da57add | 967 | die(_("index file corrupt")); |
c28b3d6e | 968 | |
1617adc7 SB |
969 | if (!ignored) |
970 | setup_standard_excludes(&dir); | |
113f10f2 | 971 | |
65edd96a | 972 | pl = add_pattern_list(&dir, EXC_CMDL, "--exclude option"); |
07de4eba | 973 | for (i = 0; i < exclude_list.nr; i++) |
65edd96a | 974 | add_pattern(exclude_list.items[i].string, "", 0, pl, -(i+1)); |
07de4eba | 975 | |
893d8399 NTND |
976 | parse_pathspec(&pathspec, 0, |
977 | PATHSPEC_PREFER_CWD, | |
978 | prefix, argv); | |
113f10f2 | 979 | |
0d32c183 | 980 | fill_directory(&dir, &the_index, &pathspec); |
6b1db431 | 981 | correct_untracked_entries(&dir); |
d871c869 JH |
982 | |
983 | for (i = 0; i < dir.nr; i++) { | |
984 | struct dir_entry *ent = dir.entries[i]; | |
f2d0df71 | 985 | int matches = 0; |
113f10f2 | 986 | struct stat st; |
396049e5 | 987 | const char *rel; |
113f10f2 | 988 | |
2e70c017 NTND |
989 | if (!cache_name_is_other(ent->name, ent->len)) |
990 | continue; | |
113f10f2 | 991 | |
893d8399 | 992 | if (pathspec.nr) |
6d2df284 | 993 | matches = dir_path_match(&the_index, ent, &pathspec, 0, NULL); |
d871c869 | 994 | |
cf424f5f JK |
995 | if (pathspec.nr && !matches) |
996 | continue; | |
997 | ||
838d6a92 DT |
998 | if (lstat(ent->name, &st)) |
999 | die_errno("Cannot lstat '%s'", ent->name); | |
1000 | ||
1f2e1088 JK |
1001 | if (S_ISDIR(st.st_mode) && !remove_directories && |
1002 | matches != MATCHED_EXACTLY) | |
1003 | continue; | |
1004 | ||
1005 | rel = relative_path(ent->name, prefix, &buf); | |
1006 | string_list_append(&del_list, rel); | |
396049e5 JX |
1007 | } |
1008 | ||
6b1db431 SL |
1009 | for (i = 0; i < dir.nr; i++) |
1010 | free(dir.entries[i]); | |
1011 | ||
1012 | for (i = 0; i < dir.ignored_nr; i++) | |
1013 | free(dir.ignored[i]); | |
1014 | ||
17696002 JX |
1015 | if (interactive && del_list.nr > 0) |
1016 | interactive_main_loop(); | |
396049e5 JX |
1017 | |
1018 | for_each_string_list_item(item, &del_list) { | |
1019 | struct stat st; | |
1020 | ||
902b90cf | 1021 | strbuf_reset(&abs_path); |
396049e5 JX |
1022 | if (prefix) |
1023 | strbuf_addstr(&abs_path, prefix); | |
1024 | ||
1025 | strbuf_addstr(&abs_path, item->string); | |
1026 | ||
1027 | /* | |
1028 | * we might have removed this as part of earlier | |
1029 | * recursive directory removal, so lstat() here could | |
1030 | * fail with ENOENT. | |
1031 | */ | |
1032 | if (lstat(abs_path.buf, &st)) | |
1033 | continue; | |
1034 | ||
1035 | if (S_ISDIR(st.st_mode)) { | |
1036 | if (remove_dirs(&abs_path, prefix, rm_flags, dry_run, quiet, &gone)) | |
1037 | errors++; | |
1038 | if (gone && !quiet) { | |
1039 | qname = quote_path_relative(item->string, NULL, &buf); | |
1040 | printf(dry_run ? _(msg_would_remove) : _(msg_remove), qname); | |
1041 | } | |
1042 | } else { | |
1043 | res = dry_run ? 0 : unlink(abs_path.buf); | |
f538a91e | 1044 | if (res) { |
cccf97d6 | 1045 | int saved_errno = errno; |
396049e5 | 1046 | qname = quote_path_relative(item->string, NULL, &buf); |
cccf97d6 NTND |
1047 | errno = saved_errno; |
1048 | warning_errno(_(msg_warn_remove_failed), qname); | |
aa9c83c2 | 1049 | errors++; |
f538a91e | 1050 | } else if (!quiet) { |
396049e5 | 1051 | qname = quote_path_relative(item->string, NULL, &buf); |
f538a91e | 1052 | printf(dry_run ? _(msg_would_remove) : _(msg_remove), qname); |
aa9c83c2 | 1053 | } |
113f10f2 SB |
1054 | } |
1055 | } | |
1056 | ||
396049e5 JX |
1057 | strbuf_release(&abs_path); |
1058 | strbuf_release(&buf); | |
1059 | string_list_clear(&del_list, 0); | |
07de4eba | 1060 | string_list_clear(&exclude_list, 0); |
aa9c83c2 | 1061 | return (errors != 0); |
113f10f2 | 1062 | } |