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