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"
14 #include "string-list.h"
20 static int force
= -1; /* unset */
21 static int interactive
;
22 static struct string_list del_list
= STRING_LIST_INIT_DUP
;
23 static unsigned int colopts
;
25 static const char *const builtin_clean_usage
[] = {
26 N_("git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>..."),
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");
36 static int clean_use_color
= -1;
37 static char clean_colors
[][COLOR_MAXLEN
] = {
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 */
46 CLEAN_COLOR_RESET
= 0,
47 CLEAN_COLOR_PLAIN
= 1,
48 CLEAN_COLOR_PROMPT
= 2,
49 CLEAN_COLOR_HEADER
= 3,
54 #define MENU_OPTS_SINGLETON 01
55 #define MENU_OPTS_IMMEDIATE 02
56 #define MENU_OPTS_LIST_ONLY 04
64 #define MENU_RETURN_NO_LOOP 10
73 enum menu_stuff_type
{
74 MENU_STUFF_TYPE_STRING_LIST
= 1,
75 MENU_STUFF_TYPE_MENU_ITEM
79 enum menu_stuff_type type
;
84 static int parse_clean_color_slot(const char *var
)
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
;
101 static int git_clean_config(const char *var
, const char *value
, void *cb
)
103 if (starts_with(var
, "column."))
104 return git_column_config(var
, value
, "clean", &colopts
);
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
);
112 if (starts_with(var
, "color.interactive.")) {
113 int slot
= parse_clean_color_slot(var
+
114 strlen("color.interactive."));
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
);
150 static int remove_dirs(struct strbuf
*path
, const char *prefix
, int force_flag
,
151 int dry_run
, int quiet
, int *dir_gone
)
154 struct strbuf quoted
= STRBUF_INIT
;
156 int res
= 0, ret
= 0, gone
= 1, original_len
= path
->len
, len
;
157 unsigned char submodule_head
[20];
158 struct string_list dels
= STRING_LIST_INIT_DUP
;
162 if ((force_flag
& REMOVE_DIR_KEEP_NESTED_GIT
) &&
163 !resolve_gitlink_ref(path
->buf
, "HEAD", submodule_head
)) {
165 quote_path_relative(path
->buf
, prefix
, "ed
);
166 printf(dry_run
? _(msg_would_skip_git_dir
) : _(msg_skip_git_dir
),
174 dir
= opendir(path
->buf
);
176 /* an empty dir could be removed even if it is unreadble */
177 res
= dry_run
? 0 : rmdir(path
->buf
);
179 quote_path_relative(path
->buf
, prefix
, "ed
);
180 warning(_(msg_warn_remove_failed
), quoted
.buf
);
186 if (path
->buf
[original_len
- 1] != '/')
187 strbuf_addch(path
, '/');
190 while ((e
= readdir(dir
)) != NULL
) {
192 if (is_dot_or_dotdot(e
->d_name
))
195 strbuf_setlen(path
, len
);
196 strbuf_addstr(path
, e
->d_name
);
197 if (lstat(path
->buf
, &st
))
199 else if (S_ISDIR(st
.st_mode
)) {
200 if (remove_dirs(path
, prefix
, force_flag
, dry_run
, quiet
, &gone
))
203 quote_path_relative(path
->buf
, prefix
, "ed
);
204 string_list_append(&dels
, quoted
.buf
);
209 res
= dry_run
? 0 : unlink(path
->buf
);
211 quote_path_relative(path
->buf
, prefix
, "ed
);
212 string_list_append(&dels
, quoted
.buf
);
214 quote_path_relative(path
->buf
, prefix
, "ed
);
215 warning(_(msg_warn_remove_failed
), quoted
.buf
);
222 /* path too long, stat fails, or non-directory still exists */
229 strbuf_setlen(path
, original_len
);
232 res
= dry_run
? 0 : rmdir(path
->buf
);
236 quote_path_relative(path
->buf
, prefix
, "ed
);
237 warning(_(msg_warn_remove_failed
), quoted
.buf
);
243 if (!*dir_gone
&& !quiet
) {
245 for (i
= 0; i
< dels
.nr
; i
++)
246 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), dels
.items
[i
].string
);
248 string_list_clear(&dels
, 0);
252 static void pretty_print_dels(void)
254 struct string_list list
= STRING_LIST_INIT_DUP
;
255 struct string_list_item
*item
;
256 struct strbuf buf
= STRBUF_INIT
;
258 struct column_options copts
;
260 for_each_string_list_item(item
, &del_list
) {
261 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
262 string_list_append(&list
, qname
);
266 * always enable column display, we only consult column.*
267 * about layout strategy and stuff
269 colopts
= (colopts
& ~COL_ENABLE_MASK
) | COL_ENABLED
;
270 memset(&copts
, 0, sizeof(copts
));
273 print_columns(&list
, colopts
, &copts
);
274 strbuf_release(&buf
);
275 string_list_clear(&list
, 0);
278 static void pretty_print_menus(struct string_list
*menu_list
)
280 unsigned int local_colopts
= 0;
281 struct column_options copts
;
283 local_colopts
= COL_ENABLED
| COL_ROW
;
284 memset(&copts
, 0, sizeof(copts
));
287 print_columns(menu_list
, local_colopts
, &copts
);
290 static void prompt_help_cmd(int singleton
)
292 clean_print_color(CLEAN_COLOR_HELP
);
293 printf_ln(singleton
?
295 "1 - select a numbered item\n"
296 "foo - select item based on unique prefix\n"
297 " - (empty) select nothing") :
299 "1 - select a single item\n"
300 "3-5 - select a range of items\n"
301 "2-3,6-9 - select multiple ranges\n"
302 "foo - select item based on unique prefix\n"
303 "-... - unselect specified items\n"
304 "* - choose all items\n"
305 " - (empty) finish selecting"));
306 clean_print_color(CLEAN_COLOR_RESET
);
310 * display menu stuff with number prefix and hotkey highlight
312 static void print_highlight_menu_stuff(struct menu_stuff
*stuff
, int **chosen
)
314 struct string_list menu_list
= STRING_LIST_INIT_DUP
;
315 struct strbuf menu
= STRBUF_INIT
;
316 struct strbuf buf
= STRBUF_INIT
;
317 struct menu_item
*menu_item
;
318 struct string_list_item
*string_list_item
;
321 switch (stuff
->type
) {
323 die("Bad type of menu_staff when print menu");
324 case MENU_STUFF_TYPE_MENU_ITEM
:
325 menu_item
= (struct menu_item
*)stuff
->stuff
;
326 for (i
= 0; i
< stuff
->nr
; i
++, menu_item
++) {
330 p
= menu_item
->title
;
331 if ((*chosen
)[i
] < 0)
332 (*chosen
)[i
] = menu_item
->selected
? 1 : 0;
333 strbuf_addf(&menu
, "%s%2d: ", (*chosen
)[i
] ? "*" : " ", i
+1);
335 if (!highlighted
&& *p
== menu_item
->hotkey
) {
336 strbuf_addstr(&menu
, clean_get_color(CLEAN_COLOR_PROMPT
));
337 strbuf_addch(&menu
, *p
);
338 strbuf_addstr(&menu
, clean_get_color(CLEAN_COLOR_RESET
));
341 strbuf_addch(&menu
, *p
);
344 string_list_append(&menu_list
, menu
.buf
);
348 case MENU_STUFF_TYPE_STRING_LIST
:
350 for_each_string_list_item(string_list_item
, (struct string_list
*)stuff
->stuff
) {
351 if ((*chosen
)[i
] < 0)
353 strbuf_addf(&menu
, "%s%2d: %s",
354 (*chosen
)[i
] ? "*" : " ", i
+1, string_list_item
->string
);
355 string_list_append(&menu_list
, menu
.buf
);
362 pretty_print_menus(&menu_list
);
364 strbuf_release(&menu
);
365 strbuf_release(&buf
);
366 string_list_clear(&menu_list
, 0);
369 static int find_unique(const char *choice
, struct menu_stuff
*menu_stuff
)
371 struct menu_item
*menu_item
;
372 struct string_list_item
*string_list_item
;
373 int i
, len
, found
= 0;
375 len
= strlen(choice
);
376 switch (menu_stuff
->type
) {
378 die("Bad type of menu_stuff when parse choice");
379 case MENU_STUFF_TYPE_MENU_ITEM
:
381 menu_item
= (struct menu_item
*)menu_stuff
->stuff
;
382 for (i
= 0; i
< menu_stuff
->nr
; i
++, menu_item
++) {
383 if (len
== 1 && *choice
== menu_item
->hotkey
) {
387 if (!strncasecmp(choice
, menu_item
->title
, len
)) {
390 /* continue for hotkey matching */
402 case MENU_STUFF_TYPE_STRING_LIST
:
403 string_list_item
= ((struct string_list
*)menu_stuff
->stuff
)->items
;
404 for (i
= 0; i
< menu_stuff
->nr
; i
++, string_list_item
++) {
405 if (!strncasecmp(choice
, string_list_item
->string
, len
)) {
420 * Parse user input, and return choice(s) for menu (menu_stuff).
423 * (for single choice)
424 * 1 - select a numbered item
425 * foo - select item based on menu title
426 * - (empty) select nothing
428 * (for multiple choice)
429 * 1 - select a single item
430 * 3-5 - select a range of items
431 * 2-3,6-9 - select multiple ranges
432 * foo - select item based on menu title
433 * -... - unselect specified items
434 * * - choose all items
435 * - (empty) finish selecting
437 * The parse result will be saved in array **chosen, and
438 * return number of total selections.
440 static int parse_choice(struct menu_stuff
*menu_stuff
,
445 struct strbuf
**choice_list
, **ptr
;
450 choice_list
= strbuf_split_max(&input
, '\n', 0);
457 choice_list
= strbuf_split_max(&input
, ' ', 0);
460 for (ptr
= choice_list
; *ptr
; ptr
++) {
463 int bottom
= 0, top
= 0;
464 int is_range
, is_number
;
470 /* Input that begins with '-'; unchoose */
471 if (*(*ptr
)->buf
== '-') {
473 strbuf_remove((*ptr
), 0, 1);
478 for (p
= (*ptr
)->buf
; *p
; p
++) {
488 } else if (!isdigit(*p
)) {
496 bottom
= atoi((*ptr
)->buf
);
498 } else if (is_range
) {
499 bottom
= atoi((*ptr
)->buf
);
500 /* a range can be specified like 5-7 or 5- */
501 if (!*(strchr((*ptr
)->buf
, '-') + 1))
502 top
= menu_stuff
->nr
;
504 top
= atoi(strchr((*ptr
)->buf
, '-') + 1);
505 } else if (!strcmp((*ptr
)->buf
, "*")) {
507 top
= menu_stuff
->nr
;
509 bottom
= find_unique((*ptr
)->buf
, menu_stuff
);
513 if (top
<= 0 || bottom
<= 0 || top
> menu_stuff
->nr
|| bottom
> top
||
514 (is_single
&& bottom
!= top
)) {
515 clean_print_color(CLEAN_COLOR_ERROR
);
516 printf_ln(_("Huh (%s)?"), (*ptr
)->buf
);
517 clean_print_color(CLEAN_COLOR_RESET
);
521 for (i
= bottom
; i
<= top
; i
++)
522 (*chosen
)[i
-1] = choose
;
525 strbuf_list_free(choice_list
);
527 for (i
= 0; i
< menu_stuff
->nr
; i
++)
533 * Implement a git-add-interactive compatible UI, which is borrowed
534 * from git-add--interactive.perl.
538 * - Return an array of integers
539 * - , and it is up to you to free the allocated memory.
540 * - The array ends with EOF.
541 * - If user pressed CTRL-D (i.e. EOF), no selection returned.
543 static int *list_and_choose(struct menu_opts
*opts
, struct menu_stuff
*stuff
)
545 struct strbuf choice
= STRBUF_INIT
;
546 int *chosen
, *result
;
551 chosen
= xmalloc(sizeof(int) * stuff
->nr
);
552 /* set chosen as uninitialized */
553 for (i
= 0; i
< stuff
->nr
; i
++)
559 clean_get_color(CLEAN_COLOR_HEADER
),
561 clean_get_color(CLEAN_COLOR_RESET
));
564 /* chosen will be initialized by print_highlight_menu_stuff */
565 print_highlight_menu_stuff(stuff
, &chosen
);
567 if (opts
->flags
& MENU_OPTS_LIST_ONLY
)
572 clean_get_color(CLEAN_COLOR_PROMPT
),
574 opts
->flags
& MENU_OPTS_SINGLETON
? "> " : ">> ",
575 clean_get_color(CLEAN_COLOR_RESET
));
578 if (strbuf_getline(&choice
, stdin
, '\n') != EOF
) {
579 strbuf_trim(&choice
);
585 /* help for prompt */
586 if (!strcmp(choice
.buf
, "?")) {
587 prompt_help_cmd(opts
->flags
& MENU_OPTS_SINGLETON
);
591 /* for a multiple-choice menu, press ENTER (empty) will return back */
592 if (!(opts
->flags
& MENU_OPTS_SINGLETON
) && !choice
.len
)
595 nr
= parse_choice(stuff
,
596 opts
->flags
& MENU_OPTS_SINGLETON
,
600 if (opts
->flags
& MENU_OPTS_SINGLETON
) {
603 } else if (opts
->flags
& MENU_OPTS_IMMEDIATE
) {
609 result
= xmalloc(sizeof(int));
615 * recalculate nr, if return back from menu directly with
616 * default selections.
619 for (i
= 0; i
< stuff
->nr
; i
++)
623 result
= xcalloc(nr
+ 1, sizeof(int));
624 for (i
= 0; i
< stuff
->nr
&& j
< nr
; i
++) {
632 strbuf_release(&choice
);
636 static int clean_cmd(void)
638 return MENU_RETURN_NO_LOOP
;
641 static int filter_by_patterns_cmd(void)
643 struct dir_struct dir
;
644 struct strbuf confirm
= STRBUF_INIT
;
645 struct strbuf
**ignore_list
;
646 struct string_list_item
*item
;
647 struct exclude_list
*el
;
657 clean_print_color(CLEAN_COLOR_PROMPT
);
658 printf(_("Input ignore patterns>> "));
659 clean_print_color(CLEAN_COLOR_RESET
);
660 if (strbuf_getline(&confirm
, stdin
, '\n') != EOF
)
661 strbuf_trim(&confirm
);
665 /* quit filter_by_pattern mode if press ENTER or Ctrl-D */
669 memset(&dir
, 0, sizeof(dir
));
670 el
= add_exclude_list(&dir
, EXC_CMDL
, "manual exclude");
671 ignore_list
= strbuf_split_max(&confirm
, ' ', 0);
673 for (i
= 0; ignore_list
[i
]; i
++) {
674 strbuf_trim(ignore_list
[i
]);
675 if (!ignore_list
[i
]->len
)
678 add_exclude(ignore_list
[i
]->buf
, "", 0, el
, -(i
+1));
682 for_each_string_list_item(item
, &del_list
) {
683 int dtype
= DT_UNKNOWN
;
685 if (is_excluded(&dir
, item
->string
, &dtype
)) {
686 *item
->string
= '\0';
692 string_list_remove_empty_items(&del_list
, 0);
694 clean_print_color(CLEAN_COLOR_ERROR
);
695 printf_ln(_("WARNING: Cannot find items matched by: %s"), confirm
.buf
);
696 clean_print_color(CLEAN_COLOR_RESET
);
699 strbuf_list_free(ignore_list
);
700 clear_directory(&dir
);
703 strbuf_release(&confirm
);
707 static int select_by_numbers_cmd(void)
709 struct menu_opts menu_opts
;
710 struct menu_stuff menu_stuff
;
711 struct string_list_item
*items
;
715 menu_opts
.header
= NULL
;
716 menu_opts
.prompt
= N_("Select items to delete");
719 menu_stuff
.type
= MENU_STUFF_TYPE_STRING_LIST
;
720 menu_stuff
.stuff
= &del_list
;
721 menu_stuff
.nr
= del_list
.nr
;
723 chosen
= list_and_choose(&menu_opts
, &menu_stuff
);
724 items
= del_list
.items
;
725 for (i
= 0, j
= 0; i
< del_list
.nr
; i
++) {
727 *(items
[i
].string
) = '\0';
728 } else if (i
== chosen
[j
]) {
729 /* delete selected item */
733 /* end of chosen (chosen[j] == EOF), won't delete */
734 *(items
[i
].string
) = '\0';
738 string_list_remove_empty_items(&del_list
, 0);
744 static int ask_each_cmd(void)
746 struct strbuf confirm
= STRBUF_INIT
;
747 struct strbuf buf
= STRBUF_INIT
;
748 struct string_list_item
*item
;
750 int changed
= 0, eof
= 0;
752 for_each_string_list_item(item
, &del_list
) {
753 /* Ctrl-D should stop removing files */
755 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
756 printf(_("remove %s? "), qname
);
757 if (strbuf_getline(&confirm
, stdin
, '\n') != EOF
) {
758 strbuf_trim(&confirm
);
764 if (!confirm
.len
|| strncasecmp(confirm
.buf
, "yes", confirm
.len
)) {
765 *item
->string
= '\0';
771 string_list_remove_empty_items(&del_list
, 0);
773 strbuf_release(&buf
);
774 strbuf_release(&confirm
);
775 return MENU_RETURN_NO_LOOP
;
778 static int quit_cmd(void)
780 string_list_clear(&del_list
, 0);
781 printf_ln(_("Bye."));
782 return MENU_RETURN_NO_LOOP
;
785 static int help_cmd(void)
787 clean_print_color(CLEAN_COLOR_HELP
);
789 "clean - start cleaning\n"
790 "filter by pattern - exclude items from deletion\n"
791 "select by numbers - select items to be deleted by numbers\n"
792 "ask each - confirm each deletion (like \"rm -i\")\n"
793 "quit - stop cleaning\n"
794 "help - this screen\n"
795 "? - help for prompt selection"
797 clean_print_color(CLEAN_COLOR_RESET
);
801 static void interactive_main_loop(void)
803 while (del_list
.nr
) {
804 struct menu_opts menu_opts
;
805 struct menu_stuff menu_stuff
;
806 struct menu_item menus
[] = {
807 {'c', "clean", 0, clean_cmd
},
808 {'f', "filter by pattern", 0, filter_by_patterns_cmd
},
809 {'s', "select by numbers", 0, select_by_numbers_cmd
},
810 {'a', "ask each", 0, ask_each_cmd
},
811 {'q', "quit", 0, quit_cmd
},
812 {'h', "help", 0, help_cmd
},
816 menu_opts
.header
= N_("*** Commands ***");
817 menu_opts
.prompt
= N_("What now");
818 menu_opts
.flags
= MENU_OPTS_SINGLETON
;
820 menu_stuff
.type
= MENU_STUFF_TYPE_MENU_ITEM
;
821 menu_stuff
.stuff
= menus
;
822 menu_stuff
.nr
= sizeof(menus
) / sizeof(struct menu_item
);
824 clean_print_color(CLEAN_COLOR_HEADER
);
825 printf_ln(Q_("Would remove the following item:",
826 "Would remove the following items:",
828 clean_print_color(CLEAN_COLOR_RESET
);
832 chosen
= list_and_choose(&menu_opts
, &menu_stuff
);
834 if (*chosen
!= EOF
) {
836 ret
= menus
[*chosen
].fn();
837 if (ret
!= MENU_RETURN_NO_LOOP
) {
841 clean_print_color(CLEAN_COLOR_ERROR
);
842 printf_ln(_("No more files to clean, exiting."));
843 clean_print_color(CLEAN_COLOR_RESET
);
858 int cmd_clean(int argc
, const char **argv
, const char *prefix
)
861 int dry_run
= 0, remove_directories
= 0, quiet
= 0, ignored
= 0;
862 int ignored_only
= 0, config_set
= 0, errors
= 0, gone
= 1;
863 int rm_flags
= REMOVE_DIR_KEEP_NESTED_GIT
;
864 struct strbuf abs_path
= STRBUF_INIT
;
865 struct dir_struct dir
;
866 struct pathspec pathspec
;
867 struct strbuf buf
= STRBUF_INIT
;
868 struct string_list exclude_list
= STRING_LIST_INIT_NODUP
;
869 struct exclude_list
*el
;
870 struct string_list_item
*item
;
872 struct option options
[] = {
873 OPT__QUIET(&quiet
, N_("do not print names of files removed")),
874 OPT__DRY_RUN(&dry_run
, N_("dry run")),
875 OPT__FORCE(&force
, N_("force")),
876 OPT_BOOL('i', "interactive", &interactive
, N_("interactive cleaning")),
877 OPT_BOOL('d', NULL
, &remove_directories
,
878 N_("remove whole directories")),
879 { OPTION_CALLBACK
, 'e', "exclude", &exclude_list
, N_("pattern"),
880 N_("add <pattern> to ignore rules"), PARSE_OPT_NONEG
, exclude_cb
},
881 OPT_BOOL('x', NULL
, &ignored
, N_("remove ignored files, too")),
882 OPT_BOOL('X', NULL
, &ignored_only
,
883 N_("remove only ignored files")),
887 git_config(git_clean_config
, NULL
);
893 argc
= parse_options(argc
, argv
, prefix
, options
, builtin_clean_usage
,
896 memset(&dir
, 0, sizeof(dir
));
898 dir
.flags
|= DIR_SHOW_IGNORED
;
900 if (ignored
&& ignored_only
)
901 die(_("-x and -X cannot be used together"));
903 if (!interactive
&& !dry_run
&& !force
) {
905 die(_("clean.requireForce set to true and neither -i, -n, nor -f given; "
906 "refusing to clean"));
908 die(_("clean.requireForce defaults to true and neither -i, -n, nor -f given;"
909 " refusing to clean"));
915 dir
.flags
|= DIR_SHOW_OTHER_DIRECTORIES
;
917 if (read_cache() < 0)
918 die(_("index file corrupt"));
921 setup_standard_excludes(&dir
);
923 el
= add_exclude_list(&dir
, EXC_CMDL
, "--exclude option");
924 for (i
= 0; i
< exclude_list
.nr
; i
++)
925 add_exclude(exclude_list
.items
[i
].string
, "", 0, el
, -(i
+1));
927 parse_pathspec(&pathspec
, 0,
931 fill_directory(&dir
, &pathspec
);
933 for (i
= 0; i
< dir
.nr
; i
++) {
934 struct dir_entry
*ent
= dir
.entries
[i
];
939 if (!cache_name_is_other(ent
->name
, ent
->len
))
942 if (lstat(ent
->name
, &st
))
943 die_errno("Cannot lstat '%s'", ent
->name
);
946 matches
= dir_path_match(ent
, &pathspec
, 0, NULL
);
948 if (pathspec
.nr
&& !matches
)
951 if (S_ISDIR(st
.st_mode
) && !remove_directories
&&
952 matches
!= MATCHED_EXACTLY
)
955 rel
= relative_path(ent
->name
, prefix
, &buf
);
956 string_list_append(&del_list
, rel
);
959 if (interactive
&& del_list
.nr
> 0)
960 interactive_main_loop();
962 for_each_string_list_item(item
, &del_list
) {
966 strbuf_addstr(&abs_path
, prefix
);
968 strbuf_addstr(&abs_path
, item
->string
);
971 * we might have removed this as part of earlier
972 * recursive directory removal, so lstat() here could
975 if (lstat(abs_path
.buf
, &st
))
978 if (S_ISDIR(st
.st_mode
)) {
979 if (remove_dirs(&abs_path
, prefix
, rm_flags
, dry_run
, quiet
, &gone
))
981 if (gone
&& !quiet
) {
982 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
983 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), qname
);
986 res
= dry_run
? 0 : unlink(abs_path
.buf
);
988 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
989 warning(_(msg_warn_remove_failed
), qname
);
992 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
993 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), qname
);
996 strbuf_reset(&abs_path
);
999 strbuf_release(&abs_path
);
1000 strbuf_release(&buf
);
1001 string_list_clear(&del_list
, 0);
1002 string_list_clear(&exclude_list
, 0);
1003 return (errors
!= 0);