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

[PATCH v11 11/15] git-clean: add filter by pattern interactive action

From
Jiang Xin <worldhello.net@gmail.com>
Date
May 17, 2013, 09:20 UTC
Message-ID
<cfca09010d04bb56e0a82c0403552dfa4dad97d0.1368782129.git.worldhello.net@gmail.com>
In-Reply-To
<cover.1368782129.git.worldhello.net@gmail.com>

Add a new action for interactive git-clean: filter by pattern. When the user chooses this action, user can input space-separated patterns (the same syntax as gitignore), and each clean candidate that matches with one of the patterns will be excluded from cleaning. When the user feels it's OK, presses ENTER and backs to the confirmation dialog.

Signed-off-by: Jiang Xin <worldhello.net@gmail.com>
Suggested-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/clean.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)
diff --git a/builtin/clean.c b/builtin/clean.c
index 464e0..d761ae 100644
--- a/builtin/clean.c
+++ b/builtin/clean.c
@@ -614,6 +614,72 @@ static int clean_cmd(void)
 	return MENU_RETURN_NO_LOOP;
 }
 
+static int filter_by_patterns_cmd(void)
+{
+	struct dir_struct dir;
+	struct strbuf confirm = STRBUF_INIT;
+	struct strbuf **ignore_list;
+	struct string_list_item *item;
+	struct exclude_list *el;
+	int changed = -1, i;
+
+	for (;;) {
+		if (!del_list.nr)
+			break;
+
+		if (changed)
+			pretty_print_dels();
+
+		clean_print_color(CLEAN_COLOR_PROMPT);
+		printf(_("Input ignore patterns>> "));
+		clean_print_color(CLEAN_COLOR_RESET);
+		if (strbuf_getline(&confirm, stdin, '\n') != EOF)
+			strbuf_trim(&confirm);
+		else
+			putchar('\n');
+
+		/* quit filter_by_pattern mode if press ENTER or Ctrl-D */
+		if (!confirm.len)
+			break;
+
+		memset(&dir, 0, sizeof(dir));
+		el = add_exclude_list(&dir, EXC_CMDL, "manual exclude");
+		ignore_list = strbuf_split_max(&confirm, ' ', 0);
+
+		for (i = 0; ignore_list[i]; i++) {
+			strbuf_trim(ignore_list[i]);
+			if (!ignore_list[i]->len)
+				continue;
+
+			add_exclude(ignore_list[i]->buf, "", 0, el, -(i+1));
+		}
+
+		changed = 0;
+		for_each_string_list_item(item, &del_list) {
+			int dtype = DT_UNKNOWN;
+
+			if (is_excluded(&dir, item->string, &dtype)) {
+				*item->string = '\0';
+				changed++;
+			}
+		}
+
+		if (changed) {
+			string_list_remove_empty_items(&del_list, 0);
+		} else {
+			clean_print_color(CLEAN_COLOR_ERROR);
+			printf_ln(_("WARNING: Cannot find items matched by: %s"), confirm.buf);
+			clean_print_color(CLEAN_COLOR_RESET);
+		}
+
+		strbuf_list_free(ignore_list);
+		clear_directory(&dir);
+	}
+
+	strbuf_release(&confirm);
+	return 0;
+}
+
 static int quit_cmd(void)
 {
 	string_list_clear(&del_list, 0);
@@ -626,6 +692,7 @@ static int help_cmd(void)
 	clean_print_color(CLEAN_COLOR_HELP);
 	printf_ln(_(
 		    "clean               - start cleaning\n"
+		    "filter by pattern   - exclude items from deletion\n"
 		    "quit                - stop cleaning\n"
 		    "help                - this screen\n"
 		    "?                   - help for prompt selection"
@@ -641,6 +708,7 @@ static void interactive_main_loop(void)
 		struct menu_stuff menu_stuff;
 		struct menu_item menus[] = {
 			{'c', "clean",			0, clean_cmd},
+			{'f', "filter by pattern",	0, filter_by_patterns_cmd},
 			{'q', "quit",			0, quit_cmd},
 			{'h', "help",			0, help_cmd},
 		};
-- 
1.8.3.rc2.380.g956c2b2
Previous: Jiang XinNext: Jiang Xin
Message 12 of 16 in “*** Interactive git-clean ***”
  1. 00/15 *** Interactive git-clean ***Jiang Xin, May 17, 2013
  2. 01/15 path.c: refactor relative_path(), not only strip prefixJiang Xin, May 17, 2013
  3. 02/15 test: test relative_path through test-path-utilsJiang Xin, May 17, 2013
  4. 03/15 quote.c: remove path_relative, use relative_path insteadJiang Xin, May 17, 2013
  5. 04/15 Refactor quote_path_relative, remove unused paramsJiang Xin, May 17, 2013
  6. 05/15 Refactor write_name_quoted_relative, remove unused paramsJiang Xin, May 17, 2013
  7. 06/15 git-clean: refactor git-clean into two phasesJiang Xin, May 17, 2013
  8. 07/15 git-clean: add support for -i/--interactiveJiang Xin, May 17, 2013
  9. 08/15 git-clean: show items of del_list in columnsJiang Xin, May 17, 2013
  10. 09/15 git-clean: add colors to interactive git-cleanJiang Xin, May 17, 2013
  11. 10/15 git-clean: use a git-add-interactive compatible UIJiang Xin, May 17, 2013
  12. 11/15 git-clean: add filter by pattern interactive actionJiang Xin, May 17, 2013
  13. 12/15 git-clean: add select by numbers interactive actionJiang Xin, May 17, 2013
  14. 13/15 git-clean: add ask each interactive actionJiang Xin, May 17, 2013
  15. 14/15 git-clean: add documentation for interactive git-cleanJiang Xin, May 17, 2013
  16. 15/15 test: add t7301 for git-clean--interactiveJiang Xin, May 17, 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.