Skip to content

Commit c5e2df0

Browse files
committed
Merge branch 'jk/add-i-diff-filter'
The "interactive.diffFilter" used by "git add -i" must retain one-to-one correspondence between its input and output, but it was not enforced and caused end-user confusion. We now at least make sure the filtered result has the same number of lines as its input to detect a broken filter. * jk/add-i-diff-filter: add--interactive: detect bogus diffFilter output t3701: add a test for interactive.diffFilter
2 parents bd0f794 + 42f7d45 commit c5e2df0

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,26 @@ test_expect_success TTY 'diffs can be colorized' '
397397
grep "$(printf "\\033")" output
398398
'
399399

400+
test_expect_success TTY 'diffFilter filters diff' '
401+
git reset --hard &&
402+
403+
echo content >test &&
404+
test_config interactive.diffFilter "sed s/^/foo:/" &&
405+
printf y | test_terminal git add -p >output 2>&1 &&
406+
407+
# avoid depending on the exact coloring or content of the prompts,
408+
# and just make sure we saw our diff prefixed
409+
grep foo:.*content output
410+
'
411+
412+
test_expect_success TTY 'detect bogus diffFilter output' '
413+
git reset --hard &&
414+
415+
echo content >test &&
416+
test_config interactive.diffFilter "echo too-short" &&
417+
printf y | test_must_fail test_terminal git add -p
418+
'
419+
400420
test_expect_success 'patch-mode via -i prompts for files' '
401421
git reset --hard &&
402422

0 commit comments

Comments
 (0)