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"
23 static int force
= -1; /* unset */
24 static int interactive
;
25 static struct string_list del_list
= STRING_LIST_INIT_DUP
;
26 static unsigned int colopts
;
28 static const char *const builtin_clean_usage
[] = {
29 N_("git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>..."),
33 static const char *msg_remove
= N_("Removing %s\n");
34 static const char *msg_would_remove
= N_("Would remove %s\n");
35 static const char *msg_skip_git_dir
= N_("Skipping repository %s\n");
36 static const char *msg_would_skip_git_dir
= N_("Would skip repository %s\n");
37 static const char *msg_warn_remove_failed
= N_("failed to remove %s");
38 static const char *msg_warn_lstat_failed
= N_("could not lstat %s\n");
41 CLEAN_COLOR_RESET
= 0,
42 CLEAN_COLOR_PLAIN
= 1,
43 CLEAN_COLOR_PROMPT
= 2,
44 CLEAN_COLOR_HEADER
= 3,
49 static const char *color_interactive_slots
[] = {
50 [CLEAN_COLOR_ERROR
] = "error",
51 [CLEAN_COLOR_HEADER
] = "header",
52 [CLEAN_COLOR_HELP
] = "help",
53 [CLEAN_COLOR_PLAIN
] = "plain",
54 [CLEAN_COLOR_PROMPT
] = "prompt",
55 [CLEAN_COLOR_RESET
] = "reset",
58 static int clean_use_color
= -1;
59 static char clean_colors
[][COLOR_MAXLEN
] = {
60 [CLEAN_COLOR_ERROR
] = GIT_COLOR_BOLD_RED
,
61 [CLEAN_COLOR_HEADER
] = GIT_COLOR_BOLD
,
62 [CLEAN_COLOR_HELP
] = GIT_COLOR_BOLD_RED
,
63 [CLEAN_COLOR_PLAIN
] = GIT_COLOR_NORMAL
,
64 [CLEAN_COLOR_PROMPT
] = GIT_COLOR_BOLD_BLUE
,
65 [CLEAN_COLOR_RESET
] = GIT_COLOR_RESET
,
68 #define MENU_OPTS_SINGLETON 01
69 #define MENU_OPTS_IMMEDIATE 02
70 #define MENU_OPTS_LIST_ONLY 04
78 #define MENU_RETURN_NO_LOOP 10
87 enum menu_stuff_type
{
88 MENU_STUFF_TYPE_STRING_LIST
= 1,
89 MENU_STUFF_TYPE_MENU_ITEM
93 enum menu_stuff_type type
;
98 define_list_config_array(color_interactive_slots
);
100 static int git_clean_config(const char *var
, const char *value
, void *cb
)
102 const char *slot_name
;
104 if (starts_with(var
, "column."))
105 return git_column_config(var
, value
, "clean", &colopts
);
107 /* honors the color.interactive* config variables which also
108 applied in git-add--interactive and git-stash */
109 if (!strcmp(var
, "color.interactive")) {
110 clean_use_color
= git_config_colorbool(var
, value
);
113 if (skip_prefix(var
, "color.interactive.", &slot_name
)) {
114 int slot
= LOOKUP_CONFIG(color_interactive_slots
, slot_name
);
118 return config_error_nonbool(var
);
119 return color_parse(value
, clean_colors
[slot
]);
122 if (!strcmp(var
, "clean.requireforce")) {
123 force
= !git_config_bool(var
, value
);
127 /* inspect the color.ui config variable and others */
128 return git_color_default_config(var
, value
, cb
);
131 static const char *clean_get_color(enum color_clean ix
)
133 if (want_color(clean_use_color
))
134 return clean_colors
[ix
];
138 static void clean_print_color(enum color_clean ix
)
140 printf("%s", clean_get_color(ix
));
143 static int exclude_cb(const struct option
*opt
, const char *arg
, int unset
)
145 struct string_list
*exclude_list
= opt
->value
;
146 BUG_ON_OPT_NEG(unset
);
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
) &&
163 is_nonbare_repository_dir(path
)) {
165 quote_path(path
->buf
, prefix
, "ed
, 0);
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(path
->buf
, prefix
, "ed
, 0);
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
))
200 warning_errno(_(msg_warn_lstat_failed
), path
->buf
);
201 else if (S_ISDIR(st
.st_mode
)) {
202 if (remove_dirs(path
, prefix
, force_flag
, dry_run
, quiet
, &gone
))
205 quote_path(path
->buf
, prefix
, "ed
, 0);
206 string_list_append(&dels
, quoted
.buf
);
211 res
= dry_run
? 0 : unlink(path
->buf
);
213 quote_path(path
->buf
, prefix
, "ed
, 0);
214 string_list_append(&dels
, quoted
.buf
);
216 int saved_errno
= errno
;
217 quote_path(path
->buf
, prefix
, "ed
, 0);
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(path
->buf
, prefix
, "ed
, 0);
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(item
->string
, NULL
, &buf
, 0);
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
)) {
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 (git_read_line_interactively(&choice
) == EOF
) {
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 pattern_list
*pl
;
660 clean_print_color(CLEAN_COLOR_PROMPT
);
661 printf(_("Input ignore patterns>> "));
662 clean_print_color(CLEAN_COLOR_RESET
);
663 if (git_read_line_interactively(&confirm
) == EOF
)
666 /* quit filter_by_pattern mode if press ENTER or Ctrl-D */
671 pl
= add_pattern_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_pattern(ignore_list
[i
]->buf
, "", 0, pl
, -(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
);
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(item
->string
, NULL
, &buf
, 0);
757 /* TRANSLATORS: Make sure to keep [y/N] as is */
758 printf(_("Remove %s [y/N]? "), qname
);
759 if (git_read_line_interactively(&confirm
) == EOF
) {
764 if (!confirm
.len
|| strncasecmp(confirm
.buf
, "yes", confirm
.len
)) {
765 *item
->string
= '\0';
771 string_list_remove_empty_items(&del_list
, 0);
773 strbuf_release(&buf
);
774 strbuf_release(&confirm
);
775 return MENU_RETURN_NO_LOOP
;
778 static int quit_cmd(void)
780 string_list_clear(&del_list
, 0);
782 return MENU_RETURN_NO_LOOP
;
785 static int help_cmd(void)
787 clean_print_color(CLEAN_COLOR_HELP
);
789 "clean - start cleaning\n"
790 "filter by pattern - exclude items from deletion\n"
791 "select by numbers - select items to be deleted by numbers\n"
792 "ask each - confirm each deletion (like \"rm -i\")\n"
793 "quit - stop cleaning\n"
794 "help - this screen\n"
795 "? - help for prompt selection"
797 clean_print_color(CLEAN_COLOR_RESET
);
801 static void interactive_main_loop(void)
803 while (del_list
.nr
) {
804 struct menu_opts menu_opts
;
805 struct menu_stuff menu_stuff
;
806 struct menu_item menus
[] = {
807 {'c', "clean", 0, clean_cmd
},
808 {'f', "filter by pattern", 0, filter_by_patterns_cmd
},
809 {'s', "select by numbers", 0, select_by_numbers_cmd
},
810 {'a', "ask each", 0, ask_each_cmd
},
811 {'q', "quit", 0, quit_cmd
},
812 {'h', "help", 0, help_cmd
},
816 menu_opts
.header
= N_("*** Commands ***");
817 menu_opts
.prompt
= N_("What now");
818 menu_opts
.flags
= MENU_OPTS_SINGLETON
;
820 menu_stuff
.type
= MENU_STUFF_TYPE_MENU_ITEM
;
821 menu_stuff
.stuff
= menus
;
822 menu_stuff
.nr
= sizeof(menus
) / sizeof(struct menu_item
);
824 clean_print_color(CLEAN_COLOR_HEADER
);
825 printf_ln(Q_("Would remove the following item:",
826 "Would remove the following items:",
828 clean_print_color(CLEAN_COLOR_RESET
);
832 chosen
= list_and_choose(&menu_opts
, &menu_stuff
);
834 if (*chosen
!= EOF
) {
836 ret
= menus
[*chosen
].fn();
837 if (ret
!= MENU_RETURN_NO_LOOP
) {
838 FREE_AND_NULL(chosen
);
840 clean_print_color(CLEAN_COLOR_ERROR
);
841 printf_ln(_("No more files to clean, exiting."));
842 clean_print_color(CLEAN_COLOR_RESET
);
851 FREE_AND_NULL(chosen
);
856 static void correct_untracked_entries(struct dir_struct
*dir
)
860 for (src
= dst
= ign
= 0; src
< dir
->nr
; src
++) {
861 /* skip paths in ignored[] that cannot be inside entries[src] */
862 while (ign
< dir
->ignored_nr
&&
863 0 <= cmp_dir_entry(&dir
->entries
[src
], &dir
->ignored
[ign
]))
866 if (ign
< dir
->ignored_nr
&&
867 check_dir_entry_contains(dir
->entries
[src
], dir
->ignored
[ign
])) {
868 /* entries[src] contains an ignored path, so we drop it */
869 free(dir
->entries
[src
]);
871 struct dir_entry
*ent
= dir
->entries
[src
++];
873 /* entries[src] does not contain an ignored path, so we keep it */
874 dir
->entries
[dst
++] = ent
;
876 /* then discard paths in entries[] contained inside entries[src] */
877 while (src
< dir
->nr
&&
878 check_dir_entry_contains(ent
, dir
->entries
[src
]))
879 free(dir
->entries
[src
++]);
881 /* compensate for the outer loop's loop control */
888 int cmd_clean(int argc
, const char **argv
, const char *prefix
)
891 int dry_run
= 0, remove_directories
= 0, quiet
= 0, ignored
= 0;
892 int ignored_only
= 0, config_set
= 0, errors
= 0, gone
= 1;
893 int rm_flags
= REMOVE_DIR_KEEP_NESTED_GIT
;
894 struct strbuf abs_path
= STRBUF_INIT
;
895 struct dir_struct dir
;
896 struct pathspec pathspec
;
897 struct strbuf buf
= STRBUF_INIT
;
898 struct string_list exclude_list
= STRING_LIST_INIT_NODUP
;
899 struct pattern_list
*pl
;
900 struct string_list_item
*item
;
902 struct option options
[] = {
903 OPT__QUIET(&quiet
, N_("do not print names of files removed")),
904 OPT__DRY_RUN(&dry_run
, N_("dry run")),
905 OPT__FORCE(&force
, N_("force"), PARSE_OPT_NOCOMPLETE
),
906 OPT_BOOL('i', "interactive", &interactive
, N_("interactive cleaning")),
907 OPT_BOOL('d', NULL
, &remove_directories
,
908 N_("remove whole directories")),
909 OPT_CALLBACK_F('e', "exclude", &exclude_list
, N_("pattern"),
910 N_("add <pattern> to ignore rules"), PARSE_OPT_NONEG
, exclude_cb
),
911 OPT_BOOL('x', NULL
, &ignored
, N_("remove ignored files, too")),
912 OPT_BOOL('X', NULL
, &ignored_only
,
913 N_("remove only ignored files")),
917 git_config(git_clean_config
, NULL
);
923 argc
= parse_options(argc
, argv
, prefix
, options
, builtin_clean_usage
,
927 if (!interactive
&& !dry_run
&& !force
) {
929 die(_("clean.requireForce set to true and neither -i, -n, nor -f given; "
930 "refusing to clean"));
932 die(_("clean.requireForce defaults to true and neither -i, -n, nor -f given;"
933 " refusing to clean"));
939 dir
.flags
|= DIR_SKIP_NESTED_GIT
;
941 dir
.flags
|= DIR_SHOW_OTHER_DIRECTORIES
;
943 if (ignored
&& ignored_only
)
944 die(_("-x and -X cannot be used together"));
946 setup_standard_excludes(&dir
);
948 dir
.flags
|= DIR_SHOW_IGNORED
;
952 * Remaining args implies pathspecs specified, and we should
953 * recurse within those.
955 remove_directories
= 1;
958 if (remove_directories
&& !ignored_only
) {
960 * We need to know about ignored files too:
962 * If (ignored), then we will delete ignored files as well.
964 * If (!ignored), then even though we not are doing
965 * anything with ignored files, we need to know about them
966 * so that we can avoid deleting a directory of untracked
967 * files that also contains an ignored file within it.
969 * For the (!ignored) case, since we only need to avoid
970 * deleting ignored files, we can set
971 * DIR_SHOW_IGNORED_TOO_MODE_MATCHING in order to avoid
972 * recursing into a directory which is itself ignored.
974 dir
.flags
|= DIR_SHOW_IGNORED_TOO
;
976 dir
.flags
|= DIR_SHOW_IGNORED_TOO_MODE_MATCHING
;
979 * Let the fill_directory() machinery know that we aren't
980 * just recursing to collect the ignored files; we want all
981 * the untracked ones so that we can delete them. (Note:
982 * we could also set DIR_KEEP_UNTRACKED_CONTENTS when
983 * ignored_only is true, since DIR_KEEP_UNTRACKED_CONTENTS
984 * only has effect in combination with DIR_SHOW_IGNORED_TOO. It makes
985 * the code clearer to exclude it, though.
987 dir
.flags
|= DIR_KEEP_UNTRACKED_CONTENTS
;
990 if (read_cache() < 0)
991 die(_("index file corrupt"));
993 pl
= add_pattern_list(&dir
, EXC_CMDL
, "--exclude option");
994 for (i
= 0; i
< exclude_list
.nr
; i
++)
995 add_pattern(exclude_list
.items
[i
].string
, "", 0, pl
, -(i
+1));
997 parse_pathspec(&pathspec
, 0,
1001 fill_directory(&dir
, &the_index
, &pathspec
);
1002 correct_untracked_entries(&dir
);
1004 for (i
= 0; i
< dir
.nr
; i
++) {
1005 struct dir_entry
*ent
= dir
.entries
[i
];
1010 if (!cache_name_is_other(ent
->name
, ent
->len
))
1013 if (lstat(ent
->name
, &st
))
1014 die_errno("Cannot lstat '%s'", ent
->name
);
1016 if (S_ISDIR(st
.st_mode
) && !remove_directories
&&
1017 matches
!= MATCHED_EXACTLY
)
1020 rel
= relative_path(ent
->name
, prefix
, &buf
);
1021 string_list_append(&del_list
, rel
);
1026 if (interactive
&& del_list
.nr
> 0)
1027 interactive_main_loop();
1029 for_each_string_list_item(item
, &del_list
) {
1032 strbuf_reset(&abs_path
);
1034 strbuf_addstr(&abs_path
, prefix
);
1036 strbuf_addstr(&abs_path
, item
->string
);
1039 * we might have removed this as part of earlier
1040 * recursive directory removal, so lstat() here could
1043 if (lstat(abs_path
.buf
, &st
))
1046 if (S_ISDIR(st
.st_mode
)) {
1047 if (remove_dirs(&abs_path
, prefix
, rm_flags
, dry_run
, quiet
, &gone
))
1049 if (gone
&& !quiet
) {
1050 qname
= quote_path(item
->string
, NULL
, &buf
, 0);
1051 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), qname
);
1054 res
= dry_run
? 0 : unlink(abs_path
.buf
);
1056 int saved_errno
= errno
;
1057 qname
= quote_path(item
->string
, NULL
, &buf
, 0);
1058 errno
= saved_errno
;
1059 warning_errno(_(msg_warn_remove_failed
), qname
);
1061 } else if (!quiet
) {
1062 qname
= quote_path(item
->string
, NULL
, &buf
, 0);
1063 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), qname
);
1068 strbuf_release(&abs_path
);
1069 strbuf_release(&buf
);
1070 string_list_clear(&del_list
, 0);
1071 string_list_clear(&exclude_list
, 0);
1072 return (errors
!= 0);