From 168eff3c802a47a4e8a97b2ec70d86e5f605a012 Mon Sep 17 00:00:00 2001 From: Markus Heidelberg Date: Wed, 28 Oct 2009 13:24:30 +0100 Subject: [PATCH 1/3] t4034-diff-words: add a test for word diff without context Signed-off-by: Markus Heidelberg Signed-off-by: Junio C Hamano --- t/t4034-diff-words.sh | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/t/t4034-diff-words.sh b/t/t4034-diff-words.sh index 4508effcaa..82240cf44d 100755 --- a/t/t4034-diff-words.sh +++ b/t/t4034-diff-words.sh @@ -63,6 +63,26 @@ test_expect_success 'word diff with runs of whitespace' ' ' +cat > expect <<\EOF +diff --git a/pre b/post +index 330b04f..5ed8eff 100644 +--- a/pre ++++ b/post +@@ -1 +1 @@ +h(4)h(4),hh[44] +@@ -3,0 +4,4 @@ a = b + c + +aa = a + +aeff = aeff * ( aaa ) +EOF + +test_expect_failure 'word diff without context' ' + + word_diff --color-words --unified=0 + +' + cat > expect <<\EOF diff --git a/pre b/post index 330b04f..5ed8eff 100644 From a4ca1465ec8afee798bf8f11d727179ca3da64a9 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 29 Oct 2009 11:45:03 +0100 Subject: [PATCH 2/3] diff --color-words -U0: fix the location of hunk headers Colored word diff without context lines firstly printed all the hunk headers among each other and then printed the diff. This was due to the code relying on getting at least one context line at the end of each hunk, where the colored words would be flushed (it is done that way to be able to ignore rewrapped lines). Noticed by Markus Heidelberg. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- diff.c | 6 ++++++ t/t4034-diff-words.sh | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/diff.c b/diff.c index e1be189742..b7ecfe3b17 100644 --- a/diff.c +++ b/diff.c @@ -656,6 +656,12 @@ static void fn_out_consume(void *priv, char *line, unsigned long len) for (i = 0; i < len && line[i] == '@'; i++) ; if (2 <= i && i < len && line[i] == ' ') { + /* flush --color-words even for --unified=0 */ + if (ecbdata->diff_words && + (ecbdata->diff_words->minus.text.size || + ecbdata->diff_words->plus.text.size)) + diff_words_show(ecbdata->diff_words); + ecbdata->nparents = i - 1; len = sane_truncate_line(ecbdata, line, len); emit_line(ecbdata->file, diff --git a/t/t4034-diff-words.sh b/t/t4034-diff-words.sh index 82240cf44d..21db6e95c4 100755 --- a/t/t4034-diff-words.sh +++ b/t/t4034-diff-words.sh @@ -77,7 +77,7 @@ cat > expect <<\EOF aeff = aeff * ( aaa ) EOF -test_expect_failure 'word diff without context' ' +test_expect_success 'word diff without context' ' word_diff --color-words --unified=0 From 76fd28283f7eeea246a06994edd43ab60e59d853 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 30 Oct 2009 10:09:06 -0700 Subject: [PATCH 3/3] diff --color-words: bit of clean-up When we introduced the "word diff" mode, we could have done one of three things: * change fn_out_consume() to "this is called every time a line worth of diff becomes ready from the lower-level diff routine. This function knows two sets of helpers (one for line-oriented diff, another for word diff), and each set has various functions to be called at certain places (e.g. hunk header, context, ...). The function's role is to inspect the incoming line, and dispatch appropriate helpers to produce either line- or word- oriented diff output." * introduce fn_out_consume_word_diff() that is "this is called every time a line worth of diff becomes ready from the lower-level diff routine, and here is what we do to prepare word oriented diff using that line." without touching fn_out_consume() at all. * Do neither of the above, and keep fn_out_consume() to "this is called every time a line worth of diff becomes ready from the lower-level diff routine, and here is what we do to output line oriented diff using that line." but sprinkle a handful of 'are we in word-diff mode? if so do this totally different thing' at random places. This patch is to at least abstract the details of "this totally different thing" out from the main codepath, in order to improve readability. We can later refactor it by introducing fn_out_consume_word_diff(), taking the second route above, but that is a separate topic. Signed-off-by: Junio C Hamano --- diff.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/diff.c b/diff.c index b7ecfe3b17..8c66e4a0d4 100644 --- a/diff.c +++ b/diff.c @@ -541,14 +541,18 @@ struct emit_callback { FILE *file; }; +/* In "color-words" mode, show word-diff of words accumulated in the buffer */ +static void diff_words_flush(struct emit_callback *ecbdata) +{ + if (ecbdata->diff_words->minus.text.size || + ecbdata->diff_words->plus.text.size) + diff_words_show(ecbdata->diff_words); +} + static void free_diff_words_data(struct emit_callback *ecbdata) { if (ecbdata->diff_words) { - /* flush buffers */ - if (ecbdata->diff_words->minus.text.size || - ecbdata->diff_words->plus.text.size) - diff_words_show(ecbdata->diff_words); - + diff_words_flush(ecbdata); free (ecbdata->diff_words->minus.text.ptr); free (ecbdata->diff_words->minus.orig); free (ecbdata->diff_words->plus.text.ptr); @@ -656,12 +660,8 @@ static void fn_out_consume(void *priv, char *line, unsigned long len) for (i = 0; i < len && line[i] == '@'; i++) ; if (2 <= i && i < len && line[i] == ' ') { - /* flush --color-words even for --unified=0 */ - if (ecbdata->diff_words && - (ecbdata->diff_words->minus.text.size || - ecbdata->diff_words->plus.text.size)) - diff_words_show(ecbdata->diff_words); - + if (ecbdata->diff_words) + diff_words_flush(ecbdata); ecbdata->nparents = i - 1; len = sane_truncate_line(ecbdata, line, len); emit_line(ecbdata->file, @@ -691,9 +691,7 @@ static void fn_out_consume(void *priv, char *line, unsigned long len) &ecbdata->diff_words->plus); return; } - if (ecbdata->diff_words->minus.text.size || - ecbdata->diff_words->plus.text.size) - diff_words_show(ecbdata->diff_words); + diff_words_flush(ecbdata); line++; len--; emit_line(ecbdata->file, plain, reset, line, len);