From mboxrd@z Thu Jan 1 00:00:00 1970 From: Jiang Xin Subject: [PATCH v11 11/15] git-clean: add filter by pattern interactive action Date: Fri, 17 May 2013 17:20:15 +0800 Message-ID: References: Cc: Jiang Xin To: Junio C Hamano , Eric Sunshine , Matthieu Moy , Git List X-From: git-owner@vger.kernel.org Fri May 17 11:21:18 2013 Return-path: Envelope-to: gcvg-git-2@plane.gmane.org Received: from vger.kernel.org ([209.132.180.67]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1UdGqh-0006aN-4g for gcvg-git-2@plane.gmane.org; Fri, 17 May 2013 11:21:15 +0200 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754583Ab3EQJVC (ORCPT ); Fri, 17 May 2013 05:21:02 -0400 Received: from mail-pd0-f175.google.com ([209.85.192.175]:53006 "EHLO mail-pd0-f175.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754417Ab3EQJU6 (ORCPT ); Fri, 17 May 2013 05:20:58 -0400 Received: by mail-pd0-f175.google.com with SMTP id y14so3163258pdi.34 for ; Fri, 17 May 2013 02:20:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references:in-reply-to:references; bh=DYC5kK3XPfWk7fFJQxLgAKo7fhv5nFpqFPMQ+3SJ5AM=; b=ICsB8mxP9J8BP5W6WsnN8cjlcoVYgwhS//m6YeqODoAgbOfrjegg6uxXpyO0IziQOT GhyZP5l+6MvLZCK03waMrluAGKDr+ExU2Gjy4c1KXvkaFV4G404IxU0LiVVJx5nzYSUT yMCEcMi+tvA67OoQ7gYO0uC5DOdUx/stMrmf5gsHASVDL9fii2f8adcTqc7KA4WeHZdU r6/J8gEX6LRHL+f8qRn4eZMEp82ctCC7EyP/mjp6IZ/DSVyiuak6YDZ/h35lHuwigRLg OkuIvP2+03yTc7CP2RzxJMsSeZ8sVlndIsrALAZFtuYqR4NwfA3z6hb0GhKEXSGWLGl5 SSRQ== X-Received: by 10.68.25.233 with SMTP id f9mr47923414pbg.118.1368782458121; Fri, 17 May 2013 02:20:58 -0700 (PDT) Received: from localhost.localdomain ([114.248.153.37]) by mx.google.com with ESMTPSA id 10sm10531002pbm.0.2013.05.17.02.20.56 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Fri, 17 May 2013 02:20:57 -0700 (PDT) X-Mailer: git-send-email 1.8.3.rc2.380.g956c2b2 In-Reply-To: In-Reply-To: References: Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Archived-At: 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 Suggested-by: Junio C Hamano --- 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