diff --git a/diff.c b/diff.c index 3ad1f8c31046ee3dae8a8e9bed6dc9f5a5929b78..0d7f5ea4a87d2152638f36b30d6366406d33bfa9 100644 --- a/diff.c +++ b/diff.c @@ -686,14 +686,18 @@ static void diff_words_show(struct diff_words_data *diff_words) diff_words->minus.text.size = diff_words->plus.text.size = 0; } +/* 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); @@ -773,6 +777,8 @@ static void fn_out_consume(void *priv, char *line, unsigned long len) } if (line[0] == '@') { + if (ecbdata->diff_words) + diff_words_flush(ecbdata); len = sane_truncate_line(ecbdata, line, len); find_lno(line, ecbdata); emit_line(ecbdata->file, @@ -798,9 +804,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); diff --git a/t/t4034-diff-words.sh b/t/t4034-diff-words.sh index 4508effcaacd3dbc8fadb13c7be0d631f4c946f1..21db6e95c4dd7ccf2f25d887fc2c42f3adcd4124 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_success 'word diff without context' ' + + word_diff --color-words --unified=0 + +' + cat > expect <<\EOF diff --git a/pre b/post index 330b04f..5ed8eff 100644