add--interactive: detect bogus diffFilter output

It's important that the diff-filter only filter the individual lines, and that there remain a one-to-one mapping between the input and output lines. Otherwise, things like hunk-splitting will behave quite unexpectedly (e.g., you think you are splitting at one point, but it has a different effect in the text patch we apply). We can't detect all problematic cases, but we can at least catch the obvious case where we don't even have the correct number of lines. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Mar 3, 2018 at 00:58 UTC 42f7d45428e260acf535ba0d55ecc91ee81e21da
2 files changed +16
git-add--interactive.perl
+8
@@ -705,6 +705,14 @@ sub parse_diff {
705 }
706 my (@hunk) = { TEXT => [], DISPLAY => [], TYPE => 'header' };
707
708 + if (@colored && @colored != @diff) {
709 + print STDERR
710 + "fatal: mismatched output from interactive.diffFilter\n",
711 + "hint: Your filter must maintain a one-to-one correspondence\n",
712 + "hint: between its input and output lines.\n";
713 + exit 1;
714 + }
715 +
716 for (my $i = 0; $i < @diff; $i++) {
717 if ($diff[$i] =~ /^@@ /) {
718 push @hunk, { TEXT => [], DISPLAY => [],
t/t3701-add-interactive.sh
+8
@@ -404,6 +404,14 @@ test_expect_success TTY 'diffFilter filters diff' '
404 grep foo:.*content output
405 '
406
407 +test_expect_success TTY 'detect bogus diffFilter output' '
408 + git reset --hard &&
409 +
410 + echo content >test &&
411 + test_config interactive.diffFilter "echo too-short" &&
412 + printf y | test_must_fail test_terminal git add -p
413 +'
414 +
415 test_expect_success 'patch-mode via -i prompts for files' '
416 git reset --hard &&
417