Skip to content

Commit 0e383e1

Browse files
committed
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 <[email protected]>
1 parent 0ad782f commit 0e383e1

File tree

1 file changed

+38
-12
lines changed

1 file changed

+38
-12
lines changed

diff.c

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,24 @@ static void emit_add_line(const char *reset,
498498
}
499499
}
500500

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+
501519
static void emit_hunk_header(struct emit_callback *ecbdata,
502520
const char *line, int len)
503521
{
@@ -603,7 +621,6 @@ static void emit_rewrite_lines(struct emit_callback *ecb,
603621
{
604622
const char *endp = NULL;
605623
static const char *nneof = " No newline at end of file\n";
606-
const char *old = diff_get_color(ecb->color_diff, DIFF_FILE_OLD);
607624
const char *reset = diff_get_color(ecb->color_diff, DIFF_RESET);
608625

609626
while (0 < size) {
@@ -613,8 +630,7 @@ static void emit_rewrite_lines(struct emit_callback *ecb,
613630
len = endp ? (endp - data + 1) : size;
614631
if (prefix != '+') {
615632
ecb->lno_in_preimage++;
616-
emit_line_0(ecb->opt, old, reset, '-',
617-
data, len);
633+
emit_del_line(reset, ecb, data, len);
618634
} else {
619635
ecb->lno_in_postimage++;
620636
emit_add_line(reset, ecb, data, len);
@@ -1250,17 +1266,27 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
12501266
return;
12511267
}
12521268

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 '+':
12621271
ecbdata->lno_in_postimage++;
12631272
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;
12641290
}
12651291
}
12661292

0 commit comments

Comments
 (0)