Skip to contentSkip to conversation

diff: handle ANSI chars in prefix when calculating diffstat width

PATCH Superseded LorenzoPegorari 2 participants 14 messages Feb 24 – Feb 27 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

cover diff: handle ANSI chars in prefix when calculating diffstat width

This patch aims to fix a bug where the calculation of the diffstat width incorrectly uses the strlen() of line_prefix instead of its actual display width.

This patch addresses the NEEDSWORK item added by ce8529b2 (diff: leave NEEDWORK notes in show_stats() function, 2022-10-21).

Also, this bug was reported and suggested to me by Junio C Hamano here:
https://lore.kernel.org/git/[email protected]/

Junio, do you wish to be included in this patch by a Reported-by and/or Suggested-by tag?

LorenzoPegorari (2):
diff: handle ANSI chars in prefix when calculating diffstat width
t4074: add test for diffstat width when prefix contains ANSI chars

 diff.c                                      | 12 ++----
 t/t4074-diff-stat-width-with-line-prefix.sh | 42 +++++++++++++++++++++
 2 files changed, 46 insertions(+), 8 deletions(-)
 create mode 100755 t/t4074-diff-stat-width-with-line-prefix.sh
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 diff: handle ANSI chars in prefix when calculating diffstat width

The diffstat width is calculated by taking the terminal width and incorrectly subtracting the strlen() of line_prefix, instead of the actual display width of line_prefix (which may contain ANSI chars).

Utilize the display width instead, obtained via utf8_strnwidth() with the flag to skip ANSI chars.

---
 diff.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
diff.c +4 -8
index 35b903a9a0..de1db28714 100644
@@ -2749,7 +2749,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 chars to get the
+	 * display width (e.g., to skip ANSI-colored strings in "log --graph").
 	 * 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)
@@ -2776,14 +2778,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 ?
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/2 t4074: add test for diffstat width when prefix contains ANSI chars

In reply to LorenzoPegorari

Add test checking the calculation of the diffstat display width when the line_prefix contains ANSI characters.

---
 t/meson.build                               |  1 +
 t/t4074-diff-stat-width-with-line-prefix.sh | 42 +++++++++++++++++++++
 2 files changed, 43 insertions(+)
 create mode 100755 t/t4074-diff-stat-width-with-line-prefix.sh
t/meson.build +1 -0
index f80e366cff..2867ba8a77 100644
@@ -502,6 +502,7 @@ integration_tests = [
   't4071-diff-minimal.sh',
   't4072-diff-max-depth.sh',
   't4073-diff-stat-name-width.sh',
+  't4074-diff-stat-width-with-line-prefix.sh',
   't4100-apply-stat.sh',
   't4101-apply-nonl.sh',
   't4102-apply-rename.sh',
t/t4074-diff-stat-width-with-line-prefix.sh new file +42 -0

42 lines not on this page · raw message

Signature · 1 line
2.43.0
#5 lore raw
In reply to LorenzoPegorari
Quoted 4 lines · LorenzoPegorari

This patch aims to fix a bug where the calculation of the diffstat width incorrectly uses the strlen() of line_prefix instead of its actual display width.

Quoted 8 lines

This patch addresses the NEEDSWORK item added by ce8529b2 (diff: leave NEEDWORK notes in show_stats() function, 2022-10-21).

Also, this bug was reported and suggested to me by Junio C Hamano here:
https://lore.kernel.org/git/[email protected]/

Junio, do you wish to be included in this patch by a Reported-by and/or Suggested-by tag?

Neither. I didn't report or suggest anything recently. I merely pointed at one out of the two NEEDSWORK comments that was written long time ago when you addressed the other one.

Quoted 2 lines

diff: handle ANSI chars in prefix when calculating diffstat width
t4074: add test for diffstat width when prefix contains ANSI chars

"ANSI chars" is a phrase that was hard to understand, as the theme of this was paged out of my working memory a long time ago ;-). I think the issue is that strings with ANSI color escape sequences are used in "log --graph" and other output, and in order to measure how many display columns these strings occupy, we must not be using strlen() but utr8_strnwidth() that knows how to count UTF-8 chars (whose display widths do not match the number of bytes) as well as that these color escape sequences do not occupy any display width?

Anyway, thanks for working on these patches.

#3 lore raw

