git/list[1] front-page[2] threads[3] people[4] search[5] about
wed 2026-10-07 17:17 UTC

[PATCH 2/5] t4052: test for diffstat width when prefix contains ANSI escape codes

From
LorenzoPegorari via GitGitGadget <gitgitgadget@gmail.com>
Date
Mar 10, 2026, 08:51 UTC
Message-ID
<c70043a2c0d4e33286508b5699a8bf0285b2f51f.1773132678.git.gitgitgadget@gmail.com>
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
Previous: LorenzoPegorari via GitGitGadgetNext: K Jayatheerth via GitGitGadget
Message 3 of 62 in “Advice on checkout dirty files”
  1. 0/5 Advice on checkout dirty filesArsh Srivastava via GitGitGadget, Mar 10, 2026
  2. 1/5 diff: handle ANSI escape codes in prefix when calculating diffstat widthLorenzoPegorari via GitGitGadget, Mar 10, 2026
  3. 2/5 t4052: test for diffstat width when prefix contains ANSI escape codesLorenzoPegorari via GitGitGadget, Mar 10, 2026
  4. 3/5 repo: remove unnecessary variable shadowK Jayatheerth via GitGitGadget, Mar 10, 2026
  5. 4/5 The 13th batchJunio C Hamano via GitGitGadget, Mar 10, 2026
  6. 5/5 advice: add stashBeforeCheckout advice for dirty branch switchesArsh Srivastava via GitGitGadget, Mar 10, 2026
  7. Phillip WoodMar 10, 2026
  8. Arsh SrivastavaMar 10, 2026
  9. Arsh SrivastavaMar 10, 2026
  10. advice: add stashBeforeCheckout advice for dirty branch switchesArsh Srivastava via GitGitGadget, Mar 10, 2026
  11. Arsh SrivastavaMar 10, 2026
  12. Patrick SteinhardtMar 10, 2026
  13. 0/2 Advice on checkout dirty filesArsh Srivastava via GitGitGadget, Mar 10, 2026
  14. 1/2 advice: add stashBeforeCheckout advice for dirty branch switchesArsh Srivastava via GitGitGadget, Mar 10, 2026
  15. 2/2 advice: add stashBeforeCheckout advice for dirty branch switches [GSOC]Arsh Srivastava via GitGitGadget, Mar 10, 2026
  16. Junio C HamanoMar 10, 2026
  17. Arsh SrivastavaMar 10, 2026
  18. Arsh SrivastavaMar 10, 2026
  19. Arsh SrivastavaMar 10, 2026
  20. Patrick SteinhardtMar 10, 2026
  21. Karthik NayakMar 10, 2026
  22. Arsh SrivastavaMar 10, 2026
  23. Arsh SrivastavaMar 10, 2026
  24. Arsh SrivastavaMar 10, 2026
  25. Junio C HamanoMar 10, 2026
  26. Arsh SrivastavaMar 10, 2026
  27. Arsh SrivastavaMar 10, 2026
  28. Junio C HamanoMar 10, 2026
  29. Karthik NayakMar 10, 2026
  30. Karthik NayakMar 10, 2026
  31. Arsh SrivastavaMar 10, 2026
  32. Arsh SrivastavaMar 10, 2026
  33. 0/5 Advice on checkout dirty filesArsh Srivastava via GitGitGadget, Mar 11, 2026
  34. 1/5 advice: add stashBeforeCheckout advice for dirty branch switchesArsh Srivastava via GitGitGadget, Mar 11, 2026
  35. 2/5 advice: add stashBeforeCheckout advice for dirty branch switches [GSOC]Arsh Srivastava via GitGitGadget, Mar 11, 2026
  36. 3/5 unpack-trees: suggesting 'git checkout -m <branch>' with its repercussionsArsh Srivastava via GitGitGadget, Mar 11, 2026
  37. 4/5 Updating tests and unpack-tress.c [GSOC]Arsh Srivastava via GitGitGadget, Mar 11, 2026
  38. 5/5 File updation [GSOC]Arsh Srivastava via GitGitGadget, Mar 11, 2026
  39. Junio C HamanoMar 11, 2026
  40. Arsh SrivastavaMar 11, 2026
  41. 0/3 Advice on checkout dirty filesArsh Srivastava via GitGitGadget, Mar 11, 2026
  42. 1/3 advice: add stashBeforeCheckout advice for dirty branch switchesArsh Srivastava via GitGitGadget, Mar 11, 2026
  43. 2/3 advice: add stashBeforeCheckout advice for dirty branch switches [GSOC]Arsh Srivastava via GitGitGadget, Mar 11, 2026
  44. 3/3 unpack-trees: suggesting 'git checkout -m <branch>' with its repercussionsArsh Srivastava via GitGitGadget, Mar 11, 2026
  45. Junio C HamanoMar 12, 2026
  46. Arsh SrivastavaMar 12, 2026
  47. unpack-trees: suggesting 'git checkout -m' with its repercussionsArsh Srivastava via GitGitGadget, Mar 12, 2026
  48. Junio C HamanoMar 12, 2026
  49. Arsh SrivastavaMar 12, 2026
  50. Junio C HamanoMar 12, 2026
  51. Arsh SrivastavaMar 12, 2026
  52. Junio C HamanoMar 12, 2026
  53. Arsh SrivastavaMar 12, 2026
  54. unpack-trees: suggest using 'git stash' when checkout failsArsh Srivastava via GitGitGadget, Mar 12, 2026
  55. Junio C HamanoMar 12, 2026
  56. Arsh SrivastavaMar 13, 2026
  57. Karthik NayakMar 13, 2026
  58. Arsh SrivastavaMar 13, 2026
  59. Arsh SrivastavaMar 13, 2026
  60. Arsh SrivastavaMar 13, 2026
  61. Junio C HamanoMar 13, 2026
  62. Konstantin RyabitsevMar 14, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.