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

[PATCHv5 4/8] pretty.c: teach format_commit_message() to reencode the output

From
Pat Notz <patnotz@gmail.com>
Date
Oct 7, 2010, 19:10 UTC
Message-ID
<1286478657-61581-5-git-send-email-patnotz@gmail.com>
In-Reply-To
<1286478657-61581-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.

Signed-off-by: Pat Notz <patnotz@gmail.com>
---
 archive.c               |    2 +-
 builtin/commit.c        |    6 +++---
 builtin/fmt-merge-msg.c |    2 +-
 commit.h                |    3 ++-
 log-tree.c              |    2 +-
 notes-cache.c           |    2 +-
 pretty.c                |   39 +++++++++++++++++++++++++++++++++++----
 submodule.c             |    4 ++--
 8 files changed, 46 insertions(+), 14 deletions(-)
diff --git a/archive.c b/archive.c
index edd6853..42b7ab1 100644
--- a/archive.c
+++ b/archive.c
@@ -51,7 +51,7 @@ static void format_subst(const struct commit *commit,
 		strbuf_add(&fmt, b + 8, c - b - 8);
 
 		strbuf_add(buf, src, b - src);
-		format_commit_message(commit, fmt.buf, buf, &ctx);
+		format_commit_message(commit, fmt.buf, buf, &ctx, NULL);
 		len -= c + 1 - src;
 		src  = c + 1;
 	}
diff --git a/builtin/commit.c b/builtin/commit.c
index ea3801d..e66f10c 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -826,7 +826,7 @@ static const char *find_author_by_nickname(const char *name)
 		struct pretty_print_context ctx = {0};
 		ctx.date_mode = DATE_NORMAL;
 		strbuf_release(&buf);
-		format_commit_message(commit, "%an <%ae>", &buf, &ctx);
+		format_commit_message(commit, "%an <%ae>", &buf, &ctx, NULL);
 		return strbuf_detach(&buf, NULL);
 	}
 	die("No existing author found with '%s'", name);
@@ -1135,8 +1135,8 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
 
 	strbuf_addstr(&format, "format:%h] %s");
 
-	format_commit_message(commit, "%an <%ae>", &author_ident, &pctx);
-	format_commit_message(commit, "%cn <%ce>", &committer_ident, &pctx);
+	format_commit_message(commit, "%an <%ae>", &author_ident, &pctx, NULL);
+	format_commit_message(commit, "%cn <%ce>", &committer_ident, &pctx, NULL);
 	if (strbuf_cmp(&author_ident, &committer_ident)) {
 		strbuf_addstr(&format, "\n Author: ");
 		strbuf_addbuf_percentquote(&format, &author_ident);
diff --git a/builtin/fmt-merge-msg.c b/builtin/fmt-merge-msg.c
index 78c7774..7619f4f 100644
--- a/builtin/fmt-merge-msg.c
+++ b/builtin/fmt-merge-msg.c
@@ -173,7 +173,7 @@ static void shortlog(const char *name, unsigned char *sha1,
 		if (subjects.nr > limit)
 			continue;
 
-		format_commit_message(commit, "%s", &sb, &ctx);
+		format_commit_message(commit, "%s", &sb, &ctx, NULL);
 		strbuf_ltrim(&sb);
 
 		if (!sb.len)
diff --git a/commit.h b/commit.h
index 6f4b586..51c7110 100644
--- a/commit.h
+++ b/commit.h
@@ -92,7 +92,8 @@ extern char *get_header(const struct commit *commit, const char *key);
 extern void userformat_find_requirements(const char *fmt, struct userformat_want *w);
 extern void format_commit_message(const struct commit *commit,
 				  const char *format, struct strbuf *sb,
-				  const struct pretty_print_context *context);
+				  const struct pretty_print_context *context,
+				  const char *output_encoding);
 extern void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
 				struct strbuf *sb,
 				const struct pretty_print_context *context);
