public inbox for git@vger.kernel.org
 help / color / mirror / Atom feed
From: LorenzoPegorari <lorenzo.pegorari2002@gmail.com>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>
Subject: [GSoC PATCH v3 0/2] diff: handle ANSI escape codes in prefix when calculating diffstat width
Date: Fri, 27 Feb 2026 22:43:14 +0100	[thread overview]
Message-ID: <cover.1772226209.git.lorenzo.pegorari2002@gmail.com> (raw)
In-Reply-To: <cover.1772136203.git.lorenzo.pegorari2002@gmail.com>

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).

Thanks Junio for you guidance.

V3 DIFF:
* Changed references from "UTF-8 char" to "ANSI escape codes"
* Removed mistakenly added empty file
* Slightly improved the test script comment

LorenzoPegorari (2):
  diff: handle ANSI escape codes in prefix when calculating diffstat
    width
  t4052: test for diffstat width when prefix contains ANSI escape codes

 diff.c                 | 12 ++++--------
 t/t4052-stat-output.sh | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+), 8 deletions(-)

Range-diff against v2:
1:  9e8161a700 ! 1:  f75d6d779e diff: handle UTF-8 chars in prefix when calculating diffstat width
    @@ Metadata
     Author: LorenzoPegorari <lorenzo.pegorari2002@gmail.com>
     
      ## Commit message ##
    -    diff: handle UTF-8 chars in prefix when calculating diffstat width
    +    diff: handle ANSI escape codes 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 UTF-8
    -    characters (e.g., ANSI-colored strings in `log --graph --stat`).
    +    actual display width of `line_prefix`, which may contain ANSI escape
    +    codes (e.g., ANSI-colored strings in `log --graph --stat`).
     
         Utilize the display width instead, obtained via `utf8_strnwidth()` with
         the flag `skip_ansi`.
    @@ 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 UTF-8 chars to get the display width
    -+	 * (e.g., to skip ANSI-colored strings in "log --graph --stat").
    ++	 * length of line_prefix skipping ANSI escape codes to get the display
    ++	 * width (e.g., 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:  984fa10d72 ! 2:  1d55bff06e t4052: add test for diffstat width when prefix contains UTF-8 chars
    @@ Metadata
     Author: LorenzoPegorari <lorenzo.pegorari2002@gmail.com>
     
      ## Commit message ##
    -    t4052: add test for diffstat width when prefix contains UTF-8 chars
    +    t4052: test for diffstat width when prefix contains ANSI escape codes
     
         Add test checking the calculation of the diffstat display width when the
         `line_prefix`, which is text that goes before the diffstat, contains
    -    UTF-8 characters.
    +    ANSI escape codes.
     
         This situation happens, for example, when `git log --stat --graph` is
         executed:
         * `--stat` will create a diffstat for each commit
         * `--graph` will stuff `line_prefix` with the graph portion of the log,
    -      which contains UTF-8 characters (ANSI escape codes to color the text)
    +      which contains ANSI escape codes to color the text
     
         Signed-off-by: LorenzoPegorari <lorenzo.pegorari2002@gmail.com>
     
    - ## t/b (new) ##
    -
      ## t/t4052-stat-output.sh ##
     @@ t/t4052-stat-output.sh: test_expect_success 'merge --stat respects COLUMNS with long name' '
      	test_cmp expect actual
      '
      
    -+# git-log will print only 1 commit containing a single branch graph and a diffstat.
    ++# We want git-log to print only 1 commit containing a single branch graph and a
    ++# diffstat (the diffstat display width, when not manually set through the
    ++# option "--stat-width", will be automatically calculated).
     +# The diffstat will be only one file, with a placeholder FILENAME, that, with
     +# enough terminal display width, will contain the following line:
     +#     "<RED>|<RESET>  ${FILENAME} | 0"
    @@ t/t4052-stat-output.sh: test_expect_success 'merge --stat respects COLUMNS with
     +# FILENAME in the diffstat will not be shortened, we take the FILENAME length
     +# and add 9 to it.
     +# To check if the diffstat width, when the line_prefix (the "<RED>|<RESET>" of
    -+# the graph) contains UTF-8 characters (the ANSI escape codes), is calculated
    -+# correctly, we:
    ++# the graph) contains ANSI escape codes (the ANSI escape codes to color the
    ++# text), is calculated correctly, we:
     +#     1. check if it contains the line defined before when using MIN_TERM_WIDTH
     +#     2. check if it contains the line defined before, but with the FILENAME
     +#        shortened by only one character, when using MIN_TERM_WIDTH - 1
     +
    -+test_expect_success 'diffstat where line_prefix contains UTF-8 chars is correct width' '
    ++test_expect_success 'diffstat where line_prefix contains ANSI escape codes is correct width' '
     +	FILENAME="placeholder-text-placeholder-text" &&
     +	FILENAME_TRIMMED="...eholder-text-placeholder-text" &&
     +	MIN_TERM_WIDTH=$((${#FILENAME} + 9)) &&
-- 
2.43.0


  parent reply	other threads:[~2026-02-27 21:43 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-24  1:09 [GSoC PATCH 0/2] diff: handle ANSI chars in prefix when calculating diffstat width LorenzoPegorari
2026-02-24  1:11 ` [GSoC PATCH 1/2] " LorenzoPegorari
2026-02-24  1:20 ` [GSoC PATCH 2/2] t4074: add test for diffstat width when prefix contains ANSI chars LorenzoPegorari
2026-02-24  5:43   ` Junio C Hamano
2026-02-25  2:18     ` Lorenzo Pegorari
2026-02-24  5:17 ` [GSoC PATCH 0/2] diff: handle ANSI chars in prefix when calculating diffstat width Junio C Hamano
2026-02-27 16:01 ` [GSoC PATCH v2 0/2] diff: handle UTF-8 " LorenzoPegorari
2026-02-27 16:04   ` [GSoC PATCH v2 1/2] " LorenzoPegorari
2026-02-27 16:08   ` [GSoC PATCH v2 2/2] t4052: add test for diffstat width when prefix contains UTF-8 chars LorenzoPegorari
2026-02-27 18:08     ` Junio C Hamano
2026-02-27 18:04   ` [GSoC PATCH v2 0/2] diff: handle UTF-8 chars in prefix when calculating diffstat width Junio C Hamano
2026-02-27 21:43   ` LorenzoPegorari [this message]
2026-02-27 21:45     ` [GSoC PATCH v3 1/2] diff: handle ANSI escape codes " LorenzoPegorari
2026-02-27 21:48     ` [GSoC PATCH v3 2/2] t4052: test for diffstat width when prefix contains ANSI escape codes LorenzoPegorari

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cover.1772226209.git.lorenzo.pegorari2002@gmail.com \
    --to=lorenzo.pegorari2002@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox