{"thread":{"id":"16490","subject":"[PATCH 2/3] Add / command in add --patch","startedAt":"2008-11-27T04:07:57Z","lastAt":"2008-11-27T22:50:15Z","messageCount":2,"participants":["William Pursell","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":3},"messages":[{"id":"96600","messageId":"492E1D1D.5090101@gmail.com","threadId":"16490","inReplyTo":null,"subject":"[PATCH 2/3] Add / command in add --patch","fromName":"William Pursell","fromEmail":"bill.pursell@gmail.com","sentAt":"2008-11-27T04:07:57Z","receivedAt":"2008-11-27T04:07:57Z","isPatch":true,"sender":{"key":"bill.pursell@gmail.com","avatar":"https://gravatar.com/avatar/3ab4313e5dfdc1fedb65206d829ba33f71f56f26e11326979d1b99d5e1c403c9?d=mp&s=160"},"body":"\n\nThis command allows the user to skip hunks that don't\nmatch the specified regex.\n\nBUG:  if the user enters an invalid regex, perl will abort.\nFor example: /+\\s*foo will abort with:\nQuantifier follows nothing in regex\nI am not a Perl hacker and would welcome suggestions\non the easiest way to deal with this.\n\nSigned-off-by: William Pursell <bill.pursell@gmail.com>\n---\n  git-add--interactive.perl |   27 +++++++++++++++++++++++----\n  1 files changed, 23 insertions(+), 4 deletions(-)\n\ndiff --git a/git-add--interactive.perl b/git-add--interactive.perl\nindex f20b880..547b5c8 100755\n--- a/git-add--interactive.perl\n+++ b/git-add--interactive.perl\n@@ -800,6 +800,7 @@ y - stage this hunk\n  n - do not stage this hunk\n  a - stage this and all the remaining hunks in the file\n  d - do not stage this hunk nor any of the remaining hunks in the file\n+/ - search for a hunk matching the given regex\n  j - leave this hunk undecided, see next undecided hunk\n  J - leave this hunk undecided, see next hunk\n  k - leave this hunk undecided, see previous undecided hunk\n@@ -876,12 +877,14 @@ sub patch_update_file {\n\n  \t$num = scalar @hunk;\n  \t$ix = 0;\n+\tmy $search_s; # User entered string to match a hunk.\n\n  \twhile (1) {\n  \t\tmy ($prev, $next, $other, $undecided, $i);\n  \t\t$other = '';\n\n  \t\tif ($num <= $ix) {\n+\t\t\tundef $search_s;\n  \t\t\t$ix = 0;\n  \t\t}\n  \t\tfor ($i = 0; $i < $ix; $i++) {\n@@ -916,11 +919,24 @@ sub patch_update_file {\n  \t\t\t$other .= ',s';\n  \t\t}\n  \t\t$other .= ',e';\n-\t\tfor (@{$hunk[$ix]{DISPLAY}}) {\n-\t\t\tprint;\n+\n+\t\tmy $line;\n+\t\tif (defined $search_s) {\n+\t\t\tmy $text = join (\"\", @{$hunk[$ix]{DISPLAY}});\n+\t\t\tif ($text !~ $search_s) {\n+\t\t\t\t$line = \"j\\n\";\n+\t\t\t} else {\n+\t\t\t\tprint $text;\n+\t\t\t}\n+\t\t} else {\n+\t\t\tfor (@{$hunk[$ix]{DISPLAY}}) {\n+\t\t\t\tprint;\n+\t\t\t}\n+\t\t}\n+\t\tif (!$line) {\n+\t\t\tprint colored $prompt_color, \"Stage this hunk [y,n,a,d,/$other,?]? \";\n+\t\t\t$line = <STDIN>;\n  \t\t}\n-\t\tprint colored $prompt_color, \"Stage this hunk [y,n,a,d$other,?]? \";\n-\t\tmy $line = <STDIN>;\n  \t\tif ($line) {\n  \t\t\tif ($line =~ /^y/i) {\n  \t\t\t\t$hunk[$ix]{USE} = 1;\n@@ -946,6 +962,9 @@ sub patch_update_file {\n  \t\t\t\t}\n  \t\t\t\tnext;\n  \t\t\t}\n+\t\t\telsif ($line =~ m|^/(.*)|) {\n+\t\t\t\t$search_s = qr{$1}m;\n+\t\t\t}\n  \t\t\telsif ($other =~ /K/ && $line =~ /^K/) {\n  \t\t\t\t$ix--;\n  \t\t\t\tnext;\n-- \n1.6.0.4.782.geea74.dirty\n\n\n-- \nWilliam Pursell\n"},{"id":"96655","messageId":"7vod00aimw.fsf@gitster.siamese.dyndns.org","threadId":"16490","inReplyTo":"492E1D1D.5090101@gmail.com","subject":"Re: [PATCH 2/3] Add / command in add --patch","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2008-11-27T22:50:15Z","receivedAt":"2008-11-27T22:50:15Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"William Pursell <bill.pursell@gmail.com> writes:\n\n> This command allows the user to skip hunks that don't\n> match the specified regex.\n>\n> BUG:  if the user enters an invalid regex, perl will abort.\n> For example: /+\\s*foo will abort with:\n> Quantifier follows nothing in regex\n\nI think that is a lessor bug that can be fixed more easily.  I think the\nbigger problem with your patch is that it breaks the code structure.\n\nIf you look at the existing code, you would notice that the loop is\nstructured in such a way that we show the hunk we currently have focus,\nget a command from the user, and the command decides what to do with the\nhunk we have focus (no-op for many of them, flip {USE} bit for some) and\nwhere to move the focus (many increments $ix, some decrements $ix).  The\n\"find\" command is about not doing anything to {USE} bit and moving the\nfocus to the hunk that has the text, so you have your additional code\ntouching wrong section of the code.\n\nI'd suggest redoing [2/3] like this.\n\n git-add--interactive.perl |   25 ++++++++++++++++++++++++-\n 1 files changed, 24 insertions(+), 1 deletions(-)\n\ndiff --git c/git-add--interactive.perl w/git-add--interactive.perl\nindex f20b880..17724d1 100755\n--- c/git-add--interactive.perl\n+++ w/git-add--interactive.perl\n@@ -800,6 +800,7 @@ y - stage this hunk\n n - do not stage this hunk\n a - stage this and all the remaining hunks in the file\n d - do not stage this hunk nor any of the remaining hunks in the file\n+/ - search for a hunk matching the given regex\n j - leave this hunk undecided, see next undecided hunk\n J - leave this hunk undecided, see next hunk\n k - leave this hunk undecided, see previous undecided hunk\n@@ -919,7 +920,7 @@ sub patch_update_file {\n \t\tfor (@{$hunk[$ix]{DISPLAY}}) {\n \t\t\tprint;\n \t\t}\n-\t\tprint colored $prompt_color, \"Stage this hunk [y,n,a,d$other,?]? \";\n+\t\tprint colored $prompt_color, \"Stage this hunk [y,n,a,d,/$other,?]? \";\n \t\tmy $line = <STDIN>;\n \t\tif ($line) {\n \t\t\tif ($line =~ /^y/i) {\n@@ -946,6 +947,28 @@ sub patch_update_file {\n \t\t\t\t}\n \t\t\t\tnext;\n \t\t\t}\n+\t\t\telsif ($line =~ m|^/(.*)|) {\n+\t\t\t\tmy $search_string;\n+\t\t\t\teval {\n+\t\t\t\t\t$search_string = qr{$1}m;\n+\t\t\t\t};\n+\t\t\t\tif ($@) {\n+\t\t\t\t\tmy ($err,$exp) = ($@, $1);\n+\t\t\t\t\t$err =~ s/ at .*git-add--interactive line \\d+, <STDIN> line \\d+.*$//;\n+\t\t\t\t\tprint STDERR \"Malformed search regexp $exp: $err\\n\";\n+\t\t\t\t\tnext;\n+\t\t\t\t}\n+\t\t\t\tmy $iy = $ix;\n+\t\t\t\twhile (1) {\n+\t\t\t\t\tmy $text = join (\"\", @{$hunk[$iy]{TEXT}});\n+\t\t\t\t\tlast if ($text =~ $search_string);\n+\t\t\t\t\t$iy++;\n+\t\t\t\t\t$iy = 0 if ($iy >= $num);\n+\t\t\t\t\tlast if ($ix == $iy);\n+\t\t\t\t}\n+\t\t\t\t$ix = $iy;\n+\t\t\t\tnext;\n+\t\t\t}\n \t\t\telsif ($other =~ /K/ && $line =~ /^K/) {\n \t\t\t\t$ix--;\n \t\t\t\tnext;\n"}]}