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"
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,
50 CLEAN_COLOR_ERROR
= 5,
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 if (!prefixcmp(var
, "column."))
103 return git_column_config(var
, value
, "clean", &colopts
);
105 /* honors the color.interactive* config variables which also
106 applied in git-add--interactive and git-stash */
107 if (!strcmp(var
, "color.interactive")) {
108 clean_use_color
= git_config_colorbool(var
, value
);
111 if (!prefixcmp(var
, "color.interactive.")) {
112 int slot
= parse_clean_color_slot(var
+
113 strlen("color.interactive."));
117 return config_error_nonbool(var
);
118 color_parse(value
, var
, 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
, i
;
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
) {
244 for (i
= 0; i
< dels
.nr
; i
++)
245 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), dels
.items
[i
].string
);
247 string_list_clear(&dels
, 0);
251 static void pretty_print_dels(void)
253 struct string_list list
= STRING_LIST_INIT_DUP
;
254 struct string_list_item
*item
;
255 struct strbuf buf
= STRBUF_INIT
;
257 struct column_options copts
;
259 for_each_string_list_item(item
, &del_list
) {
260 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
261 string_list_append(&list
, qname
);
265 * always enable column display, we only consult column.*
266 * about layout strategy and stuff
268 colopts
= (colopts
& ~COL_ENABLE_MASK
) | COL_ENABLED
;
269 memset(&copts
, 0, sizeof(copts
));
272 print_columns(&list
, colopts
, &copts
);
273 strbuf_release(&buf
);
274 string_list_clear(&list
, 0);
277 static void pretty_print_menus(struct string_list
*menu_list
)
279 unsigned int local_colopts
= 0;
280 struct column_options copts
;
282 local_colopts
= COL_ENABLED
| COL_ROW
;
283 memset(&copts
, 0, sizeof(copts
));
286 print_columns(menu_list
, local_colopts
, &copts
);
289 static void prompt_help_cmd(int singleton
)
291 clean_print_color(CLEAN_COLOR_HELP
);
292 printf_ln(singleton
?
294 "1 - select a numbered item\n"
295 "foo - select item based on unique prefix\n"
296 " - (empty) select nothing") :
298 "1 - select a single item\n"
299 "3-5 - select a range of items\n"
300 "2-3,6-9 - select multiple ranges\n"
301 "foo - select item based on unique prefix\n"
302 "-... - unselect specified items\n"
303 "* - choose all items\n"
304 " - (empty) finish selecting"));
305 clean_print_color(CLEAN_COLOR_RESET
);
309 * display menu stuff with number prefix and hotkey highlight
311 static void print_highlight_menu_stuff(struct menu_stuff
*stuff
, int **chosen
)
313 struct string_list menu_list
= STRING_LIST_INIT_DUP
;
314 struct strbuf menu
= STRBUF_INIT
;
315 struct strbuf buf
= STRBUF_INIT
;
316 struct menu_item
*menu_item
;
317 struct string_list_item
*string_list_item
;
320 switch (stuff
->type
) {
322 die("Bad type of menu_staff when print menu");
323 case MENU_STUFF_TYPE_MENU_ITEM
:
324 menu_item
= (struct menu_item
*)stuff
->stuff
;
325 for (i
= 0; i
< stuff
->nr
; i
++, menu_item
++) {
329 p
= menu_item
->title
;
330 if ((*chosen
)[i
] < 0)
331 (*chosen
)[i
] = menu_item
->selected
? 1 : 0;
332 strbuf_addf(&menu
, "%s%2d: ", (*chosen
)[i
] ? "*" : " ", i
+1);
334 if (!highlighted
&& *p
== menu_item
->hotkey
) {
335 strbuf_addstr(&menu
, clean_get_color(CLEAN_COLOR_PROMPT
));
336 strbuf_addch(&menu
, *p
);
337 strbuf_addstr(&menu
, clean_get_color(CLEAN_COLOR_RESET
));
340 strbuf_addch(&menu
, *p
);
343 string_list_append(&menu_list
, menu
.buf
);
347 case MENU_STUFF_TYPE_STRING_LIST
:
349 for_each_string_list_item(string_list_item
, (struct string_list
*)stuff
->stuff
) {
350 if ((*chosen
)[i
] < 0)
352 strbuf_addf(&menu
, "%s%2d: %s",
353 (*chosen
)[i
] ? "*" : " ", i
+1, string_list_item
->string
);
354 string_list_append(&menu_list
, menu
.buf
);
361 pretty_print_menus(&menu_list
);
363 strbuf_release(&menu
);
364 strbuf_release(&buf
);
365 string_list_clear(&menu_list
, 0);
369 * Parse user input, and return choice(s) for menu (menu_stuff).
372 * (for single choice)
373 * 1 - select a numbered item
374 * foo - select item based on menu title
375 * - (empty) select nothing
377 * (for multiple choice)
378 * 1 - select a single item
379 * 3-5 - select a range of items
380 * 2-3,6-9 - select multiple ranges
381 * foo - select item based on menu title
382 * -... - unselect specified items
383 * * - choose all items
384 * - (empty) finish selecting
386 * The parse result will be saved in array **chosen, and
387 * return number of total selections.
389 static int parse_choice(struct menu_stuff
*menu_stuff
,
394 struct strbuf
**choice_list
, **ptr
;
395 struct menu_item
*menu_item
;
396 struct string_list_item
*string_list_item
;
401 choice_list
= strbuf_split_max(&input
, '\n', 0);
408 choice_list
= strbuf_split_max(&input
, ' ', 0);
411 for (ptr
= choice_list
; *ptr
; ptr
++) {
414 int bottom
= 0, top
= 0;
415 int is_range
, is_number
;
421 /* Input that begins with '-'; unchoose */
422 if (*(*ptr
)->buf
== '-') {
424 strbuf_remove((*ptr
), 0, 1);
429 for (p
= (*ptr
)->buf
; *p
; p
++) {
439 } else if (!isdigit(*p
)) {
447 bottom
= atoi((*ptr
)->buf
);
449 } else if (is_range
) {
450 bottom
= atoi((*ptr
)->buf
);
451 /* a range can be specified like 5-7 or 5- */
452 if (!*(strchr((*ptr
)->buf
, '-') + 1))
453 top
= menu_stuff
->nr
;
455 top
= atoi(strchr((*ptr
)->buf
, '-') + 1);
456 } else if (!strcmp((*ptr
)->buf
, "*")) {
458 top
= menu_stuff
->nr
;
460 switch (menu_stuff
->type
) {
462 die("Bad type of menu_stuff when parse choice");
463 case MENU_STUFF_TYPE_MENU_ITEM
:
464 menu_item
= (struct menu_item
*)menu_stuff
->stuff
;
465 for (i
= 0; i
< menu_stuff
->nr
; i
++, menu_item
++) {
466 if (((*ptr
)->len
== 1 &&
467 *(*ptr
)->buf
== menu_item
->hotkey
) ||
468 !strcasecmp((*ptr
)->buf
, menu_item
->title
)) {
475 case MENU_STUFF_TYPE_STRING_LIST
:
476 string_list_item
= ((struct string_list
*)menu_stuff
->stuff
)->items
;
477 for (i
= 0; i
< menu_stuff
->nr
; i
++, string_list_item
++) {
478 if (!strcasecmp((*ptr
)->buf
, string_list_item
->string
)) {
488 if (top
<= 0 || bottom
<= 0 || top
> menu_stuff
->nr
|| bottom
> top
||
489 (is_single
&& bottom
!= top
)) {
490 clean_print_color(CLEAN_COLOR_ERROR
);
491 printf_ln(_("Huh (%s)?"), (*ptr
)->buf
);
492 clean_print_color(CLEAN_COLOR_RESET
);
496 for (i
= bottom
; i
<= top
; i
++)
497 (*chosen
)[i
-1] = choose
;
500 strbuf_list_free(choice_list
);
502 for (i
= 0; i
< menu_stuff
->nr
; i
++)
508 * Implement a git-add-interactive compatible UI, which is borrowed
509 * from git-add--interactive.perl.
513 * - Return an array of integers
514 * - , and it is up to you to free the allocated memory.
515 * - The array ends with EOF.
516 * - If user pressed CTRL-D (i.e. EOF), no selection returned.
518 static int *list_and_choose(struct menu_opts
*opts
, struct menu_stuff
*stuff
)
520 struct strbuf choice
= STRBUF_INIT
;
521 int *chosen
, *result
;
526 chosen
= xmalloc(sizeof(int) * stuff
->nr
);
527 /* set chosen as uninitialized */
528 for (i
= 0; i
< stuff
->nr
; i
++)
534 clean_get_color(CLEAN_COLOR_HEADER
),
536 clean_get_color(CLEAN_COLOR_RESET
));
539 /* chosen will be initialized by print_highlight_menu_stuff */
540 print_highlight_menu_stuff(stuff
, &chosen
);
542 if (opts
->flags
& MENU_OPTS_LIST_ONLY
)
547 clean_get_color(CLEAN_COLOR_PROMPT
),
549 opts
->flags
& MENU_OPTS_SINGLETON
? "> " : ">> ",
550 clean_get_color(CLEAN_COLOR_RESET
));
553 if (strbuf_getline(&choice
, stdin
, '\n') != EOF
) {
554 strbuf_trim(&choice
);
560 /* help for prompt */
561 if (!strcmp(choice
.buf
, "?")) {
562 prompt_help_cmd(opts
->flags
& MENU_OPTS_SINGLETON
);
566 /* for a multiple-choice menu, press ENTER (empty) will return back */
567 if (!(opts
->flags
& MENU_OPTS_SINGLETON
) && !choice
.len
)
570 nr
= parse_choice(stuff
,
571 opts
->flags
& MENU_OPTS_SINGLETON
,
575 if (opts
->flags
& MENU_OPTS_SINGLETON
) {
578 } else if (opts
->flags
& MENU_OPTS_IMMEDIATE
) {
584 result
= xmalloc(sizeof(int));
590 * recalculate nr, if return back from menu directly with
591 * default selections.
594 for (i
= 0; i
< stuff
->nr
; i
++)
598 result
= xmalloc(sizeof(int) * (nr
+ 1));
599 memset(result
, 0, sizeof(int) * (nr
+ 1));
600 for (i
= 0; i
< stuff
->nr
&& j
< nr
; i
++) {
608 strbuf_release(&choice
);
612 static int clean_cmd(void)
614 return MENU_RETURN_NO_LOOP
;
617 static int filter_by_patterns_cmd(void)
619 struct dir_struct dir
;
620 struct strbuf confirm
= STRBUF_INIT
;
621 struct strbuf
**ignore_list
;
622 struct string_list_item
*item
;
623 struct exclude_list
*el
;
633 clean_print_color(CLEAN_COLOR_PROMPT
);
634 printf(_("Input ignore patterns>> "));
635 clean_print_color(CLEAN_COLOR_RESET
);
636 if (strbuf_getline(&confirm
, stdin
, '\n') != EOF
)
637 strbuf_trim(&confirm
);
641 /* quit filter_by_pattern mode if press ENTER or Ctrl-D */
645 memset(&dir
, 0, sizeof(dir
));
646 el
= add_exclude_list(&dir
, EXC_CMDL
, "manual exclude");
647 ignore_list
= strbuf_split_max(&confirm
, ' ', 0);
649 for (i
= 0; ignore_list
[i
]; i
++) {
650 strbuf_trim(ignore_list
[i
]);
651 if (!ignore_list
[i
]->len
)
654 add_exclude(ignore_list
[i
]->buf
, "", 0, el
, -(i
+1));
658 for_each_string_list_item(item
, &del_list
) {
659 int dtype
= DT_UNKNOWN
;
661 if (is_excluded(&dir
, item
->string
, &dtype
)) {
662 *item
->string
= '\0';
668 string_list_remove_empty_items(&del_list
, 0);
670 clean_print_color(CLEAN_COLOR_ERROR
);
671 printf_ln(_("WARNING: Cannot find items matched by: %s"), confirm
.buf
);
672 clean_print_color(CLEAN_COLOR_RESET
);
675 strbuf_list_free(ignore_list
);
676 clear_directory(&dir
);
679 strbuf_release(&confirm
);
683 static int select_by_numbers_cmd(void)
685 struct menu_opts menu_opts
;
686 struct menu_stuff menu_stuff
;
687 struct string_list_item
*items
;
691 menu_opts
.header
= NULL
;
692 menu_opts
.prompt
= N_("Select items to delete");
695 menu_stuff
.type
= MENU_STUFF_TYPE_STRING_LIST
;
696 menu_stuff
.stuff
= &del_list
;
697 menu_stuff
.nr
= del_list
.nr
;
699 chosen
= list_and_choose(&menu_opts
, &menu_stuff
);
700 items
= del_list
.items
;
701 for (i
= 0, j
= 0; i
< del_list
.nr
; i
++) {
703 *(items
[i
].string
) = '\0';
704 } else if (i
== chosen
[j
]) {
705 /* delete selected item */
709 /* end of chosen (chosen[j] == EOF), won't delete */
710 *(items
[i
].string
) = '\0';
714 string_list_remove_empty_items(&del_list
, 0);
720 static int ask_each_cmd(void)
722 struct strbuf confirm
= STRBUF_INIT
;
723 struct strbuf buf
= STRBUF_INIT
;
724 struct string_list_item
*item
;
726 int changed
= 0, eof
= 0;
728 for_each_string_list_item(item
, &del_list
) {
729 /* Ctrl-D should stop removing files */
731 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
732 printf(_("remove %s? "), qname
);
733 if (strbuf_getline(&confirm
, stdin
, '\n') != EOF
) {
734 strbuf_trim(&confirm
);
740 if (!confirm
.len
|| strncasecmp(confirm
.buf
, "yes", confirm
.len
)) {
741 *item
->string
= '\0';
747 string_list_remove_empty_items(&del_list
, 0);
749 strbuf_release(&buf
);
750 strbuf_release(&confirm
);
751 return MENU_RETURN_NO_LOOP
;
754 static int quit_cmd(void)
756 string_list_clear(&del_list
, 0);
757 printf_ln(_("Bye."));
758 return MENU_RETURN_NO_LOOP
;
761 static int help_cmd(void)
763 clean_print_color(CLEAN_COLOR_HELP
);
765 "clean - start cleaning\n"
766 "filter by pattern - exclude items from deletion\n"
767 "select by numbers - select items to be deleted by numbers\n"
768 "ask each - confirm each deletion (like \"rm -i\")\n"
769 "quit - stop cleaning\n"
770 "help - this screen\n"
771 "? - help for prompt selection"
773 clean_print_color(CLEAN_COLOR_RESET
);
777 static void interactive_main_loop(void)
779 while (del_list
.nr
) {
780 struct menu_opts menu_opts
;
781 struct menu_stuff menu_stuff
;
782 struct menu_item menus
[] = {
783 {'c', "clean", 0, clean_cmd
},
784 {'f', "filter by pattern", 0, filter_by_patterns_cmd
},
785 {'s', "select by numbers", 0, select_by_numbers_cmd
},
786 {'a', "ask each", 0, ask_each_cmd
},
787 {'q', "quit", 0, quit_cmd
},
788 {'h', "help", 0, help_cmd
},
792 menu_opts
.header
= N_("*** Commands ***");
793 menu_opts
.prompt
= N_("What now");
794 menu_opts
.flags
= MENU_OPTS_SINGLETON
;
796 menu_stuff
.type
= MENU_STUFF_TYPE_MENU_ITEM
;
797 menu_stuff
.stuff
= menus
;
798 menu_stuff
.nr
= sizeof(menus
) / sizeof(struct menu_item
);
800 clean_print_color(CLEAN_COLOR_HEADER
);
801 printf_ln(Q_("Would remove the following item:",
802 "Would remove the following items:",
804 clean_print_color(CLEAN_COLOR_RESET
);
808 chosen
= list_and_choose(&menu_opts
, &menu_stuff
);
810 if (*chosen
!= EOF
) {
812 ret
= menus
[*chosen
].fn();
813 if (ret
!= MENU_RETURN_NO_LOOP
) {
817 clean_print_color(CLEAN_COLOR_ERROR
);
818 printf_ln(_("No more files to clean, exiting."));
819 clean_print_color(CLEAN_COLOR_RESET
);
834 int cmd_clean(int argc
, const char **argv
, const char *prefix
)
837 int dry_run
= 0, remove_directories
= 0, quiet
= 0, ignored
= 0;
838 int ignored_only
= 0, config_set
= 0, errors
= 0, gone
= 1;
839 int rm_flags
= REMOVE_DIR_KEEP_NESTED_GIT
;
840 struct strbuf abs_path
= STRBUF_INIT
;
841 struct dir_struct dir
;
842 static const char **pathspec
;
843 struct strbuf buf
= STRBUF_INIT
;
844 struct string_list exclude_list
= STRING_LIST_INIT_NODUP
;
845 struct exclude_list
*el
;
846 struct string_list_item
*item
;
849 struct option options
[] = {
850 OPT__QUIET(&quiet
, N_("do not print names of files removed")),
851 OPT__DRY_RUN(&dry_run
, N_("dry run")),
852 OPT__FORCE(&force
, N_("force")),
853 OPT_BOOL('i', "interactive", &interactive
, N_("interactive cleaning")),
854 OPT_BOOL('d', NULL
, &remove_directories
,
855 N_("remove whole directories")),
856 { OPTION_CALLBACK
, 'e', "exclude", &exclude_list
, N_("pattern"),
857 N_("add <pattern> to ignore rules"), PARSE_OPT_NONEG
, exclude_cb
},
858 OPT_BOOL('x', NULL
, &ignored
, N_("remove ignored files, too")),
859 OPT_BOOL('X', NULL
, &ignored_only
,
860 N_("remove only ignored files")),
864 git_config(git_clean_config
, NULL
);
870 argc
= parse_options(argc
, argv
, prefix
, options
, builtin_clean_usage
,
873 memset(&dir
, 0, sizeof(dir
));
875 dir
.flags
|= DIR_SHOW_IGNORED
;
877 if (ignored
&& ignored_only
)
878 die(_("-x and -X cannot be used together"));
880 if (!interactive
&& !dry_run
&& !force
) {
882 die(_("clean.requireForce set to true and neither -i, -n nor -f given; "
883 "refusing to clean"));
885 die(_("clean.requireForce defaults to true and neither -i, -n nor -f given; "
886 "refusing to clean"));
892 dir
.flags
|= DIR_SHOW_OTHER_DIRECTORIES
;
894 if (read_cache() < 0)
895 die(_("index file corrupt"));
898 setup_standard_excludes(&dir
);
900 el
= add_exclude_list(&dir
, EXC_CMDL
, "--exclude option");
901 for (i
= 0; i
< exclude_list
.nr
; i
++)
902 add_exclude(exclude_list
.items
[i
].string
, "", 0, el
, -(i
+1));
904 pathspec
= get_pathspec(prefix
, argv
);
906 fill_directory(&dir
, pathspec
);
909 seen
= xmalloc(argc
> 0 ? argc
: 1);
911 for (i
= 0; i
< dir
.nr
; i
++) {
912 struct dir_entry
*ent
= dir
.entries
[i
];
915 const struct cache_entry
*ce
;
920 * Remove the '/' at the end that directory
921 * walking adds for directory entries.
924 if (len
&& ent
->name
[len
-1] == '/')
926 pos
= cache_name_pos(ent
->name
, len
);
928 continue; /* exact match */
930 if (pos
< active_nr
) {
931 ce
= active_cache
[pos
];
932 if (ce_namelen(ce
) == len
&&
933 !memcmp(ce
->name
, ent
->name
, len
))
934 continue; /* Yup, this one exists unmerged */
937 if (lstat(ent
->name
, &st
))
938 die_errno("Cannot lstat '%s'", ent
->name
);
941 memset(seen
, 0, argc
> 0 ? argc
: 1);
942 matches
= match_pathspec(pathspec
, ent
->name
, len
,
946 if (S_ISDIR(st
.st_mode
)) {
947 if (remove_directories
|| (matches
== MATCHED_EXACTLY
)) {
948 rel
= relative_path(ent
->name
, prefix
, &buf
);
949 string_list_append(&del_list
, rel
);
952 if (pathspec
&& !matches
)
954 rel
= relative_path(ent
->name
, prefix
, &buf
);
955 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
);
1000 strbuf_release(&abs_path
);
1001 strbuf_release(&buf
);
1002 string_list_clear(&del_list
, 0);
1003 string_list_clear(&exclude_list
, 0);
1004 return (errors
!= 0);