diff.c: add emit_del_line() and emit_context_line()

Traditionally, we only had emit_add_line() helper, which knows how to find and paint whitespace breakages on the given line, because we only care about whitespace breakages introduced in new lines. The context lines and old (i.e. deleted) lines are emitted with a simpler emit_line_0() that paints the entire line in plain or old colors. Identify callers of emit_line_0() that show deleted lines and context lines, have them call new helpers, emit_del_line() and emit_context_line(), so that we can later tweak what is done to these two classes of lines. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed May 26, 2015 at 09:56 UTC 0e383e185ae57a8ee6ba015a43749f6d34fe3c9a
1 file changed +38 -12
diff.c
+38 -12
@@ -498,6 +498,24 @@ static void emit_add_line(const char *reset,
498 }
499 }
500
501 +static void emit_del_line(const char *reset,
502 + struct emit_callback *ecbdata,
503 + const char *line, int len)
504 +{
505 + const char *set = diff_get_color(ecbdata->color_diff, DIFF_FILE_OLD);
506 +
507 + emit_line_0(ecbdata->opt, set, reset, '-', line, len);
508 +}
509 +
510 +static void emit_context_line(const char *reset,
511 + struct emit_callback *ecbdata,
512 + const char *line, int len)
513 +{
514 + const char *set = diff_get_color(ecbdata->color_diff, DIFF_PLAIN);
515 +
516 + emit_line_0(ecbdata->opt, set, reset, ' ', line, len);
517 +}
518 +
519 static void emit_hunk_header(struct emit_callback *ecbdata,
520 const char *line, int len)
521 {
@@ -603,7 +621,6 @@ static void emit_rewrite_lines(struct emit_callback *ecb,
621 {
622 const char *endp = NULL;
623 static const char *nneof = " No newline at end of file\n";
606 - const char *old = diff_get_color(ecb->color_diff, DIFF_FILE_OLD);
624 const char *reset = diff_get_color(ecb->color_diff, DIFF_RESET);
625
626 while (0 < size) {
@@ -613,8 +630,7 @@ static void emit_rewrite_lines(struct emit_callback *ecb,
630 len = endp ? (endp - data + 1) : size;
631 if (prefix != '+') {
632 ecb->lno_in_preimage++;
616 - emit_line_0(ecb->opt, old, reset, '-',
617 - data, len);
633 + emit_del_line(reset, ecb, data, len);
634 } else {
635 ecb->lno_in_postimage++;
636 emit_add_line(reset, ecb, data, len);
@@ -1250,17 +1266,27 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
1266 return;
1267 }
1268
1253 - if (line[0] != '+') {
1254 - const char *color =
1255 - diff_get_color(ecbdata->color_diff,
1256 - line[0] == '-' ? DIFF_FILE_OLD : DIFF_PLAIN);
1257 - ecbdata->lno_in_preimage++;
1258 - if (line[0] == ' ')
1259 - ecbdata->lno_in_postimage++;
1260 - emit_line(ecbdata->opt, color, reset, line, len);
1261 - } else {
1269 + switch (line[0]) {
1270 + case '+':
1271 ecbdata->lno_in_postimage++;
1272 emit_add_line(reset, ecbdata, line + 1, len - 1);
1273 + break;
1274 + case '-':
1275 + ecbdata->lno_in_preimage++;
1276 + emit_del_line(reset, ecbdata, line + 1, len - 1);
1277 + break;
1278 + case ' ':
1279 + ecbdata->lno_in_postimage++;
1280 + ecbdata->lno_in_preimage++;
1281 + emit_context_line(reset, ecbdata, line + 1, len - 1);
1282 + break;
1283 + default:
1284 + /* incomplete line at the end */
1285 + ecbdata->lno_in_preimage++;
1286 + emit_line(ecbdata->opt,
1287 + diff_get_color(ecbdata->color_diff, DIFF_PLAIN),
1288 + reset, line, len);
1289 + break;
1290 }
1291 }
1292