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

Re: [PATCH 2/3] Add / command in add --patch

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 27, 2008, 22:50 UTC
Message-ID
<7vod00aimw.fsf@gitster.siamese.dyndns.org>
In-Reply-To
<492E1D1D.5090101@gmail.com>
William Pursell <bill.pursell@gmail.com> writes:
Show 6 quoted lines
> This command allows the user to skip hunks that don't
> match the specified regex.
>
> BUG:  if the user enters an invalid regex, perl will abort.
> For example: /+\s*foo will abort with:
> Quantifier follows nothing in regex

I think that is a lessor bug that can be fixed more easily. I think the bigger problem with your patch is that it breaks the code structure.

If you look at the existing code, you would notice that the loop is structured in such a way that we show the hunk we currently have focus, get a command from the user, and the command decides what to do with the hunk we have focus (no-op for many of them, flip {USE} bit for some) and where to move the focus (many increments $ix, some decrements $ix). The "find" command is about not doing anything to {USE} bit and moving the focus to the hunk that has the text, so you have your additional code touching wrong section of the code.

I'd suggest redoing [2/3] like this.
 git-add--interactive.perl |   25 ++++++++++++++++++++++++-
 1 files changed, 24 insertions(+), 1 deletions(-)
diff --git c/git-add--interactive.perl w/git-add--interactive.perl
index f20b880..17724d1 100755
--- c/git-add--interactive.perl
+++ w/git-add--interactive.perl
@@ -800,6 +800,7 @@ y - stage this hunk
 n - do not stage this hunk
 a - stage this and all the remaining hunks in the file
 d - do not stage this hunk nor any of the remaining hunks in the file
+/ - search for a hunk matching the given regex
 j - leave this hunk undecided, see next undecided hunk
 J - leave this hunk undecided, see next hunk
 k - leave this hunk undecided, see previous undecided hunk
@@ -919,7 +920,7 @@ sub patch_update_file {
 		for (@{$hunk[$ix]{DISPLAY}}) {
 			print;
 		}
-		print colored $prompt_color, "Stage this hunk [y,n,a,d$other,?]? ";
+		print colored $prompt_color, "Stage this hunk [y,n,a,d,/$other,?]? ";
 		my $line = <STDIN>;
 		if ($line) {
 			if ($line =~ /^y/i) {
@@ -946,6 +947,28 @@ sub patch_update_file {
 				}
 				next;
 			}
+			elsif ($line =~ m|^/(.*)|) {
+				my $search_string;
+				eval {
+					$search_string = qr{$1}m;
+				};
+				if ($@) {
+					my ($err,$exp) = ($@, $1);
+					$err =~ s/ at .*git-add--interactive line \d+, <STDIN> line \d+.*$//;
+					print STDERR "Malformed search regexp $exp: $err\n";
+					next;
+				}
+				my $iy = $ix;
+				while (1) {
+					my $text = join ("", @{$hunk[$iy]{TEXT}});
+					last if ($text =~ $search_string);
+					$iy++;
+					$iy = 0 if ($iy >= $num);
+					last if ($ix == $iy);
+				}
+				$ix = $iy;
+				next;
+			}
 			elsif ($other =~ /K/ && $line =~ /^K/) {
 				$ix--;
 				next;
Previous: William Pursell
Message 2 of 2 in “Add / command in add --patch”
  1. 2/3 Add / command in add --patchWilliam Pursell, Nov 27, 2008
  2. Junio C HamanoNov 27, 2008

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.