2 * "git clean" builtin command
4 * Copyright (C) 2007 Shawn Bohrer
6 * Based on git-clean.sh by Pavel Roskin
8 #define USE_THE_REPOSITORY_VARIABLE
14 #include "parse-options.h"
16 #include "read-cache-ll.h"
18 #include "string-list.h"
26 static int require_force
= -1; /* unset */
27 static int interactive
;
28 static struct string_list del_list
= STRING_LIST_INIT_DUP
;
29 static unsigned int colopts
;
31 static const char *const builtin_clean_usage
[] = {
32 N_("git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] [<pathspec>...]"),
36 static const char *msg_remove
= N_("Removing %s\n");
37 static const char *msg_would_remove
= N_("Would remove %s\n");
38 static const char *msg_skip_git_dir
= N_("Skipping repository %s\n");
39 static const char *msg_would_skip_git_dir
= N_("Would skip repository %s\n");
40 static const char *msg_warn_remove_failed
= N_("failed to remove %s");
41 static const char *msg_warn_lstat_failed
= N_("could not lstat %s\n");
42 static const char *msg_skip_cwd
= N_("Refusing to remove current working directory\n");
43 static const char *msg_would_skip_cwd
= N_("Would refuse to remove current working directory\n");
46 CLEAN_COLOR_RESET
= 0,
47 CLEAN_COLOR_PLAIN
= 1,
48 CLEAN_COLOR_PROMPT
= 2,
49 CLEAN_COLOR_HEADER
= 3,
54 static const char *color_interactive_slots
[] = {
55 [CLEAN_COLOR_ERROR
] = "error",
56 [CLEAN_COLOR_HEADER
] = "header",
57 [CLEAN_COLOR_HELP
] = "help",
58 [CLEAN_COLOR_PLAIN
] = "plain",
59 [CLEAN_COLOR_PROMPT
] = "prompt",
60 [CLEAN_COLOR_RESET
] = "reset",
63 static int clean_use_color
= -1;
64 static char clean_colors
[][COLOR_MAXLEN
] = {
65 [CLEAN_COLOR_ERROR
] = GIT_COLOR_BOLD_RED
,
66 [CLEAN_COLOR_HEADER
] = GIT_COLOR_BOLD
,
67 [CLEAN_COLOR_HELP
] = GIT_COLOR_BOLD_RED
,
68 [CLEAN_COLOR_PLAIN
] = GIT_COLOR_NORMAL
,
69 [CLEAN_COLOR_PROMPT
] = GIT_COLOR_BOLD_BLUE
,
70 [CLEAN_COLOR_RESET
] = GIT_COLOR_RESET
,
73 #define MENU_OPTS_SINGLETON 01
74 #define MENU_OPTS_IMMEDIATE 02
75 #define MENU_OPTS_LIST_ONLY 04
83 #define MENU_RETURN_NO_LOOP 10
92 enum menu_stuff_type
{
93 MENU_STUFF_TYPE_STRING_LIST
= 1,
94 MENU_STUFF_TYPE_MENU_ITEM
98 enum menu_stuff_type type
;
103 define_list_config_array(color_interactive_slots
);
105 static int git_clean_config(const char *var
, const char *value
,
106 const struct config_context
*ctx
, void *cb
)
108 const char *slot_name
;
110 if (starts_with(var
, "column."))
111 return git_column_config(var
, value
, "clean", &colopts
);
113 /* honors the color.interactive* config variables which also
114 applied in git-add--interactive and git-stash */
115 if (!strcmp(var
, "color.interactive")) {
116 clean_use_color
= git_config_colorbool(var
, value
);
119 if (skip_prefix(var
, "color.interactive.", &slot_name
)) {
120 int slot
= LOOKUP_CONFIG(color_interactive_slots
, slot_name
);
124 return config_error_nonbool(var
);
125 return color_parse(value
, clean_colors
[slot
]);
128 if (!strcmp(var
, "clean.requireforce")) {
129 require_force
= git_config_bool(var
, value
);
133 if (git_color_config(var
, value
, cb
) < 0)
136 return git_default_config(var
, value
, ctx
, cb
);
139 static const char *clean_get_color(enum color_clean ix
)
141 if (want_color(clean_use_color
))
142 return clean_colors
[ix
];
146 static void clean_print_color(enum color_clean ix
)
148 printf("%s", clean_get_color(ix
));
151 static int exclude_cb(const struct option
*opt
, const char *arg
, int unset
)
153 struct string_list
*exclude_list
= opt
->value
;
154 BUG_ON_OPT_NEG(unset
);
155 string_list_append(exclude_list
, arg
);
159 static int remove_dirs(struct strbuf
*path
, const char *prefix
, int force_flag
,
160 int dry_run
, int quiet
, int *dir_gone
)
163 struct strbuf quoted
= STRBUF_INIT
;
164 struct strbuf realpath
= STRBUF_INIT
;
165 struct strbuf real_ocwd
= STRBUF_INIT
;
167 int res
= 0, ret
= 0, gone
= 1, original_len
= path
->len
, len
;
168 struct string_list dels
= STRING_LIST_INIT_DUP
;
172 if ((force_flag
& REMOVE_DIR_KEEP_NESTED_GIT
) &&
173 is_nonbare_repository_dir(path
)) {
175 quote_path(path
->buf
, prefix
, "ed
, 0);
176 printf(dry_run
? _(msg_would_skip_git_dir
) : _(msg_skip_git_dir
),
184 dir
= opendir(path
->buf
);
186 /* an empty dir could be removed even if it is unreadble */
187 res
= dry_run
? 0 : rmdir(path
->buf
);
189 int saved_errno
= errno
;
190 quote_path(path
->buf
, prefix
, "ed
, 0);
192 warning_errno(_(msg_warn_remove_failed
), quoted
.buf
);
199 strbuf_complete(path
, '/');
202 while ((e
= readdir_skip_dot_and_dotdot(dir
)) != NULL
) {
205 strbuf_setlen(path
, len
);
206 strbuf_addstr(path
, e
->d_name
);
207 if (lstat(path
->buf
, &st
))
208 warning_errno(_(msg_warn_lstat_failed
), path
->buf
);
209 else if (S_ISDIR(st
.st_mode
)) {
210 if (remove_dirs(path
, prefix
, force_flag
, dry_run
, quiet
, &gone
))
213 quote_path(path
->buf
, prefix
, "ed
, 0);
214 string_list_append(&dels
, quoted
.buf
);
219 res
= dry_run
? 0 : unlink(path
->buf
);
221 quote_path(path
->buf
, prefix
, "ed
, 0);
222 string_list_append(&dels
, quoted
.buf
);
224 int saved_errno
= errno
;
225 quote_path(path
->buf
, prefix
, "ed
, 0);
227 warning_errno(_(msg_warn_remove_failed
), quoted
.buf
);
234 /* path too long, stat fails, or non-directory still exists */
241 strbuf_setlen(path
, original_len
);
245 * Normalize path components in path->buf, e.g. change '\' to
248 strbuf_realpath(&realpath
, path
->buf
, 1);
251 * path and realpath are absolute; for comparison, we would
252 * like to transform startup_info->original_cwd to an absolute
255 if (startup_info
->original_cwd
)
256 strbuf_realpath(&real_ocwd
,
257 startup_info
->original_cwd
, 1);
259 if (!strbuf_cmp(&realpath
, &real_ocwd
)) {
260 printf("%s", dry_run
? _(msg_would_skip_cwd
) : _(msg_skip_cwd
));
263 res
= dry_run
? 0 : rmdir(path
->buf
);
267 int saved_errno
= errno
;
268 quote_path(path
->buf
, prefix
, "ed
, 0);
270 warning_errno(_(msg_warn_remove_failed
), quoted
.buf
);
277 if (!*dir_gone
&& !quiet
) {
279 for (i
= 0; i
< dels
.nr
; i
++)
280 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), dels
.items
[i
].string
);
283 strbuf_release(&realpath
);
284 strbuf_release(&real_ocwd
);
285 strbuf_release("ed
);
286 string_list_clear(&dels
, 0);
290 static void pretty_print_dels(void)
292 struct string_list list
= STRING_LIST_INIT_DUP
;
293 struct string_list_item
*item
;
294 struct strbuf buf
= STRBUF_INIT
;
296 struct column_options copts
;
298 for_each_string_list_item(item
, &del_list
) {
299 qname
= quote_path(item
->string
, NULL
, &buf
, 0);
300 string_list_append(&list
, qname
);
304 * always enable column display, we only consult column.*
305 * about layout strategy and stuff
307 colopts
= (colopts
& ~COL_ENABLE_MASK
) | COL_ENABLED
;
308 memset(&copts
, 0, sizeof(copts
));
311 print_columns(&list
, colopts
, &copts
);
312 strbuf_release(&buf
);
313 string_list_clear(&list
, 0);
316 static void pretty_print_menus(struct string_list
*menu_list
)
318 unsigned int local_colopts
= 0;
319 struct column_options copts
;
321 local_colopts
= COL_ENABLED
| COL_ROW
;
322 memset(&copts
, 0, sizeof(copts
));
325 print_columns(menu_list
, local_colopts
, &copts
);
328 static void prompt_help_cmd(int singleton
)
330 clean_print_color(CLEAN_COLOR_HELP
);
333 "1 - select a numbered item\n"
334 "foo - select item based on unique prefix\n"
335 " - (empty) select nothing\n") :
337 "1 - select a single item\n"
338 "3-5 - select a range of items\n"
339 "2-3,6-9 - select multiple ranges\n"
340 "foo - select item based on unique prefix\n"
341 "-... - unselect specified items\n"
342 "* - choose all items\n"
343 " - (empty) finish selecting\n"));
344 clean_print_color(CLEAN_COLOR_RESET
);
348 * display menu stuff with number prefix and hotkey highlight
350 static void print_highlight_menu_stuff(struct menu_stuff
*stuff
, int **chosen
)
352 struct string_list menu_list
= STRING_LIST_INIT_DUP
;
353 struct strbuf menu
= STRBUF_INIT
;
354 struct menu_item
*menu_item
;
355 struct string_list_item
*string_list_item
;
358 switch (stuff
->type
) {
360 die("Bad type of menu_stuff when print menu");
361 case MENU_STUFF_TYPE_MENU_ITEM
:
362 menu_item
= (struct menu_item
*)stuff
->stuff
;
363 for (i
= 0; i
< stuff
->nr
; i
++, menu_item
++) {
367 p
= menu_item
->title
;
368 if ((*chosen
)[i
] < 0)
369 (*chosen
)[i
] = menu_item
->selected
? 1 : 0;
370 strbuf_addf(&menu
, "%s%2d: ", (*chosen
)[i
] ? "*" : " ", i
+1);
372 if (!highlighted
&& *p
== menu_item
->hotkey
) {
373 strbuf_addstr(&menu
, clean_get_color(CLEAN_COLOR_PROMPT
));
374 strbuf_addch(&menu
, *p
);
375 strbuf_addstr(&menu
, clean_get_color(CLEAN_COLOR_RESET
));
378 strbuf_addch(&menu
, *p
);
381 string_list_append(&menu_list
, menu
.buf
);
385 case MENU_STUFF_TYPE_STRING_LIST
:
387 for_each_string_list_item(string_list_item
, (struct string_list
*)stuff
->stuff
) {
388 if ((*chosen
)[i
] < 0)
390 strbuf_addf(&menu
, "%s%2d: %s",
391 (*chosen
)[i
] ? "*" : " ", i
+1, string_list_item
->string
);
392 string_list_append(&menu_list
, menu
.buf
);
399 pretty_print_menus(&menu_list
);
401 strbuf_release(&menu
);
402 string_list_clear(&menu_list
, 0);
405 static int find_unique(const char *choice
, struct menu_stuff
*menu_stuff
)
407 struct menu_item
*menu_item
;
408 struct string_list_item
*string_list_item
;
409 int i
, len
, found
= 0;
411 len
= strlen(choice
);
412 switch (menu_stuff
->type
) {
414 die("Bad type of menu_stuff when parse choice");
415 case MENU_STUFF_TYPE_MENU_ITEM
:
417 menu_item
= (struct menu_item
*)menu_stuff
->stuff
;
418 for (i
= 0; i
< menu_stuff
->nr
; i
++, menu_item
++) {
419 if (len
== 1 && *choice
== menu_item
->hotkey
) {
423 if (!strncasecmp(choice
, menu_item
->title
, len
)) {
426 /* continue for hotkey matching */
438 case MENU_STUFF_TYPE_STRING_LIST
:
439 string_list_item
= ((struct string_list
*)menu_stuff
->stuff
)->items
;
440 for (i
= 0; i
< menu_stuff
->nr
; i
++, string_list_item
++) {
441 if (!strncasecmp(choice
, string_list_item
->string
, len
)) {
455 * Parse user input, and return choice(s) for menu (menu_stuff).
458 * (for single choice)
459 * 1 - select a numbered item
460 * foo - select item based on menu title
461 * - (empty) select nothing
463 * (for multiple choice)
464 * 1 - select a single item
465 * 3-5 - select a range of items
466 * 2-3,6-9 - select multiple ranges
467 * foo - select item based on menu title
468 * -... - unselect specified items
469 * * - choose all items
470 * - (empty) finish selecting
472 * The parse result will be saved in array **chosen, and
473 * return number of total selections.
475 static int parse_choice(struct menu_stuff
*menu_stuff
,
480 struct strbuf
**choice_list
, **ptr
;
485 choice_list
= strbuf_split_max(&input
, '\n', 0);
492 choice_list
= strbuf_split_max(&input
, ' ', 0);
495 for (ptr
= choice_list
; *ptr
; ptr
++) {
498 int bottom
= 0, top
= 0;
499 int is_range
, is_number
;
505 /* Input that begins with '-'; unchoose */
506 if (*(*ptr
)->buf
== '-') {
508 strbuf_remove((*ptr
), 0, 1);
513 for (p
= (*ptr
)->buf
; *p
; p
++) {
523 } else if (!isdigit(*p
)) {
531 bottom
= atoi((*ptr
)->buf
);
533 } else if (is_range
) {
534 bottom
= atoi((*ptr
)->buf
);
535 /* a range can be specified like 5-7 or 5- */
536 if (!*(strchr((*ptr
)->buf
, '-') + 1))
537 top
= menu_stuff
->nr
;
539 top
= atoi(strchr((*ptr
)->buf
, '-') + 1);
540 } else if (!strcmp((*ptr
)->buf
, "*")) {
542 top
= menu_stuff
->nr
;
544 bottom
= find_unique((*ptr
)->buf
, menu_stuff
);
548 if (top
<= 0 || bottom
<= 0 || top
> menu_stuff
->nr
|| bottom
> top
||
549 (is_single
&& bottom
!= top
)) {
550 clean_print_color(CLEAN_COLOR_ERROR
);
551 printf(_("Huh (%s)?\n"), (*ptr
)->buf
);
552 clean_print_color(CLEAN_COLOR_RESET
);
556 for (i
= bottom
; i
<= top
; i
++)
557 (*chosen
)[i
-1] = choose
;
560 strbuf_list_free(choice_list
);
562 for (i
= 0; i
< menu_stuff
->nr
; i
++)
568 * Implement a git-add-interactive compatible UI, which is borrowed
569 * from add-interactive.c.
573 * - Return an array of integers
574 * - , and it is up to you to free the allocated memory.
575 * - The array ends with EOF.
576 * - If user pressed CTRL-D (i.e. EOF), no selection returned.
578 static int *list_and_choose(struct menu_opts
*opts
, struct menu_stuff
*stuff
)
580 struct strbuf choice
= STRBUF_INIT
;
581 int *chosen
, *result
;
586 ALLOC_ARRAY(chosen
, stuff
->nr
);
587 /* set chosen as uninitialized */
588 for (i
= 0; i
< stuff
->nr
; i
++)
594 clean_get_color(CLEAN_COLOR_HEADER
),
596 clean_get_color(CLEAN_COLOR_RESET
));
599 /* chosen will be initialized by print_highlight_menu_stuff */
600 print_highlight_menu_stuff(stuff
, &chosen
);
602 if (opts
->flags
& MENU_OPTS_LIST_ONLY
)
607 clean_get_color(CLEAN_COLOR_PROMPT
),
609 opts
->flags
& MENU_OPTS_SINGLETON
? "> " : ">> ",
610 clean_get_color(CLEAN_COLOR_RESET
));
613 if (git_read_line_interactively(&choice
) == EOF
) {
618 /* help for prompt */
619 if (!strcmp(choice
.buf
, "?")) {
620 prompt_help_cmd(opts
->flags
& MENU_OPTS_SINGLETON
);
624 /* for a multiple-choice menu, press ENTER (empty) will return back */
625 if (!(opts
->flags
& MENU_OPTS_SINGLETON
) && !choice
.len
)
628 nr
= parse_choice(stuff
,
629 opts
->flags
& MENU_OPTS_SINGLETON
,
633 if (opts
->flags
& MENU_OPTS_SINGLETON
) {
636 } else if (opts
->flags
& MENU_OPTS_IMMEDIATE
) {
642 result
= xmalloc(sizeof(int));
648 * recalculate nr, if return back from menu directly with
649 * default selections.
652 for (i
= 0; i
< stuff
->nr
; i
++)
656 CALLOC_ARRAY(result
, st_add(nr
, 1));
657 for (i
= 0; i
< stuff
->nr
&& j
< nr
; i
++) {
665 strbuf_release(&choice
);
669 static int clean_cmd(void)
671 return MENU_RETURN_NO_LOOP
;
674 static int filter_by_patterns_cmd(void)
676 struct dir_struct dir
= DIR_INIT
;
677 struct strbuf confirm
= STRBUF_INIT
;
678 struct strbuf
**ignore_list
;
679 struct string_list_item
*item
;
680 struct pattern_list
*pl
;
690 clean_print_color(CLEAN_COLOR_PROMPT
);
691 printf(_("Input ignore patterns>> "));
692 clean_print_color(CLEAN_COLOR_RESET
);
693 if (git_read_line_interactively(&confirm
) == EOF
)
696 /* quit filter_by_pattern mode if press ENTER or Ctrl-D */
700 pl
= add_pattern_list(&dir
, EXC_CMDL
, "manual exclude");
701 ignore_list
= strbuf_split_max(&confirm
, ' ', 0);
703 for (i
= 0; ignore_list
[i
]; i
++) {
704 strbuf_trim(ignore_list
[i
]);
705 if (!ignore_list
[i
]->len
)
708 add_pattern(ignore_list
[i
]->buf
, "", 0, pl
, -(i
+1));
712 for_each_string_list_item(item
, &del_list
) {
713 int dtype
= DT_UNKNOWN
;
715 if (is_excluded(&dir
, the_repository
->index
, item
->string
, &dtype
)) {
716 *item
->string
= '\0';
722 string_list_remove_empty_items(&del_list
, 0);
724 clean_print_color(CLEAN_COLOR_ERROR
);
725 printf_ln(_("WARNING: Cannot find items matched by: %s"), confirm
.buf
);
726 clean_print_color(CLEAN_COLOR_RESET
);
729 strbuf_list_free(ignore_list
);
733 strbuf_release(&confirm
);
737 static int select_by_numbers_cmd(void)
739 struct menu_opts menu_opts
;
740 struct menu_stuff menu_stuff
;
741 struct string_list_item
*items
;
745 menu_opts
.header
= NULL
;
746 menu_opts
.prompt
= N_("Select items to delete");
749 menu_stuff
.type
= MENU_STUFF_TYPE_STRING_LIST
;
750 menu_stuff
.stuff
= &del_list
;
751 menu_stuff
.nr
= del_list
.nr
;
753 chosen
= list_and_choose(&menu_opts
, &menu_stuff
);
754 items
= del_list
.items
;
755 for (i
= 0, j
= 0; i
< del_list
.nr
; i
++) {
757 *(items
[i
].string
) = '\0';
758 } else if (i
== chosen
[j
]) {
759 /* delete selected item */
763 /* end of chosen (chosen[j] == EOF), won't delete */
764 *(items
[i
].string
) = '\0';
768 string_list_remove_empty_items(&del_list
, 0);
774 static int ask_each_cmd(void)
776 struct strbuf confirm
= STRBUF_INIT
;
777 struct strbuf buf
= STRBUF_INIT
;
778 struct string_list_item
*item
;
780 int changed
= 0, eof
= 0;
782 for_each_string_list_item(item
, &del_list
) {
783 /* Ctrl-D should stop removing files */
785 qname
= quote_path(item
->string
, NULL
, &buf
, 0);
786 /* TRANSLATORS: Make sure to keep [y/N] as is */
787 printf(_("Remove %s [y/N]? "), qname
);
788 if (git_read_line_interactively(&confirm
) == EOF
) {
793 if (!confirm
.len
|| strncasecmp(confirm
.buf
, "yes", confirm
.len
)) {
794 *item
->string
= '\0';
800 string_list_remove_empty_items(&del_list
, 0);
802 strbuf_release(&buf
);
803 strbuf_release(&confirm
);
804 return MENU_RETURN_NO_LOOP
;
807 static int quit_cmd(void)
809 string_list_clear(&del_list
, 0);
811 return MENU_RETURN_NO_LOOP
;
814 static int help_cmd(void)
816 clean_print_color(CLEAN_COLOR_HELP
);
818 "clean - start cleaning\n"
819 "filter by pattern - exclude items from deletion\n"
820 "select by numbers - select items to be deleted by numbers\n"
821 "ask each - confirm each deletion (like \"rm -i\")\n"
822 "quit - stop cleaning\n"
823 "help - this screen\n"
824 "? - help for prompt selection"
826 clean_print_color(CLEAN_COLOR_RESET
);
830 static void interactive_main_loop(void)
832 while (del_list
.nr
) {
833 struct menu_opts menu_opts
;
834 struct menu_stuff menu_stuff
;
835 struct menu_item menus
[] = {
836 {'c', "clean", 0, clean_cmd
},
837 {'f', "filter by pattern", 0, filter_by_patterns_cmd
},
838 {'s', "select by numbers", 0, select_by_numbers_cmd
},
839 {'a', "ask each", 0, ask_each_cmd
},
840 {'q', "quit", 0, quit_cmd
},
841 {'h', "help", 0, help_cmd
},
845 menu_opts
.header
= N_("*** Commands ***");
846 menu_opts
.prompt
= N_("What now");
847 menu_opts
.flags
= MENU_OPTS_SINGLETON
;
849 menu_stuff
.type
= MENU_STUFF_TYPE_MENU_ITEM
;
850 menu_stuff
.stuff
= menus
;
851 menu_stuff
.nr
= sizeof(menus
) / sizeof(struct menu_item
);
853 clean_print_color(CLEAN_COLOR_HEADER
);
854 printf_ln(Q_("Would remove the following item:",
855 "Would remove the following items:",
857 clean_print_color(CLEAN_COLOR_RESET
);
861 chosen
= list_and_choose(&menu_opts
, &menu_stuff
);
863 if (*chosen
!= EOF
) {
865 ret
= menus
[*chosen
].fn();
866 if (ret
!= MENU_RETURN_NO_LOOP
) {
867 FREE_AND_NULL(chosen
);
869 clean_print_color(CLEAN_COLOR_ERROR
);
870 printf_ln(_("No more files to clean, exiting."));
871 clean_print_color(CLEAN_COLOR_RESET
);
880 FREE_AND_NULL(chosen
);
885 static void correct_untracked_entries(struct dir_struct
*dir
)
889 for (src
= dst
= ign
= 0; src
< dir
->nr
; src
++) {
890 /* skip paths in ignored[] that cannot be inside entries[src] */
891 while (ign
< dir
->ignored_nr
&&
892 0 <= cmp_dir_entry(&dir
->entries
[src
], &dir
->ignored
[ign
]))
895 if (ign
< dir
->ignored_nr
&&
896 check_dir_entry_contains(dir
->entries
[src
], dir
->ignored
[ign
])) {
897 /* entries[src] contains an ignored path, so we drop it */
898 free(dir
->entries
[src
]);
900 struct dir_entry
*ent
= dir
->entries
[src
++];
902 /* entries[src] does not contain an ignored path, so we keep it */
903 dir
->entries
[dst
++] = ent
;
905 /* then discard paths in entries[] contained inside entries[src] */
906 while (src
< dir
->nr
&&
907 check_dir_entry_contains(ent
, dir
->entries
[src
]))
908 free(dir
->entries
[src
++]);
910 /* compensate for the outer loop's loop control */
917 int cmd_clean(int argc
,
920 struct repository
*repo UNUSED
)
923 int dry_run
= 0, remove_directories
= 0, quiet
= 0, ignored
= 0;
924 int ignored_only
= 0, force
= 0, errors
= 0, gone
= 1;
925 int rm_flags
= REMOVE_DIR_KEEP_NESTED_GIT
;
926 struct strbuf abs_path
= STRBUF_INIT
;
927 struct dir_struct dir
= DIR_INIT
;
928 struct pathspec pathspec
;
929 struct strbuf buf
= STRBUF_INIT
;
930 struct string_list exclude_list
= STRING_LIST_INIT_NODUP
;
931 struct pattern_list
*pl
;
932 struct string_list_item
*item
;
934 struct option options
[] = {
935 OPT__QUIET(&quiet
, N_("do not print names of files removed")),
936 OPT__DRY_RUN(&dry_run
, N_("dry run")),
937 OPT__FORCE(&force
, N_("force"), PARSE_OPT_NOCOMPLETE
),
938 OPT_BOOL('i', "interactive", &interactive
, N_("interactive cleaning")),
939 OPT_BOOL('d', NULL
, &remove_directories
,
940 N_("remove whole directories")),
941 OPT_CALLBACK_F('e', "exclude", &exclude_list
, N_("pattern"),
942 N_("add <pattern> to ignore rules"), PARSE_OPT_NONEG
, exclude_cb
),
943 OPT_BOOL('x', NULL
, &ignored
, N_("remove ignored files, too")),
944 OPT_BOOL('X', NULL
, &ignored_only
,
945 N_("remove only ignored files")),
949 git_config(git_clean_config
, NULL
);
951 argc
= parse_options(argc
, argv
, prefix
, options
, builtin_clean_usage
,
954 if (require_force
!= 0 && !force
&& !interactive
&& !dry_run
)
955 die(_("clean.requireForce is true and -f not given: refusing to clean"));
960 dir
.flags
|= DIR_SKIP_NESTED_GIT
;
962 dir
.flags
|= DIR_SHOW_OTHER_DIRECTORIES
;
964 if (ignored
&& ignored_only
)
965 die(_("options '%s' and '%s' cannot be used together"), "-x", "-X");
967 setup_standard_excludes(&dir
);
969 dir
.flags
|= DIR_SHOW_IGNORED
;
973 * Remaining args implies pathspecs specified, and we should
974 * recurse within those.
976 remove_directories
= 1;
979 if (remove_directories
&& !ignored_only
) {
981 * We need to know about ignored files too:
983 * If (ignored), then we will delete ignored files as well.
985 * If (!ignored), then even though we not are doing
986 * anything with ignored files, we need to know about them
987 * so that we can avoid deleting a directory of untracked
988 * files that also contains an ignored file within it.
990 * For the (!ignored) case, since we only need to avoid
991 * deleting ignored files, we can set
992 * DIR_SHOW_IGNORED_TOO_MODE_MATCHING in order to avoid
993 * recursing into a directory which is itself ignored.
995 dir
.flags
|= DIR_SHOW_IGNORED_TOO
;
997 dir
.flags
|= DIR_SHOW_IGNORED_TOO_MODE_MATCHING
;
1000 * Let the fill_directory() machinery know that we aren't
1001 * just recursing to collect the ignored files; we want all
1002 * the untracked ones so that we can delete them. (Note:
1003 * we could also set DIR_KEEP_UNTRACKED_CONTENTS when
1004 * ignored_only is true, since DIR_KEEP_UNTRACKED_CONTENTS
1005 * only has effect in combination with DIR_SHOW_IGNORED_TOO. It makes
1006 * the code clearer to exclude it, though.
1008 dir
.flags
|= DIR_KEEP_UNTRACKED_CONTENTS
;
1011 prepare_repo_settings(the_repository
);
1012 the_repository
->settings
.command_requires_full_index
= 0;
1014 if (repo_read_index(the_repository
) < 0)
1015 die(_("index file corrupt"));
1017 pl
= add_pattern_list(&dir
, EXC_CMDL
, "--exclude option");
1018 for (i
= 0; i
< exclude_list
.nr
; i
++)
1019 add_pattern(exclude_list
.items
[i
].string
, "", 0, pl
, -(i
+1));
1021 parse_pathspec(&pathspec
, 0,
1022 PATHSPEC_PREFER_CWD
,
1025 fill_directory(&dir
, the_repository
->index
, &pathspec
);
1026 correct_untracked_entries(&dir
);
1028 for (i
= 0; i
< dir
.nr
; i
++) {
1029 struct dir_entry
*ent
= dir
.entries
[i
];
1033 if (!index_name_is_other(the_repository
->index
, ent
->name
, ent
->len
))
1036 if (lstat(ent
->name
, &st
))
1037 die_errno("Cannot lstat '%s'", ent
->name
);
1039 if (S_ISDIR(st
.st_mode
) && !remove_directories
)
1042 rel
= relative_path(ent
->name
, prefix
, &buf
);
1043 string_list_append(&del_list
, rel
);
1048 if (interactive
&& del_list
.nr
> 0)
1049 interactive_main_loop();
1051 for_each_string_list_item(item
, &del_list
) {
1054 strbuf_reset(&abs_path
);
1056 strbuf_addstr(&abs_path
, prefix
);
1058 strbuf_addstr(&abs_path
, item
->string
);
1061 * we might have removed this as part of earlier
1062 * recursive directory removal, so lstat() here could
1065 if (lstat(abs_path
.buf
, &st
))
1068 if (S_ISDIR(st
.st_mode
)) {
1069 if (remove_dirs(&abs_path
, prefix
, rm_flags
, dry_run
, quiet
, &gone
))
1071 if (gone
&& !quiet
) {
1072 qname
= quote_path(item
->string
, NULL
, &buf
, 0);
1073 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), qname
);
1076 res
= dry_run
? 0 : unlink(abs_path
.buf
);
1078 int saved_errno
= errno
;
1079 qname
= quote_path(item
->string
, NULL
, &buf
, 0);
1080 errno
= saved_errno
;
1081 warning_errno(_(msg_warn_remove_failed
), qname
);
1083 } else if (!quiet
) {
1084 qname
= quote_path(item
->string
, NULL
, &buf
, 0);
1085 printf(dry_run
? _(msg_would_remove
) : _(msg_remove
), qname
);
1090 strbuf_release(&abs_path
);
1091 strbuf_release(&buf
);
1092 string_list_clear(&del_list
, 0);
1093 string_list_clear(&exclude_list
, 0);
1094 clear_pathspec(&pathspec
);
1095 return (errors
!= 0);