From 755c9fd22528074b884785134fd76c63b1b2e742 Mon Sep 17 00:00:00 2001 From: Sultan Alsawaf Date: Fri, 12 Sep 2025 13:50:36 -0700 Subject: [PATCH] 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. --- src/interdiff.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/interdiff.c b/src/interdiff.c index 54d6c0f4..ab6d9719 100644 --- a/src/interdiff.c +++ b/src/interdiff.c @@ -988,18 +988,28 @@ trim_context (FILE *f /* positioned at start of @@ line */, if (read_atatline (line, &orig_offset, &orig_count, &new_offset, &new_count)) - error (EXIT_FAILURE, 0, "Line not understood: %s", - line); + goto line_not_understood; orig_orig_count = new_orig_count = orig_count; orig_new_count = new_new_count = new_count; fgetpos (f, &pos); while (orig_count || new_count) { + char *first_char; + if (getline (&line, &linelen, f) < 0) break; + /* Skip past any color escape code for a +/- line */ + if (line[0] == 0x1b) { + first_char = strpbrk(line, "+-"); + if (!first_char) + goto line_not_understood; + } else { + first_char = line; + } + total_count++; - switch (line[0]) { + switch (*first_char) { case '\n': whitespace_damage("input"); case ' ' : @@ -1081,6 +1091,10 @@ trim_context (FILE *f /* positioned at start of @@ line */, error (0, 0, "hunk-splitting is required in this case, but is not yet implemented"); error (1, 0, "use the -U option to work around this"); return 0; + + line_not_understood: + error (EXIT_FAILURE, 0, "Line not understood: %s", line); + return 0; } static int