From: Pat Notz Date: Tue, 2 Nov 2010 19:59:08 +0000 (-0600) Subject: pretty.c: teach format_commit_message() to reencode the output X-Git-Tag: v1.7.4-rc0~90^2~4 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=177b29dcab4c6a1b291b718c1fb192d151356f3f;p=git.git pretty.c: teach format_commit_message() to reencode the output 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 Signed-off-by: Junio C Hamano --- diff --git a/commit.h b/commit.h index a0b710f84..3bfb31b5e 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 c253172ef..854993475 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,