E586 Merge branch 'lp/diff-stat-utf8-display-width-fix' · git/git@e757df8 · GitHub
[go: up one dir, main page]

Skip to content

Commit e757df8

Browse files
committed
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
2 parents 3fe08b8 + 064b869 commit e757df8

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

diff.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2756,7 +2756,9 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
27562756
count = i; /* where we can stop scanning in data->files[] */
27572757

27582758
/*
2759-
* We have width = stat_width or term_columns() columns total.
2759+
* We have width = stat_width or term_columns() columns total minus the
2760+
* length of line_prefix skipping ANSI escape codes to get the display
2761+
* width (e.g., skip ANSI-colored strings in "log --graph --stat").
27602762
* We want a maximum of min(max_len, stat_name_width) for the name part.
27612763
* We want a maximum of min(max_change, stat_graph_width) for the +- part.
27622764
* 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)
27832785
* separators and this message, this message will "overflow"
27842786
* making the line longer than the maximum width.
27852787
*/
2786-
2787-
/*
2788-
* NEEDSWORK: line_prefix is often used for "log --graph" output
2789-
* and contains ANSI-colored string. utf8_strnwidth() should be
2790-
* used to correctly count the display width instead of strlen().
2791-
*/
27922788
if (options->stat_width == -1)
2793-
width = term_columns() - strlen(line_prefix);
2789+
width = term_columns() - utf8_strnwidth(line_prefix, strlen(line_prefix), 1);
27942790
else
27952791
width = options->stat_width ? options->stat_width : 80;
27962792
number_width = decimal_width(max_change) > number_width ?

t/t4052-stat-output.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,4 +413,36 @@ test_expect_success 'merge --stat respects COLUMNS with long name' '
413413
test_cmp expect actual
414414
'
415415

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+
416448
test_done

0 commit comments

Comments
 (0)
0