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

LorenzoPegorari committed Feb 27, 2026 at 22:48 UTC 064b869efc50be2557015665bc3dc8ac9008a6e4
1 file changed +32
t/t4052-stat-output.sh
+32
@@ -413,4 +413,36 @@ test_expect_success 'merge --stat respects COLUMNS with long name' '
413 test_cmp expect actual
414 '
415
416 +# We want git-log to print only 1 commit containing a single branch graph and a
417 +# diffstat (the diffstat display width, when not manually set through the
418 +# option "--stat-width", will be automatically calculated).
419 +# The diffstat will be only one file, with a placeholder FILENAME, that, with
420 +# enough terminal display width, will contain the following line:
421 +# "<RED>|<RESET> ${FILENAME} | 0"
422 +# where "<RED>" and "<RESET>" are ANSI escape codes to color the text.
423 +# To calculate the minimium terminal display width MIN_TERM_WIDTH so that the
424 +# FILENAME in the diffstat will not be shortened, we take the FILENAME length
425 +# and add 9 to it.
426 +# To check if the diffstat width, when the line_prefix (the "<RED>|<RESET>" of
427 +# the graph) contains ANSI escape codes (the ANSI escape codes to color the
428 +# text), is calculated correctly, we:
429 +# 1. check if it contains the line defined before when using MIN_TERM_WIDTH
430 +# 2. check if it contains the line defined before, but with the FILENAME
431 +# shortened by only one character, when using MIN_TERM_WIDTH - 1
432 +
433 +test_expect_success 'diffstat where line_prefix contains ANSI escape codes is correct width' '
434 + FILENAME="placeholder-text-placeholder-text" &&
435 + FILENAME_TRIMMED="...eholder-text-placeholder-text" &&
436 + MIN_TERM_WIDTH=$((${#FILENAME} + 9)) &&
437 + test_config color.diff always &&
438 + git commit --allow-empty --allow-empty-message &&
439 + >${FILENAME} &&
440 + git add ${FILENAME} &&
441 + git commit --allow-empty-message &&
442 + COLUMNS=$((MIN_TERM_WIDTH)) git log --graph --stat -n1 | test_decode_color >out &&
443 + test_grep "<RED>|<RESET> ${FILENAME} | 0" out &&
444 + COLUMNS=$((MIN_TERM_WIDTH - 1)) git log --graph --stat -n1 | test_decode_color >out &&
445 + test_grep "<RED>|<RESET> ${FILENAME_TRIMMED} | 0" out
446 +'
447 +
448 test_done