Skip to content

Commit fdfb4cf

Browse files
committed
Merge branch 'jc/hide-cr-in-diff-from-less'
* jc/hide-cr-in-diff-from-less: diff: Help "less" hide ^M from the output
2 parents 11bd3dd + 3928097 commit fdfb4cf

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

combine-diff.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,18 @@ static int hunk_comment_line(const char *bol)
496496
return (isalpha(ch) || ch == '_' || ch == '$');
497497
}
498498

499+
static void show_line_to_eol(const char *line, int len, const char *reset)
500+
{
501+
int saw_cr_at_eol = 0;
502+
if (len < 0)
503+
len = strlen(line);
504+
saw_cr_at_eol = (len && line[len-1] == '\r');
505+
506+
printf("%.*s%s%s\n", len - saw_cr_at_eol, line,
507+
reset,
508+
saw_cr_at_eol ? "\r" : "");
509+
}
510+
499511
static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent,
500512
int use_color)
501513
{
@@ -589,7 +601,7 @@ static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent,
589601
else
590602
putchar(' ');
591603
}
592-
printf("%s%s\n", ll->line, c_reset);
604+
show_line_to_eol(ll->line, -1, c_reset);
593605
ll = ll->next;
594606
}
595607
if (cnt < lno)
@@ -613,7 +625,7 @@ static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent,
613625
putchar(' ');
614626
p_mask <<= 1;
615627
}
616-
printf("%.*s%s\n", sl->len, sl->bol, c_reset);
628+
show_line_to_eol(sl->bol, sl->len, c_reset);
617629
}
618630
}
619631
}

diff.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,13 +513,20 @@ const char *diff_get_color(int diff_use_color, enum color_diff ix)
513513

514514
static void emit_line(FILE *file, const char *set, const char *reset, const char *line, int len)
515515
{
516-
int has_trailing_newline = (len > 0 && line[len-1] == '\n');
516+
int has_trailing_newline, has_trailing_carriage_return;
517+
518+
has_trailing_newline = (len > 0 && line[len-1] == '\n');
517519
if (has_trailing_newline)
518520
len--;
521+
has_trailing_carriage_return = (len > 0 && line[len-1] == '\r');
522+
if (has_trailing_carriage_return)
523+
len--;
519524

520525
fputs(set, file);
521526
fwrite(line, len, 1, file);
522527
fputs(reset, file);
528+
if (has_trailing_carriage_return)
529+
fputc('\r', file);
523530
if (has_trailing_newline)
524531
fputc('\n', file);
525532
}

t/t4019-diff-wserror.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,16 @@ test_expect_success 'trailing empty lines (2)' '
178178
179179
'
180180

181+
test_expect_success 'do not color trailing cr in context' '
182+
git config --unset core.whitespace
183+
rm -f .gitattributes &&
184+
echo AAAQ | tr Q "\015" >G &&
185+
git add G &&
186+
echo BBBQ | tr Q "\015" >>G
187+
git diff --color G | tr "\015" Q >output &&
188+
grep "BBB.*${blue_grep}Q" output &&
189+
grep "AAA.*\[mQ" output
190+
191+
'
192+
181193
test_done

0 commit comments

Comments
 (0)