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 const char *slot_name
;
105 if (starts_with(var
, "column."))
106 return git_column_config(var
, value
, "clean", &colopts
);
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
);
114 if (skip_prefix(var
, "color.interactive.", &slot_name
)) {
115 int slot
= parse_clean_color_slot(slot_name
);
119 return config_error_nonbool(var
);
120 color_parse(value
, var
, clean_colors
[slot
]);
124 if (!strcmp(var
, "clean.requireforce")) {
125 force
= !git_config_bool(var
, value
);
129 /* inspect the color.ui config variable and others */
130 return git_color_default_config(var
, value
, cb
);
133 static const char *clean_get_color(enum color_clean ix
)
135 if (want_color(clean_use_color
))
136 return clean_colors
[ix
];
140 static void clean_print_color(enum color_clean ix
)
142 printf("%s", clean_get_color(ix
));
145 static int exclude_cb(const struct option
*opt
, const char *arg
, int unset
)
147 struct string_list
*exclude_list
= opt
->value
;
148 string_list_append(exclude_list
, arg
);
152 static int remove_dirs(struct strbuf
*path
, const char *prefix
, int force_flag
,
153 int dry_run
, int quiet
, int *dir_gone
)
156 struct strbuf quoted
= STRBUF_INIT
;
158 int res
= 0, ret
= 0, gone
= 1, original_len
= path
->len
, len
;
159 unsigned char submodule_head
[20];
160 struct string_list dels
= STRING_LIST_INIT_DUP
;
164 if ((force_flag
& REMOVE_DIR_KEEP_NESTED_GIT
) &&
165 !resolve_gitlink_ref(path
->buf
, "HEAD", submodule_head
)) {
167 quote_path_relative(path
->buf
, prefix
, "ed
);
168 printf(dry_run
? _(msg_would_skip_git_dir
) : _(msg_skip_git_dir
),
176 dir
= opendir(path
->buf
);
178 /* an empty dir could be removed even if it is unreadble */
179 res
= dry_run
? 0 : rmdir(path
->buf
);
181 quote_path_relative(path
->buf
, prefix
, "ed
);
182 warning(_(msg_warn_remove_failed
), quoted
.buf
);
188 if (path
->buf
[original_len
- 1] != '/')
189 strbuf_addch(path
, '/');
192 while ((e
= readdir(dir
)) != NULL
) {
194 if (is_dot_or_dotdot(e
->d_name
))
197 strbuf_setlen(path
, len
);
198 strbuf_addstr(path
, e
->d_name
);
199 if (lstat(path
->buf
, &st
))
201 else if (S_ISDIR(st
.st_mode
)) {
202 if (remove_dirs(path
, prefix
, force_flag
, dry_run
, quiet
, &gone
))
205 quote_path_relative(path
->buf
, prefix
, "ed
);
206 string_list_append(&dels
, quoted
.buf
);
211 res
= dry_run
? 0 : unlink(path
->buf
);
213 quote_path_relative(path
->buf
, prefix
, "ed
);
214 string_list_append(&dels
, quoted
.buf
);
216 quote_path_relative(path
->buf
, prefix
, "ed
);
217 warning(_(msg_warn_remove_failed
), quoted
.buf
);
224 /* path too long, stat fails, or non-directory still exists */
231 strbuf_setlen(path
, original_len
);
234 res
= dry_run
? 0 : rmdir(path
->buf
);
238 quote_path_relative(path
->buf
, prefix
, "ed
);
239 warning(_(msg_warn_remove_failed
), quoted
.buf
);
245 if (!*dir_gone
&& !quiet
) {
247 for (i
= 0; i
< dels
.nr
; i
++)
248 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), dels
.items
[i
].string
);
250 string_list_clear(&dels
, 0);
254 static void pretty_print_dels(void)
256 struct string_list list
= STRING_LIST_INIT_DUP
;
257 struct string_list_item
*item
;
258 struct strbuf buf
= STRBUF_INIT
;
260 struct column_options copts
;
262 for_each_string_list_item(item
, &del_list
) {
263 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
264 string_list_append(&list
, qname
);
268 * always enable column display, we only consult column.*
269 * about layout strategy and stuff
271 colopts
= (colopts
& ~COL_ENABLE_MASK
) | COL_ENABLED
;
272 memset(&copts
, 0, sizeof(copts
));
275 print_columns(&list
, colopts
, &copts
);
276 strbuf_release(&buf
);
277 string_list_clear(&list
, 0);
280 static void pretty_print_menus(struct string_list
*menu_list
)
282 unsigned int local_colopts
= 0;
283 struct column_options copts
;
285 local_colopts
= COL_ENABLED
| COL_ROW
;
286 memset(&copts
, 0, sizeof(copts
));
289 print_columns(menu_list
, local_colopts
, &copts
);
292 static void prompt_help_cmd(int singleton
)
294 clean_print_color(CLEAN_COLOR_HELP
);
295 printf_ln(singleton
?
297 "1 - select a numbered item\n"
298 "foo - select item based on unique prefix\n"
299 " - (empty) select nothing") :
301 "1 - select a single item\n"
302 "3-5 - select a range of items\n"
303 "2-3,6-9 - select multiple ranges\n"
304 "foo - select item based on unique prefix\n"
305 "-... - unselect specified items\n"
306 "* - choose all items\n"
307 " - (empty) finish selecting"));
308 clean_print_color(CLEAN_COLOR_RESET
);
312 * display menu stuff with number prefix and hotkey highlight
314 static void print_highlight_menu_stuff(struct menu_stuff
*stuff
, int **chosen
)
316 struct string_list menu_list
= STRING_LIST_INIT_DUP
;
317 struct strbuf menu
= STRBUF_INIT
;
318 struct strbuf buf
= STRBUF_INIT
;
319 struct menu_item
*menu_item
;
320 struct string_list_item
*string_list_item
;
323 switch (stuff
->type
) {
325 die("Bad type of menu_staff when print menu");
326 case MENU_STUFF_TYPE_MENU_ITEM
:
327 menu_item
= (struct menu_item
*)stuff
->stuff
;
328 for (i
= 0; i
< stuff
->nr
; i
++, menu_item
++) {
332 p
= menu_item
->title
;
333 if ((*chosen
)[i
] < 0)
334 (*chosen
)[i
] = menu_item
->selected
? 1 : 0;
335 strbuf_addf(&menu
, "%s%2d: ", (*chosen
)[i
] ? "*" : " ", i
+1);
337 if (!highlighted
&& *p
== menu_item
->hotkey
) {
338 strbuf_addstr(&menu
, clean_get_color(CLEAN_COLOR_PROMPT
));
339 strbuf_addch(&menu
, *p
);
340 strbuf_addstr(&menu
, clean_get_color(CLEAN_COLOR_RESET
));
343 strbuf_addch(&menu
, *p
);
346 string_list_append(&menu_list
, menu
.buf
);
350 case MENU_STUFF_TYPE_STRING_LIST
:
352 for_each_string_list_item(string_list_item
, (struct string_list
*)stuff
->stuff
) {
353 if ((*chosen
)[i
] < 0)
355 strbuf_addf(&menu
, "%s%2d: %s",
356 (*chosen
)[i
] ? "*" : " ", i
+1, string_list_item
->string
);
357 string_list_append(&menu_list
, menu
.buf
);
364 pretty_print_menus(&menu_list
);
366 strbuf_release(&menu
);
367 strbuf_release(&buf
);
368 string_list_clear(&menu_list
, 0);
371 static int find_unique(const char *choice
, struct menu_stuff
*menu_stuff
)
373 struct menu_item
*menu_item
;
374 struct string_list_item
*string_list_item
;
375 int i
, len
, found
= 0;
377 len
= strlen(choice
);
378 switch (menu_stuff
->type
) {
380 die("Bad type of menu_stuff when parse choice");
381 case MENU_STUFF_TYPE_MENU_ITEM
:
383 menu_item
= (struct menu_item
*)menu_stuff
->stuff
;
384 for (i
= 0; i
< menu_stuff
->nr
; i
++, menu_item
++) {
385 if (len
== 1 && *choice
== menu_item
->hotkey
) {
389 if (!strncasecmp(choice
, menu_item
->title
, len
)) {
392 /* continue for hotkey matching */
404 case MENU_STUFF_TYPE_STRING_LIST
:
405 string_list_item
= ((struct string_list
*)menu_stuff
->stuff
)->items
;
406 for (i
= 0; i
< menu_stuff
->nr
; i
++, string_list_item
++) {
407 if (!strncasecmp(choice
, string_list_item
->string
, len
)) {
422 * Parse user input, and return choice(s) for menu (menu_stuff).
425 * (for single choice)
426 * 1 - select a numbered item
427 * foo - select item based on menu title
428 * - (empty) select nothing
430 * (for multiple choice)
431 * 1 - select a single item
432 * 3-5 - select a range of items
433 * 2-3,6-9 - select multiple ranges
434 * foo - select item based on menu title
435 * -... - unselect specified items
436 * * - choose all items
437 * - (empty) finish selecting
439 * The parse result will be saved in array **chosen, and
440 * return number of total selections.
442 static int parse_choice(struct menu_stuff
*menu_stuff
,
447 struct strbuf
**choice_list
, **ptr
;
452 choice_list
= strbuf_split_max(&input
, '\n', 0);
459 choice_list
= strbuf_split_max(&input
, ' ', 0);
462 for (ptr
= choice_list
; *ptr
; ptr
++) {
465 int bottom
= 0, top
= 0;
466 int is_range
, is_number
;
472 /* Input that begins with '-'; unchoose */
473 if (*(*ptr
)->buf
== '-') {
475 strbuf_remove((*ptr
), 0, 1);
480 for (p
= (*ptr
)->buf
; *p
; p
++) {
490 } else if (!isdigit(*p
)) {
498 bottom
= atoi((*ptr
)->buf
);
500 } else if (is_range
) {
501 bottom
= atoi((*ptr
)->buf
);
502 /* a range can be specified like 5-7 or 5- */
503 if (!*(strchr((*ptr
)->buf
, '-') + 1))
504 top
= menu_stuff
->nr
;
506 top
= atoi(strchr((*ptr
)->buf
, '-') + 1);
507 } else if (!strcmp((*ptr
)->buf
, "*")) {
509 top
= menu_stuff
->nr
;
511 bottom
= find_unique((*ptr
)->buf
, menu_stuff
);
515 if (top
<= 0 || bottom
<= 0 || top
> menu_stuff
->nr
|| bottom
> top
||
516 (is_single
&& bottom
!= top
)) {
517 clean_print_color(CLEAN_COLOR_ERROR
);
518 printf_ln(_("Huh (%s)?"), (*ptr
)->buf
);
519 clean_print_color(CLEAN_COLOR_RESET
);
523 for (i
= bottom
; i
<= top
; i
++)
524 (*chosen
)[i
-1] = choose
;
527 strbuf_list_free(choice_list
);
529 for (i
= 0; i
< menu_stuff
->nr
; i
++)
535 * Implement a git-add-interactive compatible UI, which is borrowed
536 * from git-add--interactive.perl.
540 * - Return an array of integers
541 * - , and it is up to you to free the allocated memory.
542 * - The array ends with EOF.
543 * - If user pressed CTRL-D (i.e. EOF), no selection returned.
545 static int *list_and_choose(struct menu_opts
*opts
, struct menu_stuff
*stuff
)
547 struct strbuf choice
= STRBUF_INIT
;
548 int *chosen
, *result
;
553 chosen
= xmalloc(sizeof(int) * stuff
->nr
);
554 /* set chosen as uninitialized */
555 for (i
= 0; i
< stuff
->nr
; i
++)
561 clean_get_color(CLEAN_COLOR_HEADER
),
563 clean_get_color(CLEAN_COLOR_RESET
));
566 /* chosen will be initialized by print_highlight_menu_stuff */
567 print_highlight_menu_stuff(stuff
, &chosen
);
569 if (opts
->flags
& MENU_OPTS_LIST_ONLY
)
574 clean_get_color(CLEAN_COLOR_PROMPT
),
576 opts
->flags
& MENU_OPTS_SINGLETON
? "> " : ">> ",
577 clean_get_color(CLEAN_COLOR_RESET
));
580 if (strbuf_getline(&choice
, stdin
, '\n') != EOF
) {
581 strbuf_trim(&choice
);
587 /* help for prompt */
588 if (!strcmp(choice
.buf
, "?")) {
589 prompt_help_cmd(opts
->flags
& MENU_OPTS_SINGLETON
);
593 /* for a multiple-choice menu, press ENTER (empty) will return back */
594 if (!(opts
->flags
& MENU_OPTS_SINGLETON
) && !choice
.len
)
597 nr
= parse_choice(stuff
,
598 opts
->flags
& MENU_OPTS_SINGLETON
,
602 if (opts
->flags
& MENU_OPTS_SINGLETON
) {
605 } else if (opts
->flags
& MENU_OPTS_IMMEDIATE
) {
611 result
= xmalloc(sizeof(int));
617 * recalculate nr, if return back from menu directly with
618 * default selections.
621 for (i
= 0; i
< stuff
->nr
; i
++)
625 result
= xcalloc(nr
+ 1, sizeof(int));
626 for (i
= 0; i
< stuff
->nr
&& j
< nr
; i
++) {
634 strbuf_release(&choice
);
638 static int clean_cmd(void)
640 return MENU_RETURN_NO_LOOP
;
643 static int filter_by_patterns_cmd(void)
645 struct dir_struct dir
;
646 struct strbuf confirm
= STRBUF_INIT
;
647 struct strbuf
**ignore_list
;
648 struct string_list_item
*item
;
649 struct exclude_list
*el
;
659 clean_print_color(CLEAN_COLOR_PROMPT
);
660 printf(_("Input ignore patterns>> "));
661 clean_print_color(CLEAN_COLOR_RESET
);
662 if (strbuf_getline(&confirm
, stdin
, '\n') != EOF
)
663 strbuf_trim(&confirm
);
667 /* quit filter_by_pattern mode if press ENTER or Ctrl-D */
671 memset(&dir
, 0, sizeof(dir
));
672 el
= add_exclude_list(&dir
, EXC_CMDL
, "manual exclude");
673 ignore_list
= strbuf_split_max(&confirm
, ' ', 0);
675 for (i
= 0; ignore_list
[i
]; i
++) {
676 strbuf_trim(ignore_list
[i
]);
677 if (!ignore_list
[i
]->len
)
680 add_exclude(ignore_list
[i
]->buf
, "", 0, el
, -(i
+1));
684 for_each_string_list_item(item
, &del_list
) {
685 int dtype
= DT_UNKNOWN
;
687 if (is_excluded(&dir
, item
->string
, &dtype
)) {
688 *item
->string
= '\0';
694 string_list_remove_empty_items(&del_list
, 0);
696 clean_print_color(CLEAN_COLOR_ERROR
);
697 printf_ln(_("WARNING: Cannot find items matched by: %s"), confirm
.buf
);
698 clean_print_color(CLEAN_COLOR_RESET
);
701 strbuf_list_free(ignore_list
);
702 clear_directory(&dir
);
705 strbuf_release(&confirm
);
709 static int select_by_numbers_cmd(void)
711 struct menu_opts menu_opts
;
712 struct menu_stuff menu_stuff
;
713 struct string_list_item
*items
;
717 menu_opts
.header
= NULL
;
718 menu_opts
.prompt
= N_("Select items to delete");
721 menu_stuff
.type
= MENU_STUFF_TYPE_STRING_LIST
;
722 menu_stuff
.stuff
= &del_list
;
723 menu_stuff
.nr
= del_list
.nr
;
725 chosen
= list_and_choose(&menu_opts
, &menu_stuff
);
726 items
= del_list
.items
;
727 for (i
= 0, j
= 0; i
< del_list
.nr
; i
++) {
729 *(items
[i
].string
) = '\0';
730 } else if (i
== chosen
[j
]) {
731 /* delete selected item */
735 /* end of chosen (chosen[j] == EOF), won't delete */
736 *(items
[i
].string
) = '\0';
740 string_list_remove_empty_items(&del_list
, 0);
746 static int ask_each_cmd(void)
748 struct strbuf confirm
= STRBUF_INIT
;
749 struct strbuf buf
= STRBUF_INIT
;
750 struct string_list_item
*item
;
752 int changed
= 0, eof
= 0;
754 for_each_string_list_item(item
, &del_list
) {
755 /* Ctrl-D should stop removing files */
757 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
758 printf(_("remove %s? "), qname
);
759 if (strbuf_getline(&confirm
, stdin
, '\n') != EOF
) {
760 strbuf_trim(&confirm
);
766 if (!confirm
.len
|| strncasecmp(confirm
.buf
, "yes", confirm
.len
)) {
767 *item
->string
= '\0';
773 string_list_remove_empty_items(&del_list
, 0);
775 strbuf_release(&buf
);
776 strbuf_release(&confirm
);
777 return MENU_RETURN_NO_LOOP
;
780 static int quit_cmd(void)
782 string_list_clear(&del_list
, 0);
783 printf_ln(_("Bye."));
784 return MENU_RETURN_NO_LOOP
;
787 static int help_cmd(void)
789 clean_print_color(CLEAN_COLOR_HELP
);
791 "clean - start cleaning\n"
792 "filter by pattern - exclude items from deletion\n"
793 "select by numbers - select items to be deleted by numbers\n"
794 "ask each - confirm each deletion (like \"rm -i\")\n"
795 "quit - stop cleaning\n"
796 "help - this screen\n"
797 "? - help for prompt selection"
799 clean_print_color(CLEAN_COLOR_RESET
);
803 static void interactive_main_loop(void)
805 while (del_list
.nr
) {
806 struct menu_opts menu_opts
;
807 struct menu_stuff menu_stuff
;
808 struct menu_item menus
[] = {
809 {'c', "clean", 0, clean_cmd
},
810 {'f', "filter by pattern", 0, filter_by_patterns_cmd
},
811 {'s', "select by numbers", 0, select_by_numbers_cmd
},
812 {'a', "ask each", 0, ask_each_cmd
},
813 {'q', "quit", 0, quit_cmd
},
814 {'h', "help", 0, help_cmd
},
818 menu_opts
.header
= N_("*** Commands ***");
819 menu_opts
.prompt
= N_("What now");
820 menu_opts
.flags
= MENU_OPTS_SINGLETON
;
822 menu_stuff
.type
= MENU_STUFF_TYPE_MENU_ITEM
;
823 menu_stuff
.stuff
= menus
;
824 menu_stuff
.nr
= sizeof(menus
) / sizeof(struct menu_item
);
826 clean_print_color(CLEAN_COLOR_HEADER
);
827 printf_ln(Q_("Would remove the following item:",
828 "Would remove the following items:",
830 clean_print_color(CLEAN_COLOR_RESET
);
834 chosen
= list_and_choose(&menu_opts
, &menu_stuff
);
836 if (*chosen
!= EOF
) {
838 ret
= menus
[*chosen
].fn();
839 if (ret
!= MENU_RETURN_NO_LOOP
) {
843 clean_print_color(CLEAN_COLOR_ERROR
);
844 printf_ln(_("No more files to clean, exiting."));
845 clean_print_color(CLEAN_COLOR_RESET
);
860 int cmd_clean(int argc
, const char **argv
, const char *prefix
)
863 int dry_run
= 0, remove_directories
= 0, quiet
= 0, ignored
= 0;
864 int ignored_only
= 0, config_set
= 0, errors
= 0, gone
= 1;
865 int rm_flags
= REMOVE_DIR_KEEP_NESTED_GIT
;
866 struct strbuf abs_path
= STRBUF_INIT
;
867 struct dir_struct dir
;
868 struct pathspec pathspec
;
869 struct strbuf buf
= STRBUF_INIT
;
870 struct string_list exclude_list
= STRING_LIST_INIT_NODUP
;
871 struct exclude_list
*el
;
872 struct string_list_item
*item
;
874 struct option options
[] = {
875 OPT__QUIET(&quiet
, N_("do not print names of files removed")),
876 OPT__DRY_RUN(&dry_run
, N_("dry run")),
877 OPT__FORCE(&force
, N_("force")),
878 OPT_BOOL('i', "interactive", &interactive
, N_("interactive cleaning")),
879 OPT_BOOL('d', NULL
, &remove_directories
,
880 N_("remove whole directories")),
881 { OPTION_CALLBACK
, 'e', "exclude", &exclude_list
, N_("pattern"),
882 N_("add <pattern> to ignore rules"), PARSE_OPT_NONEG
, exclude_cb
},
883 OPT_BOOL('x', NULL
, &ignored
, N_("remove ignored files, too")),
884 OPT_BOOL('X', NULL
, &ignored_only
,
885 N_("remove only ignored files")),
889 git_config(git_clean_config
, NULL
);
895 argc
= parse_options(argc
, argv
, prefix
, options
, builtin_clean_usage
,
898 memset(&dir
, 0, sizeof(dir
));
900 dir
.flags
|= DIR_SHOW_IGNORED
;
902 if (ignored
&& ignored_only
)
903 die(_("-x and -X cannot be used together"));
905 if (!interactive
&& !dry_run
&& !force
) {
907 die(_("clean.requireForce set to true and neither -i, -n, nor -f given; "
908 "refusing to clean"));
910 die(_("clean.requireForce defaults to true and neither -i, -n, nor -f given;"
911 " refusing to clean"));
917 dir
.flags
|= DIR_SHOW_OTHER_DIRECTORIES
;
919 if (read_cache() < 0)
920 die(_("index file corrupt"));
923 setup_standard_excludes(&dir
);
925 el
= add_exclude_list(&dir
, EXC_CMDL
, "--exclude option");
926 for (i
= 0; i
< exclude_list
.nr
; i
++)
927 add_exclude(exclude_list
.items
[i
].string
, "", 0, el
, -(i
+1));
929 parse_pathspec(&pathspec
, 0,
933 fill_directory(&dir
, &pathspec
);
935 for (i
= 0; i
< dir
.nr
; i
++) {
936 struct dir_entry
*ent
= dir
.entries
[i
];
941 if (!cache_name_is_other(ent
->name
, ent
->len
))
944 if (lstat(ent
->name
, &st
))
945 die_errno("Cannot lstat '%s'", ent
->name
);
948 matches
= dir_path_match(ent
, &pathspec
, 0, NULL
);
950 if (pathspec
.nr
&& !matches
)
953 if (S_ISDIR(st
.st_mode
) && !remove_directories
&&
954 matches
!= MATCHED_EXACTLY
)
957 rel
= relative_path(ent
->name
, prefix
, &buf
);
958 string_list_append(&del_list
, rel
);
961 if (interactive
&& del_list
.nr
> 0)
962 interactive_main_loop();
964 for_each_string_list_item(item
, &del_list
) {
968 strbuf_addstr(&abs_path
, prefix
);
970 strbuf_addstr(&abs_path
, item
->string
);
973 * we might have removed this as part of earlier
974 * recursive directory removal, so lstat() here could
977 if (lstat(abs_path
.buf
, &st
))
980 if (S_ISDIR(st
.st_mode
)) {
981 if (remove_dirs(&abs_path
, prefix
, rm_flags
, dry_run
, quiet
, &gone
))
983 if (gone
&& !quiet
) {
984 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
985 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), qname
);
988 res
= dry_run
? 0 : unlink(abs_path
.buf
);
990 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
991 warning(_(msg_warn_remove_failed
), qname
);
994 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
995 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), qname
);
998 strbuf_reset(&abs_path
);
1001 strbuf_release(&abs_path
);
1002 strbuf_release(&buf
);
1003 string_list_clear(&del_list
, 0);
1004 string_list_clear(&exclude_list
, 0);
1005 return (errors
!= 0);