[PATCH 3/3] diff: fix the location of hunk headers for "git diff --color-words -U0"

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Colored word diff without context lines firstly printed all the hunk
headers among each other and then printed the diff.

Because the word diff cannot be calculated before the end of the diff
(added/removed lines) hunk, it was calculated directly before first line
of context after the diff. But this didn't work if there was no context.
In this case the diff wasn't printed in fn_out_consume(), but entirely
in free_diff_words_data(). This also led to calculate the colored diff
from the whole diff in one swoop instead of calculating it in several
independent steps (one step per hunk).

We now calculate and print the word diff directly before the next hunk
header. The word diff of the last hunk is still printed in
free_diff_words_data().

Signed-off-by: Markus Heidelberg <markus.heidelberg@xxxxxx>
---
 diff.c                |   13 +++++++++----
 t/t4034-diff-words.sh |    2 +-
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/diff.c b/diff.c
index 067e5a0..e95fe9b 100644
--- a/diff.c
+++ b/diff.c
@@ -785,10 +785,15 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
 			diff_words_append(line, len,
 					  &ecbdata->diff_words->plus);
 			return;
-		} else if (line[0] == ' ') {
-			if (ecbdata->diff_words->minus.text.size ||
-			    ecbdata->diff_words->plus.text.size)
-				diff_words_show(ecbdata->diff_words);
+		}
+		/*
+		 * If line[0] == '@' then this prints the content of the
+		 * previous hunk, necessary for 0-context.
+		 */
+		if (ecbdata->diff_words->minus.text.size ||
+		    ecbdata->diff_words->plus.text.size)
+			diff_words_show(ecbdata->diff_words);
+		if (line[0] == ' ') {
 			line++;
 			len--;
 			emit_line(ecbdata->file, plain, reset, line, len);
diff --git a/t/t4034-diff-words.sh b/t/t4034-diff-words.sh
index 82240cf..21db6e9 100755
--- a/t/t4034-diff-words.sh
+++ b/t/t4034-diff-words.sh
@@ -77,7 +77,7 @@ cat > expect <<\EOF
 <GREEN>aeff = aeff * ( aaa )<RESET>
 EOF
 
-test_expect_failure 'word diff without context' '
+test_expect_success 'word diff without context' '
 
 	word_diff --color-words --unified=0
 
-- 
1.6.5.2.86.g61663

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]