git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/2] FIX: use utf8_strnwidth for line_prefix in diff.c

From
MGMd Isfarul Haque via GitGitGadget <gitgitgadget@gmail.com>
Date
Jan 24, 2024, 14:04 UTC
Message-ID
<ac9338533c9096c090d1463c1b29505bde019731.1706105064.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1653.git.git.1706105064.gitgitgadget@gmail.com>
From: Md Isfarul Haque <isfarul.876@gmail.com>
This patch adresses diff.c:2721 and proposes the fix using a new function.
Signed-off-by: Md Isfarul Haque <isfarul.876@gmail.com>
---
 diff.c | 18 ++++++++++++++++--
 diff.h |  1 +
 2 files changed, 17 insertions(+), 2 deletions(-)
diff --git a/diff.c b/diff.c
index a89a6a6128a..e3223b8ce5b 100644
--- a/diff.c
+++ b/diff.c
@@ -2300,6 +2300,20 @@ const char *diff_line_prefix(struct diff_options *opt)
 	return msgbuf->buf;
 }
 
+const struct strbuf *diff_line_prefix_buf(struct diff_options *opt)
+{
+	struct strbuf *msgbuf = (struct strbuf *)malloc(sizeof(*msgbuf));
+	if (!opt->output_prefix){
+		msgbuf->buf = "";
+		msgbuf->len = 0;
+		msgbuf->alloc = 1;
+	}
+	else {
+		msgbuf = opt->output_prefix(opt, opt->output_prefix_data);
+	}
+	return msgbuf;
+}
+
 static unsigned long sane_truncate_line(char *line, unsigned long len)
 {
 	const char *cp;
@@ -2635,7 +2649,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
 	int width, name_width, graph_width, number_width = 0, bin_width = 0;
 	const char *reset, *add_c, *del_c;
 	int extra_shown = 0;
-	const char *line_prefix = diff_line_prefix(options);
+	const struct strbuf *line_prefix = diff_line_prefix_buf(options);
 	struct strbuf out = STRBUF_INIT;
 
 	if (data->nr == 0)
@@ -2718,7 +2732,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
 	 * 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->buf, line_prefix->len, 1);
 	else
 		width = options->stat_width ? options->stat_width : 80;
 	number_width = decimal_width(max_change) > number_width ?
diff --git a/diff.h b/diff.h
index 66bd8aeb293..6eb8dc9a97e 100644
--- a/diff.h
+++ b/diff.h
@@ -460,6 +460,7 @@ const char *diff_get_color(int diff_use_color, enum color_diff ix);
 
 const char *diff_line_prefix(struct diff_options *);
 
+const struct strbuf *diff_line_prefix_buf(struct diff_options *);
 
 extern const char mime_boundary_leader[];
 
-- 
gitgitgadget
Previous: Md Isfarul Haque via GitGitGadgetNext: Christian Couder
Message 2 of 9 in “FIX: use utf8_strnwidth for line_prefix in diff.c”
  1. 0/2 [GSoC][RFC/Patch] FIX: use utf8_strnwidth for line_prefix in diff.cMd Isfarul Haque via GitGitGadget, Jan 24, 2024
  2. 1/2 FIX: use utf8_strnwidth for line_prefix in diff.cMd Isfarul Haque via GitGitGadget, Jan 24, 2024
  3. Christian CouderJan 24, 2024
  4. Md Isfarul HaqueJan 25, 2024
  5. Junio C HamanoJan 24, 2024
  6. Md Isfarul HaqueJan 25, 2024
  7. 2/2 FIX memory leak in one branchMd Isfarul Haque via GitGitGadget, Jan 24, 2024
  8. Junio C HamanoJan 24, 2024
  9. Junio C HamanoJan 24, 2024

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.