Skip to content

Commit c7c2bc0

Browse files
trastgitster
authored andcommitted
word-diff: ignore '\ No newline at eof' marker
The word-diff logic accumulates + and - lines until another line type appears (normally [ @\]), at which point it generates the word diff. This is usually correct, but it breaks when the preimage does not have a newline at EOF: $ printf "%s" "a a a" >a $ printf "%s\n" "a ab a" >b $ git diff --no-index --word-diff a b diff --git 1/a 2/b index 9f68e94..6a7c02f 100644 --- 1/a +++ 2/b @@ -1 +1 @@ [-a a a-] No newline at end of file {+a ab a+} Because of the order of the lines in a unified diff @@ -1 +1 @@ -a a a \ No newline at end of file +a ab a the '\' line flushed the buffers, and the - and + lines were never matched with each other. A proper fix would defer such markers until the end of the hunk. However, word-diff is inherently whitespace-ignoring, so as a cheap fix simply ignore the marker (and hide it from the output). We use a prefix match for '\ ' to parallel the logic in apply.c:parse_fragment(). We currently do not localize this string (just accept other variants of it in git-apply), but this should be future-proof. Noticed-by: Ivan Shirokoff <[email protected]> Signed-off-by: Thomas Rast <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 37475f9 commit c7c2bc0

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

diff.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,15 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
11111111
diff_words_append(line, len,
11121112
&ecbdata->diff_words->plus);
11131113
return;
1114+
} else if (!prefixcmp(line, "\\ ")) {
1115+
/*
1116+
* Eat the "no newline at eof" marker as if we
1117+
* saw a "+" or "-" line with nothing on it,
1118+
* and return without diff_words_flush() to
1119+
* defer processing. If this is the end of
1120+
* preimage, more "+" lines may come after it.
1121+
*/
1122+
return;
11141123
}
11151124
diff_words_flush(ecbdata);
11161125
if (ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN) {

t/t4034-diff-words.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,4 +333,18 @@ test_expect_success 'word-diff with diff.sbe' '
333333
word_diff --word-diff=plain
334334
'
335335
336+
test_expect_success 'word-diff with no newline at EOF' '
337+
cat >expect <<-\EOF &&
338+
diff --git a/pre b/post
339+
index 7bf316e..3dd0303 100644
340+
--- a/pre
341+
+++ b/post
342+
@@ -1 +1 @@
343+
a a [-a-]{+ab+} a a
344+
EOF
345+
printf "%s" "a a a a a" >pre &&
346+
printf "%s" "a a ab a a" >post &&
347+
word_diff --word-diff=plain
348+
'
349+
336350
test_done

0 commit comments

Comments
 (0)