git diff --word-diff problem
Hello.
I've got a couple of generated files with obfuscated code. I want to word-diff them just to make sure that everything is right.
The thing is when word-diff gets oneline file without newline at the end it compares it with regular line by line diff.
Here is an example. Two one line files generated with perl -e "print 'a 'x10" > file
git diff --word-diff=plain file1 file2
file2 renamed from file1 +0 -0
@@ -1 +1 @@
[- a a a a a a a a a a-]
No newline at end of file
{+a a a a a ab a a a a+}
Git shows that the whole line is different.
And if I add newlines to that files everything works just as expected
git diff --word-diff=plain file1 file2
file2 renamed from file1 +0 -0
@@ -1,2 +1,2 @@ a a a a a [-a -]{+ab +}a a a a
Is that a bug or I've missed explanation in docs?
Signature · 1 line
Ivan Shirokoff
PATCH 1/1 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.
--- diff.c | 8 ++++++++ t/t4034-diff-words.sh | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 0 deletions(-)
diff.c +8 -0
@@ -1113,6 +1113,14 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)diff_words_append (line, len, &ecbdata->diff_words->plus);return ; + }else if (!prefixcmp (line,"\\ " )) { +/* +* Silently eat the "no newline at eof" marker +* (we are diffing without regard to +* whitespace anyway), and defer processing: +* more '+' lines could be after it. +*/ +return ; }diff_words_flush (ecbdata);if (ecbdata->diff_words->type ==DIFF_WORDS_PORCELAIN ) {
t/t4034-diff-words.sh +14 -0
@@ -334,4 +334,18 @@ test_expect_success 'word-diff with diff.sbe' ' word_diff --word-diff=plain ' +test_expect_success 'word-diff with no newline at EOF' ' + cat >expect <<-\EOF && + diff --git a/pre b/post + index 7bf316e..3dd0303 100644 + --- a/pre + +++ b/post + @@ -1 +1 @@ + a a [-a-]{+ab+} a a + EOF + printf "%s" "a a a a a" >pre && + printf "%s" "a a ab a a" >post && + word_diff --word-diff=plain +' + test_done
Signature · 1 line
1.7.9.rc0.168.g3847c
Re: [PATCH] word-diff: ignore '\ No newline at eof' marker
Quoted 3 lines · Thomas Rast
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).
Sounds like a very sensible simplification of the issue.
Quoted 21 lines
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]>---
diff.c | 8 ++++++++ t/t4034-diff-words.sh | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 0 deletions(-)diff.c +0
-0@@ -1113,6 +1113,14 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)diff_words_append (line, len, &ecbdata->diff_words->plus);return ;
diff.c static void fn_out_consume(void *priv, char *line, unsigned long len) · line 1125
Quoted 10 lines
diff.c +8
-0+ }else if (!prefixcmp (line,"\\ " )) { +/* +* Silently eat the "no newline at eof" marker +* (we are diffing without regard to +* whitespace anyway), and defer processing: +* more '+' lines could be after it. +*/ +return ; }diff_words_flush (ecbdata);
It took me a while to realize "defer processing" in the comment was meant to justify the placement of the new block _before_ this flush. Perhaps rephrasing it to "return without calling diff_words_flush()" would make it more readable?
Otherwise the patch looks good.
Thanks.
Quoted 24 lines
diff.c +0
-0if (ecbdata->diff_words->type ==DIFF_WORDS_PORCELAIN ) {t/t4034-diff-words.sh +14
-0@@ -334,4 +334,18 @@ test_expect_success 'word-diff with diff.sbe' ' word_diff --word-diff=plain ' +test_expect_success 'word-diff with no newline at EOF' ' + cat >expect <<-\EOF && + diff --git a/pre b/post + index 7bf316e..3dd0303 100644 + --- a/pre + +++ b/post + @@ -1 +1 @@ + a a [-a-]{+ab+} a a + EOF + printf "%s" "a a a a a" >pre && + printf "%s" "a a ab a a" >post && + word_diff --word-diff=plain +' + test_done