From af3570ed6ca97605d62ebe1836fc893014ae844b Mon Sep 17 00:00:00 2001 From: Jeff King Date: Sat, 3 Mar 2018 00:58:10 -0500 Subject: [PATCH 1/2] t3701: add a test for interactive.diffFilter This feature was added in 01143847db (add--interactive: allow custom diff highlighting programs, 2016-02-27) but never tested. Let's add a basic test. Note that we only apply the filter when color is enabled, so we have to use test_terminal. This is an open limitation explicitly mentioned in the original commit. So take this commit as testing the status quo, and not making a statement on whether we'd want to enhance that in the future. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- t/t3701-add-interactive.sh | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh index 058698df6a..64fe56c3d5 100755 --- a/t/t3701-add-interactive.sh +++ b/t/t3701-add-interactive.sh @@ -392,6 +392,18 @@ test_expect_success TTY 'diffs can be colorized' ' grep "$(printf "\\033")" output ' +test_expect_success TTY 'diffFilter filters diff' ' + git reset --hard && + + echo content >test && + test_config interactive.diffFilter "sed s/^/foo:/" && + printf y | test_terminal git add -p >output 2>&1 && + + # avoid depending on the exact coloring or content of the prompts, + # and just make sure we saw our diff prefixed + grep foo:.*content output +' + test_expect_success 'patch-mode via -i prompts for files' ' git reset --hard && From 42f7d45428e260acf535ba0d55ecc91ee81e21da Mon Sep 17 00:00:00 2001 From: Jeff King Date: Sat, 3 Mar 2018 00:58:49 -0500 Subject: [PATCH 2/2] 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 Signed-off-by: Junio C Hamano --- git-add--interactive.perl | 8 ++++++++ t/t3701-add-interactive.sh | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/git-add--interactive.perl b/git-add--interactive.perl index 964c3a7542..ff02008abe 100755 --- a/git-add--interactive.perl +++ b/git-add--interactive.perl @@ -705,6 +705,14 @@ sub parse_diff { } my (@hunk) = { TEXT => [], DISPLAY => [], TYPE => 'header' }; + if (@colored && @colored != @diff) { + print STDERR + "fatal: mismatched output from interactive.diffFilter\n", + "hint: Your filter must maintain a one-to-one correspondence\n", + "hint: between its input and output lines.\n"; + exit 1; + } + for (my $i = 0; $i < @diff; $i++) { if ($diff[$i] =~ /^@@ /) { push @hunk, { TEXT => [], DISPLAY => [], diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh index 64fe56c3d5..9bb17f91b2 100755 --- a/t/t3701-add-interactive.sh +++ b/t/t3701-add-interactive.sh @@ -404,6 +404,14 @@ test_expect_success TTY 'diffFilter filters diff' ' grep foo:.*content output ' +test_expect_success TTY 'detect bogus diffFilter output' ' + git reset --hard && + + echo content >test && + test_config interactive.diffFilter "echo too-short" && + printf y | test_must_fail test_terminal git add -p +' + test_expect_success 'patch-mode via -i prompts for files' ' git reset --hard &&