2 * "git clean" builtin command
4 * Copyright (C) 2007 Shawn Bohrer
6 * Based on git-clean.sh by Pavel Roskin
13 #include "parse-options.h"
14 #include "string-list.h"
21 static int force
= -1; /* unset */
22 static int interactive
;
23 static struct string_list del_list
= STRING_LIST_INIT_DUP
;
24 static unsigned int colopts
;
26 static const char *const builtin_clean_usage
[] = {
27 N_("git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>..."),
31 static const char *msg_remove
= N_("Removing %s\n");
32 static const char *msg_would_remove
= N_("Would remove %s\n");
33 static const char *msg_skip_git_dir
= N_("Skipping repository %s\n");
34 static const char *msg_would_skip_git_dir
= N_("Would skip repository %s\n");
35 static const char *msg_warn_remove_failed
= N_("failed to remove %s");
38 CLEAN_COLOR_RESET
= 0,
39 CLEAN_COLOR_PLAIN
= 1,
40 CLEAN_COLOR_PROMPT
= 2,
41 CLEAN_COLOR_HEADER
= 3,
46 static const char *color_interactive_slots
[] = {
47 [CLEAN_COLOR_ERROR
] = "error",
48 [CLEAN_COLOR_HEADER
] = "header",
49 [CLEAN_COLOR_HELP
] = "help",
50 [CLEAN_COLOR_PLAIN
] = "plain",
51 [CLEAN_COLOR_PROMPT
] = "prompt",
52 [CLEAN_COLOR_RESET
] = "reset",
55 static int clean_use_color
= -1;
56 static char clean_colors
[][COLOR_MAXLEN
] = {
57 [CLEAN_COLOR_ERROR
] = GIT_COLOR_BOLD_RED
,
58 [CLEAN_COLOR_HEADER
] = GIT_COLOR_BOLD
,
59 [CLEAN_COLOR_HELP
] = GIT_COLOR_BOLD_RED
,
60 [CLEAN_COLOR_PLAIN
] = GIT_COLOR_NORMAL
,
61 [CLEAN_COLOR_PROMPT
] = GIT_COLOR_BOLD_BLUE
,
62 [CLEAN_COLOR_RESET
] = GIT_COLOR_RESET
,
65 #define MENU_OPTS_SINGLETON 01
66 #define MENU_OPTS_IMMEDIATE 02
67 #define MENU_OPTS_LIST_ONLY 04
75 #define MENU_RETURN_NO_LOOP 10
84 enum menu_stuff_type
{
85 MENU_STUFF_TYPE_STRING_LIST
= 1,
86 MENU_STUFF_TYPE_MENU_ITEM
90 enum menu_stuff_type type
;
95 define_list_config_array(color_interactive_slots
);
97 static int git_clean_config(const char *var
, const char *value
, void *cb
)
99 const char *slot_name
;
101 if (starts_with(var
, "column."))
102 return git_column_config(var
, value
, "clean", &colopts
);
104 /* honors the color.interactive* config variables which also
105 applied in git-add--interactive and git-stash */
106 if (!strcmp(var
, "color.interactive")) {
107 clean_use_color
= git_config_colorbool(var
, value
);
110 if (skip_prefix(var
, "color.interactive.", &slot_name
)) {
111 int slot
= LOOKUP_CONFIG(color_interactive_slots
, slot_name
);
115 return config_error_nonbool(var
);
116 return color_parse(value
, clean_colors
[slot
]);
119 if (!strcmp(var
, "clean.requireforce")) {
120 force
= !git_config_bool(var
, value
);
124 /* inspect the color.ui config variable and others */
125 return git_color_default_config(var
, value
, cb
);
128 static const char *clean_get_color(enum color_clean ix
)
130 if (want_color(clean_use_color
))
131 return clean_colors
[ix
];
135 static void clean_print_color(enum color_clean ix
)
137 printf("%s", clean_get_color(ix
));
140 static int exclude_cb(const struct option
*opt
, const char *arg
, int unset
)
142 struct string_list
*exclude_list
= opt
->value
;
143 string_list_append(exclude_list
, arg
);
147 static int remove_dirs(struct strbuf
*path
, const char *prefix
, int force_flag
,
148 int dry_run
, int quiet
, int *dir_gone
)
151 struct strbuf quoted
= STRBUF_INIT
;
153 int res
= 0, ret
= 0, gone
= 1, original_len
= path
->len
, len
;
154 struct string_list dels
= STRING_LIST_INIT_DUP
;
158 if ((force_flag
& REMOVE_DIR_KEEP_NESTED_GIT
) && is_nonbare_repository_dir(path
)) {
160 quote_path_relative(path
->buf
, prefix
, "ed
);
161 printf(dry_run
? _(msg_would_skip_git_dir
) : _(msg_skip_git_dir
),
169 dir
= opendir(path
->buf
);
171 /* an empty dir could be removed even if it is unreadble */
172 res
= dry_run
? 0 : rmdir(path
->buf
);
174 int saved_errno
= errno
;
175 quote_path_relative(path
->buf
, prefix
, "ed
);
177 warning_errno(_(msg_warn_remove_failed
), quoted
.buf
);
184 strbuf_complete(path
, '/');
187 while ((e
= readdir(dir
)) != NULL
) {
189 if (is_dot_or_dotdot(e
->d_name
))
192 strbuf_setlen(path
, len
);
193 strbuf_addstr(path
, e
->d_name
);
194 if (lstat(path
->buf
, &st
))
196 else if (S_ISDIR(st
.st_mode
)) {
197 if (remove_dirs(path
, prefix
, force_flag
, dry_run
, quiet
, &gone
))
200 quote_path_relative(path
->buf
, prefix
, "ed
);
201 string_list_append(&dels
, quoted
.buf
);
206 res
= dry_run
? 0 : unlink(path
->buf
);
208 quote_path_relative(path
->buf
, prefix
, "ed
);
209 string_list_append(&dels
, quoted
.buf
);
211 int saved_errno
= errno
;
212 quote_path_relative(path
->buf
, prefix
, "ed
);
214 warning_errno(_(msg_warn_remove_failed
), quoted
.buf
);
221 /* path too long, stat fails, or non-directory still exists */
228 strbuf_setlen(path
, original_len
);
231 res
= dry_run
? 0 : rmdir(path
->buf
);
235 int saved_errno
= errno
;
236 quote_path_relative(path
->buf
, prefix
, "ed
);
238 warning_errno(_(msg_warn_remove_failed
), quoted
.buf
);
244 if (!*dir_gone
&& !quiet
) {
246 for (i
= 0; i
< dels
.nr
; i
++)
247 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), dels
.items
[i
].string
);
250 strbuf_release("ed
);
251 string_list_clear(&dels
, 0);
255 static void pretty_print_dels(void)
257 struct string_list list
= STRING_LIST_INIT_DUP
;
258 struct string_list_item
*item
;
259 struct strbuf buf
= STRBUF_INIT
;
261 struct column_options copts
;
263 for_each_string_list_item(item
, &del_list
) {
264 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
265 string_list_append(&list
, qname
);
269 * always enable column display, we only consult column.*
270 * about layout strategy and stuff
272 colopts
= (colopts
& ~COL_ENABLE_MASK
) | COL_ENABLED
;
273 memset(&copts
, 0, sizeof(copts
));
276 print_columns(&list
, colopts
, &copts
);
277 strbuf_release(&buf
);
278 string_list_clear(&list
, 0);
281 static void pretty_print_menus(struct string_list
*menu_list
)
283 unsigned int local_colopts
= 0;
284 struct column_options copts
;
286 local_colopts
= COL_ENABLED
| COL_ROW
;
287 memset(&copts
, 0, sizeof(copts
));
290 print_columns(menu_list
, local_colopts
, &copts
);
293 static void prompt_help_cmd(int singleton
)
295 clean_print_color(CLEAN_COLOR_HELP
);
298 "1 - select a numbered item\n"
299 "foo - select item based on unique prefix\n"
300 " - (empty) select nothing\n") :
302 "1 - select a single item\n"
303 "3-5 - select a range of items\n"
304 "2-3,6-9 - select multiple ranges\n"
305 "foo - select item based on unique prefix\n"
306 "-... - unselect specified items\n"
307 "* - choose all items\n"
308 " - (empty) finish selecting\n"));
309 clean_print_color(CLEAN_COLOR_RESET
);
313 * display menu stuff with number prefix and hotkey highlight
315 static void print_highlight_menu_stuff(struct menu_stuff
*stuff
, int **chosen
)
317 struct string_list menu_list
= STRING_LIST_INIT_DUP
;
318 struct strbuf menu
= 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_stuff 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 string_list_clear(&menu_list
, 0);
370 static int find_unique(const char *choice
, struct menu_stuff
*menu_stuff
)
372 struct menu_item
*menu_item
;
373 struct string_list_item
*string_list_item
;
374 int i
, len
, found
= 0;
376 len
= strlen(choice
);
377 switch (menu_stuff
->type
) {
379 die("Bad type of menu_stuff when parse choice");
380 case MENU_STUFF_TYPE_MENU_ITEM
:
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
) {
388 if (!strncasecmp(choice
, menu_item
->title
, len
)) {
391 /* continue for hotkey matching */
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
)) {
421 * Parse user input, and return choice(s) for menu (menu_stuff).
424 * (for single choice)
425 * 1 - select a numbered item
426 * foo - select item based on menu title
427 * - (empty) select nothing
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
438 * The parse result will be saved in array **chosen, and
439 * return number of total selections.
441 static int parse_choice(struct menu_stuff
*menu_stuff
,
446 struct strbuf
**choice_list
, **ptr
;
451 choice_list
= strbuf_split_max(&input
, '\n', 0);
458 choice_list
= strbuf_split_max(&input
, ' ', 0);
461 for (ptr
= choice_list
; *ptr
; ptr
++) {
464 int bottom
= 0, top
= 0;
465 int is_range
, is_number
;
471 /* Input that begins with '-'; unchoose */
472 if (*(*ptr
)->buf
== '-') {
474 strbuf_remove((*ptr
), 0, 1);
479 for (p
= (*ptr
)->buf
; *p
; p
++) {
489 } else if (!isdigit(*p
)) {
497 bottom
= atoi((*ptr
)->buf
);
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
;
505 top
= atoi(strchr((*ptr
)->buf
, '-') + 1);
506 } else if (!strcmp((*ptr
)->buf
, "*")) {
508 top
= menu_stuff
->nr
;
510 bottom
= find_unique((*ptr
)->buf
, menu_stuff
);
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(_("Huh (%s)?\n"), (*ptr
)->buf
);
518 clean_print_color(CLEAN_COLOR_RESET
);
522 for (i
= bottom
; i
<= top
; i
++)
523 (*chosen
)[i
-1] = choose
;
526 strbuf_list_free(choice_list
);
528 for (i
= 0; i
< menu_stuff
->nr
; i
++)
534 * Implement a git-add-interactive compatible UI, which is borrowed
535 * from git-add--interactive.perl.
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.
544 static int *list_and_choose(struct menu_opts
*opts
, struct menu_stuff
*stuff
)
546 struct strbuf choice
= STRBUF_INIT
;
547 int *chosen
, *result
;
552 ALLOC_ARRAY(chosen
, stuff
->nr
);
553 /* set chosen as uninitialized */
554 for (i
= 0; i
< stuff
->nr
; i
++)
560 clean_get_color(CLEAN_COLOR_HEADER
),
562 clean_get_color(CLEAN_COLOR_RESET
));
565 /* chosen will be initialized by print_highlight_menu_stuff */
566 print_highlight_menu_stuff(stuff
, &chosen
);
568 if (opts
->flags
& MENU_OPTS_LIST_ONLY
)
573 clean_get_color(CLEAN_COLOR_PROMPT
),
575 opts
->flags
& MENU_OPTS_SINGLETON
? "> " : ">> ",
576 clean_get_color(CLEAN_COLOR_RESET
));
579 if (strbuf_getline_lf(&choice
, stdin
) != EOF
) {
580 strbuf_trim(&choice
);
586 /* help for prompt */
587 if (!strcmp(choice
.buf
, "?")) {
588 prompt_help_cmd(opts
->flags
& MENU_OPTS_SINGLETON
);
592 /* for a multiple-choice menu, press ENTER (empty) will return back */
593 if (!(opts
->flags
& MENU_OPTS_SINGLETON
) && !choice
.len
)
596 nr
= parse_choice(stuff
,
597 opts
->flags
& MENU_OPTS_SINGLETON
,
601 if (opts
->flags
& MENU_OPTS_SINGLETON
) {
604 } else if (opts
->flags
& MENU_OPTS_IMMEDIATE
) {
610 result
= xmalloc(sizeof(int));
616 * recalculate nr, if return back from menu directly with
617 * default selections.
620 for (i
= 0; i
< stuff
->nr
; i
++)
624 result
= xcalloc(st_add(nr
, 1), sizeof(int));
625 for (i
= 0; i
< stuff
->nr
&& j
< nr
; i
++) {
633 strbuf_release(&choice
);
637 static int clean_cmd(void)
639 return MENU_RETURN_NO_LOOP
;
642 static int filter_by_patterns_cmd(void)
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
;
658 clean_print_color(CLEAN_COLOR_PROMPT
);
659 printf(_("Input ignore patterns>> "));
660 clean_print_color(CLEAN_COLOR_RESET
);
661 if (strbuf_getline_lf(&confirm
, stdin
) != EOF
)
662 strbuf_trim(&confirm
);
666 /* quit filter_by_pattern mode if press ENTER or Ctrl-D */
670 memset(&dir
, 0, sizeof(dir
));
671 el
= add_exclude_list(&dir
, EXC_CMDL
, "manual exclude");
672 ignore_list
= strbuf_split_max(&confirm
, ' ', 0);
674 for (i
= 0; ignore_list
[i
]; i
++) {
675 strbuf_trim(ignore_list
[i
]);
676 if (!ignore_list
[i
]->len
)
679 add_exclude(ignore_list
[i
]->buf
, "", 0, el
, -(i
+1));
683 for_each_string_list_item(item
, &del_list
) {
684 int dtype
= DT_UNKNOWN
;
686 if (is_excluded(&dir
, &the_index
, item
->string
, &dtype
)) {
687 *item
->string
= '\0';
693 string_list_remove_empty_items(&del_list
, 0);
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
);
700 strbuf_list_free(ignore_list
);
701 clear_directory(&dir
);
704 strbuf_release(&confirm
);
708 static int select_by_numbers_cmd(void)
710 struct menu_opts menu_opts
;
711 struct menu_stuff menu_stuff
;
712 struct string_list_item
*items
;
716 menu_opts
.header
= NULL
;
717 menu_opts
.prompt
= N_("Select items to delete");
720 menu_stuff
.type
= MENU_STUFF_TYPE_STRING_LIST
;
721 menu_stuff
.stuff
= &del_list
;
722 menu_stuff
.nr
= del_list
.nr
;
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
++) {
728 *(items
[i
].string
) = '\0';
729 } else if (i
== chosen
[j
]) {
730 /* delete selected item */
734 /* end of chosen (chosen[j] == EOF), won't delete */
735 *(items
[i
].string
) = '\0';
739 string_list_remove_empty_items(&del_list
, 0);
745 static int ask_each_cmd(void)
747 struct strbuf confirm
= STRBUF_INIT
;
748 struct strbuf buf
= STRBUF_INIT
;
749 struct string_list_item
*item
;
751 int changed
= 0, eof
= 0;
753 for_each_string_list_item(item
, &del_list
) {
754 /* Ctrl-D should stop removing files */
756 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
757 /* TRANSLATORS: Make sure to keep [y/N] as is */
758 printf(_("Remove %s [y/N]? "), qname
);
759 if (strbuf_getline_lf(&confirm
, stdin
) != 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);
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
) {
840 FREE_AND_NULL(chosen
);
842 clean_print_color(CLEAN_COLOR_ERROR
);
843 printf_ln(_("No more files to clean, exiting."));
844 clean_print_color(CLEAN_COLOR_RESET
);
853 FREE_AND_NULL(chosen
);
858 static void correct_untracked_entries(struct dir_struct
*dir
)
862 for (src
= dst
= ign
= 0; src
< dir
->nr
; src
++) {
863 /* skip paths in ignored[] that cannot be inside entries[src] */
864 while (ign
< dir
->ignored_nr
&&
865 0 <= cmp_dir_entry(&dir
->entries
[src
], &dir
->ignored
[ign
]))
868 if (ign
< dir
->ignored_nr
&&
869 check_dir_entry_contains(dir
->entries
[src
], dir
->ignored
[ign
])) {
870 /* entries[src] contains an ignored path, so we drop it */
871 free(dir
->entries
[src
]);
873 struct dir_entry
*ent
= dir
->entries
[src
++];
875 /* entries[src] does not contain an ignored path, so we keep it */
876 dir
->entries
[dst
++] = ent
;
878 /* then discard paths in entries[] contained inside entries[src] */
879 while (src
< dir
->nr
&&
880 check_dir_entry_contains(ent
, dir
->entries
[src
]))
881 free(dir
->entries
[src
++]);
883 /* compensate for the outer loop's loop control */
890 int cmd_clean(int argc
, const char **argv
, const char *prefix
)
893 int dry_run
= 0, remove_directories
= 0, quiet
= 0, ignored
= 0;
894 int ignored_only
= 0, config_set
= 0, errors
= 0, gone
= 1;
895 int rm_flags
= REMOVE_DIR_KEEP_NESTED_GIT
;
896 struct strbuf abs_path
= STRBUF_INIT
;
897 struct dir_struct dir
;
898 struct pathspec pathspec
;
899 struct strbuf buf
= STRBUF_INIT
;
900 struct string_list exclude_list
= STRING_LIST_INIT_NODUP
;
901 struct exclude_list
*el
;
902 struct string_list_item
*item
;
904 struct option options
[] = {
905 OPT__QUIET(&quiet
, N_("do not print names of files removed")),
906 OPT__DRY_RUN(&dry_run
, N_("dry run")),
907 OPT__FORCE(&force
, N_("force"), PARSE_OPT_NOCOMPLETE
),
908 OPT_BOOL('i', "interactive", &interactive
, N_("interactive cleaning")),
909 OPT_BOOL('d', NULL
, &remove_directories
,
910 N_("remove whole directories")),
911 { OPTION_CALLBACK
, 'e', "exclude", &exclude_list
, N_("pattern"),
912 N_("add <pattern> to ignore rules"), PARSE_OPT_NONEG
, exclude_cb
},
913 OPT_BOOL('x', NULL
, &ignored
, N_("remove ignored files, too")),
914 OPT_BOOL('X', NULL
, &ignored_only
,
915 N_("remove only ignored files")),
919 git_config(git_clean_config
, NULL
);
925 argc
= parse_options(argc
, argv
, prefix
, options
, builtin_clean_usage
,
928 memset(&dir
, 0, sizeof(dir
));
930 dir
.flags
|= DIR_SHOW_IGNORED
;
932 if (ignored
&& ignored_only
)
933 die(_("-x and -X cannot be used together"));
935 if (!interactive
&& !dry_run
&& !force
) {
937 die(_("clean.requireForce set to true and neither -i, -n, nor -f given; "
938 "refusing to clean"));
940 die(_("clean.requireForce defaults to true and neither -i, -n, nor -f given;"
941 " refusing to clean"));
947 dir
.flags
|= DIR_SHOW_OTHER_DIRECTORIES
;
949 if (remove_directories
)
950 dir
.flags
|= DIR_SHOW_IGNORED_TOO
| DIR_KEEP_UNTRACKED_CONTENTS
;
952 if (read_cache() < 0)
953 die(_("index file corrupt"));
956 setup_standard_excludes(&dir
);
958 el
= add_exclude_list(&dir
, EXC_CMDL
, "--exclude option");
959 for (i
= 0; i
< exclude_list
.nr
; i
++)
960 add_exclude(exclude_list
.items
[i
].string
, "", 0, el
, -(i
+1));
962 parse_pathspec(&pathspec
, 0,
966 fill_directory(&dir
, &the_index
, &pathspec
);
967 correct_untracked_entries(&dir
);
969 for (i
= 0; i
< dir
.nr
; i
++) {
970 struct dir_entry
*ent
= dir
.entries
[i
];
975 if (!cache_name_is_other(ent
->name
, ent
->len
))
979 matches
= dir_path_match(ent
, &pathspec
, 0, NULL
);
981 if (pathspec
.nr
&& !matches
)
984 if (lstat(ent
->name
, &st
))
985 die_errno("Cannot lstat '%s'", ent
->name
);
987 if (S_ISDIR(st
.st_mode
) && !remove_directories
&&
988 matches
!= MATCHED_EXACTLY
)
991 rel
= relative_path(ent
->name
, prefix
, &buf
);
992 string_list_append(&del_list
, rel
);
995 for (i
= 0; i
< dir
.nr
; i
++)
996 free(dir
.entries
[i
]);
998 for (i
= 0; i
< dir
.ignored_nr
; i
++)
999 free(dir
.ignored
[i
]);
1001 if (interactive
&& del_list
.nr
> 0)
1002 interactive_main_loop();
1004 for_each_string_list_item(item
, &del_list
) {
1008 strbuf_addstr(&abs_path
, prefix
);
1010 strbuf_addstr(&abs_path
, item
->string
);
1013 * we might have removed this as part of earlier
1014 * recursive directory removal, so lstat() here could
1017 if (lstat(abs_path
.buf
, &st
))
1020 if (S_ISDIR(st
.st_mode
)) {
1021 if (remove_dirs(&abs_path
, prefix
, rm_flags
, dry_run
, quiet
, &gone
))
1023 if (gone
&& !quiet
) {
1024 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
1025 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), qname
);
1028 res
= dry_run
? 0 : unlink(abs_path
.buf
);
1030 int saved_errno
= errno
;
1031 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
1032 errno
= saved_errno
;
1033 warning_errno(_(msg_warn_remove_failed
), qname
);
1035 } else if (!quiet
) {
1036 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
1037 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), qname
);
1040 strbuf_reset(&abs_path
);
1043 strbuf_release(&abs_path
);
1044 strbuf_release(&buf
);
1045 string_list_clear(&del_list
, 0);
1046 string_list_clear(&exclude_list
, 0);
1047 return (errors
!= 0);