Skip to content

Commit b6633a0

Browse files
dschogitster
authored andcommitted
add -p: detect more mismatches between plain vs colored diffs
When parsing the colored version of a diff, the interactive `add` command really relies on the colored version having the same number of lines as the plain (uncolored) version. That is an invariant. We already have code to verify correctly when the colored diff has less lines than the plain diff. Modulo an off-by-one bug: If the last diff line has no matching colored one, the code pretends to succeed, still. To make matters worse, when we adjusted the test in 1e4ffc7 (t3701: adjust difffilter test, 2020-01-14), we did not catch this because `add -p` fails for a _different_ reason: it does not find any colored hunk header that contains a parseable line range. If we change the test case so that the line range _can_ be parsed, the bug is exposed. Let's address all of the above by - fixing the off-by-one, - adjusting the test case to allow `add -p` to parse the line range - making the test case more stringent by verifying that the expected error message is shown Also adjust a misleading code comment about the now-fixed code. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ad60ddd commit b6633a0

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

add-patch.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,10 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
592592
if (colored_eol)
593593
colored_p = colored_eol + 1;
594594
else if (p != pend)
595-
/* colored shorter than non-colored? */
595+
/* non-colored has more lines? */
596+
goto mismatched_output;
597+
else if (colored_p == colored_pend)
598+
/* last line has no matching colored one? */
596599
goto mismatched_output;
597600
else
598601
colored_p = colored_pend;

t/t3701-add-interactive.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -761,9 +761,10 @@ test_expect_success 'detect bogus diffFilter output' '
761761
git reset --hard &&
762762
763763
echo content >test &&
764-
test_config interactive.diffFilter "sed 1d" &&
764+
test_config interactive.diffFilter "sed 6d" &&
765765
printf y >y &&
766-
force_color test_must_fail git add -p <y
766+
force_color test_must_fail git add -p <y >output 2>&1 &&
767+
grep "mismatched output" output
767768
'
768769

769770
test_expect_success 'diff.algorithm is passed to `git diff-files`' '

0 commit comments

Comments
 (0)