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 /* inspect the color.ui config variable and others */
130 return git_color_default_config(var
, value
, cb
);
133 static const char *clean_get_color(enum color_clean ix
)
135 if (want_color(clean_use_color
))
136 return clean_colors
[ix
];
140 static void clean_print_color(enum color_clean ix
)
142 printf("%s", clean_get_color(ix
));
145 static int exclude_cb(const struct option
*opt
, const char *arg
, int unset
)
147 struct string_list
*exclude_list
= opt
->value
;
148 string_list_append(exclude_list
, arg
);
152 static int remove_dirs(struct strbuf
*path
, const char *prefix
, int force_flag
,
153 int dry_run
, int quiet
, int *dir_gone
)
156 struct strbuf quoted
= STRBUF_INIT
;
158 int res
= 0, ret
= 0, gone
= 1, original_len
= path
->len
, len
;
159 struct string_list dels
= STRING_LIST_INIT_DUP
;
163 if ((force_flag
& REMOVE_DIR_KEEP_NESTED_GIT
) && is_nonbare_repository_dir(path
)) {
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 int saved_errno
= errno
;
180 quote_path_relative(path
->buf
, prefix
, "ed
);
182 warning_errno(_(msg_warn_remove_failed
), quoted
.buf
);
189 strbuf_complete(path
, '/');
192 while ((e
= readdir(dir
)) != NULL
) {
194 if (is_dot_or_dotdot(e
->d_name
))
197 strbuf_setlen(path
, len
);
198 strbuf_addstr(path
, e
->d_name
);
199 if (lstat(path
->buf
, &st
))
201 else if (S_ISDIR(st
.st_mode
)) {
202 if (remove_dirs(path
, prefix
, force_flag
, dry_run
, quiet
, &gone
))
205 quote_path_relative(path
->buf
, prefix
, "ed
);
206 string_list_append(&dels
, quoted
.buf
);
211 res
= dry_run
? 0 : unlink(path
->buf
);
213 quote_path_relative(path
->buf
, prefix
, "ed
);
214 string_list_append(&dels
, quoted
.buf
);
216 int saved_errno
= errno
;
217 quote_path_relative(path
->buf
, prefix
, "ed
);
219 warning_errno(_(msg_warn_remove_failed
), quoted
.buf
);
226 /* path too long, stat fails, or non-directory still exists */
233 strbuf_setlen(path
, original_len
);
236 res
= dry_run
? 0 : rmdir(path
->buf
);
240 int saved_errno
= errno
;
241 quote_path_relative(path
->buf
, prefix
, "ed
);
243 warning_errno(_(msg_warn_remove_failed
), quoted
.buf
);
249 if (!*dir_gone
&& !quiet
) {
251 for (i
= 0; i
< dels
.nr
; i
++)
252 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), dels
.items
[i
].string
);
255 strbuf_release("ed
);
256 string_list_clear(&dels
, 0);
260 static void pretty_print_dels(void)
262 struct string_list list
= STRING_LIST_INIT_DUP
;
263 struct string_list_item
*item
;
264 struct strbuf buf
= STRBUF_INIT
;
266 struct column_options copts
;
268 for_each_string_list_item(item
, &del_list
) {
269 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
270 string_list_append(&list
, qname
);
274 * always enable column display, we only consult column.*
275 * about layout strategy and stuff
277 colopts
= (colopts
& ~COL_ENABLE_MASK
) | COL_ENABLED
;
278 memset(&copts
, 0, sizeof(copts
));
281 print_columns(&list
, colopts
, &copts
);
282 strbuf_release(&buf
);
283 string_list_clear(&list
, 0);
286 static void pretty_print_menus(struct string_list
*menu_list
)
288 unsigned int local_colopts
= 0;
289 struct column_options copts
;
291 local_colopts
= COL_ENABLED
| COL_ROW
;
292 memset(&copts
, 0, sizeof(copts
));
295 print_columns(menu_list
, local_colopts
, &copts
);
298 static void prompt_help_cmd(int singleton
)
300 clean_print_color(CLEAN_COLOR_HELP
);
303 "1 - select a numbered item\n"
304 "foo - select item based on unique prefix\n"
305 " - (empty) select nothing\n") :
307 "1 - select a single item\n"
308 "3-5 - select a range of items\n"
309 "2-3,6-9 - select multiple ranges\n"
310 "foo - select item based on unique prefix\n"
311 "-... - unselect specified items\n"
312 "* - choose all items\n"
313 " - (empty) finish selecting\n"));
314 clean_print_color(CLEAN_COLOR_RESET
);
318 * display menu stuff with number prefix and hotkey highlight
320 static void print_highlight_menu_stuff(struct menu_stuff
*stuff
, int **chosen
)
322 struct string_list menu_list
= STRING_LIST_INIT_DUP
;
323 struct strbuf menu
= STRBUF_INIT
;
324 struct menu_item
*menu_item
;
325 struct string_list_item
*string_list_item
;
328 switch (stuff
->type
) {
330 die("Bad type of menu_stuff when print menu");
331 case MENU_STUFF_TYPE_MENU_ITEM
:
332 menu_item
= (struct menu_item
*)stuff
->stuff
;
333 for (i
= 0; i
< stuff
->nr
; i
++, menu_item
++) {
337 p
= menu_item
->title
;
338 if ((*chosen
)[i
] < 0)
339 (*chosen
)[i
] = menu_item
->selected
? 1 : 0;
340 strbuf_addf(&menu
, "%s%2d: ", (*chosen
)[i
] ? "*" : " ", i
+1);
342 if (!highlighted
&& *p
== menu_item
->hotkey
) {
343 strbuf_addstr(&menu
, clean_get_color(CLEAN_COLOR_PROMPT
));
344 strbuf_addch(&menu
, *p
);
345 strbuf_addstr(&menu
, clean_get_color(CLEAN_COLOR_RESET
));
348 strbuf_addch(&menu
, *p
);
351 string_list_append(&menu_list
, menu
.buf
);
355 case MENU_STUFF_TYPE_STRING_LIST
:
357 for_each_string_list_item(string_list_item
, (struct string_list
*)stuff
->stuff
) {
358 if ((*chosen
)[i
] < 0)
360 strbuf_addf(&menu
, "%s%2d: %s",
361 (*chosen
)[i
] ? "*" : " ", i
+1, string_list_item
->string
);
362 string_list_append(&menu_list
, menu
.buf
);
369 pretty_print_menus(&menu_list
);
371 strbuf_release(&menu
);
372 string_list_clear(&menu_list
, 0);
375 static int find_unique(const char *choice
, struct menu_stuff
*menu_stuff
)
377 struct menu_item
*menu_item
;
378 struct string_list_item
*string_list_item
;
379 int i
, len
, found
= 0;
381 len
= strlen(choice
);
382 switch (menu_stuff
->type
) {
384 die("Bad type of menu_stuff when parse choice");
385 case MENU_STUFF_TYPE_MENU_ITEM
:
387 menu_item
= (struct menu_item
*)menu_stuff
->stuff
;
388 for (i
= 0; i
< menu_stuff
->nr
; i
++, menu_item
++) {
389 if (len
== 1 && *choice
== menu_item
->hotkey
) {
393 if (!strncasecmp(choice
, menu_item
->title
, len
)) {
396 /* continue for hotkey matching */
408 case MENU_STUFF_TYPE_STRING_LIST
:
409 string_list_item
= ((struct string_list
*)menu_stuff
->stuff
)->items
;
410 for (i
= 0; i
< menu_stuff
->nr
; i
++, string_list_item
++) {
411 if (!strncasecmp(choice
, string_list_item
->string
, len
)) {
426 * Parse user input, and return choice(s) for menu (menu_stuff).
429 * (for single choice)
430 * 1 - select a numbered item
431 * foo - select item based on menu title
432 * - (empty) select nothing
434 * (for multiple choice)
435 * 1 - select a single item
436 * 3-5 - select a range of items
437 * 2-3,6-9 - select multiple ranges
438 * foo - select item based on menu title
439 * -... - unselect specified items
440 * * - choose all items
441 * - (empty) finish selecting
443 * The parse result will be saved in array **chosen, and
444 * return number of total selections.
446 static int parse_choice(struct menu_stuff
*menu_stuff
,
451 struct strbuf
**choice_list
, **ptr
;
456 choice_list
= strbuf_split_max(&input
, '\n', 0);
463 choice_list
= strbuf_split_max(&input
, ' ', 0);
466 for (ptr
= choice_list
; *ptr
; ptr
++) {
469 int bottom
= 0, top
= 0;
470 int is_range
, is_number
;
476 /* Input that begins with '-'; unchoose */
477 if (*(*ptr
)->buf
== '-') {
479 strbuf_remove((*ptr
), 0, 1);
484 for (p
= (*ptr
)->buf
; *p
; p
++) {
494 } else if (!isdigit(*p
)) {
502 bottom
= atoi((*ptr
)->buf
);
504 } else if (is_range
) {
505 bottom
= atoi((*ptr
)->buf
);
506 /* a range can be specified like 5-7 or 5- */
507 if (!*(strchr((*ptr
)->buf
, '-') + 1))
508 top
= menu_stuff
->nr
;
510 top
= atoi(strchr((*ptr
)->buf
, '-') + 1);
511 } else if (!strcmp((*ptr
)->buf
, "*")) {
513 top
= menu_stuff
->nr
;
515 bottom
= find_unique((*ptr
)->buf
, menu_stuff
);
519 if (top
<= 0 || bottom
<= 0 || top
> menu_stuff
->nr
|| bottom
> top
||
520 (is_single
&& bottom
!= top
)) {
521 clean_print_color(CLEAN_COLOR_ERROR
);
522 printf(_("Huh (%s)?\n"), (*ptr
)->buf
);
523 clean_print_color(CLEAN_COLOR_RESET
);
527 for (i
= bottom
; i
<= top
; i
++)
528 (*chosen
)[i
-1] = choose
;
531 strbuf_list_free(choice_list
);
533 for (i
= 0; i
< menu_stuff
->nr
; i
++)
539 * Implement a git-add-interactive compatible UI, which is borrowed
540 * from git-add--interactive.perl.
544 * - Return an array of integers
545 * - , and it is up to you to free the allocated memory.
546 * - The array ends with EOF.
547 * - If user pressed CTRL-D (i.e. EOF), no selection returned.
549 static int *list_and_choose(struct menu_opts
*opts
, struct menu_stuff
*stuff
)
551 struct strbuf choice
= STRBUF_INIT
;
552 int *chosen
, *result
;
557 ALLOC_ARRAY(chosen
, stuff
->nr
);
558 /* set chosen as uninitialized */
559 for (i
= 0; i
< stuff
->nr
; i
++)
565 clean_get_color(CLEAN_COLOR_HEADER
),
567 clean_get_color(CLEAN_COLOR_RESET
));
570 /* chosen will be initialized by print_highlight_menu_stuff */
571 print_highlight_menu_stuff(stuff
, &chosen
);
573 if (opts
->flags
& MENU_OPTS_LIST_ONLY
)
578 clean_get_color(CLEAN_COLOR_PROMPT
),
580 opts
->flags
& MENU_OPTS_SINGLETON
? "> " : ">> ",
581 clean_get_color(CLEAN_COLOR_RESET
));
584 if (strbuf_getline_lf(&choice
, stdin
) != EOF
) {
585 strbuf_trim(&choice
);
591 /* help for prompt */
592 if (!strcmp(choice
.buf
, "?")) {
593 prompt_help_cmd(opts
->flags
& MENU_OPTS_SINGLETON
);
597 /* for a multiple-choice menu, press ENTER (empty) will return back */
598 if (!(opts
->flags
& MENU_OPTS_SINGLETON
) && !choice
.len
)
601 nr
= parse_choice(stuff
,
602 opts
->flags
& MENU_OPTS_SINGLETON
,
606 if (opts
->flags
& MENU_OPTS_SINGLETON
) {
609 } else if (opts
->flags
& MENU_OPTS_IMMEDIATE
) {
615 result
= xmalloc(sizeof(int));
621 * recalculate nr, if return back from menu directly with
622 * default selections.
625 for (i
= 0; i
< stuff
->nr
; i
++)
629 result
= xcalloc(st_add(nr
, 1), sizeof(int));
630 for (i
= 0; i
< stuff
->nr
&& j
< nr
; i
++) {
638 strbuf_release(&choice
);
642 static int clean_cmd(void)
644 return MENU_RETURN_NO_LOOP
;
647 static int filter_by_patterns_cmd(void)
649 struct dir_struct dir
;
650 struct strbuf confirm
= STRBUF_INIT
;
651 struct strbuf
**ignore_list
;
652 struct string_list_item
*item
;
653 struct exclude_list
*el
;
663 clean_print_color(CLEAN_COLOR_PROMPT
);
664 printf(_("Input ignore patterns>> "));
665 clean_print_color(CLEAN_COLOR_RESET
);
666 if (strbuf_getline_lf(&confirm
, stdin
) != EOF
)
667 strbuf_trim(&confirm
);
671 /* quit filter_by_pattern mode if press ENTER or Ctrl-D */
675 memset(&dir
, 0, sizeof(dir
));
676 el
= add_exclude_list(&dir
, EXC_CMDL
, "manual exclude");
677 ignore_list
= strbuf_split_max(&confirm
, ' ', 0);
679 for (i
= 0; ignore_list
[i
]; i
++) {
680 strbuf_trim(ignore_list
[i
]);
681 if (!ignore_list
[i
]->len
)
684 add_exclude(ignore_list
[i
]->buf
, "", 0, el
, -(i
+1));
688 for_each_string_list_item(item
, &del_list
) {
689 int dtype
= DT_UNKNOWN
;
691 if (is_excluded(&dir
, &the_index
, item
->string
, &dtype
)) {
692 *item
->string
= '\0';
698 string_list_remove_empty_items(&del_list
, 0);
700 clean_print_color(CLEAN_COLOR_ERROR
);
701 printf_ln(_("WARNING: Cannot find items matched by: %s"), confirm
.buf
);
702 clean_print_color(CLEAN_COLOR_RESET
);
705 strbuf_list_free(ignore_list
);
706 clear_directory(&dir
);
709 strbuf_release(&confirm
);
713 static int select_by_numbers_cmd(void)
715 struct menu_opts menu_opts
;
716 struct menu_stuff menu_stuff
;
717 struct string_list_item
*items
;
721 menu_opts
.header
= NULL
;
722 menu_opts
.prompt
= N_("Select items to delete");
725 menu_stuff
.type
= MENU_STUFF_TYPE_STRING_LIST
;
726 menu_stuff
.stuff
= &del_list
;
727 menu_stuff
.nr
= del_list
.nr
;
729 chosen
= list_and_choose(&menu_opts
, &menu_stuff
);
730 items
= del_list
.items
;
731 for (i
= 0, j
= 0; i
< del_list
.nr
; i
++) {
733 *(items
[i
].string
) = '\0';
734 } else if (i
== chosen
[j
]) {
735 /* delete selected item */
739 /* end of chosen (chosen[j] == EOF), won't delete */
740 *(items
[i
].string
) = '\0';
744 string_list_remove_empty_items(&del_list
, 0);
750 static int ask_each_cmd(void)
752 struct strbuf confirm
= STRBUF_INIT
;
753 struct strbuf buf
= STRBUF_INIT
;
754 struct string_list_item
*item
;
756 int changed
= 0, eof
= 0;
758 for_each_string_list_item(item
, &del_list
) {
759 /* Ctrl-D should stop removing files */
761 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
762 /* TRANSLATORS: Make sure to keep [y/N] as is */
763 printf(_("Remove %s [y/N]? "), qname
);
764 if (strbuf_getline_lf(&confirm
, stdin
) != EOF
) {
765 strbuf_trim(&confirm
);
771 if (!confirm
.len
|| strncasecmp(confirm
.buf
, "yes", confirm
.len
)) {
772 *item
->string
= '\0';
778 string_list_remove_empty_items(&del_list
, 0);
780 strbuf_release(&buf
);
781 strbuf_release(&confirm
);
782 return MENU_RETURN_NO_LOOP
;
785 static int quit_cmd(void)
787 string_list_clear(&del_list
, 0);
789 return MENU_RETURN_NO_LOOP
;
792 static int help_cmd(void)
794 clean_print_color(CLEAN_COLOR_HELP
);
796 "clean - start cleaning\n"
797 "filter by pattern - exclude items from deletion\n"
798 "select by numbers - select items to be deleted by numbers\n"
799 "ask each - confirm each deletion (like \"rm -i\")\n"
800 "quit - stop cleaning\n"
801 "help - this screen\n"
802 "? - help for prompt selection"
804 clean_print_color(CLEAN_COLOR_RESET
);
808 static void interactive_main_loop(void)
810 while (del_list
.nr
) {
811 struct menu_opts menu_opts
;
812 struct menu_stuff menu_stuff
;
813 struct menu_item menus
[] = {
814 {'c', "clean", 0, clean_cmd
},
815 {'f', "filter by pattern", 0, filter_by_patterns_cmd
},
816 {'s', "select by numbers", 0, select_by_numbers_cmd
},
817 {'a', "ask each", 0, ask_each_cmd
},
818 {'q', "quit", 0, quit_cmd
},
819 {'h', "help", 0, help_cmd
},
823 menu_opts
.header
= N_("*** Commands ***");
824 menu_opts
.prompt
= N_("What now");
825 menu_opts
.flags
= MENU_OPTS_SINGLETON
;
827 menu_stuff
.type
= MENU_STUFF_TYPE_MENU_ITEM
;
828 menu_stuff
.stuff
= menus
;
829 menu_stuff
.nr
= sizeof(menus
) / sizeof(struct menu_item
);
831 clean_print_color(CLEAN_COLOR_HEADER
);
832 printf_ln(Q_("Would remove the following item:",
833 "Would remove the following items:",
835 clean_print_color(CLEAN_COLOR_RESET
);
839 chosen
= list_and_choose(&menu_opts
, &menu_stuff
);
841 if (*chosen
!= EOF
) {
843 ret
= menus
[*chosen
].fn();
844 if (ret
!= MENU_RETURN_NO_LOOP
) {
845 FREE_AND_NULL(chosen
);
847 clean_print_color(CLEAN_COLOR_ERROR
);
848 printf_ln(_("No more files to clean, exiting."));
849 clean_print_color(CLEAN_COLOR_RESET
);
858 FREE_AND_NULL(chosen
);
863 static void correct_untracked_entries(struct dir_struct
*dir
)
867 for (src
= dst
= ign
= 0; src
< dir
->nr
; src
++) {
868 /* skip paths in ignored[] that cannot be inside entries[src] */
869 while (ign
< dir
->ignored_nr
&&
870 0 <= cmp_dir_entry(&dir
->entries
[src
], &dir
->ignored
[ign
]))
873 if (ign
< dir
->ignored_nr
&&
874 check_dir_entry_contains(dir
->entries
[src
], dir
->ignored
[ign
])) {
875 /* entries[src] contains an ignored path, so we drop it */
876 free(dir
->entries
[src
]);
878 struct dir_entry
*ent
= dir
->entries
[src
++];
880 /* entries[src] does not contain an ignored path, so we keep it */
881 dir
->entries
[dst
++] = ent
;
883 /* then discard paths in entries[] contained inside entries[src] */
884 while (src
< dir
->nr
&&
885 check_dir_entry_contains(ent
, dir
->entries
[src
]))
886 free(dir
->entries
[src
++]);
888 /* compensate for the outer loop's loop control */
895 int cmd_clean(int argc
, const char **argv
, const char *prefix
)
898 int dry_run
= 0, remove_directories
= 0, quiet
= 0, ignored
= 0;
899 int ignored_only
= 0, config_set
= 0, errors
= 0, gone
= 1;
900 int rm_flags
= REMOVE_DIR_KEEP_NESTED_GIT
;
901 struct strbuf abs_path
= STRBUF_INIT
;
902 struct dir_struct dir
;
903 struct pathspec pathspec
;
904 struct strbuf buf
= STRBUF_INIT
;
905 struct string_list exclude_list
= STRING_LIST_INIT_NODUP
;
906 struct exclude_list
*el
;
907 struct string_list_item
*item
;
909 struct option options
[] = {
910 OPT__QUIET(&quiet
, N_("do not print names of files removed")),
911 OPT__DRY_RUN(&dry_run
, N_("dry run")),
912 OPT__FORCE(&force
, N_("force"), PARSE_OPT_NOCOMPLETE
),
913 OPT_BOOL('i', "interactive", &interactive
, N_("interactive cleaning")),
914 OPT_BOOL('d', NULL
, &remove_directories
,
915 N_("remove whole directories")),
916 { OPTION_CALLBACK
, 'e', "exclude", &exclude_list
, N_("pattern"),
917 N_("add <pattern> to ignore rules"), PARSE_OPT_NONEG
, exclude_cb
},
918 OPT_BOOL('x', NULL
, &ignored
, N_("remove ignored files, too")),
919 OPT_BOOL('X', NULL
, &ignored_only
,
920 N_("remove only ignored files")),
924 git_config(git_clean_config
, NULL
);
930 argc
= parse_options(argc
, argv
, prefix
, options
, builtin_clean_usage
,
933 memset(&dir
, 0, sizeof(dir
));
935 dir
.flags
|= DIR_SHOW_IGNORED
;
937 if (ignored
&& ignored_only
)
938 die(_("-x and -X cannot be used together"));
940 if (!interactive
&& !dry_run
&& !force
) {
942 die(_("clean.requireForce set to true and neither -i, -n, nor -f given; "
943 "refusing to clean"));
945 die(_("clean.requireForce defaults to true and neither -i, -n, nor -f given;"
946 " refusing to clean"));
952 dir
.flags
|= DIR_SHOW_OTHER_DIRECTORIES
;
954 if (remove_directories
)
955 dir
.flags
|= DIR_SHOW_IGNORED_TOO
| DIR_KEEP_UNTRACKED_CONTENTS
;
957 if (read_cache() < 0)
958 die(_("index file corrupt"));
961 setup_standard_excludes(&dir
);
963 el
= add_exclude_list(&dir
, EXC_CMDL
, "--exclude option");
964 for (i
= 0; i
< exclude_list
.nr
; i
++)
965 add_exclude(exclude_list
.items
[i
].string
, "", 0, el
, -(i
+1));
967 parse_pathspec(&pathspec
, 0,
971 fill_directory(&dir
, &the_index
, &pathspec
);
972 correct_untracked_entries(&dir
);
974 for (i
= 0; i
< dir
.nr
; i
++) {
975 struct dir_entry
*ent
= dir
.entries
[i
];
980 if (!cache_name_is_other(ent
->name
, ent
->len
))
984 matches
= dir_path_match(ent
, &pathspec
, 0, NULL
);
986 if (pathspec
.nr
&& !matches
)
989 if (lstat(ent
->name
, &st
))
990 die_errno("Cannot lstat '%s'", ent
->name
);
992 if (S_ISDIR(st
.st_mode
) && !remove_directories
&&
993 matches
!= MATCHED_EXACTLY
)
996 rel
= relative_path(ent
->name
, prefix
, &buf
);
997 string_list_append(&del_list
, rel
);
1000 for (i
= 0; i
< dir
.nr
; i
++)
1001 free(dir
.entries
[i
]);
1003 for (i
= 0; i
< dir
.ignored_nr
; i
++)
1004 free(dir
.ignored
[i
]);
1006 if (interactive
&& del_list
.nr
> 0)
1007 interactive_main_loop();
1009 for_each_string_list_item(item
, &del_list
) {
1013 strbuf_addstr(&abs_path
, prefix
);
1015 strbuf_addstr(&abs_path
, item
->string
);
1018 * we might have removed this as part of earlier
1019 * recursive directory removal, so lstat() here could
1022 if (lstat(abs_path
.buf
, &st
))
1025 if (S_ISDIR(st
.st_mode
)) {
1026 if (remove_dirs(&abs_path
, prefix
, rm_flags
, dry_run
, quiet
, &gone
))
1028 if (gone
&& !quiet
) {
1029 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
1030 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), qname
);
1033 res
= dry_run
? 0 : unlink(abs_path
.buf
);
1035 int saved_errno
= errno
;
1036 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
1037 errno
= saved_errno
;
1038 warning_errno(_(msg_warn_remove_failed
), qname
);
1040 } else if (!quiet
) {
1041 qname
= quote_path_relative(item
->string
, NULL
, &buf
);
1042 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), qname
);
1045 strbuf_reset(&abs_path
);
1048 strbuf_release(&abs_path
);
1049 strbuf_release(&buf
);
1050 string_list_clear(&del_list
, 0);
1051 string_list_clear(&exclude_list
, 0);
1052 return (errors
!= 0);