Re: [GSoC PATCH 2/2] t4074: add test for diffstat width when prefix contains ANSI chars

In reply to LorenzoPegorari
Quoted 2 lines · LorenzoPegorari

Add test checking the calculation of the diffstat display width when the line_prefix contains ANSI characters.

Hmph, who stuffs the "line_prefix" and with what?

Quoted 6 lines
---
 t/meson.build                               |  1 +
 t/t4074-diff-stat-width-with-line-prefix.sh | 42 +++++++++++++++++++++
 2 files changed, 43 insertions(+)
 create mode 100755 t/t4074-diff-stat-width-with-line-prefix.sh

Do we need a brand new test script only to host just two tests? Is it too cumbersome to modify existing test scripts that already test "git log --graph"?

t/t4074-diff-stat-width-with-line-prefix.sh line 16

Quoted 5 lines
t/t4074-diff-stat-width-with-line-prefix.sh new file +5 -0
+setup () {
+	rm -rf * ".git" &&
+	git init &&
+	git config color.diff always
+}

Why?

Review

Quoted 2 lines
(quoted diff) +2 -0
+test_expect_success 'check width with max name-width' '
+	setup &&

Why? Shouldn't something like

	test_config color.diff always &&

be sufficient?

t/t4074-diff-stat-width-with-line-prefix.sh line 20

Quoted 1 line
t/t4074-diff-stat-width-with-line-prefix.sh new file +1 -0
+	touch "${FILE_MAX}" &&

Use of "touch" when you do not care about the timestamp of the resulting file is misleading. If you only care about its existence, do something like

	>"${FILE_MAX}" &&

instead.

t/t4074-diff-stat-width-with-line-prefix.sh line 25

Quoted 5 lines
t/t4074-diff-stat-width-with-line-prefix.sh new file +5 -0
+	git add . &&
+	git commit -m "init" &&
+	echo "text" >"${FILE_MAX}" &&
+	git add . &&
+	git commit -m "text" &&

OK, so we have two commits, one adds an empty file, followed by another that adds one line of "text" to the file.

t/t4074-diff-stat-width-with-line-prefix.sh line 28

Quoted 3 lines
t/t4074-diff-stat-width-with-line-prefix.sh new file +3 -0
+	git log --graph --stat >out &&
+	grep "${FILE} | 1" out
+'

Use "test_grep" instead of "grep" to make it easier to debug when things break, perhaps?

It is unclear what we are exactly looking for. What's the failure mode and how the miscounting of the width of the line-prefix contribute to that failure?

We have two commits, "log" without "--reverse" would give the creation of an empty file i.e., "file | 0" first and then one line addition to the file i.e., "file | 1 +" next. We only care about "${FILE} | 1" existing in the output and we do not bother checking what comes at the beginning of the line before "${FILE}" or what comes on the line fter "| 1", because we can detect the breakage we expect to see only by looking at the middle of the line? How would that work? The test deserves a bit more comment to help readers who wonder about these things.

Side note. I think I know the answer to these questions, but this project is not about me ;-) but other future contributors also would need help when they encounter this test and want to learn what it is testing.

t/t4074-diff-stat-width-with-line-prefix.sh line 42

Quoted 13 lines
t/t4074-diff-stat-width-with-line-prefix.sh new file +13 -0
+test_expect_success 'check width with longer name-width' '
+	setup &&
+	touch "${FILE_MAX}+" &&
+	git add . &&
+	git commit -m "init" &&
+	echo "text" >"${FILE_MAX}+" &&
+	git add . &&
+	git commit -m "text" &&
+	git log --graph --stat >out &&
+	grep "${FILE_LONGER} | 1" out
+'
+
+test_done

Instead of doing the repository construction twice, you can see the same effect by running "git log --graph --stat" on the same history with different values exported on COLUMNS environment variable, which I think would be easier to see and simpler to debug.

Something along the lines of ...

    COLUMNS=80 git log --graph --stat >out &&
    test_decode_color out >out.decoded &&
    test_grep "^<RED>|<RESET> ${FILE_MAX} | 1 <GREEN>+<RESET>$" out.decoded &&

    COLUMNS=79 git log --graph --stat >out &&
    test_decode_color out >out.decoded &&
    test_grep "^<RED>|<RESET> ${FILE_TRUNCATED} | 1 <GREEN>+<RESET>$" out.decoded

