Skip to content

Commit 76fd282

Browse files
committed
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 <[email protected]>
1 parent a4ca146 commit 76fd282

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

diff.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -541,14 +541,18 @@ struct emit_callback {
541541
FILE *file;
542542
};
543543

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+
544552
static void free_diff_words_data(struct emit_callback *ecbdata)
545553
{
546554
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);
552556
free (ecbdata->diff_words->minus.text.ptr);
553557
free (ecbdata->diff_words->minus.orig);
554558
free (ecbdata->diff_words->plus.text.ptr);
@@ -656,12 +660,8 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
656660
for (i = 0; i < len && line[i] == '@'; i++)
657661
;
658662
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);
665665
ecbdata->nparents = i - 1;
666666
len = sane_truncate_line(ecbdata, line, len);
667667
emit_line(ecbdata->file,
@@ -691,9 +691,7 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
691691
&ecbdata->diff_words->plus);
692692
return;
693693
}
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);
697695
line++;
698696
len--;
699697
emit_line(ecbdata->file, plain, reset, line, len);

0 commit comments

Comments
 (0)