diff --git a/log-tree.c b/log-tree.c
index b46ed3b..af3200d 100644
--- a/log-tree.c
+++ b/log-tree.c
@@ -274,7 +274,7 @@ void get_patch_filename(struct commit *commit, int nr, const char *suffix,
 		struct pretty_print_context ctx = {0};
 		ctx.date_mode = DATE_NORMAL;
 
-		format_commit_message(commit, "%f", buf, &ctx);
+		format_commit_message(commit, "%f", buf, &ctx, NULL);
 		if (max_len < buf->len)
 			strbuf_setlen(buf, max_len);
 		strbuf_addstr(buf, suffix);
diff --git a/notes-cache.c b/notes-cache.c
index dee6d62..461c474 100644
--- a/notes-cache.c
+++ b/notes-cache.c
@@ -19,7 +19,7 @@ static int notes_cache_match_validity(const char *ref, const char *validity)
 		return 0;
 
 	memset(&pretty_ctx, 0, sizeof(pretty_ctx));
-	format_commit_message(commit, "%s", &msg, &pretty_ctx);
+	format_commit_message(commit, "%s", &msg, &pretty_ctx, NULL);
 	strbuf_trim(&msg);
 
 	ret = !strcmp(msg.buf, validity);
diff --git a/pretty.c b/pretty.c
index a607fd6..e5ce7fb 100644
--- a/pretty.c
+++ b/pretty.c
@@ -1009,16 +1009,47 @@ void userformat_find_requirements(const char *fmt, struct userformat_want *w)
 
 void format_commit_message(const struct commit *commit,
 			   const char *format, struct strbuf *sb,
-			   const struct pretty_print_context *pretty_ctx)
+			   const struct pretty_print_context *pretty_ctx,
+			   const char *output_encoding)
 {
 	struct format_commit_context context;
+	static char utf8[] = "UTF-8";
+	char *enc;
+	char *buffer;
+	char *enc_buffer;
+	struct strbuf scratch_sb = STRBUF_INIT;
+	struct strbuf *sb_ptr;
+
+	enc = get_header(commit, "encoding");
+	enc = enc ? enc : utf8;
+	if(output_encoding && strcmp(enc,output_encoding)) {
+		sb_ptr = &scratch_sb;
+	} else {
+		sb_ptr = sb;
+	}
 
 	memset(&context, 0, sizeof(context));
 	context.commit = commit;
 	context.pretty_ctx = pretty_ctx;
 	context.wrap_start = sb->len;
-	strbuf_expand(sb, format, format_commit_item, &context);
-	rewrap_message_tail(sb, &context, 0, 0, 0);
+	strbuf_expand(sb_ptr, format, format_commit_item, &context);
+	rewrap_message_tail(sb_ptr, &context, 0, 0, 0);
+
+	if(sb_ptr != sb) {
+		/* if re-encoding fails, take the content byte-for-byte */
+		buffer = strbuf_detach(sb_ptr, 0);
+		enc_buffer = reencode_string(buffer, output_encoding, enc);
+		enc_buffer = enc_buffer ? enc_buffer : buffer;
+
+		strbuf_addstr(sb,enc_buffer);
+
+		if(enc_buffer != buffer)
+			free(enc_buffer);
+		free(buffer);
+	}
+
+	if(enc != utf8)
+		free(enc);
 }
 
 static void pp_header(enum cmit_fmt fmt,
@@ -1177,7 +1208,7 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
 	int need_8bit_cte = context->need_8bit_cte;
 
 	if (fmt == CMIT_FMT_USERFORMAT) {
-		format_commit_message(commit, user_format, sb, context);
+		format_commit_message(commit, user_format, sb, context, NULL);
 		return;
 	}
 
diff --git a/submodule.c b/submodule.c
index 91a4758..c108ff6 100644
--- a/submodule.c
+++ b/submodule.c
@@ -217,7 +217,7 @@ void show_submodule_summary(FILE *f, const char *path,
 			}
 			else if (add)
 				strbuf_addstr(&sb, add);
-			format_commit_message(commit, format, &sb, &ctx);
+			format_commit_message(commit, format, &sb, &ctx, NULL);
 			if (reset)
 				strbuf_addstr(&sb, reset);
 			strbuf_addch(&sb, '\n');
@@ -362,7 +362,7 @@ static void print_commit(struct commit *commit)
 	struct strbuf sb = STRBUF_INIT;
 	struct pretty_print_context ctx = {0};
 	ctx.date_mode = DATE_NORMAL;
-	format_commit_message(commit, " %h: %m %s", &sb, &ctx);
+	format_commit_message(commit, " %h: %m %s", &sb, &ctx, NULL);
 	fprintf(stderr, "%s\n", sb.buf);
 	strbuf_release(&sb);
 }
-- 
1.7.3.1
Previous: Pat NotzNext: Junio C Hamano
Message 9 of 17 in “[PATCHv5 0/8] Add commit message options for rebase --autosquash”
  1. Pat NotzOct 7, 2010
  2. 1/8 commit.c: prefer get_header() to manual searchingPat Notz, Oct 7, 2010
  3. Sverre RabbelierOct 7, 2010
  4. Sverre RabbelierOct 7, 2010
  5. Junio C HamanoOct 13, 2010
  6. 2/8 commit.c: new function for looking up a comit by namePat Notz, Oct 7, 2010
  7. Junio C HamanoOct 13, 2010
  8. 3/8 pretty.c: helper methods for getting output encodingsPat Notz, Oct 7, 2010
  9. 4/8 pretty.c: teach format_commit_message() to reencode the outputPat Notz, Oct 7, 2010
  10. Junio C HamanoOct 13, 2010
  11. Pat NotzOct 13, 2010
  12. 5/8 commit: --fixup option for use with rebase --autosquashPat Notz, Oct 7, 2010
  13. 6/8 add tests of commit --fixupPat Notz, Oct 7, 2010
  14. 7/8 commit: --squash option for use with rebase --autosquashPat Notz, Oct 7, 2010
  15. 8/8 add tests of commit --squashPat Notz, Oct 7, 2010
  16. Pat NotzOct 11, 2010
  17. Sverre RabbelierOct 12, 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.