2 * "git clean" builtin command
4 * Copyright (C) 2007 Shawn Bohrer
6 * Based on git-clean.sh by Pavel Roskin
12 #include "parse-options.h"
13 #include "string-list.h"
19 static int force
= -1; /* unset */
20 static int interactive
;
21 static struct string_list del_list
= STRING_LIST_INIT_DUP
;
22 static unsigned int colopts
;
24 static const char *const builtin_clean_usage
[] = {
25 N_("git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>..."),
29 static const char *msg_remove
= N_("Removing %s\n");
30 static const char *msg_would_remove
= N_("Would remove %s\n");
31 static const char *msg_skip_git_dir
= N_("Skipping repository %s\n");
32 static const char *msg_would_skip_git_dir
= N_("Would skip repository %s\n");
33 static const char *msg_warn_remove_failed
= N_("failed to remove %s");
35 static int clean_use_color
= -1;
36 static char clean_colors
[][COLOR_MAXLEN
] = {
38 GIT_COLOR_NORMAL
, /* PLAIN */
39 GIT_COLOR_BOLD_BLUE
, /* PROMPT */
40 GIT_COLOR_BOLD
, /* HEADER */
41 GIT_COLOR_BOLD_RED
, /* HELP */
42 GIT_COLOR_BOLD_RED
, /* ERROR */
45 CLEAN_COLOR_RESET
= 0,
46 CLEAN_COLOR_PLAIN
= 1,
47 CLEAN_COLOR_PROMPT
= 2,
48 CLEAN_COLOR_HEADER
= 3,
53 #define MENU_OPTS_SINGLETON 01
54 #define MENU_OPTS_IMMEDIATE 02
55 #define MENU_OPTS_LIST_ONLY 04
63 #define MENU_RETURN_NO_LOOP 10
72 enum menu_stuff_type
{
73 MENU_STUFF_TYPE_STRING_LIST
= 1,
74 MENU_STUFF_TYPE_MENU_ITEM
78 enum menu_stuff_type type
;
83 static int parse_clean_color_slot(const char *var
)
85 if (!strcasecmp(var
, "reset"))
86 return CLEAN_COLOR_RESET
;
87 if (!strcasecmp(var
, "plain"))
88 return CLEAN_COLOR_PLAIN
;
89 if (!strcasecmp(var
, "prompt"))
90 return CLEAN_COLOR_PROMPT
;
91 if (!strcasecmp(var
, "header"))
92 return CLEAN_COLOR_HEADER
;
93 if (!strcasecmp(var
, "help"))
94 return CLEAN_COLOR_HELP
;
95 if (!strcasecmp(var
, "error"))
96 return CLEAN_COLOR_ERROR
;
100 static int git_clean_config(const char *var
, const char *value
, void *cb
)
102 const char *slot_name
;
104 if (starts_with(var
, "column."))
105 return git_column_config(var
, value
, "clean", &colopts
);
107 /* honors the color.interactive* config variables which also
108 applied in git-add--interactive and git-stash */
109 if (!strcmp(var
, "color.interactive")) {
110 clean_use_color
= git_config_colorbool(var
, value
);
113 if (skip_prefix(var
, "color.interactive.", &slot_name
)) {
114 int slot
= parse_clean_color_slot(slot_name
);
118 return config_error_nonbool(var
);
119 return color_parse(value
, clean_colors
[slot
]);
122 if (!strcmp(var
, "clean.requireforce")) {
123 force
= !git_config_bool(var
, value
);
127 /* inspect the color.ui config variable and others */
128 return git_color_default_config(var
, value
, cb
);
131 static const char *clean_get_color(enum color_clean ix
)
133 if (want_color(clean_use_color
))
134 return clean_colors
[ix
];
138 static void clean_print_color(enum color_clean ix
)
140 printf("%s", clean_get_color(ix
));
143 static int exclude_cb(const struct option
*opt
, const char *arg
, int unset
)
145 struct string_list
*exclude_list
= opt
->value
;
146 string_list_append(exclude_list
, arg
);
151 * Return 1 if the given path is the root of a git repository or
152 * submodule else 0. Will not return 1 for bare repositories with the
153 * exception of creating a bare repository in "foo/.git" and calling
154 * is_git_repository("foo").
156 static int is_git_repository(struct strbuf
*path
)
160 size_t orig_path_len
= path
->len
;
161 assert(orig_path_len
!= 0);
162 strbuf_complete(path
, '/');
163 strbuf_addstr(path
, ".git");
164 if (read_gitfile_gently(path
->buf
, &gitfile_error
) || is_git_directory(path
->buf
))
166 if (gitfile_error
== READ_GITFILE_ERR_OPEN_FAILED
||
167 gitfile_error
== READ_GITFILE_ERR_READ_FAILED
)
168 ret
= 1; /* This could be a real .git file, take the
169 * safe option and avoid cleaning */
170 strbuf_setlen(path
, orig_path_len
);
174 static int remove_dirs(struct strbuf
*path
, const char *prefix
, int force_flag
,
175 int dry_run
, int quiet
, int *dir_gone
)
178 struct strbuf quoted
= STRBUF_INIT
;
180 int res
= 0, ret
= 0, gone
= 1, original_len
= path
->len
, len
;
181 struct string_list dels
= STRING_LIST_INIT_DUP
;
185 if ((force_flag
& REMOVE_DIR_KEEP_NESTED_GIT
) && is_git_repository(path
)) {
187 quote_path_relative(path
->buf
, prefix
, "ed
);
188 printf(dry_run
? _(msg_would_skip_git_dir
) : _(msg_skip_git_dir
),
196 dir
= opendir(path
->buf
);
198 /* an empty dir could be removed even if it is unreadble */
199 res
= dry_run
? 0 : rmdir(path
->buf
);
201 quote_path_relative(path
->buf
, prefix
, "ed
);
202 warning(_(msg_warn_remove_failed
), quoted
.buf
);
208 strbuf_complete(path
, '/');
211 while ((e
= readdir(dir
)) != NULL
) {
213 if (is_dot_or_dotdot(e
->d_name
))
216 strbuf_setlen(path
, len
);
217 strbuf_addstr(path
, e
->d_name
);
218 if (lstat(path
->buf
, &st
))
220 else if (S_ISDIR(st
.st_mode
)) {
221 if (remove_dirs(path
, prefix
, force_flag
, dry_run
, quiet
, &gone
))
224 quote_path_relative(path
->buf
, prefix
, "ed
);
225 string_list_append(&dels
, quoted
.buf
);
230 res
= dry_run
? 0 : unlink(path
->buf
);
232 quote_path_relative(path
->buf
, prefix
, "ed
);
233 string_list_append(&dels
, quoted
.buf
);
235 quote_path_relative(path
->buf
, prefix
, "ed
);
236 warning(_(msg_warn_remove_failed
), quoted
.buf
);
243 /* path too long, stat fails, or non-directory still exists */
250 strbuf_setlen(path
, original_len
);
253 res
= dry_run
? 0 : rmdir(path
->buf
);
257 quote_path_relative(path
->buf
, prefix
, "ed
);
258 warning(_(msg_warn_remove_failed
), quoted
.buf
);
264 if (!*dir_gone
&& !quiet
) {
266 for (i
= 0; i
< dels
.nr
; i
++)
267 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), dels
.items
[i
].string
);
269 string_list_clear(&dels
, 0);
273 static void pretty_print_dels(void)
275 struct string_list list
= STRING_LIST_INIT_DUP
;
276 struct string_list_item
*item
;
277 struct strbuf buf
= STRBUF_INIT
;
279 struct column_options copts
;
281 for_each_string_list_item(item
, &del_list
) {
282 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
283 string_list_append(&list
, qname
);
287 * always enable column display, we only consult column.*
288 * about layout strategy and stuff
290 colopts
= (colopts
& ~COL_ENABLE_MASK
) | COL_ENABLED
;
291 memset(&copts
, 0, sizeof(copts
));
294 print_columns(&list
, colopts
, &copts
);
295 strbuf_release(&buf
);
296 string_list_clear(&list
, 0);
299 static void pretty_print_menus(struct string_list
*menu_list
)
301 unsigned int local_colopts
= 0;
302 struct column_options copts
;
304 local_colopts
= COL_ENABLED
| COL_ROW
;
305 memset(&copts
, 0, sizeof(copts
));
308 print_columns(menu_list
, local_colopts
, &copts
);
311 static void prompt_help_cmd(int singleton
)
313 clean_print_color(CLEAN_COLOR_HELP
);
314 printf_ln(singleton
?
316 "1 - select a numbered item\n"
317 "foo - select item based on unique prefix\n"
318 " - (empty) select nothing") :
320 "1 - select a single item\n"
321 "3-5 - select a range of items\n"
322 "2-3,6-9 - select multiple ranges\n"
323 "foo - select item based on unique prefix\n"
324 "-... - unselect specified items\n"
325 "* - choose all items\n"
326 " - (empty) finish selecting"));
327 clean_print_color(CLEAN_COLOR_RESET
);
331 * display menu stuff with number prefix and hotkey highlight
333 static void print_highlight_menu_stuff(struct menu_stuff
*stuff
, int **chosen
)
335 struct string_list menu_list
= STRING_LIST_INIT_DUP
;
336 struct strbuf menu
= STRBUF_INIT
;
337 struct menu_item
*menu_item
;
338 struct string_list_item
*string_list_item
;
341 switch (stuff
->type
) {
343 die("Bad type of menu_stuff when print menu");
344 case MENU_STUFF_TYPE_MENU_ITEM
:
345 menu_item
= (struct menu_item
*)stuff
->stuff
;
346 for (i
= 0; i
< stuff
->nr
; i
++, menu_item
++) {
350 p
= menu_item
->title
;
351 if ((*chosen
)[i
] < 0)
352 (*chosen
)[i
] = menu_item
->selected
? 1 : 0;
353 strbuf_addf(&menu
, "%s%2d: ", (*chosen
)[i
] ? "*" : " ", i
+1);
355 if (!highlighted
&& *p
== menu_item
->hotkey
) {
356 strbuf_addstr(&menu
, clean_get_color(CLEAN_COLOR_PROMPT
));
357 strbuf_addch(&menu
, *p
);
358 strbuf_addstr(&menu
, clean_get_color(CLEAN_COLOR_RESET
));
361 strbuf_addch(&menu
, *p
);
364 string_list_append(&menu_list
, menu
.buf
);
368 case MENU_STUFF_TYPE_STRING_LIST
:
370 for_each_string_list_item(string_list_item
, (struct string_list
*)stuff
->stuff
) {
371 if ((*chosen
)[i
] < 0)
373 strbuf_addf(&menu
, "%s%2d: %s",
374 (*chosen
)[i
] ? "*" : " ", i
+1, string_list_item
->string
);
375 string_list_append(&menu_list
, menu
.buf
);
382 pretty_print_menus(&menu_list
);
384 strbuf_release(&menu
);
385 string_list_clear(&menu_list
, 0);
388 static int find_unique(const char *choice
, struct menu_stuff
*menu_stuff
)
390 struct menu_item
*menu_item
;
391 struct string_list_item
*string_list_item
;
392 int i
, len
, found
= 0;
394 len
= strlen(choice
);
395 switch (menu_stuff
->type
) {
397 die("Bad type of menu_stuff when parse choice");
398 case MENU_STUFF_TYPE_MENU_ITEM
:
400 menu_item
= (struct menu_item
*)menu_stuff
->stuff
;
401 for (i
= 0; i
< menu_stuff
->nr
; i
++, menu_item
++) {
402 if (len
== 1 && *choice
== menu_item
->hotkey
) {
406 if (!strncasecmp(choice
, menu_item
->title
, len
)) {
409 /* continue for hotkey matching */
421 case MENU_STUFF_TYPE_STRING_LIST
:
422 string_list_item
= ((struct string_list
*)menu_stuff
->stuff
)->items
;
423 for (i
= 0; i
< menu_stuff
->nr
; i
++, string_list_item
++) {
424 if (!strncasecmp(choice
, string_list_item
->string
, len
)) {
439 * Parse user input, and return choice(s) for menu (menu_stuff).
442 * (for single choice)
443 * 1 - select a numbered item
444 * foo - select item based on menu title
445 * - (empty) select nothing
447 * (for multiple choice)
448 * 1 - select a single item
449 * 3-5 - select a range of items
450 * 2-3,6-9 - select multiple ranges
451 * foo - select item based on menu title
452 * -... - unselect specified items
453 * * - choose all items
454 * - (empty) finish selecting
456 * The parse result will be saved in array **chosen, and
457 * return number of total selections.
459 static int parse_choice(struct menu_stuff
*menu_stuff
,
464 struct strbuf
**choice_list
, **ptr
;
469 choice_list
= strbuf_split_max(&input
, '\n', 0);
476 choice_list
= strbuf_split_max(&input
, ' ', 0);
479 for (ptr
= choice_list
; *ptr
; ptr
++) {
482 int bottom
= 0, top
= 0;
483 int is_range
, is_number
;
489 /* Input that begins with '-'; unchoose */
490 if (*(*ptr
)->buf
== '-') {
492 strbuf_remove((*ptr
), 0, 1);
497 for (p
= (*ptr
)->buf
; *p
; p
++) {
507 } else if (!isdigit(*p
)) {
515 bottom
= atoi((*ptr
)->buf
);
517 } else if (is_range
) {
518 bottom
= atoi((*ptr
)->buf
);
519 /* a range can be specified like 5-7 or 5- */
520 if (!*(strchr((*ptr
)->buf
, '-') + 1))
521 top
= menu_stuff
->nr
;
523 top
= atoi(strchr((*ptr
)->buf
, '-') + 1);
524 } else if (!strcmp((*ptr
)->buf
, "*")) {
526 top
= menu_stuff
->nr
;
528 bottom
= find_unique((*ptr
)->buf
, menu_stuff
);
532 if (top
<= 0 || bottom
<= 0 || top
> menu_stuff
->nr
|| bottom
> top
||
533 (is_single
&& bottom
!= top
)) {
534 clean_print_color(CLEAN_COLOR_ERROR
);
535 printf_ln(_("Huh (%s)?"), (*ptr
)->buf
);
536 clean_print_color(CLEAN_COLOR_RESET
);
540 for (i
= bottom
; i
<= top
; i
++)
541 (*chosen
)[i
-1] = choose
;
544 strbuf_list_free(choice_list
);
546 for (i
= 0; i
< menu_stuff
->nr
; i
++)
552 * Implement a git-add-interactive compatible UI, which is borrowed
553 * from git-add--interactive.perl.
557 * - Return an array of integers
558 * - , and it is up to you to free the allocated memory.
559 * - The array ends with EOF.
560 * - If user pressed CTRL-D (i.e. EOF), no selection returned.
562 static int *list_and_choose(struct menu_opts
*opts
, struct menu_stuff
*stuff
)
564 struct strbuf choice
= STRBUF_INIT
;
565 int *chosen
, *result
;
570 chosen
= xmalloc(sizeof(int) * stuff
->nr
);
571 /* set chosen as uninitialized */
572 for (i
= 0; i
< stuff
->nr
; i
++)
578 clean_get_color(CLEAN_COLOR_HEADER
),
580 clean_get_color(CLEAN_COLOR_RESET
));
583 /* chosen will be initialized by print_highlight_menu_stuff */
584 print_highlight_menu_stuff(stuff
, &chosen
);
586 if (opts
->flags
& MENU_OPTS_LIST_ONLY
)
591 clean_get_color(CLEAN_COLOR_PROMPT
),
593 opts
->flags
& MENU_OPTS_SINGLETON
? "> " : ">> ",
594 clean_get_color(CLEAN_COLOR_RESET
));
597 if (strbuf_getline(&choice
, stdin
, '\n') != EOF
) {
598 strbuf_trim(&choice
);
604 /* help for prompt */
605 if (!strcmp(choice
.buf
, "?")) {
606 prompt_help_cmd(opts
->flags
& MENU_OPTS_SINGLETON
);
610 /* for a multiple-choice menu, press ENTER (empty) will return back */
611 if (!(opts
->flags
& MENU_OPTS_SINGLETON
) && !choice
.len
)
614 nr
= parse_choice(stuff
,
615 opts
->flags
& MENU_OPTS_SINGLETON
,
619 if (opts
->flags
& MENU_OPTS_SINGLETON
) {
622 } else if (opts
->flags
& MENU_OPTS_IMMEDIATE
) {
628 result
= xmalloc(sizeof(int));
634 * recalculate nr, if return back from menu directly with
635 * default selections.
638 for (i
= 0; i
< stuff
->nr
; i
++)
642 result
= xcalloc(nr
+ 1, sizeof(int));
643 for (i
= 0; i
< stuff
->nr
&& j
< nr
; i
++) {
651 strbuf_release(&choice
);
655 static int clean_cmd(void)
657 return MENU_RETURN_NO_LOOP
;
660 static int filter_by_patterns_cmd(void)
662 struct dir_struct dir
;
663 struct strbuf confirm
= STRBUF_INIT
;
664 struct strbuf
**ignore_list
;
665 struct string_list_item
*item
;
666 struct exclude_list
*el
;
676 clean_print_color(CLEAN_COLOR_PROMPT
);
677 printf(_("Input ignore patterns>> "));
678 clean_print_color(CLEAN_COLOR_RESET
);
679 if (strbuf_getline(&confirm
, stdin
, '\n') != EOF
)
680 strbuf_trim(&confirm
);
684 /* quit filter_by_pattern mode if press ENTER or Ctrl-D */
688 memset(&dir
, 0, sizeof(dir
));
689 el
= add_exclude_list(&dir
, EXC_CMDL
, "manual exclude");
690 ignore_list
= strbuf_split_max(&confirm
, ' ', 0);
692 for (i
= 0; ignore_list
[i
]; i
++) {
693 strbuf_trim(ignore_list
[i
]);
694 if (!ignore_list
[i
]->len
)
697 add_exclude(ignore_list
[i
]->buf
, "", 0, el
, -(i
+1));
701 for_each_string_list_item(item
, &del_list
) {
702 int dtype
= DT_UNKNOWN
;
704 if (is_excluded(&dir
, item
->string
, &dtype
)) {
705 *item
->string
= '\0';
711 string_list_remove_empty_items(&del_list
, 0);
713 clean_print_color(CLEAN_COLOR_ERROR
);
714 printf_ln(_("WARNING: Cannot find items matched by: %s"), confirm
.buf
);
715 clean_print_color(CLEAN_COLOR_RESET
);
718 strbuf_list_free(ignore_list
);
719 clear_directory(&dir
);
722 strbuf_release(&confirm
);
726 static int select_by_numbers_cmd(void)
728 struct menu_opts menu_opts
;
729 struct menu_stuff menu_stuff
;
730 struct string_list_item
*items
;
734 menu_opts
.header
= NULL
;
735 menu_opts
.prompt
= N_("Select items to delete");
738 menu_stuff
.type
= MENU_STUFF_TYPE_STRING_LIST
;
739 menu_stuff
.stuff
= &del_list
;
740 menu_stuff
.nr
= del_list
.nr
;
742 chosen
= list_and_choose(&menu_opts
, &menu_stuff
);
743 items
= del_list
.items
;
744 for (i
= 0, j
= 0; i
< del_list
.nr
; i
++) {
746 *(items
[i
].string
) = '\0';
747 } else if (i
== chosen
[j
]) {
748 /* delete selected item */
752 /* end of chosen (chosen[j] == EOF), won't delete */
753 *(items
[i
].string
) = '\0';
757 string_list_remove_empty_items(&del_list
, 0);
763 static int ask_each_cmd(void)
765 struct strbuf confirm
= STRBUF_INIT
;
766 struct strbuf buf
= STRBUF_INIT
;
767 struct string_list_item
*item
;
769 int changed
= 0, eof
= 0;
771 for_each_string_list_item(item
, &del_list
) {
772 /* Ctrl-D should stop removing files */
774 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
775 /* TRANSLATORS: Make sure to keep [y/N] as is */
776 printf(_("Remove %s [y/N]? "), qname
);
777 if (strbuf_getline(&confirm
, stdin
, '\n') != EOF
) {
778 strbuf_trim(&confirm
);
784 if (!confirm
.len
|| strncasecmp(confirm
.buf
, "yes", confirm
.len
)) {
785 *item
->string
= '\0';
791 string_list_remove_empty_items(&del_list
, 0);
793 strbuf_release(&buf
);
794 strbuf_release(&confirm
);
795 return MENU_RETURN_NO_LOOP
;
798 static int quit_cmd(void)
800 string_list_clear(&del_list
, 0);
801 printf_ln(_("Bye."));
802 return MENU_RETURN_NO_LOOP
;
805 static int help_cmd(void)
807 clean_print_color(CLEAN_COLOR_HELP
);
809 "clean - start cleaning\n"
810 "filter by pattern - exclude items from deletion\n"
811 "select by numbers - select items to be deleted by numbers\n"
812 "ask each - confirm each deletion (like \"rm -i\")\n"
813 "quit - stop cleaning\n"
814 "help - this screen\n"
815 "? - help for prompt selection"
817 clean_print_color(CLEAN_COLOR_RESET
);
821 static void interactive_main_loop(void)
823 while (del_list
.nr
) {
824 struct menu_opts menu_opts
;
825 struct menu_stuff menu_stuff
;
826 struct menu_item menus
[] = {
827 {'c', "clean", 0, clean_cmd
},
828 {'f', "filter by pattern", 0, filter_by_patterns_cmd
},
829 {'s', "select by numbers", 0, select_by_numbers_cmd
},
830 {'a', "ask each", 0, ask_each_cmd
},
831 {'q', "quit", 0, quit_cmd
},
832 {'h', "help", 0, help_cmd
},
836 menu_opts
.header
= N_("*** Commands ***");
837 menu_opts
.prompt
= N_("What now");
838 menu_opts
.flags
= MENU_OPTS_SINGLETON
;
840 menu_stuff
.type
= MENU_STUFF_TYPE_MENU_ITEM
;
841 menu_stuff
.stuff
= menus
;
842 menu_stuff
.nr
= sizeof(menus
) / sizeof(struct menu_item
);
844 clean_print_color(CLEAN_COLOR_HEADER
);
845 printf_ln(Q_("Would remove the following item:",
846 "Would remove the following items:",
848 clean_print_color(CLEAN_COLOR_RESET
);
852 chosen
= list_and_choose(&menu_opts
, &menu_stuff
);
854 if (*chosen
!= EOF
) {
856 ret
= menus
[*chosen
].fn();
857 if (ret
!= MENU_RETURN_NO_LOOP
) {
861 clean_print_color(CLEAN_COLOR_ERROR
);
862 printf_ln(_("No more files to clean, exiting."));
863 clean_print_color(CLEAN_COLOR_RESET
);
878 int cmd_clean(int argc
, const char **argv
, const char *prefix
)
881 int dry_run
= 0, remove_directories
= 0, quiet
= 0, ignored
= 0;
882 int ignored_only
= 0, config_set
= 0, errors
= 0, gone
= 1;
883 int rm_flags
= REMOVE_DIR_KEEP_NESTED_GIT
;
884 struct strbuf abs_path
= STRBUF_INIT
;
885 struct dir_struct dir
;
886 struct pathspec pathspec
;
887 struct strbuf buf
= STRBUF_INIT
;
888 struct string_list exclude_list
= STRING_LIST_INIT_NODUP
;
889 struct exclude_list
*el
;
890 struct string_list_item
*item
;
892 struct option options
[] = {
893 OPT__QUIET(&quiet
, N_("do not print names of files removed")),
894 OPT__DRY_RUN(&dry_run
, N_("dry run")),
895 OPT__FORCE(&force
, N_("force")),
896 OPT_BOOL('i', "interactive", &interactive
, N_("interactive cleaning")),
897 OPT_BOOL('d', NULL
, &remove_directories
,
898 N_("remove whole directories")),
899 { OPTION_CALLBACK
, 'e', "exclude", &exclude_list
, N_("pattern"),
900 N_("add <pattern> to ignore rules"), PARSE_OPT_NONEG
, exclude_cb
},
901 OPT_BOOL('x', NULL
, &ignored
, N_("remove ignored files, too")),
902 OPT_BOOL('X', NULL
, &ignored_only
,
903 N_("remove only ignored files")),
907 git_config(git_clean_config
, NULL
);
913 argc
= parse_options(argc
, argv
, prefix
, options
, builtin_clean_usage
,
916 memset(&dir
, 0, sizeof(dir
));
918 dir
.flags
|= DIR_SHOW_IGNORED
;
920 if (ignored
&& ignored_only
)
921 die(_("-x and -X cannot be used together"));
923 if (!interactive
&& !dry_run
&& !force
) {
925 die(_("clean.requireForce set to true and neither -i, -n, nor -f given; "
926 "refusing to clean"));
928 die(_("clean.requireForce defaults to true and neither -i, -n, nor -f given;"
929 " refusing to clean"));
935 dir
.flags
|= DIR_SHOW_OTHER_DIRECTORIES
;
937 if (read_cache() < 0)
938 die(_("index file corrupt"));
941 setup_standard_excludes(&dir
);
943 el
= add_exclude_list(&dir
, EXC_CMDL
, "--exclude option");
944 for (i
= 0; i
< exclude_list
.nr
; i
++)
945 add_exclude(exclude_list
.items
[i
].string
, "", 0, el
, -(i
+1));
947 parse_pathspec(&pathspec
, 0,
951 fill_directory(&dir
, &pathspec
);
953 for (i
= 0; i
< dir
.nr
; i
++) {
954 struct dir_entry
*ent
= dir
.entries
[i
];
959 if (!cache_name_is_other(ent
->name
, ent
->len
))
963 matches
= dir_path_match(ent
, &pathspec
, 0, NULL
);
965 if (pathspec
.nr
&& !matches
)
968 if (lstat(ent
->name
, &st
))
969 die_errno("Cannot lstat '%s'", ent
->name
);
971 if (S_ISDIR(st
.st_mode
) && !remove_directories
&&
972 matches
!= MATCHED_EXACTLY
)
975 rel
= relative_path(ent
->name
, prefix
, &buf
);
976 string_list_append(&del_list
, rel
);
979 if (interactive
&& del_list
.nr
> 0)
980 interactive_main_loop();
982 for_each_string_list_item(item
, &del_list
) {
986 strbuf_addstr(&abs_path
, prefix
);
988 strbuf_addstr(&abs_path
, item
->string
);
991 * we might have removed this as part of earlier
992 * recursive directory removal, so lstat() here could
995 if (lstat(abs_path
.buf
, &st
))
998 if (S_ISDIR(st
.st_mode
)) {
999 if (remove_dirs(&abs_path
, prefix
, rm_flags
, dry_run
, quiet
, &gone
))
1001 if (gone
&& !quiet
) {
1002 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
1003 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), qname
);
1006 res
= dry_run
? 0 : unlink(abs_path
.buf
);
1008 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
1009 warning(_(msg_warn_remove_failed
), qname
);
1011 } else if (!quiet
) {
1012 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
1013 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), qname
);
1016 strbuf_reset(&abs_path
);
1019 strbuf_release(&abs_path
);
1020 strbuf_release(&buf
);
1021 string_list_clear(&del_list
, 0);
1022 string_list_clear(&exclude_list
, 0);
1023 return (errors
!= 0);