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

[PATCH] git add -i: allow list (un)selection by regexp

From
AKAarni Koskela <aarni.koskela@andersinnovations.com>
Date
Dec 1, 2014, 09:31 UTC
Message-ID
<DB3PR04MB25020F247555E521936E676EB7D0@DB3PR04MB250.eurprd04.prod.outlook.com>
Hello!

This patch makes it possible to select or unselect files in `git add -i` by regular expression instead of unique prefix only.

The command syntax is `/foo` for selection and `-/foo` for unselection. I don't think the syntax will conflict with any existing use cases, but feel free to prove me wrong.

I'm not a Perl programmer, but I've tried to follow the style of the existing code as much as possible. :)

Note I'm currently not on the mailing list, so please cc.
Best regards,
Aarni Koskela
>From 53c12d9c9928dc93a57595e92d785ecc0b245390 Mon Sep 17 00:00:00 2001
From: Aarni Koskela <akx@iki.fi>
Date: Mon, 1 Dec 2014 11:06:10 +0200
Subject: [PATCH] git-add--interactive: allow list (un)selection by regular
 expression

Teach `list_and_choose` to allow `/regexp` and `-/regexp` syntax to select items based on regular expression match.

For instance, `/jpg$` will select all options whose display name ends with `jpg`.

Signed-off-by: Aarni Koskela <akx@iki.fi>
---
 git-add--interactive.perl | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
diff --git a/git-add--interactive.perl b/git-add--interactive.perl
index 1fadd69..34cc603 100755
--- a/git-add--interactive.perl
+++ b/git-add--interactive.perl
@@ -483,6 +483,8 @@ sub is_valid_prefix {
 	    !($prefix =~ /[\s,]/) && # separators
 	    !($prefix =~ /^-/) &&    # deselection
 	    !($prefix =~ /^\d+/) &&  # selection
+	    !($prefix =~ /^\//) &&   # regexp selection
+	    !($prefix =~ /^-\//) &&  # regexp unselection
 	    ($prefix ne '*') &&      # "all" wildcard
 	    ($prefix ne '?');        # prompt help
 }
@@ -585,6 +587,28 @@ sub list_and_choose {
 			    prompt_help_cmd();
 			next TOPLOOP;
 		}
+		if ($line =~ /^(-)*\/(.+)$/) {
+			my $choose = !($1 && $1 eq '-');
+			my $re = $2;
+			my $found = 0;
+			for ($i = 0; $i < @stuff; $i++) {
+				my $val = $stuff[$i];
+				my $ref = ref $val;
+				if ($ref eq 'ARRAY') {
+					$val = $val->[0];
+				}
+				elsif ($ref eq 'HASH') {
+					$val = $val->{VALUE};
+				}
+				if ($val =~ /$re/) {
+					$chosen[$i] = $choose;
+					$found = 1;
+					last if $opts->{SINGLETON};
+				}
+			}
+			last if $found && ($opts->{IMMEDIATE});
+			next TOPLOOP;
+		}
 		for my $choice (split(/[\s,]+/, $line)) {
 			my $choose = 1;
 			my ($bottom, $top);
@@ -635,6 +659,7 @@ sub singleton_prompt_help_cmd {
 Prompt help:
 1          - select a numbered item
 foo        - select item based on unique prefix
+/regexp    - select item based on regular expression
            - (empty) select nothing
 EOF
 }
@@ -648,6 +673,8 @@ Prompt help:
 foo        - select item based on unique prefix
 -...       - unselect specified items
 *          - choose all items
+/regexp    - select items based on regular expression
+-/regexp   - unselect items based on regular expression
            - (empty) finish selecting
 EOF
 }
-- 
1.9.2.msysgit.0
Next: Junio C Hamano
Message 1 of 6 in “git add -i: allow list (un)selection by regexp”
  1. git add -i: allow list (un)selection by regexpAarni Koskela, Dec 1, 2014
  2. Junio C HamanoDec 1, 2014
  3. Aarni KoskelaDec 2, 2014
  4. Junio C HamanoDec 2, 2014
  5. Eric SunshineDec 3, 2014
  6. Junio C HamanoDec 3, 2014

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.