From d5addcf522deb05d259ecbc0946584d977879565 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Wed, 21 Jun 2017 15:26:36 -0400 Subject: [PATCH 1/2] add--interactive: handle EOF in prompt_yesno The prompt_yesno function loops indefinitely waiting for a "y" or "n" response. But it doesn't handle EOF, meaning that we can end up in an infinite loop of reading EOF from stdin. One way to simulate that is with: echo e | GIT_EDITOR='echo corrupt >' git add -p Let's break out of the loop and propagate the undef to the caller. Without modifying the callers that effectively turns it into a "no" response. This is reasonable for both of the current callers, and it leaves room for any future caller to check for undef explicitly. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- git-add--interactive.perl | 1 + 1 file changed, 1 insertion(+) diff --git a/git-add--interactive.perl b/git-add--interactive.perl index 4e0ab5a9bc2..7c953247379 100755 --- a/git-add--interactive.perl +++ b/git-add--interactive.perl @@ -1152,6 +1152,7 @@ sub prompt_yesno { while (1) { print colored $prompt_color, $prompt; my $line = prompt_single_character; + return undef unless defined $line; return 0 if $line =~ /^n/i; return 1 if $line =~ /^y/i; } From d85d7ecb80ebc93f7380b4196c303756ee051668 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Wed, 21 Jun 2017 15:28:59 -0400 Subject: [PATCH 2/2] add--interactive: quote commentChar regex MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since c9d961647 (i18n: add--interactive: mark edit_hunk_manually message for translation, 2016-12-14), when the user asks to edit a hunk manually, we respect core.commentChar in generating the edit instructions. However, when we then strip out comment lines, we use a simple regex like: /^$commentChar/ If your chosen comment character is a regex metacharacter, then that will behave in a confusing manner ("$", for instance, would only eliminate blank lines, not actual comment lines). We can fix that by telling perl not to respect metacharacters. Reported-by: Christian Rösch Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- git-add--interactive.perl | 2 +- t/t3701-add-interactive.sh | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/git-add--interactive.perl b/git-add--interactive.perl index 7c953247379..395dd5eb4b8 100755 --- a/git-add--interactive.perl +++ b/git-add--interactive.perl @@ -1097,7 +1097,7 @@ sub edit_hunk_manually { open $fh, '<', $hunkfile or die sprintf(__("failed to open hunk edit file for reading: %s"), $!); - my @newtext = grep { !/^$comment_line_char/ } <$fh>; + my @newtext = grep { !/^\Q$comment_line_char\E/ } <$fh>; close $fh; unlink $hunkfile; diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh index deae948c76b..2bfd41f06e4 100755 --- a/t/t3701-add-interactive.sh +++ b/t/t3701-add-interactive.sh @@ -380,4 +380,12 @@ test_expect_success 'patch mode ignores unmerged entries' ' test_cmp expected diff ' +test_expect_success 'hunk-editing handles custom comment char' ' + git reset --hard && + echo change >>file && + test_config core.commentChar "\$" && + echo e | GIT_EDITOR=true git add -p && + git diff --exit-code +' + test_done