public inbox for git@vger.kernel.org
 help / color / mirror / Atom feed
From: "LorenzoPegorari via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Arsh Srivastava <arshsrivastava00@gmail.com>,
	LorenzoPegorari <lorenzo.pegorari2002@gmail.com>
Subject: [PATCH 2/5] t4052: test for diffstat width when prefix contains ANSI escape codes
Date: Tue, 10 Mar 2026 08:51:15 +0000	[thread overview]
Message-ID: <c70043a2c0d4e33286508b5699a8bf0285b2f51f.1773132678.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.2233.git.git.1773132678.gitgitgadget@gmail.com>

From: LorenzoPegorari <lorenzo.pegorari2002@gmail.com>

Add test checking the calculation of the diffstat display width when the
`line_prefix`, which is text that goes before the diffstat, contains
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 ANSI escape codes to color the text

Signed-off-by: LorenzoPegorari <lorenzo.pegorari2002@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 t/t4052-stat-output.sh | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/t/t4052-stat-output.sh b/t/t4052-stat-output.sh
index 740bb97091..7c749062e2 100755
--- a/t/t4052-stat-output.sh
+++ b/t/t4052-stat-output.sh
@@ -413,4 +413,36 @@ test_expect_success 'merge --stat respects COLUMNS with long name' '
 	test_cmp expect actual
 '
 
