Subject: [GSoC PATCH v2 0/2] diff: handle UTF-8 chars in prefix when calculating diffstat width

From: LorenzoPegorari <[email-masked]>
Date: Fri, 27 Feb 2026 17:01:07 +0100

This patch aims to fix a bug where the calculation of the diffstat width
incorrectly uses the strlen() of line_prefix instead of its actual
display width.

This patch addresses the NEEDSWORK item added by ce8529b2 (diff: leave
NEEDWORK notes in show_stats() function, 2022-10-21).

V2 DIFF:
* Changed references from "ANSI char" to "UTF-8 char"
* Compacted the 2 test scripts in a single script, that I placed in the
  already existing test file t4052, which "tests --stat output for
  various commands"
* Added many of the changes suggested to me by Junio C Hamano [1], in
  order to make the test easier to read and to debug
* Added a descriptive comment for the test script, and a more complete
  commit message to describe the goal of the test

[1]: https://lore.kernel.org/git/[email-masked]/

LorenzoPegorari (2):
  diff: handle UTF-8 chars in prefix when calculating diffstat width
  t4052: add test for diffstat width when prefix contains UTF-8 chars

 diff.c                 | 12 ++++--------
 t/b                    |  0
 t/t4052-stat-output.sh | 30 ++++++++++++++++++++++++++++++
 3 files changed, 34 insertions(+), 8 deletions(-)
 create mode 100644 t/b

Range-diff against v1:
1:  a798eda511 ! 1:  9e8161a700 diff: handle ANSI chars in prefix when calculating diffstat width
    @@ Metadata
     Author: LorenzoPegorari <[email-masked]>
     
      ## Commit message ##
    -    diff: handle ANSI chars in prefix when calculating diffstat width
    +    diff: handle UTF-8 chars in prefix when calculating diffstat width
     
         The diffstat width is calculated by taking the terminal width and
    -    incorrectly subtracting the strlen() of line_prefix, instead of the
    -    actual display width of line_prefix (which may contain ANSI chars).
    +    incorrectly subtracting the `strlen()` of `line_prefix`, instead of the
    +    actual display width of `line_prefix`, which may contain UTF-8
    +    characters (e.g., ANSI-colored strings in `log --graph --stat`).
     
    -    Utilize the display width instead, obtained via utf8_strnwidth() with
    -    the flag to skip ANSI chars.
    +    Utilize the display width instead, obtained via `utf8_strnwidth()` with
    +    the flag `skip_ansi`.
     
         Signed-off-by: LorenzoPegorari <[email-masked]>
     
    @@ diff.c: static void show_stats(struct diffstat_t *data, struct diff_options *opt
      
      	/*
     -	 * We have width = stat_width or term_columns() columns total.
    -+	 * We have width = stat_width or term_columns() columns total
    -+	 * minus the length of line_prefix skipping ANSI chars to get the
    -+	 * display width (e.g., to skip ANSI-colored strings in "log --graph").
    ++	 * We have width = stat_width or term_columns() columns total minus the
    ++	 * length of line_prefix skipping UTF-8 chars to get the display width
    ++	 * (e.g., to skip ANSI-colored strings in "log --graph --stat").
      	 * We want a maximum of min(max_len, stat_name_width) for the name part.
      	 * We want a maximum of min(max_change, stat_graph_width) for the +- part.
      	 * We also need 1 for " " and 4 + decimal_width(max_change)
2:  ce25150593 < -:  ---------- t4074: add test for diffstat width when prefix contains ANSI chars
-:  ---------- > 2:  984fa10d72 t4052: add test for diffstat width when prefix contains UTF-8 chars
-- 
2.43.0