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"
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");
37 CLEAN_COLOR_RESET
= 0,
38 CLEAN_COLOR_PLAIN
= 1,
39 CLEAN_COLOR_PROMPT
= 2,
40 CLEAN_COLOR_HEADER
= 3,
45 static int clean_use_color
= -1;
46 static char clean_colors
[][COLOR_MAXLEN
] = {
47 [CLEAN_COLOR_ERROR
] = GIT_COLOR_BOLD_RED
,
48 [CLEAN_COLOR_HEADER
] = GIT_COLOR_BOLD
,
49 [CLEAN_COLOR_HELP
] = GIT_COLOR_BOLD_RED
,
50 [CLEAN_COLOR_PLAIN
] = GIT_COLOR_NORMAL
,
51 [CLEAN_COLOR_PROMPT
] = GIT_COLOR_BOLD_BLUE
,
52 [CLEAN_COLOR_RESET
] = GIT_COLOR_RESET
,
55 #define MENU_OPTS_SINGLETON 01
56 #define MENU_OPTS_IMMEDIATE 02
57 #define MENU_OPTS_LIST_ONLY 04
65 #define MENU_RETURN_NO_LOOP 10
74 enum menu_stuff_type
{
75 MENU_STUFF_TYPE_STRING_LIST
= 1,
76 MENU_STUFF_TYPE_MENU_ITEM
80 enum menu_stuff_type type
;
85 static int parse_clean_color_slot(const char *var
)
87 if (!strcasecmp(var
, "reset"))
88 return CLEAN_COLOR_RESET
;
89 if (!strcasecmp(var
, "plain"))
90 return CLEAN_COLOR_PLAIN
;
91 if (!strcasecmp(var
, "prompt"))
92 return CLEAN_COLOR_PROMPT
;
93 if (!strcasecmp(var
, "header"))
94 return CLEAN_COLOR_HEADER
;
95 if (!strcasecmp(var
, "help"))
96 return CLEAN_COLOR_HELP
;
97 if (!strcasecmp(var
, "error"))
98 return CLEAN_COLOR_ERROR
;
102 static int git_clean_config(const char *var
, const char *value
, void *cb
)
104 const char *slot_name
;
106 if (starts_with(var
, "column."))
107 return git_column_config(var
, value
, "clean", &colopts
);
109 /* honors the color.interactive* config variables which also
110 applied in git-add--interactive and git-stash */
111 if (!strcmp(var
, "color.interactive")) {
112 clean_use_color
= git_config_colorbool(var
, value
);
115 if (skip_prefix(var
, "color.interactive.", &slot_name
)) {
116 int slot
= parse_clean_color_slot(slot_name
);
120 return config_error_nonbool(var
);
121 return color_parse(value
, clean_colors
[slot
]);
124 if (!strcmp(var
, "clean.requireforce")) {
125 force
= !git_config_bool(var
, value
);
129 return git_default_config(var
, value
, cb
);
132 static const char *clean_get_color(enum color_clean ix
)
134 if (want_color(clean_use_color
))
135 return clean_colors
[ix
];
139 static void clean_print_color(enum color_clean ix
)
141 printf("%s", clean_get_color(ix
));
144 static int exclude_cb(const struct option
*opt
, const char *arg
, int unset
)
146 struct string_list
*exclude_list
= opt
->value
;
147 string_list_append(exclude_list
, arg
);
151 static int remove_dirs(struct strbuf
*path
, const char *prefix
, int force_flag
,
152 int dry_run
, int quiet
, int *dir_gone
)
155 struct strbuf quoted
= STRBUF_INIT
;
157 int res
= 0, ret
= 0, gone
= 1, original_len
= path
->len
, len
;
158 struct string_list dels
= STRING_LIST_INIT_DUP
;
162 if ((force_flag
& REMOVE_DIR_KEEP_NESTED_GIT
) && is_nonbare_repository_dir(path
)) {
164 quote_path_relative(path
->buf
, prefix
, "ed
);
165 printf(dry_run
? _(msg_would_skip_git_dir
) : _(msg_skip_git_dir
),
173 dir
= opendir(path
->buf
);
175 /* an empty dir could be removed even if it is unreadble */
176 res
= dry_run
? 0 : rmdir(path
->buf
);
178 int saved_errno
= errno
;
179 quote_path_relative(path
->buf
, prefix
, "ed
);
181 warning_errno(_(msg_warn_remove_failed
), quoted
.buf
);
188 strbuf_complete(path
, '/');
191 while ((e
= readdir(dir
)) != NULL
) {
193 if (is_dot_or_dotdot(e
->d_name
))
196 strbuf_setlen(path
, len
);
197 strbuf_addstr(path
, e
->d_name
);
198 if (lstat(path
->buf
, &st
))
200 else if (S_ISDIR(st
.st_mode
)) {
201 if (remove_dirs(path
, prefix
, force_flag
, dry_run
, quiet
, &gone
))
204 quote_path_relative(path
->buf
, prefix
, "ed
);
205 string_list_append(&dels
, quoted
.buf
);
210 res
= dry_run
? 0 : unlink(path
->buf
);
212 quote_path_relative(path
->buf
, prefix
, "ed
);
213 string_list_append(&dels
, quoted
.buf
);
215 int saved_errno
= errno
;
216 quote_path_relative(path
->buf
, prefix
, "ed
);
218 warning_errno(_(msg_warn_remove_failed
), quoted
.buf
);
225 /* path too long, stat fails, or non-directory still exists */
232 strbuf_setlen(path
, original_len
);
235 res
= dry_run
? 0 : rmdir(path
->buf
);
239 int saved_errno
= errno
;
240 quote_path_relative(path
->buf
, prefix
, "ed
);
242 warning_errno(_(msg_warn_remove_failed
), quoted
.buf
);
248 if (!*dir_gone
&& !quiet
) {
250 for (i
= 0; i
< dels
.nr
; i
++)
251 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), dels
.items
[i
].string
);
254 strbuf_release("ed
);
255 string_list_clear(&dels
, 0);
259 static void pretty_print_dels(void)
261 struct string_list list
= STRING_LIST_INIT_DUP
;
262 struct string_list_item
*item
;
263 struct strbuf buf
= STRBUF_INIT
;
265 struct column_options copts
;
267 for_each_string_list_item(item
, &del_list
) {
268 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
269 string_list_append(&list
, qname
);
273 * always enable column display, we only consult column.*
274 * about layout strategy and stuff
276 colopts
= (colopts
& ~COL_ENABLE_MASK
) | COL_ENABLED
;
277 memset(&copts
, 0, sizeof(copts
));
280 print_columns(&list
, colopts
, &copts
);
281 strbuf_release(&buf
);
282 string_list_clear(&list
, 0);
285 static void pretty_print_menus(struct string_list
*menu_list
)
287 unsigned int local_colopts
= 0;
288 struct column_options copts
;
290 local_colopts
= COL_ENABLED
| COL_ROW
;
291 memset(&copts
, 0, sizeof(copts
));
294 print_columns(menu_list
, local_colopts
, &copts
);
297 static void prompt_help_cmd(int singleton
)
299 clean_print_color(CLEAN_COLOR_HELP
);
302 "1 - select a numbered item\n"
303 "foo - select item based on unique prefix\n"
304 " - (empty) select nothing\n") :
306 "1 - select a single item\n"
307 "3-5 - select a range of items\n"
308 "2-3,6-9 - select multiple ranges\n"
309 "foo - select item based on unique prefix\n"
310 "-... - unselect specified items\n"
311 "* - choose all items\n"
312 " - (empty) finish selecting\n"));
313 clean_print_color(CLEAN_COLOR_RESET
);
317 * display menu stuff with number prefix and hotkey highlight
319 static void print_highlight_menu_stuff(struct menu_stuff
*stuff
, int **chosen
)
321 struct string_list menu_list
= STRING_LIST_INIT_DUP
;
322 struct strbuf menu
= STRBUF_INIT
;
323 struct menu_item
*menu_item
;
324 struct string_list_item
*string_list_item
;
327 switch (stuff
->type
) {
329 die("Bad type of menu_stuff when print menu");
330 case MENU_STUFF_TYPE_MENU_ITEM
:
331 menu_item
= (struct menu_item
*)stuff
->stuff
;
332 for (i
= 0; i
< stuff
->nr
; i
++, menu_item
++) {
336 p
= menu_item
->title
;
337 if ((*chosen
)[i
] < 0)
338 (*chosen
)[i
] = menu_item
->selected
? 1 : 0;
339 strbuf_addf(&menu
, "%s%2d: ", (*chosen
)[i
] ? "*" : " ", i
+1);
341 if (!highlighted
&& *p
== menu_item
->hotkey
) {
342 strbuf_addstr(&menu
, clean_get_color(CLEAN_COLOR_PROMPT
));
343 strbuf_addch(&menu
, *p
);
344 strbuf_addstr(&menu
, clean_get_color(CLEAN_COLOR_RESET
));
347 strbuf_addch(&menu
, *p
);
350 string_list_append(&menu_list
, menu
.buf
);
354 case MENU_STUFF_TYPE_STRING_LIST
:
356 for_each_string_list_item(string_list_item
, (struct string_list
*)stuff
->stuff
) {
357 if ((*chosen
)[i
] < 0)
359 strbuf_addf(&menu
, "%s%2d: %s",
360 (*chosen
)[i
] ? "*" : " ", i
+1, string_list_item
->string
);
361 string_list_append(&menu_list
, menu
.buf
);
368 pretty_print_menus(&menu_list
);
370 strbuf_release(&menu
);
371 string_list_clear(&menu_list
, 0);
374 static int find_unique(const char *choice
, struct menu_stuff
*menu_stuff
)
376 struct menu_item
*menu_item
;
377 struct string_list_item
*string_list_item
;
378 int i
, len
, found
= 0;
380 len
= strlen(choice
);
381 switch (menu_stuff
->type
) {
383 die("Bad type of menu_stuff when parse choice");
384 case MENU_STUFF_TYPE_MENU_ITEM
:
386 menu_item
= (struct menu_item
*)menu_stuff
->stuff
;
387 for (i
= 0; i
< menu_stuff
->nr
; i
++, menu_item
++) {
388 if (len
== 1 && *choice
== menu_item
->hotkey
) {
392 if (!strncasecmp(choice
, menu_item
->title
, len
)) {
395 /* continue for hotkey matching */
407 case MENU_STUFF_TYPE_STRING_LIST
:
408 string_list_item
= ((struct string_list
*)menu_stuff
->stuff
)->items
;
409 for (i
= 0; i
< menu_stuff
->nr
; i
++, string_list_item
++) {
410 if (!strncasecmp(choice
, string_list_item
->string
, len
)) {
425 * Parse user input, and return choice(s) for menu (menu_stuff).
428 * (for single choice)
429 * 1 - select a numbered item
430 * foo - select item based on menu title
431 * - (empty) select nothing
433 * (for multiple choice)
434 * 1 - select a single item
435 * 3-5 - select a range of items
436 * 2-3,6-9 - select multiple ranges
437 * foo - select item based on menu title
438 * -... - unselect specified items
439 * * - choose all items
440 * - (empty) finish selecting
442 * The parse result will be saved in array **chosen, and
443 * return number of total selections.
445 static int parse_choice(struct menu_stuff
*menu_stuff
,
450 struct strbuf
**choice_list
, **ptr
;
455 choice_list
= strbuf_split_max(&input
, '\n', 0);
462 choice_list
= strbuf_split_max(&input
, ' ', 0);
465 for (ptr
= choice_list
; *ptr
; ptr
++) {
468 int bottom
= 0, top
= 0;
469 int is_range
, is_number
;
475 /* Input that begins with '-'; unchoose */
476 if (*(*ptr
)->buf
== '-') {
478 strbuf_remove((*ptr
), 0, 1);
483 for (p
= (*ptr
)->buf
; *p
; p
++) {
493 } else if (!isdigit(*p
)) {
501 bottom
= atoi((*ptr
)->buf
);
503 } else if (is_range
) {
504 bottom
= atoi((*ptr
)->buf
);
505 /* a range can be specified like 5-7 or 5- */
506 if (!*(strchr((*ptr
)->buf
, '-') + 1))
507 top
= menu_stuff
->nr
;
509 top
= atoi(strchr((*ptr
)->buf
, '-') + 1);
510 } else if (!strcmp((*ptr
)->buf
, "*")) {
512 top
= menu_stuff
->nr
;
514 bottom
= find_unique((*ptr
)->buf
, menu_stuff
);
518 if (top
<= 0 || bottom
<= 0 || top
> menu_stuff
->nr
|| bottom
> top
||
519 (is_single
&& bottom
!= top
)) {
520 clean_print_color(CLEAN_COLOR_ERROR
);
521 printf(_("Huh (%s)?\n"), (*ptr
)->buf
);
522 clean_print_color(CLEAN_COLOR_RESET
);
526 for (i
= bottom
; i
<= top
; i
++)
527 (*chosen
)[i
-1] = choose
;
530 strbuf_list_free(choice_list
);
532 for (i
= 0; i
< menu_stuff
->nr
; i
++)
538 * Implement a git-add-interactive compatible UI, which is borrowed
539 * from git-add--interactive.perl.
543 * - Return an array of integers
544 * - , and it is up to you to free the allocated memory.
545 * - The array ends with EOF.
546 * - If user pressed CTRL-D (i.e. EOF), no selection returned.
548 static int *list_and_choose(struct menu_opts
*opts
, struct menu_stuff
*stuff
)
550 struct strbuf choice
= STRBUF_INIT
;
551 int *chosen
, *result
;
556 ALLOC_ARRAY(chosen
, stuff
->nr
);
557 /* set chosen as uninitialized */
558 for (i
= 0; i
< stuff
->nr
; i
++)
564 clean_get_color(CLEAN_COLOR_HEADER
),
566 clean_get_color(CLEAN_COLOR_RESET
));
569 /* chosen will be initialized by print_highlight_menu_stuff */
570 print_highlight_menu_stuff(stuff
, &chosen
);
572 if (opts
->flags
& MENU_OPTS_LIST_ONLY
)
577 clean_get_color(CLEAN_COLOR_PROMPT
),
579 opts
->flags
& MENU_OPTS_SINGLETON
? "> " : ">> ",
580 clean_get_color(CLEAN_COLOR_RESET
));
583 if (strbuf_getline_lf(&choice
, stdin
) != EOF
) {
584 strbuf_trim(&choice
);
590 /* help for prompt */
591 if (!strcmp(choice
.buf
, "?")) {
592 prompt_help_cmd(opts
->flags
& MENU_OPTS_SINGLETON
);
596 /* for a multiple-choice menu, press ENTER (empty) will return back */
597 if (!(opts
->flags
& MENU_OPTS_SINGLETON
) && !choice
.len
)
600 nr
= parse_choice(stuff
,
601 opts
->flags
& MENU_OPTS_SINGLETON
,
605 if (opts
->flags
& MENU_OPTS_SINGLETON
) {
608 } else if (opts
->flags
& MENU_OPTS_IMMEDIATE
) {
614 result
= xmalloc(sizeof(int));
620 * recalculate nr, if return back from menu directly with
621 * default selections.
624 for (i
= 0; i
< stuff
->nr
; i
++)
628 result
= xcalloc(st_add(nr
, 1), sizeof(int));
629 for (i
= 0; i
< stuff
->nr
&& j
< nr
; i
++) {
637 strbuf_release(&choice
);
641 static int clean_cmd(void)
643 return MENU_RETURN_NO_LOOP
;
646 static int filter_by_patterns_cmd(void)
648 struct dir_struct dir
;
649 struct strbuf confirm
= STRBUF_INIT
;
650 struct strbuf
**ignore_list
;
651 struct string_list_item
*item
;
652 struct exclude_list
*el
;
662 clean_print_color(CLEAN_COLOR_PROMPT
);
663 printf(_("Input ignore patterns>> "));
664 clean_print_color(CLEAN_COLOR_RESET
);
665 if (strbuf_getline_lf(&confirm
, stdin
) != EOF
)
666 strbuf_trim(&confirm
);
670 /* quit filter_by_pattern mode if press ENTER or Ctrl-D */
674 memset(&dir
, 0, sizeof(dir
));
675 el
= add_exclude_list(&dir
, EXC_CMDL
, "manual exclude");
676 ignore_list
= strbuf_split_max(&confirm
, ' ', 0);
678 for (i
= 0; ignore_list
[i
]; i
++) {
679 strbuf_trim(ignore_list
[i
]);
680 if (!ignore_list
[i
]->len
)
683 add_exclude(ignore_list
[i
]->buf
, "", 0, el
, -(i
+1));
687 for_each_string_list_item(item
, &del_list
) {
688 int dtype
= DT_UNKNOWN
;
690 if (is_excluded(&dir
, &the_index
, item
->string
, &dtype
)) {
691 *item
->string
= '\0';
697 string_list_remove_empty_items(&del_list
, 0);
699 clean_print_color(CLEAN_COLOR_ERROR
);
700 printf_ln(_("WARNING: Cannot find items matched by: %s"), confirm
.buf
);
701 clean_print_color(CLEAN_COLOR_RESET
);
704 strbuf_list_free(ignore_list
);
705 clear_directory(&dir
);
708 strbuf_release(&confirm
);
712 static int select_by_numbers_cmd(void)
714 struct menu_opts menu_opts
;
715 struct menu_stuff menu_stuff
;
716 struct string_list_item
*items
;
720 menu_opts
.header
= NULL
;
721 menu_opts
.prompt
= N_("Select items to delete");
724 menu_stuff
.type
= MENU_STUFF_TYPE_STRING_LIST
;
725 menu_stuff
.stuff
= &del_list
;
726 menu_stuff
.nr
= del_list
.nr
;
728 chosen
= list_and_choose(&menu_opts
, &menu_stuff
);
729 items
= del_list
.items
;
730 for (i
= 0, j
= 0; i
< del_list
.nr
; i
++) {
732 *(items
[i
].string
) = '\0';
733 } else if (i
== chosen
[j
]) {
734 /* delete selected item */
738 /* end of chosen (chosen[j] == EOF), won't delete */
739 *(items
[i
].string
) = '\0';
743 string_list_remove_empty_items(&del_list
, 0);
749 static int ask_each_cmd(void)
751 struct strbuf confirm
= STRBUF_INIT
;
752 struct strbuf buf
= STRBUF_INIT
;
753 struct string_list_item
*item
;
755 int changed
= 0, eof
= 0;
757 for_each_string_list_item(item
, &del_list
) {
758 /* Ctrl-D should stop removing files */
760 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
761 /* TRANSLATORS: Make sure to keep [y/N] as is */
762 printf(_("Remove %s [y/N]? "), qname
);
763 if (strbuf_getline_lf(&confirm
, stdin
) != EOF
) {
764 strbuf_trim(&confirm
);
770 if (!confirm
.len
|| strncasecmp(confirm
.buf
, "yes", confirm
.len
)) {
771 *item
->string
= '\0';
777 string_list_remove_empty_items(&del_list
, 0);
779 strbuf_release(&buf
);
780 strbuf_release(&confirm
);
781 return MENU_RETURN_NO_LOOP
;
784 static int quit_cmd(void)
786 string_list_clear(&del_list
, 0);
788 return MENU_RETURN_NO_LOOP
;
791 static int help_cmd(void)
793 clean_print_color(CLEAN_COLOR_HELP
);
795 "clean - start cleaning\n"
796 "filter by pattern - exclude items from deletion\n"
797 "select by numbers - select items to be deleted by numbers\n"
798 "ask each - confirm each deletion (like \"rm -i\")\n"
799 "quit - stop cleaning\n"
800 "help - this screen\n"
801 "? - help for prompt selection"
803 clean_print_color(CLEAN_COLOR_RESET
);
807 static void interactive_main_loop(void)
809 while (del_list
.nr
) {
810 struct menu_opts menu_opts
;
811 struct menu_stuff menu_stuff
;
812 struct menu_item menus
[] = {
813 {'c', "clean", 0, clean_cmd
},
814 {'f', "filter by pattern", 0, filter_by_patterns_cmd
},
815 {'s', "select by numbers", 0, select_by_numbers_cmd
},
816 {'a', "ask each", 0, ask_each_cmd
},
817 {'q', "quit", 0, quit_cmd
},
818 {'h', "help", 0, help_cmd
},
822 menu_opts
.header
= N_("*** Commands ***");
823 menu_opts
.prompt
= N_("What now");
824 menu_opts
.flags
= MENU_OPTS_SINGLETON
;
826 menu_stuff
.type
= MENU_STUFF_TYPE_MENU_ITEM
;
827 menu_stuff
.stuff
= menus
;
828 menu_stuff
.nr
= sizeof(menus
) / sizeof(struct menu_item
);
830 clean_print_color(CLEAN_COLOR_HEADER
);
831 printf_ln(Q_("Would remove the following item:",
832 "Would remove the following items:",
834 clean_print_color(CLEAN_COLOR_RESET
);
838 chosen
= list_and_choose(&menu_opts
, &menu_stuff
);
840 if (*chosen
!= EOF
) {
842 ret
= menus
[*chosen
].fn();
843 if (ret
!= MENU_RETURN_NO_LOOP
) {
844 FREE_AND_NULL(chosen
);
846 clean_print_color(CLEAN_COLOR_ERROR
);
847 printf_ln(_("No more files to clean, exiting."));
848 clean_print_color(CLEAN_COLOR_RESET
);
857 FREE_AND_NULL(chosen
);
862 static void correct_untracked_entries(struct dir_struct
*dir
)
866 for (src
= dst
= ign
= 0; src
< dir
->nr
; src
++) {
867 /* skip paths in ignored[] that cannot be inside entries[src] */
868 while (ign
< dir
->ignored_nr
&&
869 0 <= cmp_dir_entry(&dir
->entries
[src
], &dir
->ignored
[ign
]))
872 if (ign
< dir
->ignored_nr
&&
873 check_dir_entry_contains(dir
->entries
[src
], dir
->ignored
[ign
])) {
874 /* entries[src] contains an ignored path, so we drop it */
875 free(dir
->entries
[src
]);
877 struct dir_entry
*ent
= dir
->entries
[src
++];
879 /* entries[src] does not contain an ignored path, so we keep it */
880 dir
->entries
[dst
++] = ent
;
882 /* then discard paths in entries[] contained inside entries[src] */
883 while (src
< dir
->nr
&&
884 check_dir_entry_contains(ent
, dir
->entries
[src
]))
885 free(dir
->entries
[src
++]);
887 /* compensate for the outer loop's loop control */
894 int cmd_clean(int argc
, const char **argv
, const char *prefix
)
897 int dry_run
= 0, remove_directories
= 0, quiet
= 0, ignored
= 0;
898 int ignored_only
= 0, config_set
= 0, errors
= 0, gone
= 1;
899 int rm_flags
= REMOVE_DIR_KEEP_NESTED_GIT
;
900 struct strbuf abs_path
= STRBUF_INIT
;
901 struct dir_struct dir
;
902 struct pathspec pathspec
;
903 struct strbuf buf
= STRBUF_INIT
;
904 struct string_list exclude_list
= STRING_LIST_INIT_NODUP
;
905 struct exclude_list
*el
;
906 struct string_list_item
*item
;
908 struct option options
[] = {
909 OPT__QUIET(&quiet
, N_("do not print names of files removed")),
910 OPT__DRY_RUN(&dry_run
, N_("dry run")),
911 OPT__FORCE(&force
, N_("force")),
912 OPT_BOOL('i', "interactive", &interactive
, N_("interactive cleaning")),
913 OPT_BOOL('d', NULL
, &remove_directories
,
914 N_("remove whole directories")),
915 { OPTION_CALLBACK
, 'e', "exclude", &exclude_list
, N_("pattern"),
916 N_("add <pattern> to ignore rules"), PARSE_OPT_NONEG
, exclude_cb
},
917 OPT_BOOL('x', NULL
, &ignored
, N_("remove ignored files, too")),
918 OPT_BOOL('X', NULL
, &ignored_only
,
919 N_("remove only ignored files")),
923 git_config(git_clean_config
, NULL
);
929 argc
= parse_options(argc
, argv
, prefix
, options
, builtin_clean_usage
,
932 memset(&dir
, 0, sizeof(dir
));
934 dir
.flags
|= DIR_SHOW_IGNORED
;
936 if (ignored
&& ignored_only
)
937 die(_("-x and -X cannot be used together"));
939 if (!interactive
&& !dry_run
&& !force
) {
941 die(_("clean.requireForce set to true and neither -i, -n, nor -f given; "
942 "refusing to clean"));
944 die(_("clean.requireForce defaults to true and neither -i, -n, nor -f given;"
945 " refusing to clean"));
951 dir
.flags
|= DIR_SHOW_OTHER_DIRECTORIES
;
953 if (remove_directories
)
954 dir
.flags
|= DIR_SHOW_IGNORED_TOO
| DIR_KEEP_UNTRACKED_CONTENTS
;
956 if (read_cache() < 0)
957 die(_("index file corrupt"));
960 setup_standard_excludes(&dir
);
962 el
= add_exclude_list(&dir
, EXC_CMDL
, "--exclude option");
963 for (i
= 0; i
< exclude_list
.nr
; i
++)
964 add_exclude(exclude_list
.items
[i
].string
, "", 0, el
, -(i
+1));
966 parse_pathspec(&pathspec
, 0,
970 fill_directory(&dir
, &the_index
, &pathspec
);
971 correct_untracked_entries(&dir
);
973 for (i
= 0; i
< dir
.nr
; i
++) {
974 struct dir_entry
*ent
= dir
.entries
[i
];
979 if (!cache_name_is_other(ent
->name
, ent
->len
))
983 matches
= dir_path_match(ent
, &pathspec
, 0, NULL
);
985 if (pathspec
.nr
&& !matches
)
988 if (lstat(ent
->name
, &st
))
989 die_errno("Cannot lstat '%s'", ent
->name
);
991 if (S_ISDIR(st
.st_mode
) && !remove_directories
&&
992 matches
!= MATCHED_EXACTLY
)
995 rel
= relative_path(ent
->name
, prefix
, &buf
);
996 string_list_append(&del_list
, rel
);
999 for (i
= 0; i
< dir
.nr
; i
++)
1000 free(dir
.entries
[i
]);
1002 for (i
= 0; i
< dir
.ignored_nr
; i
++)
1003 free(dir
.ignored
[i
]);
1005 if (interactive
&& del_list
.nr
> 0)
1006 interactive_main_loop();
1008 for_each_string_list_item(item
, &del_list
) {
1012 strbuf_addstr(&abs_path
, prefix
);
1014 strbuf_addstr(&abs_path
, item
->string
);
1017 * we might have removed this as part of earlier
1018 * recursive directory removal, so lstat() here could
1021 if (lstat(abs_path
.buf
, &st
))
1024 if (S_ISDIR(st
.st_mode
)) {
1025 if (remove_dirs(&abs_path
, prefix
, rm_flags
, dry_run
, quiet
, &gone
))
1027 if (gone
&& !quiet
) {
1028 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
1029 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), qname
);
1032 res
= dry_run
? 0 : unlink(abs_path
.buf
);
1034 int saved_errno
= errno
;
1035 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
1036 errno
= saved_errno
;
1037 warning_errno(_(msg_warn_remove_failed
), qname
);
1039 } else if (!quiet
) {
1040 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
1041 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), qname
);
1044 strbuf_reset(&abs_path
);
1047 strbuf_release(&abs_path
);
1048 strbuf_release(&buf
);
1049 string_list_clear(&del_list
, 0);
1050 string_list_clear(&exclude_list
, 0);
1051 return (errors
!= 0);