git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v9 8/9] git-clean: add ask each interactive action

From
Jiang Xin <worldhello.net@gmail.com>
Date
May 14, 2013, 08:45 UTC
Message-ID
<dd85bb5724f571585a26d41b01b987616ff7cfb2.1368518327.git.worldhello.net@gmail.com>
In-Reply-To
<cover.1368518327.git.worldhello.net@gmail.com>

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>
---
 builtin/clean.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
diff --git a/builtin/clean.c b/builtin/clean.c
index 74fc9..fc68b 100644
--- a/builtin/clean.c
+++ b/builtin/clean.c
@@ -752,6 +752,40 @@ static int select_by_numbers_cmd(void)
 	return 0;
 }
 
+static int ask_each_cmd(void)
+{
+	struct strbuf confirm = STRBUF_INIT;
+	struct strbuf buf = STRBUF_INIT;
+	struct string_list_item *item;
+	const char *qname;
+	int changed = 0, eof = 0;
+
+	for_each_string_list_item(item, &del_list) {
+		/* Ctrl-D should stop removing files */
+		if (!eof) {
+			qname = quote_path_relative(item->string, -1, &buf, NULL);
+			printf(_("remove %s? "), qname);
+			if (strbuf_getline(&confirm, stdin, '\n') != EOF) {
+				strbuf_trim(&confirm);
+			} else {
+				putchar('\n');
+				eof = 1;
+			}
+		}
+		if (!confirm.len || !strncasecmp(confirm.buf, "no", confirm.len)) {
+			*item->string = '\0';
+			changed++;
+		}
+	}
+
+	if (changed)
+		string_list_remove_empty_items(&del_list, 0);
+
+	strbuf_release(&buf);
+	strbuf_release(&confirm);
+	return MENU_RETURN_NO_LOOP;
+}
+
 static int quit_cmd(void)
 {
 	string_list_clear(&del_list, 0);
@@ -766,6 +800,7 @@ static int help_cmd(void)
 		    "clean               - start cleaning\n"
 		    "filter by pattern   - exclude items from deletion\n"
 		    "select by numbers   - select items to be deleted by numbers\n"
+		    "ask each            - confirm each deletion (like \"rm -i\")\n"
 		    "quit                - stop cleaning\n"
 		    "help                - this screen\n"
 		    "?                   - help for prompt selection"
@@ -783,6 +818,7 @@ static void interactive_main_loop(void)
 			{'c', "clean",			0, clean_cmd},
 			{'f', "filter by pattern",	0, filter_by_patterns_cmd},
 			{'s', "select by numbers",	0, select_by_numbers_cmd},
+			{'a', "ask each",		0, ask_each_cmd},
 			{'q', "quit",			0, quit_cmd},
 			{'h', "help",			0, help_cmd},
 		};
-- 
1.8.3.rc1.404.gb9fcf3e
Previous: Jiang XinNext: Jiang Xin
Message 17 of 19 in “interactive git-clean”
  1. 0/9 interactive git-cleanJiang Xin, May 14, 2013
  2. 1/9 git-clean: refactor git-clean into two phasesJiang Xin, May 14, 2013
  3. Junio C HamanoMay 14, 2013
  4. Jiang XinMay 15, 2013
  5. Junio C HamanoMay 15, 2013
  6. Jiang XinMay 15, 2013
  7. 0/2 refactor relative_path in path.cJiang Xin, May 15, 2013
  8. 1/2 path.c: refactor relative_path(), not only strip prefixJiang Xin, May 15, 2013
  9. 2/2 quote.c: remove path_relative, use relative_path insteadJiang Xin, May 15, 2013
  10. Junio C HamanoMay 15, 2013
  11. 2/9 git-clean: add support for -i/--interactiveJiang Xin, May 14, 2013
  12. 3/9 git-clean: show items of del_list in columnsJiang Xin, May 14, 2013
  13. 4/9 git-clean: add colors to interactive git-cleanJiang Xin, May 14, 2013
  14. 5/9 git-clean: use a git-add-interactive compatible UIJiang Xin, May 14, 2013
  15. 6/9 git-clean: add filter by pattern interactive actionJiang Xin, May 14, 2013
  16. 7/9 git-clean: add select by numbers interactive actionJiang Xin, May 14, 2013
  17. 8/9 git-clean: add ask each interactive actionJiang Xin, May 14, 2013
  18. 9/9 git-clean: add documentation for interactive git-cleanJiang Xin, May 14, 2013
  19. Junio C HamanoMay 14, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.