aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2026-03-09 14:36:55 -0700
committerJunio C Hamano <gitster@pobox.com>2026-03-09 14:36:55 -0700
commite757df82af5e9283fec2ff54d1e0eec2662c623c (patch)
treecd5826bb03074d2e2af42116301aa4384bf01b21
parent3fe08b8fd1f7731edabeab8138547ec88d6407de (diff)
parent064b869efc50be2557015665bc3dc8ac9008a6e4 (diff)
downloadgit-e757df82af5e9283fec2ff54d1e0eec2662c623c.tar.xz
Merge branch 'lp/diff-stat-utf8-display-width-fix'
"git log --graph --stat" did not count the display width of colored graph part of its own output correctly, which has been corrected. * lp/diff-stat-utf8-display-width-fix: t4052: test for diffstat width when prefix contains ANSI escape codes diff: handle ANSI escape codes in prefix when calculating diffstat width
-rw-r--r--diff.c12
-rwxr-xr-xt/t4052-stat-output.sh32
2 files changed, 36 insertions, 8 deletions
diff --git a/diff.c b/diff.c
index 501648a5c4..da6e9e7f49 100644
--- a/diff.c
+++ b/diff.c
@@ -2756,7 +2756,9 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
count = i; /* where we can stop scanning in data->files[] */
/*
- * 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 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)
@@ -2783,14 +2785,8 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
* separators and this message, this message will "overflow"
* making the line longer than the maximum width.
*/
-
- /*
- * NEEDSWORK: line_prefix is often used for "log --graph" output
- * and contains ANSI-colored string. utf8_strnwidth() should be
- * used to correctly count the display width instead of strlen().
- */
if (options->stat_width == -1)
- width = term_columns() - strlen(line_prefix);
+ width = term_columns() - utf8_strnwidth(line_prefix, strlen(line_prefix), 1);
else
width = options->stat_width ? options->stat_width : 80;
number_width = decimal_width(max_change) > number_width ?
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