... perhaps?

Quoted 3 lines
t/t4074-diff-stat-width-with-line-prefix.sh new file +3 -0
+# The terminal, during a test, should default to a width of 80 columns
+FILE_MAX="filename-with-exact-length-to-take-the-max-amount-of-space-in-diffstat"
+FILE_LONGER="...name-with-exact-length-to-take-the-max-amount-of-space-in-diffstat+"
#4 lore raw

Re: [GSoC PATCH 2/2] t4074: add test for diffstat width when prefix contains ANSI chars

Quoted 6 lines · Junio C Hamano
Quoted 2 lines · LorenzoPegorari

Add test checking the calculation of the diffstat display width when the line_prefix contains ANSI characters.

Hmph, who stuffs the "line_prefix" and with what?

Yeah, I should make the commit description clearer. Will improve that.

Quoted 10 lines
Quoted 6 lines
---
 t/meson.build                               |  1 +
 t/t4074-diff-stat-width-with-line-prefix.sh | 42 +++++++++++++++++++++
 2 files changed, 43 insertions(+)
 create mode 100755 t/t4074-diff-stat-width-with-line-prefix.sh

Do we need a brand new test script only to host just two tests? Is it too cumbersome to modify existing test scripts that already test "git log --graph"?

Mmh I guess the test could be added to "t4052-stat-output.sh", which "tests --stat output of various commands". Do you agree that this the correct place? (btw, t4052 seems to me like a quite messy test...)

Quoted 8 lines

t/t4074-diff-stat-width-with-line-prefix.sh line 16

Quoted 5 lines
t/t4074-diff-stat-width-with-line-prefix.sh new file +5 -0
+setup () {
+   rm -rf * ".git" &&
+   git init &&
+   git config color.diff always
+}

Why?

Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+test_expect_success 'check width with max name-width' '
+   setup &&

Why? Shouldn't something like

      test_config color.diff always &&

be sufficient?

This is sufficient, and the setup() func makes no sense now that I know this. Thanks for pointing that out!

Quoted 9 lines
Quoted 1 line

+ touch "${FILE_MAX}" &&

Use of "touch" when you do not care about the timestamp of the resulting file is misleading. If you only care about its existence, do something like

      >"${FILE_MAX}" &&

instead.

Ack.

Quoted 8 lines

t/t4074-diff-stat-width-with-line-prefix.sh line 25

Quoted 5 lines
t/t4074-diff-stat-width-with-line-prefix.sh new file +5 -0
+   git add . &&
+   git commit -m "init" &&
+   echo "text" >"${FILE_MAX}" &&
+   git add . &&
+   git commit -m "text" &&

OK, so we have two commits, one adds an empty file, followed by another that adds one line of "text" to the file.

Yes, these are just placeholder commits. We need at least 2 commits to create a graph with "log --graph". I will make them "--allow-empty" and "--allow-empty-message" emphasize that they are placeholders.

Quoted 6 lines

t/t4074-diff-stat-width-with-line-prefix.sh line 28

Quoted 3 lines
t/t4074-diff-stat-width-with-line-prefix.sh new file +3 -0
+   git log --graph --stat >out &&
+   grep "${FILE} | 1" out
+'

Use "test_grep" instead of "grep" to make it easier to debug when things break, perhaps?

Ack.

Quoted 4 lines

It is unclear what we are exactly looking for. What's the failure mode and how the miscounting of the width of the line-prefix contribute to that failure?

Quoted 9 lines

We have two commits, "log" without "--reverse" would give the creation of an empty file i.e., "file | 0" first and then one line addition to the file i.e., "file | 1 +" next. We only care about "${FILE} | 1" existing in the output and we do not bother checking what comes at the beginning of the line before "${FILE}" or what comes on the line fter "| 1", because we can detect the breakage we expect to see only by looking at the middle of the line? How would that work? The test deserves a bit more comment to help readers who wonder about these things.

I will add comments to explain the test better.
The goal is to have a file with name that is just the exact length so that the line in the git-log output which includes the diffstat will take all the available space in the terminal (if the diffstat width is calculated correctly).
Then we test the diffstat width calculation with the terminal being the correct size for the file name, and then 1 column short.
If the file name is fully displayed first, and is then shortened by only 1 character, it means that the diffstat width was correctly calculated.

