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

[PATCHv7 2/6] pretty.c: teach format_commit_message() to reencode the output

From
Pat Notz <patnotz@gmail.com>
Date
Nov 2, 2010, 19:59 UTC
Message-ID
<1288727952-57498-3-git-send-email-patnotz@gmail.com>
In-Reply-To
<1288727952-57498-1-git-send-email-patnotz@gmail.com>

format_commit_message() will now reencode the content if the desired output encoding is different from the encoding in the passed in commit. Callers wanting to specify the output encoding do so via the pretty_print_context struct.

Signed-off-by: Pat Notz <patnotz@gmail.com>
---
 commit.h |    3 +++
 pretty.c |   27 +++++++++++++++++++++------
 2 files changed, 24 insertions(+), 6 deletions(-)
diff --git a/commit.h b/commit.h
index a0b710f..3bfb31b 100644
--- a/commit.h
+++ b/commit.h
@@ -77,6 +77,7 @@ struct pretty_print_context
 	int need_8bit_cte;
 	int show_notes;
 	struct reflog_walk_info *reflog_info;
+	const char *output_encoding;
 };
 
 struct userformat_want {
@@ -85,6 +86,8 @@ struct userformat_want {
 
 extern int has_non_ascii(const char *text);
 struct rev_info; /* in revision.h, it circularly uses enum cmit_fmt */
+extern char *logmsg_reencode(const struct commit *commit,
+			     const char *output_encoding);
 extern char *reencode_commit_message(const struct commit *commit,
 				     const char **encoding_p);
 extern void get_commit_format(const char *arg, struct rev_info *);
diff --git a/pretty.c b/pretty.c
index c253172..8549934 100644
--- a/pretty.c
+++ b/pretty.c
@@ -403,8 +403,8 @@ static char *replace_encoding_header(char *buf, const char *encoding)
 	return strbuf_detach(&tmp, NULL);
 }
 
-static char *logmsg_reencode(const struct commit *commit,
-			     const char *output_encoding)
+char *logmsg_reencode(const struct commit *commit,
+		      const char *output_encoding)
 {
 	static const char *utf8 = "UTF-8";
 	const char *use_encoding;
@@ -555,6 +555,7 @@ struct format_commit_context {
 	const struct pretty_print_context *pretty_ctx;
 	unsigned commit_header_parsed:1;
 	unsigned commit_message_parsed:1;
+	char *message;
 	size_t width, indent1, indent2;
 
 	/* These offsets are relative to the start of the commit message. */
@@ -591,7 +592,7 @@ static int add_again(struct strbuf *sb, struct chunk *chunk)
 
 static void parse_commit_header(struct format_commit_context *context)
 {
-	const char *msg = context->commit->buffer;
+	const char *msg = context->message;
 	int i;
 
 	for (i = 0; msg[i]; i++) {
@@ -677,8 +678,8 @@ const char *format_subject(struct strbuf *sb, const char *msg,
 
 static void parse_commit_message(struct format_commit_context *c)
 {
-	const char *msg = c->commit->buffer + c->message_off;
-	const char *start = c->commit->buffer;
+	const char *msg = c->message + c->message_off;
+	const char *start = c->message;
 
 	msg = skip_empty_lines(msg);
 	c->subject_off = msg - start;
@@ -741,7 +742,7 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
 {
 	struct format_commit_context *c = context;
 	const struct commit *commit = c->commit;
-	const char *msg = commit->buffer;
+	const char *msg = c->message;
 	struct commit_list *p;
 	int h1, h2;
 
@@ -1011,13 +1012,27 @@ void format_commit_message(const struct commit *commit,
 			   const struct pretty_print_context *pretty_ctx)
 {
 	struct format_commit_context context;
+	static const char utf8[] = "UTF-8";
+	const char *enc;
+	const char *output_enc = pretty_ctx->output_encoding;
 
 	memset(&context, 0, sizeof(context));
 	context.commit = commit;
 	context.pretty_ctx = pretty_ctx;
 	context.wrap_start = sb->len;
+	context.message = commit->buffer;
+	if (output_enc) {
+		enc = get_header(commit, "encoding");
+		enc = enc ? enc : utf8;
+		if (strcmp(enc, output_enc))
+			context.message = logmsg_reencode(commit, output_enc);
+	}
+
 	strbuf_expand(sb, format, format_commit_item, &context);
 	rewrap_message_tail(sb, &context, 0, 0, 0);
+
+	if (context.message != commit->buffer)
+		free(context.message);
 }
 
 static void pp_header(enum cmit_fmt fmt,
-- 
1.7.3.2
Previous: Thiago FarinaNext: Pat Notz
Message 4 of 9 in “[PATCHv7 0/6] Add commit message options for rebase --autosquash”
  1. Pat NotzNov 2, 2010
  2. 1/6 commit: helper methods to reduce redundant blocks of codePat Notz, Nov 2, 2010
  3. Thiago FarinaNov 2, 2010
  4. 2/6 pretty.c: teach format_commit_message() to reencode the outputPat Notz, Nov 2, 2010
  5. 3/6 commit: --fixup option for use with rebase --autosquashPat Notz, Nov 2, 2010
  6. 4/6 add tests of commit --fixupPat Notz, Nov 2, 2010
  7. 5/6 commit: --squash option for use with rebase --autosquashPat Notz, Nov 2, 2010
  8. Stephen BoydNov 3, 2010
  9. 6/6 add tests of commit --squashPat Notz, Nov 2, 2010

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.