2 * "git clean" builtin command
4 * Copyright (C) 2007 Shawn Bohrer
6 * Based on git-clean.sh by Pavel Roskin
9 #define USE_THE_INDEX_COMPATIBILITY_MACROS
14 #include "parse-options.h"
15 #include "string-list.h"
22 static int force
= -1; /* unset */
23 static int interactive
;
24 static struct string_list del_list
= STRING_LIST_INIT_DUP
;
25 static unsigned int colopts
;
27 static const char *const builtin_clean_usage
[] = {
28 N_("git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>..."),
32 static const char *msg_remove
= N_("Removing %s\n");
33 static const char *msg_would_remove
= N_("Would remove %s\n");
34 static const char *msg_skip_git_dir
= N_("Skipping repository %s\n");
35 static const char *msg_would_skip_git_dir
= N_("Would skip repository %s\n");
36 static const char *msg_warn_remove_failed
= N_("failed to remove %s");
37 static const char *msg_warn_lstat_failed
= N_("could not lstat %s\n");
40 CLEAN_COLOR_RESET
= 0,
41 CLEAN_COLOR_PLAIN
= 1,
42 CLEAN_COLOR_PROMPT
= 2,
43 CLEAN_COLOR_HEADER
= 3,
48 static const char *color_interactive_slots
[] = {
49 [CLEAN_COLOR_ERROR
] = "error",
50 [CLEAN_COLOR_HEADER
] = "header",
51 [CLEAN_COLOR_HELP
] = "help",
52 [CLEAN_COLOR_PLAIN
] = "plain",
53 [CLEAN_COLOR_PROMPT
] = "prompt",
54 [CLEAN_COLOR_RESET
] = "reset",
57 static int clean_use_color
= -1;
58 static char clean_colors
[][COLOR_MAXLEN
] = {
59 [CLEAN_COLOR_ERROR
] = GIT_COLOR_BOLD_RED
,
60 [CLEAN_COLOR_HEADER
] = GIT_COLOR_BOLD
,
61 [CLEAN_COLOR_HELP
] = GIT_COLOR_BOLD_RED
,
62 [CLEAN_COLOR_PLAIN
] = GIT_COLOR_NORMAL
,
63 [CLEAN_COLOR_PROMPT
] = GIT_COLOR_BOLD_BLUE
,
64 [CLEAN_COLOR_RESET
] = GIT_COLOR_RESET
,
67 #define MENU_OPTS_SINGLETON 01
68 #define MENU_OPTS_IMMEDIATE 02
69 #define MENU_OPTS_LIST_ONLY 04
77 #define MENU_RETURN_NO_LOOP 10
86 enum menu_stuff_type
{
87 MENU_STUFF_TYPE_STRING_LIST
= 1,
88 MENU_STUFF_TYPE_MENU_ITEM
92 enum menu_stuff_type type
;
97 define_list_config_array(color_interactive_slots
);
99 static int git_clean_config(const char *var
, const char *value
, void *cb
)
101 const char *slot_name
;
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 (skip_prefix(var
, "color.interactive.", &slot_name
)) {
113 int slot
= LOOKUP_CONFIG(color_interactive_slots
, slot_name
);
117 return config_error_nonbool(var
);
118 return color_parse(value
, clean_colors
[slot
]);
121 if (!strcmp(var
, "clean.requireforce")) {
122 force
= !git_config_bool(var
, value
);
126 /* inspect the color.ui config variable and others */
127 return git_color_default_config(var
, value
, cb
);
130 static const char *clean_get_color(enum color_clean ix
)
132 if (want_color(clean_use_color
))
133 return clean_colors
[ix
];
137 static void clean_print_color(enum color_clean ix
)
139 printf("%s", clean_get_color(ix
));
142 static int exclude_cb(const struct option
*opt
, const char *arg
, int unset
)
144 struct string_list
*exclude_list
= opt
->value
;
145 BUG_ON_OPT_NEG(unset
);
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 struct string_list dels
= STRING_LIST_INIT_DUP
;
161 if ((force_flag
& REMOVE_DIR_KEEP_NESTED_GIT
) && is_nonbare_repository_dir(path
)) {
163 quote_path_relative(path
->buf
, prefix
, "ed
);
164 printf(dry_run
? _(msg_would_skip_git_dir
) : _(msg_skip_git_dir
),
172 dir
= opendir(path
->buf
);
174 /* an empty dir could be removed even if it is unreadble */
175 res
= dry_run
? 0 : rmdir(path
->buf
);
177 int saved_errno
= errno
;
178 quote_path_relative(path
->buf
, prefix
, "ed
);
180 warning_errno(_(msg_warn_remove_failed
), quoted
.buf
);
187 strbuf_complete(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
))
198 warning_errno(_(msg_warn_lstat_failed
), path
->buf
);
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 int saved_errno
= errno
;
215 quote_path_relative(path
->buf
, prefix
, "ed
);
217 warning_errno(_(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 int saved_errno
= errno
;
239 quote_path_relative(path
->buf
, prefix
, "ed
);
241 warning_errno(_(msg_warn_remove_failed
), quoted
.buf
);
247 if (!*dir_gone
&& !quiet
) {
249 for (i
= 0; i
< dels
.nr
; i
++)
250 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), dels
.items
[i
].string
);
253 strbuf_release("ed
);
254 string_list_clear(&dels
, 0);
258 static void pretty_print_dels(void)
260 struct string_list list
= STRING_LIST_INIT_DUP
;
261 struct string_list_item
*item
;
262 struct strbuf buf
= STRBUF_INIT
;
264 struct column_options copts
;
266 for_each_string_list_item(item
, &del_list
) {
267 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
268 string_list_append(&list
, qname
);
272 * always enable column display, we only consult column.*
273 * about layout strategy and stuff
275 colopts
= (colopts
& ~COL_ENABLE_MASK
) | COL_ENABLED
;
276 memset(&copts
, 0, sizeof(copts
));
279 print_columns(&list
, colopts
, &copts
);
280 strbuf_release(&buf
);
281 string_list_clear(&list
, 0);
284 static void pretty_print_menus(struct string_list
*menu_list
)
286 unsigned int local_colopts
= 0;
287 struct column_options copts
;
289 local_colopts
= COL_ENABLED
| COL_ROW
;
290 memset(&copts
, 0, sizeof(copts
));
293 print_columns(menu_list
, local_colopts
, &copts
);
296 static void prompt_help_cmd(int singleton
)
298 clean_print_color(CLEAN_COLOR_HELP
);
301 "1 - select a numbered item\n"
302 "foo - select item based on unique prefix\n"
303 " - (empty) select nothing\n") :
305 "1 - select a single item\n"
306 "3-5 - select a range of items\n"
307 "2-3,6-9 - select multiple ranges\n"
308 "foo - select item based on unique prefix\n"
309 "-... - unselect specified items\n"
310 "* - choose all items\n"
311 " - (empty) finish selecting\n"));
312 clean_print_color(CLEAN_COLOR_RESET
);
316 * display menu stuff with number prefix and hotkey highlight
318 static void print_highlight_menu_stuff(struct menu_stuff
*stuff
, int **chosen
)
320 struct string_list menu_list
= STRING_LIST_INIT_DUP
;
321 struct strbuf menu
= STRBUF_INIT
;
322 struct menu_item
*menu_item
;
323 struct string_list_item
*string_list_item
;
326 switch (stuff
->type
) {
328 die("Bad type of menu_stuff when print menu");
329 case MENU_STUFF_TYPE_MENU_ITEM
:
330 menu_item
= (struct menu_item
*)stuff
->stuff
;
331 for (i
= 0; i
< stuff
->nr
; i
++, menu_item
++) {
335 p
= menu_item
->title
;
336 if ((*chosen
)[i
] < 0)
337 (*chosen
)[i
] = menu_item
->selected
? 1 : 0;
338 strbuf_addf(&menu
, "%s%2d: ", (*chosen
)[i
] ? "*" : " ", i
+1);
340 if (!highlighted
&& *p
== menu_item
->hotkey
) {
341 strbuf_addstr(&menu
, clean_get_color(CLEAN_COLOR_PROMPT
));
342 strbuf_addch(&menu
, *p
);
343 strbuf_addstr(&menu
, clean_get_color(CLEAN_COLOR_RESET
));
346 strbuf_addch(&menu
, *p
);
349 string_list_append(&menu_list
, menu
.buf
);
353 case MENU_STUFF_TYPE_STRING_LIST
:
355 for_each_string_list_item(string_list_item
, (struct string_list
*)stuff
->stuff
) {
356 if ((*chosen
)[i
] < 0)
358 strbuf_addf(&menu
, "%s%2d: %s",
359 (*chosen
)[i
] ? "*" : " ", i
+1, string_list_item
->string
);
360 string_list_append(&menu_list
, menu
.buf
);
367 pretty_print_menus(&menu_list
);
369 strbuf_release(&menu
);
370 string_list_clear(&menu_list
, 0);
373 static int find_unique(const char *choice
, struct menu_stuff
*menu_stuff
)
375 struct menu_item
*menu_item
;
376 struct string_list_item
*string_list_item
;
377 int i
, len
, found
= 0;
379 len
= strlen(choice
);
380 switch (menu_stuff
->type
) {
382 die("Bad type of menu_stuff when parse choice");
383 case MENU_STUFF_TYPE_MENU_ITEM
:
385 menu_item
= (struct menu_item
*)menu_stuff
->stuff
;
386 for (i
= 0; i
< menu_stuff
->nr
; i
++, menu_item
++) {
387 if (len
== 1 && *choice
== menu_item
->hotkey
) {
391 if (!strncasecmp(choice
, menu_item
->title
, len
)) {
394 /* continue for hotkey matching */
406 case MENU_STUFF_TYPE_STRING_LIST
:
407 string_list_item
= ((struct string_list
*)menu_stuff
->stuff
)->items
;
408 for (i
= 0; i
< menu_stuff
->nr
; i
++, string_list_item
++) {
409 if (!strncasecmp(choice
, string_list_item
->string
, len
)) {
424 * Parse user input, and return choice(s) for menu (menu_stuff).
427 * (for single choice)
428 * 1 - select a numbered item
429 * foo - select item based on menu title
430 * - (empty) select nothing
432 * (for multiple choice)
433 * 1 - select a single item
434 * 3-5 - select a range of items
435 * 2-3,6-9 - select multiple ranges
436 * foo - select item based on menu title
437 * -... - unselect specified items
438 * * - choose all items
439 * - (empty) finish selecting
441 * The parse result will be saved in array **chosen, and
442 * return number of total selections.
444 static int parse_choice(struct menu_stuff
*menu_stuff
,
449 struct strbuf
**choice_list
, **ptr
;
454 choice_list
= strbuf_split_max(&input
, '\n', 0);
461 choice_list
= strbuf_split_max(&input
, ' ', 0);
464 for (ptr
= choice_list
; *ptr
; ptr
++) {
467 int bottom
= 0, top
= 0;
468 int is_range
, is_number
;
474 /* Input that begins with '-'; unchoose */
475 if (*(*ptr
)->buf
== '-') {
477 strbuf_remove((*ptr
), 0, 1);
482 for (p
= (*ptr
)->buf
; *p
; p
++) {
492 } else if (!isdigit(*p
)) {
500 bottom
= atoi((*ptr
)->buf
);
502 } else if (is_range
) {
503 bottom
= atoi((*ptr
)->buf
);
504 /* a range can be specified like 5-7 or 5- */
505 if (!*(strchr((*ptr
)->buf
, '-') + 1))
506 top
= menu_stuff
->nr
;
508 top
= atoi(strchr((*ptr
)->buf
, '-') + 1);
509 } else if (!strcmp((*ptr
)->buf
, "*")) {
511 top
= menu_stuff
->nr
;
513 bottom
= find_unique((*ptr
)->buf
, menu_stuff
);
517 if (top
<= 0 || bottom
<= 0 || top
> menu_stuff
->nr
|| bottom
> top
||
518 (is_single
&& bottom
!= top
)) {
519 clean_print_color(CLEAN_COLOR_ERROR
);
520 printf(_("Huh (%s)?\n"), (*ptr
)->buf
);
521 clean_print_color(CLEAN_COLOR_RESET
);
525 for (i
= bottom
; i
<= top
; i
++)
526 (*chosen
)[i
-1] = choose
;
529 strbuf_list_free(choice_list
);
531 for (i
= 0; i
< menu_stuff
->nr
; i
++)
537 * Implement a git-add-interactive compatible UI, which is borrowed
538 * from git-add--interactive.perl.
542 * - Return an array of integers
543 * - , and it is up to you to free the allocated memory.
544 * - The array ends with EOF.
545 * - If user pressed CTRL-D (i.e. EOF), no selection returned.
547 static int *list_and_choose(struct menu_opts
*opts
, struct menu_stuff
*stuff
)
549 struct strbuf choice
= STRBUF_INIT
;
550 int *chosen
, *result
;
555 ALLOC_ARRAY(chosen
, stuff
->nr
);
556 /* set chosen as uninitialized */
557 for (i
= 0; i
< stuff
->nr
; i
++)
563 clean_get_color(CLEAN_COLOR_HEADER
),
565 clean_get_color(CLEAN_COLOR_RESET
));
568 /* chosen will be initialized by print_highlight_menu_stuff */
569 print_highlight_menu_stuff(stuff
, &chosen
);
571 if (opts
->flags
& MENU_OPTS_LIST_ONLY
)
576 clean_get_color(CLEAN_COLOR_PROMPT
),
578 opts
->flags
& MENU_OPTS_SINGLETON
? "> " : ">> ",
579 clean_get_color(CLEAN_COLOR_RESET
));
582 if (strbuf_getline_lf(&choice
, stdin
) != EOF
) {
583 strbuf_trim(&choice
);
589 /* help for prompt */
590 if (!strcmp(choice
.buf
, "?")) {
591 prompt_help_cmd(opts
->flags
& MENU_OPTS_SINGLETON
);
595 /* for a multiple-choice menu, press ENTER (empty) will return back */
596 if (!(opts
->flags
& MENU_OPTS_SINGLETON
) && !choice
.len
)
599 nr
= parse_choice(stuff
,
600 opts
->flags
& MENU_OPTS_SINGLETON
,
604 if (opts
->flags
& MENU_OPTS_SINGLETON
) {
607 } else if (opts
->flags
& MENU_OPTS_IMMEDIATE
) {
613 result
= xmalloc(sizeof(int));
619 * recalculate nr, if return back from menu directly with
620 * default selections.
623 for (i
= 0; i
< stuff
->nr
; i
++)
627 result
= xcalloc(st_add(nr
, 1), sizeof(int));
628 for (i
= 0; i
< stuff
->nr
&& j
< nr
; i
++) {
636 strbuf_release(&choice
);
640 static int clean_cmd(void)
642 return MENU_RETURN_NO_LOOP
;
645 static int filter_by_patterns_cmd(void)
647 struct dir_struct dir
;
648 struct strbuf confirm
= STRBUF_INIT
;
649 struct strbuf
**ignore_list
;
650 struct string_list_item
*item
;
651 struct exclude_list
*el
;
661 clean_print_color(CLEAN_COLOR_PROMPT
);
662 printf(_("Input ignore patterns>> "));
663 clean_print_color(CLEAN_COLOR_RESET
);
664 if (strbuf_getline_lf(&confirm
, stdin
) != EOF
)
665 strbuf_trim(&confirm
);
669 /* quit filter_by_pattern mode if press ENTER or Ctrl-D */
673 memset(&dir
, 0, sizeof(dir
));
674 el
= add_exclude_list(&dir
, EXC_CMDL
, "manual exclude");
675 ignore_list
= strbuf_split_max(&confirm
, ' ', 0);
677 for (i
= 0; ignore_list
[i
]; i
++) {
678 strbuf_trim(ignore_list
[i
]);
679 if (!ignore_list
[i
]->len
)
682 add_exclude(ignore_list
[i
]->buf
, "", 0, el
, -(i
+1));
686 for_each_string_list_item(item
, &del_list
) {
687 int dtype
= DT_UNKNOWN
;
689 if (is_excluded(&dir
, &the_index
, item
->string
, &dtype
)) {
690 *item
->string
= '\0';
696 string_list_remove_empty_items(&del_list
, 0);
698 clean_print_color(CLEAN_COLOR_ERROR
);
699 printf_ln(_("WARNING: Cannot find items matched by: %s"), confirm
.buf
);
700 clean_print_color(CLEAN_COLOR_RESET
);
703 strbuf_list_free(ignore_list
);
704 clear_directory(&dir
);
707 strbuf_release(&confirm
);
711 static int select_by_numbers_cmd(void)
713 struct menu_opts menu_opts
;
714 struct menu_stuff menu_stuff
;
715 struct string_list_item
*items
;
719 menu_opts
.header
= NULL
;
720 menu_opts
.prompt
= N_("Select items to delete");
723 menu_stuff
.type
= MENU_STUFF_TYPE_STRING_LIST
;
724 menu_stuff
.stuff
= &del_list
;
725 menu_stuff
.nr
= del_list
.nr
;
727 chosen
= list_and_choose(&menu_opts
, &menu_stuff
);
728 items
= del_list
.items
;
729 for (i
= 0, j
= 0; i
< del_list
.nr
; i
++) {
731 *(items
[i
].string
) = '\0';
732 } else if (i
== chosen
[j
]) {
733 /* delete selected item */
737 /* end of chosen (chosen[j] == EOF), won't delete */
738 *(items
[i
].string
) = '\0';
742 string_list_remove_empty_items(&del_list
, 0);
748 static int ask_each_cmd(void)
750 struct strbuf confirm
= STRBUF_INIT
;
751 struct strbuf buf
= STRBUF_INIT
;
752 struct string_list_item
*item
;
754 int changed
= 0, eof
= 0;
756 for_each_string_list_item(item
, &del_list
) {
757 /* Ctrl-D should stop removing files */
759 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
760 /* TRANSLATORS: Make sure to keep [y/N] as is */
761 printf(_("Remove %s [y/N]? "), qname
);
762 if (strbuf_getline_lf(&confirm
, stdin
) != EOF
) {
763 strbuf_trim(&confirm
);
769 if (!confirm
.len
|| strncasecmp(confirm
.buf
, "yes", confirm
.len
)) {
770 *item
->string
= '\0';
776 string_list_remove_empty_items(&del_list
, 0);
778 strbuf_release(&buf
);
779 strbuf_release(&confirm
);
780 return MENU_RETURN_NO_LOOP
;
783 static int quit_cmd(void)
785 string_list_clear(&del_list
, 0);
787 return MENU_RETURN_NO_LOOP
;
790 static int help_cmd(void)
792 clean_print_color(CLEAN_COLOR_HELP
);
794 "clean - start cleaning\n"
795 "filter by pattern - exclude items from deletion\n"
796 "select by numbers - select items to be deleted by numbers\n"
797 "ask each - confirm each deletion (like \"rm -i\")\n"
798 "quit - stop cleaning\n"
799 "help - this screen\n"
800 "? - help for prompt selection"
802 clean_print_color(CLEAN_COLOR_RESET
);
806 static void interactive_main_loop(void)
808 while (del_list
.nr
) {
809 struct menu_opts menu_opts
;
810 struct menu_stuff menu_stuff
;
811 struct menu_item menus
[] = {
812 {'c', "clean", 0, clean_cmd
},
813 {'f', "filter by pattern", 0, filter_by_patterns_cmd
},
814 {'s', "select by numbers", 0, select_by_numbers_cmd
},
815 {'a', "ask each", 0, ask_each_cmd
},
816 {'q', "quit", 0, quit_cmd
},
817 {'h', "help", 0, help_cmd
},
821 menu_opts
.header
= N_("*** Commands ***");
822 menu_opts
.prompt
= N_("What now");
823 menu_opts
.flags
= MENU_OPTS_SINGLETON
;
825 menu_stuff
.type
= MENU_STUFF_TYPE_MENU_ITEM
;
826 menu_stuff
.stuff
= menus
;
827 menu_stuff
.nr
= sizeof(menus
) / sizeof(struct menu_item
);
829 clean_print_color(CLEAN_COLOR_HEADER
);
830 printf_ln(Q_("Would remove the following item:",
831 "Would remove the following items:",
833 clean_print_color(CLEAN_COLOR_RESET
);
837 chosen
= list_and_choose(&menu_opts
, &menu_stuff
);
839 if (*chosen
!= EOF
) {
841 ret
= menus
[*chosen
].fn();
842 if (ret
!= MENU_RETURN_NO_LOOP
) {
843 FREE_AND_NULL(chosen
);
845 clean_print_color(CLEAN_COLOR_ERROR
);
846 printf_ln(_("No more files to clean, exiting."));
847 clean_print_color(CLEAN_COLOR_RESET
);
856 FREE_AND_NULL(chosen
);
861 static void correct_untracked_entries(struct dir_struct
*dir
)
865 for (src
= dst
= ign
= 0; src
< dir
->nr
; src
++) {
866 /* skip paths in ignored[] that cannot be inside entries[src] */
867 while (ign
< dir
->ignored_nr
&&
868 0 <= cmp_dir_entry(&dir
->entries
[src
], &dir
->ignored
[ign
]))
871 if (ign
< dir
->ignored_nr
&&
872 check_dir_entry_contains(dir
->entries
[src
], dir
->ignored
[ign
])) {
873 /* entries[src] contains an ignored path, so we drop it */
874 free(dir
->entries
[src
]);
876 struct dir_entry
*ent
= dir
->entries
[src
++];
878 /* entries[src] does not contain an ignored path, so we keep it */
879 dir
->entries
[dst
++] = ent
;
881 /* then discard paths in entries[] contained inside entries[src] */
882 while (src
< dir
->nr
&&
883 check_dir_entry_contains(ent
, dir
->entries
[src
]))
884 free(dir
->entries
[src
++]);
886 /* compensate for the outer loop's loop control */
893 int cmd_clean(int argc
, const char **argv
, const char *prefix
)
896 int dry_run
= 0, remove_directories
= 0, quiet
= 0, ignored
= 0;
897 int ignored_only
= 0, config_set
= 0, errors
= 0, gone
= 1;
898 int rm_flags
= REMOVE_DIR_KEEP_NESTED_GIT
;
899 struct strbuf abs_path
= STRBUF_INIT
;
900 struct dir_struct dir
;
901 struct pathspec pathspec
;
902 struct strbuf buf
= STRBUF_INIT
;
903 struct string_list exclude_list
= STRING_LIST_INIT_NODUP
;
904 struct exclude_list
*el
;
905 struct string_list_item
*item
;
907 struct option options
[] = {
908 OPT__QUIET(&quiet
, N_("do not print names of files removed")),
909 OPT__DRY_RUN(&dry_run
, N_("dry run")),
910 OPT__FORCE(&force
, N_("force"), PARSE_OPT_NOCOMPLETE
),
911 OPT_BOOL('i', "interactive", &interactive
, N_("interactive cleaning")),
912 OPT_BOOL('d', NULL
, &remove_directories
,
913 N_("remove whole directories")),
914 { OPTION_CALLBACK
, 'e', "exclude", &exclude_list
, N_("pattern"),
915 N_("add <pattern> to ignore rules"), PARSE_OPT_NONEG
, exclude_cb
},
916 OPT_BOOL('x', NULL
, &ignored
, N_("remove ignored files, too")),
917 OPT_BOOL('X', NULL
, &ignored_only
,
918 N_("remove only ignored files")),
922 git_config(git_clean_config
, NULL
);
928 argc
= parse_options(argc
, argv
, prefix
, options
, builtin_clean_usage
,
931 memset(&dir
, 0, sizeof(dir
));
933 dir
.flags
|= DIR_SHOW_IGNORED
;
935 if (ignored
&& ignored_only
)
936 die(_("-x and -X cannot be used together"));
938 if (!interactive
&& !dry_run
&& !force
) {
940 die(_("clean.requireForce set to true and neither -i, -n, nor -f given; "
941 "refusing to clean"));
943 die(_("clean.requireForce defaults to true and neither -i, -n, nor -f given;"
944 " refusing to clean"));
950 dir
.flags
|= DIR_SHOW_OTHER_DIRECTORIES
;
952 if (remove_directories
)
953 dir
.flags
|= DIR_SHOW_IGNORED_TOO
| DIR_KEEP_UNTRACKED_CONTENTS
;
955 if (read_cache() < 0)
956 die(_("index file corrupt"));
959 setup_standard_excludes(&dir
);
961 el
= add_exclude_list(&dir
, EXC_CMDL
, "--exclude option");
962 for (i
= 0; i
< exclude_list
.nr
; i
++)
963 add_exclude(exclude_list
.items
[i
].string
, "", 0, el
, -(i
+1));
965 parse_pathspec(&pathspec
, 0,
969 fill_directory(&dir
, &the_index
, &pathspec
);
970 correct_untracked_entries(&dir
);
972 for (i
= 0; i
< dir
.nr
; i
++) {
973 struct dir_entry
*ent
= dir
.entries
[i
];
978 if (!cache_name_is_other(ent
->name
, ent
->len
))
982 matches
= dir_path_match(&the_index
, ent
, &pathspec
, 0, NULL
);
984 if (pathspec
.nr
&& !matches
)
987 if (lstat(ent
->name
, &st
))
988 die_errno("Cannot lstat '%s'", ent
->name
);
990 if (S_ISDIR(st
.st_mode
) && !remove_directories
&&
991 matches
!= MATCHED_EXACTLY
)
994 rel
= relative_path(ent
->name
, prefix
, &buf
);
995 string_list_append(&del_list
, rel
);
998 for (i
= 0; i
< dir
.nr
; i
++)
999 free(dir
.entries
[i
]);
1001 for (i
= 0; i
< dir
.ignored_nr
; i
++)
1002 free(dir
.ignored
[i
]);
1004 if (interactive
&& del_list
.nr
> 0)
1005 interactive_main_loop();
1007 for_each_string_list_item(item
, &del_list
) {
1011 strbuf_addstr(&abs_path
, prefix
);
1013 strbuf_addstr(&abs_path
, item
->string
);
1016 * we might have removed this as part of earlier
1017 * recursive directory removal, so lstat() here could
1020 if (lstat(abs_path
.buf
, &st
))
1023 if (S_ISDIR(st
.st_mode
)) {
1024 if (remove_dirs(&abs_path
, prefix
, rm_flags
, dry_run
, quiet
, &gone
))
1026 if (gone
&& !quiet
) {
1027 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
1028 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), qname
);
1031 res
= dry_run
? 0 : unlink(abs_path
.buf
);
1033 int saved_errno
= errno
;
1034 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
1035 errno
= saved_errno
;
1036 warning_errno(_(msg_warn_remove_failed
), qname
);
1038 } else if (!quiet
) {
1039 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
1040 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), qname
);
1043 strbuf_reset(&abs_path
);
1046 strbuf_release(&abs_path
);
1047 strbuf_release(&buf
);
1048 string_list_clear(&del_list
, 0);
1049 string_list_clear(&exclude_list
, 0);
1050 return (errors
!= 0);