Skip to content

Commit 96a799b

Browse files
jiangxingitster
authored andcommitted
git-clean: add ask each interactive action
Add a new action for interactive git-clean: ask each. It's just like the "rm -i" command, that the user must confirm one by one for each file or directory to be cleaned. Signed-off-by: Jiang Xin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c1f1d24 commit 96a799b

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

builtin/clean.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,40 @@ static int select_by_numbers_cmd(void)
717717
return 0;
718718
}
719719

720+
static int ask_each_cmd(void)
721+
{
722+
struct strbuf confirm = STRBUF_INIT;
723+
struct strbuf buf = STRBUF_INIT;
724+
struct string_list_item *item;
725+
const char *qname;
726+
int changed = 0, eof = 0;
727+
728+
for_each_string_list_item(item, &del_list) {
729+
/* Ctrl-D should stop removing files */
730+
if (!eof) {
731+
qname = quote_path_relative(item->string, NULL, &buf);
732+
printf(_("remove %s? "), qname);
733+
if (strbuf_getline(&confirm, stdin, '\n') != EOF) {
734+
strbuf_trim(&confirm);
735+
} else {
736+
putchar('\n');
737+
eof = 1;
738+
}
739+
}
740+
if (!confirm.len || strncasecmp(confirm.buf, "yes", confirm.len)) {
741+
*item->string = '\0';
742+
changed++;
743+
}
744+
}
745+
746+
if (changed)
747+
string_list_remove_empty_items(&del_list, 0);
748+
749+
strbuf_release(&buf);
750+
strbuf_release(&confirm);
751+
return MENU_RETURN_NO_LOOP;
752+
}
753+
720754
static int quit_cmd(void)
721755
{
722756
string_list_clear(&del_list, 0);
@@ -731,6 +765,7 @@ static int help_cmd(void)
731765
"clean - start cleaning\n"
732766
"filter by pattern - exclude items from deletion\n"
733767
"select by numbers - select items to be deleted by numbers\n"
768+
"ask each - confirm each deletion (like \"rm -i\")\n"
734769
"quit - stop cleaning\n"
735770
"help - this screen\n"
736771
"? - help for prompt selection"
@@ -748,6 +783,7 @@ static void interactive_main_loop(void)
748783
{'c', "clean", 0, clean_cmd},
749784
{'f', "filter by pattern", 0, filter_by_patterns_cmd},
750785
{'s', "select by numbers", 0, select_by_numbers_cmd},
786+
{'a', "ask each", 0, ask_each_cmd},
751787
{'q', "quit", 0, quit_cmd},
752788
{'h', "help", 0, help_cmd},
753789
};

0 commit comments

Comments
 (0)