Quoted 4 lines

Side note. I think I know the answer to these questions, but this project is not about me ;-) but other future contributors also would need help when they encounter this test and want to learn what it is testing.

Thank you so much for helping me out without just giving me the solution. It must have taken longer :-)

Quoted 29 lines

t/t4074-diff-stat-width-with-line-prefix.sh line 42

Quoted 13 lines
t/t4074-diff-stat-width-with-line-prefix.sh new file +13 -0
+test_expect_success 'check width with longer name-width' '
+   setup &&
+   touch "${FILE_MAX}+" &&
+   git add . &&
+   git commit -m "init" &&
+   echo "text" >"${FILE_MAX}+" &&
+   git add . &&
+   git commit -m "text" &&
+   git log --graph --stat >out &&
+   grep "${FILE_LONGER} | 1" out
+'
+
+test_done

Instead of doing the repository construction twice, you can see the same effect by running "git log --graph --stat" on the same history with different values exported on COLUMNS environment variable, which I think would be easier to see and simpler to debug.

Something along the lines of ...

    COLUMNS=80 git log --graph --stat >out &&
    test_decode_color out >out.decoded &&
    test_grep "^<RED>|<RESET> ${FILE_MAX} | 1 <GREEN>+<RESET>$" out.decoded &&

    COLUMNS=79 git log --graph --stat >out &&
    test_decode_color out >out.decoded &&
    test_grep "^<RED>|<RESET> ${FILE_TRUNCATED} | 1 <GREEN>+<RESET>$" out.decoded
Quoted 1 line

... perhaps?

Didn't know about this. Will use it for sure.

Again, thanks a lot for the through review Junio. Just let me know if t4052 (described above) is the correct place for the test!

