Skip to content

Commit 755c9fd

Browse files
committed
Fix interdiff context trimming when output is colorized
When interdiff's output is set as colorized, that means the output from the diff execution inside interdiff contains color escape codes. The color escape codes break trim_context(), which doesn't expect a line to start with an escape character. Fix trim_context() to handle colorized output from diff.
1 parent ecb9554 commit 755c9fd

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/interdiff.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -988,18 +988,28 @@ trim_context (FILE *f /* positioned at start of @@ line */,
988988

989989
if (read_atatline (line, &orig_offset, &orig_count,
990990
&new_offset, &new_count))
991-
error (EXIT_FAILURE, 0, "Line not understood: %s",
992-
line);
991+
goto line_not_understood;
993992

994993
orig_orig_count = new_orig_count = orig_count;
995994
orig_new_count = new_new_count = new_count;
996995
fgetpos (f, &pos);
997996
while (orig_count || new_count) {
997+
char *first_char;
998+
998999
if (getline (&line, &linelen, f) < 0)
9991000
break;
10001001

1002+
/* Skip past any color escape code for a +/- line */
1003+
if (line[0] == 0x1b) {
1004+
first_char = strpbrk(line, "+-");
1005+
if (!first_char)
1006+
goto line_not_understood;
1007+
} else {
1008+
first_char = line;
1009+
}
1010+
10011011
total_count++;
1002-
switch (line[0]) {
1012+
switch (*first_char) {
10031013
case '\n':
10041014
whitespace_damage("input");
10051015
case ' ' :
@@ -1081,6 +1091,10 @@ trim_context (FILE *f /* positioned at start of @@ line */,
10811091
error (0, 0, "hunk-splitting is required in this case, but is not yet implemented");
10821092
error (1, 0, "use the -U option to work around this");
10831093
return 0;
1094+
1095+
line_not_understood:
1096+
error (EXIT_FAILURE, 0, "Line not understood: %s", line);
1097+
return 0;
10841098
}
10851099

10861100
static int

0 commit comments

Comments
 (0)