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");
39 CLEAN_COLOR_RESET
= 0,
40 CLEAN_COLOR_PLAIN
= 1,
41 CLEAN_COLOR_PROMPT
= 2,
42 CLEAN_COLOR_HEADER
= 3,
47 static const char *color_interactive_slots
[] = {
48 [CLEAN_COLOR_ERROR
] = "error",
49 [CLEAN_COLOR_HEADER
] = "header",
50 [CLEAN_COLOR_HELP
] = "help",
51 [CLEAN_COLOR_PLAIN
] = "plain",
52 [CLEAN_COLOR_PROMPT
] = "prompt",
53 [CLEAN_COLOR_RESET
] = "reset",
56 static int clean_use_color
= -1;
57 static char clean_colors
[][COLOR_MAXLEN
] = {
58 [CLEAN_COLOR_ERROR
] = GIT_COLOR_BOLD_RED
,
59 [CLEAN_COLOR_HEADER
] = GIT_COLOR_BOLD
,
60 [CLEAN_COLOR_HELP
] = GIT_COLOR_BOLD_RED
,
61 [CLEAN_COLOR_PLAIN
] = GIT_COLOR_NORMAL
,
62 [CLEAN_COLOR_PROMPT
] = GIT_COLOR_BOLD_BLUE
,
63 [CLEAN_COLOR_RESET
] = GIT_COLOR_RESET
,
66 #define MENU_OPTS_SINGLETON 01
67 #define MENU_OPTS_IMMEDIATE 02
68 #define MENU_OPTS_LIST_ONLY 04
76 #define MENU_RETURN_NO_LOOP 10
85 enum menu_stuff_type
{
86 MENU_STUFF_TYPE_STRING_LIST
= 1,
87 MENU_STUFF_TYPE_MENU_ITEM
91 enum menu_stuff_type type
;
96 define_list_config_array(color_interactive_slots
);
98 static int git_clean_config(const char *var
, const char *value
, void *cb
)
100 const char *slot_name
;
102 if (starts_with(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 (skip_prefix(var
, "color.interactive.", &slot_name
)) {
112 int slot
= LOOKUP_CONFIG(color_interactive_slots
, slot_name
);
116 return config_error_nonbool(var
);
117 return color_parse(value
, clean_colors
[slot
]);
120 if (!strcmp(var
, "clean.requireforce")) {
121 force
= !git_config_bool(var
, value
);
125 /* inspect the color.ui config variable and others */
126 return git_color_default_config(var
, value
, cb
);
129 static const char *clean_get_color(enum color_clean ix
)
131 if (want_color(clean_use_color
))
132 return clean_colors
[ix
];
136 static void clean_print_color(enum color_clean ix
)
138 printf("%s", clean_get_color(ix
));
141 static int exclude_cb(const struct option
*opt
, const char *arg
, int unset
)
143 struct string_list
*exclude_list
= opt
->value
;
144 BUG_ON_OPT_NEG(unset
);
145 string_list_append(exclude_list
, arg
);
149 static int remove_dirs(struct strbuf
*path
, const char *prefix
, int force_flag
,
150 int dry_run
, int quiet
, int *dir_gone
)
153 struct strbuf quoted
= STRBUF_INIT
;
155 int res
= 0, ret
= 0, gone
= 1, original_len
= path
->len
, len
;
156 struct string_list dels
= STRING_LIST_INIT_DUP
;
160 if ((force_flag
& REMOVE_DIR_KEEP_NESTED_GIT
) && is_nonbare_repository_dir(path
)) {
162 quote_path_relative(path
->buf
, prefix
, "ed
);
163 printf(dry_run
? _(msg_would_skip_git_dir
) : _(msg_skip_git_dir
),
171 dir
= opendir(path
->buf
);
173 /* an empty dir could be removed even if it is unreadble */
174 res
= dry_run
? 0 : rmdir(path
->buf
);
176 int saved_errno
= errno
;
177 quote_path_relative(path
->buf
, prefix
, "ed
);
179 warning_errno(_(msg_warn_remove_failed
), quoted
.buf
);
186 strbuf_complete(path
, '/');
189 while ((e
= readdir(dir
)) != NULL
) {
191 if (is_dot_or_dotdot(e
->d_name
))
194 strbuf_setlen(path
, len
);
195 strbuf_addstr(path
, e
->d_name
);
196 if (lstat(path
->buf
, &st
))
198 else if (S_ISDIR(st
.st_mode
)) {
199 if (remove_dirs(path
, prefix
, force_flag
, dry_run
, quiet
, &gone
))
202 quote_path_relative(path
->buf
, prefix
, "ed
);
203 string_list_append(&dels
, quoted
.buf
);
208 res
= dry_run
? 0 : unlink(path
->buf
);
210 quote_path_relative(path
->buf
, prefix
, "ed
);
211 string_list_append(&dels
, quoted
.buf
);
213 int saved_errno
= errno
;
214 quote_path_relative(path
->buf
, prefix
, "ed
);
216 warning_errno(_(msg_warn_remove_failed
), quoted
.buf
);
223 /* path too long, stat fails, or non-directory still exists */
230 strbuf_setlen(path
, original_len
);
233 res
= dry_run
? 0 : rmdir(path
->buf
);
237 int saved_errno
= errno
;
238 quote_path_relative(path
->buf
, prefix
, "ed
);
240 warning_errno(_(msg_warn_remove_failed
), quoted
.buf
);
246 if (!*dir_gone
&& !quiet
) {
248 for (i
= 0; i
< dels
.nr
; i
++)
249 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), dels
.items
[i
].string
);
252 strbuf_release("ed
);
253 string_list_clear(&dels
, 0);
257 static void pretty_print_dels(void)
259 struct string_list list
= STRING_LIST_INIT_DUP
;
260 struct string_list_item
*item
;
261 struct strbuf buf
= STRBUF_INIT
;
263 struct column_options copts
;
265 for_each_string_list_item(item
, &del_list
) {
266 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
267 string_list_append(&list
, qname
);
271 * always enable column display, we only consult column.*
272 * about layout strategy and stuff
274 colopts
= (colopts
& ~COL_ENABLE_MASK
) | COL_ENABLED
;
275 memset(&copts
, 0, sizeof(copts
));
278 print_columns(&list
, colopts
, &copts
);
279 strbuf_release(&buf
);
280 string_list_clear(&list
, 0);
283 static void pretty_print_menus(struct string_list
*menu_list
)
285 unsigned int local_colopts
= 0;
286 struct column_options copts
;
288 local_colopts
= COL_ENABLED
| COL_ROW
;
289 memset(&copts
, 0, sizeof(copts
));
292 print_columns(menu_list
, local_colopts
, &copts
);
295 static void prompt_help_cmd(int singleton
)
297 clean_print_color(CLEAN_COLOR_HELP
);
300 "1 - select a numbered item\n"
301 "foo - select item based on unique prefix\n"
302 " - (empty) select nothing\n") :
304 "1 - select a single item\n"
305 "3-5 - select a range of items\n"
306 "2-3,6-9 - select multiple ranges\n"
307 "foo - select item based on unique prefix\n"
308 "-... - unselect specified items\n"
309 "* - choose all items\n"
310 " - (empty) finish selecting\n"));
311 clean_print_color(CLEAN_COLOR_RESET
);
315 * display menu stuff with number prefix and hotkey highlight
317 static void print_highlight_menu_stuff(struct menu_stuff
*stuff
, int **chosen
)
319 struct string_list menu_list
= STRING_LIST_INIT_DUP
;
320 struct strbuf menu
= STRBUF_INIT
;
321 struct menu_item
*menu_item
;
322 struct string_list_item
*string_list_item
;
325 switch (stuff
->type
) {
327 die("Bad type of menu_stuff when print menu");
328 case MENU_STUFF_TYPE_MENU_ITEM
:
329 menu_item
= (struct menu_item
*)stuff
->stuff
;
330 for (i
= 0; i
< stuff
->nr
; i
++, menu_item
++) {
334 p
= menu_item
->title
;
335 if ((*chosen
)[i
] < 0)
336 (*chosen
)[i
] = menu_item
->selected
? 1 : 0;
337 strbuf_addf(&menu
, "%s%2d: ", (*chosen
)[i
] ? "*" : " ", i
+1);
339 if (!highlighted
&& *p
== menu_item
->hotkey
) {
340 strbuf_addstr(&menu
, clean_get_color(CLEAN_COLOR_PROMPT
));
341 strbuf_addch(&menu
, *p
);
342 strbuf_addstr(&menu
, clean_get_color(CLEAN_COLOR_RESET
));
345 strbuf_addch(&menu
, *p
);
348 string_list_append(&menu_list
, menu
.buf
);
352 case MENU_STUFF_TYPE_STRING_LIST
:
354 for_each_string_list_item(string_list_item
, (struct string_list
*)stuff
->stuff
) {
355 if ((*chosen
)[i
] < 0)
357 strbuf_addf(&menu
, "%s%2d: %s",
358 (*chosen
)[i
] ? "*" : " ", i
+1, string_list_item
->string
);
359 string_list_append(&menu_list
, menu
.buf
);
366 pretty_print_menus(&menu_list
);
368 strbuf_release(&menu
);
369 string_list_clear(&menu_list
, 0);
372 static int find_unique(const char *choice
, struct menu_stuff
*menu_stuff
)
374 struct menu_item
*menu_item
;
375 struct string_list_item
*string_list_item
;
376 int i
, len
, found
= 0;
378 len
= strlen(choice
);
379 switch (menu_stuff
->type
) {
381 die("Bad type of menu_stuff when parse choice");
382 case MENU_STUFF_TYPE_MENU_ITEM
:
384 menu_item
= (struct menu_item
*)menu_stuff
->stuff
;
385 for (i
= 0; i
< menu_stuff
->nr
; i
++, menu_item
++) {
386 if (len
== 1 && *choice
== menu_item
->hotkey
) {
390 if (!strncasecmp(choice
, menu_item
->title
, len
)) {
393 /* continue for hotkey matching */
405 case MENU_STUFF_TYPE_STRING_LIST
:
406 string_list_item
= ((struct string_list
*)menu_stuff
->stuff
)->items
;
407 for (i
= 0; i
< menu_stuff
->nr
; i
++, string_list_item
++) {
408 if (!strncasecmp(choice
, string_list_item
->string
, len
)) {
423 * Parse user input, and return choice(s) for menu (menu_stuff).
426 * (for single choice)
427 * 1 - select a numbered item
428 * foo - select item based on menu title
429 * - (empty) select nothing
431 * (for multiple choice)
432 * 1 - select a single item
433 * 3-5 - select a range of items
434 * 2-3,6-9 - select multiple ranges
435 * foo - select item based on menu title
436 * -... - unselect specified items
437 * * - choose all items
438 * - (empty) finish selecting
440 * The parse result will be saved in array **chosen, and
441 * return number of total selections.
443 static int parse_choice(struct menu_stuff
*menu_stuff
,
448 struct strbuf
**choice_list
, **ptr
;
453 choice_list
= strbuf_split_max(&input
, '\n', 0);
460 choice_list
= strbuf_split_max(&input
, ' ', 0);
463 for (ptr
= choice_list
; *ptr
; ptr
++) {
466 int bottom
= 0, top
= 0;
467 int is_range
, is_number
;
473 /* Input that begins with '-'; unchoose */
474 if (*(*ptr
)->buf
== '-') {
476 strbuf_remove((*ptr
), 0, 1);
481 for (p
= (*ptr
)->buf
; *p
; p
++) {
491 } else if (!isdigit(*p
)) {
499 bottom
= atoi((*ptr
)->buf
);
501 } else if (is_range
) {
502 bottom
= atoi((*ptr
)->buf
);
503 /* a range can be specified like 5-7 or 5- */
504 if (!*(strchr((*ptr
)->buf
, '-') + 1))
505 top
= menu_stuff
->nr
;
507 top
= atoi(strchr((*ptr
)->buf
, '-') + 1);
508 } else if (!strcmp((*ptr
)->buf
, "*")) {
510 top
= menu_stuff
->nr
;
512 bottom
= find_unique((*ptr
)->buf
, menu_stuff
);
516 if (top
<= 0 || bottom
<= 0 || top
> menu_stuff
->nr
|| bottom
> top
||
517 (is_single
&& bottom
!= top
)) {
518 clean_print_color(CLEAN_COLOR_ERROR
);
519 printf(_("Huh (%s)?\n"), (*ptr
)->buf
);
520 clean_print_color(CLEAN_COLOR_RESET
);
524 for (i
= bottom
; i
<= top
; i
++)
525 (*chosen
)[i
-1] = choose
;
528 strbuf_list_free(choice_list
);
530 for (i
= 0; i
< menu_stuff
->nr
; i
++)
536 * Implement a git-add-interactive compatible UI, which is borrowed
537 * from git-add--interactive.perl.
541 * - Return an array of integers
542 * - , and it is up to you to free the allocated memory.
543 * - The array ends with EOF.
544 * - If user pressed CTRL-D (i.e. EOF), no selection returned.
546 static int *list_and_choose(struct menu_opts
*opts
, struct menu_stuff
*stuff
)
548 struct strbuf choice
= STRBUF_INIT
;
549 int *chosen
, *result
;
554 ALLOC_ARRAY(chosen
, stuff
->nr
);
555 /* set chosen as uninitialized */
556 for (i
= 0; i
< stuff
->nr
; i
++)
562 clean_get_color(CLEAN_COLOR_HEADER
),
564 clean_get_color(CLEAN_COLOR_RESET
));
567 /* chosen will be initialized by print_highlight_menu_stuff */
568 print_highlight_menu_stuff(stuff
, &chosen
);
570 if (opts
->flags
& MENU_OPTS_LIST_ONLY
)
575 clean_get_color(CLEAN_COLOR_PROMPT
),
577 opts
->flags
& MENU_OPTS_SINGLETON
? "> " : ">> ",
578 clean_get_color(CLEAN_COLOR_RESET
));
581 if (strbuf_getline_lf(&choice
, stdin
) != EOF
) {
582 strbuf_trim(&choice
);
588 /* help for prompt */
589 if (!strcmp(choice
.buf
, "?")) {
590 prompt_help_cmd(opts
->flags
& MENU_OPTS_SINGLETON
);
594 /* for a multiple-choice menu, press ENTER (empty) will return back */
595 if (!(opts
->flags
& MENU_OPTS_SINGLETON
) && !choice
.len
)
598 nr
= parse_choice(stuff
,
599 opts
->flags
& MENU_OPTS_SINGLETON
,
603 if (opts
->flags
& MENU_OPTS_SINGLETON
) {
606 } else if (opts
->flags
& MENU_OPTS_IMMEDIATE
) {
612 result
= xmalloc(sizeof(int));
618 * recalculate nr, if return back from menu directly with
619 * default selections.
622 for (i
= 0; i
< stuff
->nr
; i
++)
626 result
= xcalloc(st_add(nr
, 1), sizeof(int));
627 for (i
= 0; i
< stuff
->nr
&& j
< nr
; i
++) {
635 strbuf_release(&choice
);
639 static int clean_cmd(void)
641 return MENU_RETURN_NO_LOOP
;
644 static int filter_by_patterns_cmd(void)
646 struct dir_struct dir
;
647 struct strbuf confirm
= STRBUF_INIT
;
648 struct strbuf
**ignore_list
;
649 struct string_list_item
*item
;
650 struct exclude_list
*el
;
660 clean_print_color(CLEAN_COLOR_PROMPT
);
661 printf(_("Input ignore patterns>> "));
662 clean_print_color(CLEAN_COLOR_RESET
);
663 if (strbuf_getline_lf(&confirm
, stdin
) != EOF
)
664 strbuf_trim(&confirm
);
668 /* quit filter_by_pattern mode if press ENTER or Ctrl-D */
672 memset(&dir
, 0, sizeof(dir
));
673 el
= add_exclude_list(&dir
, EXC_CMDL
, "manual exclude");
674 ignore_list
= strbuf_split_max(&confirm
, ' ', 0);
676 for (i
= 0; ignore_list
[i
]; i
++) {
677 strbuf_trim(ignore_list
[i
]);
678 if (!ignore_list
[i
]->len
)
681 add_exclude(ignore_list
[i
]->buf
, "", 0, el
, -(i
+1));
685 for_each_string_list_item(item
, &del_list
) {
686 int dtype
= DT_UNKNOWN
;
688 if (is_excluded(&dir
, &the_index
, item
->string
, &dtype
)) {
689 *item
->string
= '\0';
695 string_list_remove_empty_items(&del_list
, 0);
697 clean_print_color(CLEAN_COLOR_ERROR
);
698 printf_ln(_("WARNING: Cannot find items matched by: %s"), confirm
.buf
);
699 clean_print_color(CLEAN_COLOR_RESET
);
702 strbuf_list_free(ignore_list
);
703 clear_directory(&dir
);
706 strbuf_release(&confirm
);
710 static int select_by_numbers_cmd(void)
712 struct menu_opts menu_opts
;
713 struct menu_stuff menu_stuff
;
714 struct string_list_item
*items
;
718 menu_opts
.header
= NULL
;
719 menu_opts
.prompt
= N_("Select items to delete");
722 menu_stuff
.type
= MENU_STUFF_TYPE_STRING_LIST
;
723 menu_stuff
.stuff
= &del_list
;
724 menu_stuff
.nr
= del_list
.nr
;
726 chosen
= list_and_choose(&menu_opts
, &menu_stuff
);
727 items
= del_list
.items
;
728 for (i
= 0, j
= 0; i
< del_list
.nr
; i
++) {
730 *(items
[i
].string
) = '\0';
731 } else if (i
== chosen
[j
]) {
732 /* delete selected item */
736 /* end of chosen (chosen[j] == EOF), won't delete */
737 *(items
[i
].string
) = '\0';
741 string_list_remove_empty_items(&del_list
, 0);
747 static int ask_each_cmd(void)
749 struct strbuf confirm
= STRBUF_INIT
;
750 struct strbuf buf
= STRBUF_INIT
;
751 struct string_list_item
*item
;
753 int changed
= 0, eof
= 0;
755 for_each_string_list_item(item
, &del_list
) {
756 /* Ctrl-D should stop removing files */
758 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
759 /* TRANSLATORS: Make sure to keep [y/N] as is */
760 printf(_("Remove %s [y/N]? "), qname
);
761 if (strbuf_getline_lf(&confirm
, stdin
) != EOF
) {
762 strbuf_trim(&confirm
);
768 if (!confirm
.len
|| strncasecmp(confirm
.buf
, "yes", confirm
.len
)) {
769 *item
->string
= '\0';
775 string_list_remove_empty_items(&del_list
, 0);
777 strbuf_release(&buf
);
778 strbuf_release(&confirm
);
779 return MENU_RETURN_NO_LOOP
;
782 static int quit_cmd(void)
784 string_list_clear(&del_list
, 0);
786 return MENU_RETURN_NO_LOOP
;
789 static int help_cmd(void)
791 clean_print_color(CLEAN_COLOR_HELP
);
793 "clean - start cleaning\n"
794 "filter by pattern - exclude items from deletion\n"
795 "select by numbers - select items to be deleted by numbers\n"
796 "ask each - confirm each deletion (like \"rm -i\")\n"
797 "quit - stop cleaning\n"
798 "help - this screen\n"
799 "? - help for prompt selection"
801 clean_print_color(CLEAN_COLOR_RESET
);
805 static void interactive_main_loop(void)
807 while (del_list
.nr
) {
808 struct menu_opts menu_opts
;
809 struct menu_stuff menu_stuff
;
810 struct menu_item menus
[] = {
811 {'c', "clean", 0, clean_cmd
},
812 {'f', "filter by pattern", 0, filter_by_patterns_cmd
},
813 {'s', "select by numbers", 0, select_by_numbers_cmd
},
814 {'a', "ask each", 0, ask_each_cmd
},
815 {'q', "quit", 0, quit_cmd
},
816 {'h', "help", 0, help_cmd
},
820 menu_opts
.header
= N_("*** Commands ***");
821 menu_opts
.prompt
= N_("What now");
822 menu_opts
.flags
= MENU_OPTS_SINGLETON
;
824 menu_stuff
.type
= MENU_STUFF_TYPE_MENU_ITEM
;
825 menu_stuff
.stuff
= menus
;
826 menu_stuff
.nr
= sizeof(menus
) / sizeof(struct menu_item
);
828 clean_print_color(CLEAN_COLOR_HEADER
);
829 printf_ln(Q_("Would remove the following item:",
830 "Would remove the following items:",
832 clean_print_color(CLEAN_COLOR_RESET
);
836 chosen
= list_and_choose(&menu_opts
, &menu_stuff
);
838 if (*chosen
!= EOF
) {
840 ret
= menus
[*chosen
].fn();
841 if (ret
!= MENU_RETURN_NO_LOOP
) {
842 FREE_AND_NULL(chosen
);
844 clean_print_color(CLEAN_COLOR_ERROR
);
845 printf_ln(_("No more files to clean, exiting."));
846 clean_print_color(CLEAN_COLOR_RESET
);
855 FREE_AND_NULL(chosen
);
860 static void correct_untracked_entries(struct dir_struct
*dir
)
864 for (src
= dst
= ign
= 0; src
< dir
->nr
; src
++) {
865 /* skip paths in ignored[] that cannot be inside entries[src] */
866 while (ign
< dir
->ignored_nr
&&
867 0 <= cmp_dir_entry(&dir
->entries
[src
], &dir
->ignored
[ign
]))
870 if (ign
< dir
->ignored_nr
&&
871 check_dir_entry_contains(dir
->entries
[src
], dir
->ignored
[ign
])) {
872 /* entries[src] contains an ignored path, so we drop it */
873 free(dir
->entries
[src
]);
875 struct dir_entry
*ent
= dir
->entries
[src
++];
877 /* entries[src] does not contain an ignored path, so we keep it */
878 dir
->entries
[dst
++] = ent
;
880 /* then discard paths in entries[] contained inside entries[src] */
881 while (src
< dir
->nr
&&
882 check_dir_entry_contains(ent
, dir
->entries
[src
]))
883 free(dir
->entries
[src
++]);
885 /* compensate for the outer loop's loop control */
892 int cmd_clean(int argc
, const char **argv
, const char *prefix
)
895 int dry_run
= 0, remove_directories
= 0, quiet
= 0, ignored
= 0;
896 int ignored_only
= 0, config_set
= 0, errors
= 0, gone
= 1;
897 int rm_flags
= REMOVE_DIR_KEEP_NESTED_GIT
;
898 struct strbuf abs_path
= STRBUF_INIT
;
899 struct dir_struct dir
;
900 struct pathspec pathspec
;
901 struct strbuf buf
= STRBUF_INIT
;
902 struct string_list exclude_list
= STRING_LIST_INIT_NODUP
;
903 struct exclude_list
*el
;
904 struct string_list_item
*item
;
906 struct option options
[] = {
907 OPT__QUIET(&quiet
, N_("do not print names of files removed")),
908 OPT__DRY_RUN(&dry_run
, N_("dry run")),
909 OPT__FORCE(&force
, N_("force"), PARSE_OPT_NOCOMPLETE
),
910 OPT_BOOL('i', "interactive", &interactive
, N_("interactive cleaning")),
911 OPT_BOOL('d', NULL
, &remove_directories
,
912 N_("remove whole directories")),
913 { OPTION_CALLBACK
, 'e', "exclude", &exclude_list
, N_("pattern"),
914 N_("add <pattern> to ignore rules"), PARSE_OPT_NONEG
, exclude_cb
},
915 OPT_BOOL('x', NULL
, &ignored
, N_("remove ignored files, too")),
916 OPT_BOOL('X', NULL
, &ignored_only
,
917 N_("remove only ignored files")),
921 git_config(git_clean_config
, NULL
);
927 argc
= parse_options(argc
, argv
, prefix
, options
, builtin_clean_usage
,
930 memset(&dir
, 0, sizeof(dir
));
932 dir
.flags
|= DIR_SHOW_IGNORED
;
934 if (ignored
&& ignored_only
)
935 die(_("-x and -X cannot be used together"));
937 if (!interactive
&& !dry_run
&& !force
) {
939 die(_("clean.requireForce set to true and neither -i, -n, nor -f given; "
940 "refusing to clean"));
942 die(_("clean.requireForce defaults to true and neither -i, -n, nor -f given;"
943 " refusing to clean"));
949 dir
.flags
|= DIR_SHOW_OTHER_DIRECTORIES
;
951 if (remove_directories
)
952 dir
.flags
|= DIR_SHOW_IGNORED_TOO
| DIR_KEEP_UNTRACKED_CONTENTS
;
954 if (read_cache() < 0)
955 die(_("index file corrupt"));
958 setup_standard_excludes(&dir
);
960 el
= add_exclude_list(&dir
, EXC_CMDL
, "--exclude option");
961 for (i
= 0; i
< exclude_list
.nr
; i
++)
962 add_exclude(exclude_list
.items
[i
].string
, "", 0, el
, -(i
+1));
964 parse_pathspec(&pathspec
, 0,
968 fill_directory(&dir
, &the_index
, &pathspec
);
969 correct_untracked_entries(&dir
);
971 for (i
= 0; i
< dir
.nr
; i
++) {
972 struct dir_entry
*ent
= dir
.entries
[i
];
977 if (!cache_name_is_other(ent
->name
, ent
->len
))
981 matches
= dir_path_match(&the_index
, ent
, &pathspec
, 0, NULL
);
983 if (pathspec
.nr
&& !matches
)
986 if (lstat(ent
->name
, &st
))
987 die_errno("Cannot lstat '%s'", ent
->name
);
989 if (S_ISDIR(st
.st_mode
) && !remove_directories
&&
990 matches
!= MATCHED_EXACTLY
)
993 rel
= relative_path(ent
->name
, prefix
, &buf
);
994 string_list_append(&del_list
, rel
);
997 for (i
= 0; i
< dir
.nr
; i
++)
998 free(dir
.entries
[i
]);
1000 for (i
= 0; i
< dir
.ignored_nr
; i
++)
1001 free(dir
.ignored
[i
]);
1003 if (interactive
&& del_list
.nr
> 0)
1004 interactive_main_loop();
1006 for_each_string_list_item(item
, &del_list
) {
1010 strbuf_addstr(&abs_path
, prefix
);
1012 strbuf_addstr(&abs_path
, item
->string
);
1015 * we might have removed this as part of earlier
1016 * recursive directory removal, so lstat() here could
1019 if (lstat(abs_path
.buf
, &st
))
1022 if (S_ISDIR(st
.st_mode
)) {
1023 if (remove_dirs(&abs_path
, prefix
, rm_flags
, dry_run
, quiet
, &gone
))
1025 if (gone
&& !quiet
) {
1026 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
1027 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), qname
);
1030 res
= dry_run
? 0 : unlink(abs_path
.buf
);
1032 int saved_errno
= errno
;
1033 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
1034 errno
= saved_errno
;
1035 warning_errno(_(msg_warn_remove_failed
), qname
);
1037 } else if (!quiet
) {
1038 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
1039 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), qname
);
1042 strbuf_reset(&abs_path
);
1045 strbuf_release(&abs_path
);
1046 strbuf_release(&buf
);
1047 string_list_clear(&del_list
, 0);
1048 string_list_clear(&exclude_list
, 0);
1049 return (errors
!= 0);