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 <worldhello.net@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jiang Xin committed Jun 25, 2013 at 23:53 UTC 96a799b6d1d4a63dccfe8b5dcabd2d738fa6c26e
1 file changed +36
builtin/clean.c
+36
@@ -717,6 +717,40 @@ static int select_by_numbers_cmd(void)
717 return 0;
718 }
719
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 +
754 static int quit_cmd(void)
755 {
756 string_list_clear(&del_list, 0);
@@ -731,6 +765,7 @@ static int help_cmd(void)
765 "clean - start cleaning\n"
766 "filter by pattern - exclude items from deletion\n"
767 "select by numbers - select items to be deleted by numbers\n"
768 + "ask each - confirm each deletion (like \"rm -i\")\n"
769 "quit - stop cleaning\n"
770 "help - this screen\n"
771 "? - help for prompt selection"
@@ -748,6 +783,7 @@ static void interactive_main_loop(void)
783 {'c', "clean", 0, clean_cmd},
784 {'f', "filter by pattern", 0, filter_by_patterns_cmd},
785 {'s', "select by numbers", 0, select_by_numbers_cmd},
786 + {'a', "ask each", 0, ask_each_cmd},
787 {'q', "quit", 0, quit_cmd},
788 {'h', "help", 0, help_cmd},
789 };