(Sorry for the double email Junio... why does mutt not automatically add the mailing list to CC while replying... :'[ )

#6 lore raw

v2 cover diff: handle UTF-8 chars in prefix when calculating diffstat width

In reply to LorenzoPegorari

This patch aims to fix a bug where the calculation of the diffstat width incorrectly uses the strlen() of line_prefix instead of its actual display width.

This patch addresses the NEEDSWORK item added by ce8529b2 (diff: leave NEEDWORK notes in show_stats() function, 2022-10-21).

V2 DIFF:
* Changed references from "ANSI char" to "UTF-8 char"
* Compacted the 2 test scripts in a single script, that I placed in the already existing test file t4052, which "tests --stat output for various commands"
* Added many of the changes suggested to me by Junio C Hamano [1], in order to make the test easier to read and to debug
* Added a descriptive comment for the test script, and a more complete commit message to describe the goal of the test

[1]: https://lore.kernel.org/git/[email protected]/

LorenzoPegorari (2):
diff: handle UTF-8 chars in prefix when calculating diffstat width
t4052: add test for diffstat width when prefix contains UTF-8 chars

 diff.c                 | 12 ++++--------
 t/b                    |  0
 t/t4052-stat-output.sh | 30 ++++++++++++++++++++++++++++++
 3 files changed, 34 insertions(+), 8 deletions(-)
 create mode 100644 t/b

Range-diff against v1:
1: a798eda511 ! 1: 9e8161a700 diff: handle ANSI chars in prefix when calculating diffstat width
@@ Metadata
Author: LorenzoPegorari <[email protected]>

## Commit message ##
- diff: handle ANSI chars in prefix when calculating diffstat width
+ diff: handle UTF-8 chars in prefix when calculating diffstat width

The diffstat width is calculated by taking the terminal width and
- incorrectly subtracting the strlen() of line_prefix, instead of the
- actual display width of line_prefix (which may contain ANSI chars).
+ incorrectly subtracting the strlen() of line_prefix, instead of the
+ actual display width of line_prefix, which may contain UTF-8
+ characters (e.g., ANSI-colored strings in log --graph --stat).

- Utilize the display width instead, obtained via utf8_strnwidth() with
- the flag to skip ANSI chars.
+ Utilize the display width instead, obtained via utf8_strnwidth() with
+ the flag skip_ansi.

@@ diff.c: static void show_stats(struct diffstat_t *data, struct diff_options *opt

/*
- * 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 chars to get the
-+ * display width (e.g., to skip ANSI-colored strings in "log --graph").
++ * We have width = stat_width or term_columns() columns total minus the
++ * length of line_prefix skipping UTF-8 chars to get the display width
++ * (e.g., to 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)

2:  ce25150593 < -:  ---------- t4074: add test for diffstat width when prefix contains ANSI chars
-:  ---------- > 2:  984fa10d72 t4052: add test for diffstat width when prefix contains UTF-8 chars
Signature · 1 line
2.43.0
#7 lore raw

PATCH v2 1/2 diff: handle UTF-8 chars in prefix when calculating diffstat width

The diffstat width is calculated by taking the terminal width and incorrectly subtracting the strlen() of line_prefix, instead of the actual display width of line_prefix, which may contain UTF-8 characters (e.g., ANSI-colored strings in log --graph --stat).

Utilize the display width instead, obtained via utf8_strnwidth() with the flag skip_ansi.

---
 diff.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
diff.c +4 -8
index 35b903a9a0..395cb464f4 100644
@@ -2749,7 +2749,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 UTF-8 chars to get the display width
+	 * (e.g., to 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)
@@ -2776,14 +2778,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 ?
Signature · 1 line
2.43.0
#8 lore raw

PATCH v2 2/2 t4052: add test for diffstat width when prefix contains UTF-8 chars

In reply to LorenzoPegorari

Add test checking the calculation of the diffstat display width when the line_prefix, which is text that goes before the diffstat, contains UTF-8 characters.

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 UTF-8 characters (ANSI escape codes to color the text)

---
 t/b                    |  0
 t/t4052-stat-output.sh | 30 ++++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+)
 create mode 100644 t/b
t/b new file +0 -0
new file mode 100644
index 0000000000..e69de29bb2
t/t4052-stat-output.sh +30 -0
index 740bb97091..cc4665047d 100755
@@ -413,4 +413,34 @@ test_expect_success 'merge --stat respects COLUMNS with long name' '
 	test_cmp expect actual
 '
 
+# git-log will print only 1 commit containing a single branch graph and a diffstat.
+# 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 UTF-8 characters (the ANSI escape codes), 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 UTF-8 chars 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
Signature · 1 line
2.43.0
#10 lore raw

Re: [GSoC PATCH v2 0/2] diff: handle UTF-8 chars in prefix when calculating diffstat width

In reply to LorenzoPegorari
Quoted 9 lines · LorenzoPegorari

This patch aims to fix a bug where the calculation of the diffstat width incorrectly uses the strlen() of line_prefix instead of its actual display width.

This patch addresses the NEEDSWORK item added by ce8529b2 (diff: leave NEEDWORK notes in show_stats() function, 2022-10-21).

V2 DIFF:
* Changed references from "ANSI char" to "UTF-8 char"

Is that correct? I thought these references are mostly about the ANSI color escape sequences that are used to paint strings in color e.g.,

    printf "This is shown in \033[31mRED\033[0m color\012"

uses "\033[31m" (use RED as the foreground color) and "\033[0m" (reset all styles and colors). The problem the NEEDSWORK comment talks about is that the code uses strlen() but these two sequences in the above example are 0-width as far as the terminal display width computation is concerned. And that is why we want to use utf8_strnwidth() with SKIP_ANSI bit on.

Quoted 5 lines
 diff.c                 | 12 ++++--------
 t/b                    |  0
 t/t4052-stat-output.sh | 30 ++++++++++++++++++++++++++++++
 3 files changed, 34 insertions(+), 8 deletions(-)
 create mode 100644 t/b

I doubt you meant to add a new file there ;-)

#9 lore raw

Re: [GSoC PATCH v2 2/2] t4052: add test for diffstat width when prefix contains UTF-8 chars

In reply to LorenzoPegorari
Quoted 3 lines · LorenzoPegorari

Add test checking the calculation of the diffstat display width when the line_prefix, which is text that goes before the diffstat, contains UTF-8 characters.

This does not want to say UTF-8 for two reasons. US-ASCII is a subset of UTF-8, and more importantly, most of the problems miscounting the display width of the line-prefix part comes from the fact that ANSI color escapes are 0-width but consumes strlen() bytes.

Curiously ...

t/t4052-stat-output.sh test_expect_success 'merge --stat respects COLUMNS with long name' ' · line 420

Quoted 5 lines
t/t4052-stat-output.sh +5 -0
+# git-log will print only 1 commit containing a single branch graph and a diffstat.
+# 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.

... this does say "ANSI escape codes to color the text", which is exactly what we want to say ;-)

#11 lore raw

v3 cover diff: handle ANSI escape codes in prefix when calculating diffstat width

In reply to LorenzoPegorari

This patch aims to fix a bug where the calculation of the diffstat width incorrectly uses the strlen() of line_prefix instead of its actual display width.

This patch addresses the NEEDSWORK item added by ce8529b2 (diff: leave NEEDWORK notes in show_stats() function, 2022-10-21).

Thanks Junio for you guidance.

V3 DIFF:
* Changed references from "UTF-8 char" to "ANSI escape codes"
* Removed mistakenly added empty file
* Slightly improved the test script comment

LorenzoPegorari (2):
diff: handle ANSI escape codes in prefix when calculating diffstat
width
t4052: test for diffstat width when prefix contains ANSI escape codes

 diff.c                 | 12 ++++--------
 t/t4052-stat-output.sh | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+), 8 deletions(-)

Range-diff against v2:
1: 9e8161a700 ! 1: f75d6d779e diff: handle UTF-8 chars in prefix when calculating diffstat width
@@ Metadata
Author: LorenzoPegorari <[email protected]>

## Commit message ##
- diff: handle UTF-8 chars in prefix when calculating diffstat width
+ diff: handle ANSI escape codes in prefix when calculating diffstat width

The diffstat width is calculated by taking the terminal width and incorrectly subtracting the strlen() of line_prefix, instead of the
- actual display width of line_prefix, which may contain UTF-8
- characters (e.g., ANSI-colored strings in log --graph --stat).
+ actual display width of line_prefix, which may contain ANSI escape
+ codes (e.g., ANSI-colored strings in log --graph --stat).

Utilize the display width instead, obtained via utf8_strnwidth() with the flag skip_ansi.
@@ diff.c: static void show_stats(struct diffstat_t *data, struct diff_options *opt
/*
- * 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 UTF-8 chars to get the display width
-+ * (e.g., to skip ANSI-colored strings in "log --graph --stat").
++ * 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)
2: 984fa10d72 ! 2: 1d55bff06e t4052: add test for diffstat width when prefix contains UTF-8 chars
@@ Metadata
Author: LorenzoPegorari <[email protected]>

## Commit message ##
- t4052: add test for diffstat width when prefix contains UTF-8 chars
+ 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
- UTF-8 characters.
+ 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 UTF-8 characters (ANSI escape codes to color the text)
+ which contains ANSI escape codes to color the text

- ## t/b (new) ##
-
## t/t4052-stat-output.sh ##
@@ t/t4052-stat-output.sh: test_expect_success 'merge --stat respects COLUMNS with long name' '
test_cmp expect actual
'

-+# git-log will print only 1 commit containing a single branch graph and a diffstat. ++# 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"
@@ t/t4052-stat-output.sh: test_expect_success 'merge --stat respects COLUMNS with
+# 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 UTF-8 characters (the ANSI escape codes), is calculated -+# correctly, we:
++# 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 UTF-8 chars is correct width' ' ++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)) &&

Signature · 1 line
2.43.0
#12 lore raw

PATCH v3 1/2 diff: handle ANSI escape codes in prefix when calculating diffstat width

The diffstat width is calculated by taking the terminal width and incorrectly subtracting the strlen() of line_prefix, instead of the actual display width of line_prefix, which may contain ANSI escape codes (e.g., ANSI-colored strings in log --graph --stat).

Utilize the display width instead, obtained via utf8_strnwidth() with the flag skip_ansi.

---
 diff.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
diff.c +4 -8
index 35b903a9a0..6eaf40fe2e 100644
@@ -2749,7 +2749,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)
@@ -2776,14 +2778,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 ?
Signature · 1 line
2.43.0
#13 lore raw

PATCH v3 2/2 t4052: test for diffstat width when prefix contains ANSI escape codes

In reply to LorenzoPegorari

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

---
 t/t4052-stat-output.sh | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
t/t4052-stat-output.sh +32 -0
index 740bb97091..7c749062e2 100755
@@ -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
Signature · 1 line
2.43.0