+# 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"
+# where "<RED>" and "<RESET>" are ANSI escape codes to color the text.
+# To calculate the minimium terminal display width MIN_TERM_WIDTH so that the
+# 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 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 ANSI escape codes is correct width' '
+	FILENAME="placeholder-text-placeholder-text" &&
+	FILENAME_TRIMMED="...eholder-text-placeholder-text" &&
+	MIN_TERM_WIDTH=$((${#FILENAME} + 9)) &&
+	test_config color.diff always &&
+	git commit --allow-empty --allow-empty-message &&
+	>${FILENAME} &&
+	git add ${FILENAME} &&
+	git commit --allow-empty-message &&
+	COLUMNS=$((MIN_TERM_WIDTH)) git log --graph --stat -n1 | test_decode_color >out &&
+	test_grep "<RED>|<RESET>  ${FILENAME} | 0" out &&
+	COLUMNS=$((MIN_TERM_WIDTH - 1)) git log --graph --stat -n1 | test_decode_color >out &&
+	test_grep "<RED>|<RESET>  ${FILENAME_TRIMMED} | 0" out
+'
+
 test_done
-- 
gitgitgadget


  parent reply	other threads:[~2026-03-10  8:51 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-10  8:51 [PATCH 0/5] Advice on checkout dirty files Arsh Srivastava via GitGitGadget
2026-03-10  8:51 ` [PATCH 1/5] diff: handle ANSI escape codes in prefix when calculating diffstat width LorenzoPegorari via GitGitGadget
2026-03-10  8:51 ` LorenzoPegorari via GitGitGadget [this message]
2026-03-10  8:51 ` [PATCH 3/5] repo: remove unnecessary variable shadow K Jayatheerth via GitGitGadget
2026-03-10  8:51 ` [PATCH 4/5] The 13th batch Junio C Hamano via GitGitGadget
2026-03-10  8:51 ` [PATCH 5/5] advice: add stashBeforeCheckout advice for dirty branch switches Arsh Srivastava via GitGitGadget
2026-03-10 10:33 ` [PATCH 0/5] Advice on checkout dirty files Phillip Wood
2026-03-10 10:42   ` Arsh Srivastava
2026-03-10 10:45     ` Arsh Srivastava
2026-03-10 13:36   ` Junio C Hamano
2026-03-10 13:40     ` Arsh Srivastava
2026-03-10 15:40       ` Junio C Hamano
2026-03-10 16:05         ` Arsh Srivastava
2026-03-10 16:08         ` Arsh Srivastava
2026-03-10 10:59 ` [PATCH v2] advice: add stashBeforeCheckout advice for dirty branch switches Arsh Srivastava via GitGitGadget
     [not found]   ` <CAOAgETOebObfZNWA5LWMDxYv8YXYpbrb9L3_ASs_AbQjiQZYZw@mail.gmail.com>
2026-03-10 11:04     ` Arsh Srivastava
2026-03-10 13:16   ` Patrick Steinhardt
2026-03-10 13:36     ` Arsh Srivastava
2026-03-10 14:24       ` Patrick Steinhardt
2026-03-10 13:28   ` [PATCH v3 0/2] Advice on checkout dirty files Arsh Srivastava via GitGitGadget
2026-03-10 13:28     ` [PATCH v3 1/2] advice: add stashBeforeCheckout advice for dirty branch switches Arsh Srivastava via GitGitGadget
2026-03-10 13:43       ` Arsh Srivastava
2026-03-10 13:28     ` [PATCH v3 2/2] advice: add stashBeforeCheckout advice for dirty branch switches [GSOC] Arsh Srivastava via GitGitGadget
2026-03-11  8:50     ` [PATCH v4 0/5] Advice on checkout dirty files Arsh Srivastava via GitGitGadget
2026-03-11  8:50       ` [PATCH v4 1/5] advice: add stashBeforeCheckout advice for dirty branch switches Arsh Srivastava via GitGitGadget
2026-03-11  8:50       ` [PATCH v4 2/5] advice: add stashBeforeCheckout advice for dirty branch switches [GSOC] Arsh Srivastava via GitGitGadget
2026-03-11  8:50       ` [PATCH v4 3/5] unpack-trees: suggesting 'git checkout -m <branch>' with its repercussions Arsh Srivastava via GitGitGadget
2026-03-11  8:50       ` [PATCH v4 4/5] Updating tests and unpack-tress.c [GSOC] Arsh Srivastava via GitGitGadget
2026-03-11  8:50       ` [PATCH v4 5/5] File updation [GSOC] Arsh Srivastava via GitGitGadget
2026-03-11 16:38       ` [PATCH v4 0/5] Advice on checkout dirty files Junio C Hamano
2026-03-11 17:06         ` Arsh Srivastava
2026-03-11 17:49       ` [PATCH v5 0/3] " Arsh Srivastava via GitGitGadget
2026-03-11 17:49         ` [PATCH v5 1/3] advice: add stashBeforeCheckout advice for dirty branch switches Arsh Srivastava via GitGitGadget
2026-03-11 17:49         ` [PATCH v5 2/3] advice: add stashBeforeCheckout advice for dirty branch switches [GSOC] Arsh Srivastava via GitGitGadget
2026-03-11 17:49         ` [PATCH v5 3/3] unpack-trees: suggesting 'git checkout -m <branch>' with its repercussions Arsh Srivastava via GitGitGadget
2026-03-12  1:02         ` [PATCH v5 0/3] Advice on checkout dirty files Junio C Hamano
2026-03-12  3:32           ` Arsh Srivastava
2026-03-12  4:00         ` [PATCH v6] unpack-trees: suggesting 'git checkout -m' with its repercussions Arsh Srivastava via GitGitGadget
2026-03-12 16:06           ` Junio C Hamano
2026-03-12 18:13             ` Arsh Srivastava
2026-03-12 18:56               ` Junio C Hamano
2026-03-12 19:03                 ` Arsh Srivastava
2026-03-12 19:07                   ` Junio C Hamano
2026-03-12 19:12                     ` Arsh Srivastava
2026-03-12 20:05           ` [PATCH v7] unpack-trees: suggest using 'git stash' when checkout fails Arsh Srivastava via GitGitGadget
2026-03-12 22:40             ` Junio C Hamano
2026-03-13  3:13               ` Arsh Srivastava
2026-03-13 10:43                 ` Karthik Nayak
2026-03-13 11:02                   ` Arsh Srivastava
2026-03-13 22:05                   ` Junio C Hamano
2026-03-13 10:49                 ` Arsh Srivastava
2026-03-13 11:04                 ` Arsh Srivastava
2026-03-10 14:31   ` [PATCH v2] advice: add stashBeforeCheckout advice for dirty branch switches Karthik Nayak
2026-03-10 14:37     ` Arsh Srivastava
2026-03-10 14:40       ` Arsh Srivastava
2026-03-10 17:15         ` Karthik Nayak
2026-03-10 18:00           ` Arsh Srivastava
2026-03-10 14:41       ` Arsh Srivastava
2026-03-10 16:48       ` Junio C Hamano
2026-03-10 17:09         ` Karthik Nayak
2026-03-14  4:27           ` Konstantin Ryabitsev
2026-03-10 17:56         ` Arsh Srivastava

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=c70043a2c0d4e33286508b5699a8bf0285b2f51f.1773132678.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=arshsrivastava00@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=lorenzo.pegorari2002@gmail.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