@matheusbsbs / matheus-bagliano / commits / 76fd28283f
Fork 0

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 <gitster@pobox.com>

Junio C Hamano committed Oct 30, 2009 at 10:09 UTC 76fd28283f7eeea246a06994edd43ab60e59d853
1 file changed +12 -14
diff.c
+12 -14
@@ -541,14 +541,18 @@ struct emit_callback {
541 FILE *file;
542 };
543
544 +/* In "color-words" mode, show word-diff of words accumulated in the buffer */
545 +static void diff_words_flush(struct emit_callback *ecbdata)
546 +{
547 + if (ecbdata->diff_words->minus.text.size ||
548 + ecbdata->diff_words->plus.text.size)
549 + diff_words_show(ecbdata->diff_words);
550 +}
551 +
552 static void free_diff_words_data(struct emit_callback *ecbdata)
553 {
554 if (ecbdata->diff_words) {
547 - /* flush buffers */
548 - if (ecbdata->diff_words->minus.text.size ||
549 - ecbdata->diff_words->plus.text.size)
550 - diff_words_show(ecbdata->diff_words);
551 -
555 + diff_words_flush(ecbdata);
556 free (ecbdata->diff_words->minus.text.ptr);
557 free (ecbdata->diff_words->minus.orig);
558 free (ecbdata->diff_words->plus.text.ptr);
@@ -656,12 +660,8 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
660 for (i = 0; i < len && line[i] == '@'; i++)
661 ;
662 if (2 <= i && i < len && line[i] == ' ') {
659 - /* flush --color-words even for --unified=0 */
660 - if (ecbdata->diff_words &&
661 - (ecbdata->diff_words->minus.text.size ||
662 - ecbdata->diff_words->plus.text.size))
663 - diff_words_show(ecbdata->diff_words);
664 -
663 + if (ecbdata->diff_words)
664 + diff_words_flush(ecbdata);
665 ecbdata->nparents = i - 1;
666 len = sane_truncate_line(ecbdata, line, len);
667 emit_line(ecbdata->file,
@@ -691,9 +691,7 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
691 &ecbdata->diff_words->plus);
692 return;
693 }
694 - if (ecbdata->diff_words->minus.text.size ||
695 - ecbdata->diff_words->plus.text.size)
696 - diff_words_show(ecbdata->diff_words);
694 + diff_words_flush(ecbdata);
695 line++;
696 len--;
697 emit_line(ecbdata->file, plain, reset, line, len);