Skip to content

Commit 42f7d45

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent af3570e commit 42f7d45

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

git-add--interactive.perl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,14 @@ sub parse_diff {
705705
}
706706
my (@hunk) = { TEXT => [], DISPLAY => [], TYPE => 'header' };
707707

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+
708716
for (my $i = 0; $i < @diff; $i++) {
709717
if ($diff[$i] =~ /^@@ /) {
710718
push @hunk, { TEXT => [], DISPLAY => [],

t/t3701-add-interactive.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,14 @@ test_expect_success TTY 'diffFilter filters diff' '
404404
grep foo:.*content output
405405
'
406406

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+
407415
test_expect_success 'patch-mode via -i prompts for files' '
408416
git reset --hard &&
409417

0 commit comments

Comments
 (0)