pretty: save commit encoding from logmsg_reencode if the caller needs it

The commit encoding is parsed by logmsg_reencode, there's no need for the caller to re-parse it again. The reencoded message now has the new encoding, not the original one. The caller would need to read commit object again before parsing. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Apr 19, 2013 at 09:08 UTC 5a10d236583f4a674e8fd969c877844c0a9ccb17
5 files changed +16 -7
builtin/blame.c
+1 -1
@@ -1425,7 +1425,7 @@ static void get_commit_info(struct commit *commit,
1425 commit_info_init(ret);
1426
1427 encoding = get_log_output_encoding();
1428 - message = logmsg_reencode(commit, encoding);
1428 + message = logmsg_reencode(commit, NULL, encoding);
1429 get_ac_line(message, "\nauthor ",
1430 &ret->author, &ret->author_mail,
1431 &ret->author_time, &ret->author_tz);
builtin/commit.c
+1 -1
@@ -955,7 +955,7 @@ static const char *read_commit_message(const char *name)
955 if (!commit)
956 die(_("could not lookup commit %s"), name);
957 out_enc = get_commit_output_encoding();
958 - return logmsg_reencode(commit, out_enc);
958 + return logmsg_reencode(commit, NULL, out_enc);
959 }
960
961 static int parse_and_validate_options(int argc, const char *argv[],
commit.h
+1
@@ -101,6 +101,7 @@ struct userformat_want {
101 extern int has_non_ascii(const char *text);
102 struct rev_info; /* in revision.h, it circularly uses enum cmit_fmt */
103 extern char *logmsg_reencode(const struct commit *commit,
104 + char **commit_encoding,
105 const char *output_encoding);
106 extern void logmsg_free(char *msg, const struct commit *commit);
107 extern void get_commit_format(const char *arg, struct rev_info *);
pretty.c
+12 -4
@@ -594,6 +594,7 @@ static char *replace_encoding_header(char *buf, const char *encoding)
594 }
595
596 char *logmsg_reencode(const struct commit *commit,
597 + char **commit_encoding,
598 const char *output_encoding)
599 {
600 static const char *utf8 = "UTF-8";
@@ -615,9 +616,15 @@ char *logmsg_reencode(const struct commit *commit,
616 sha1_to_hex(commit->object.sha1), typename(type));
617 }
618
618 - if (!output_encoding || !*output_encoding)
619 + if (!output_encoding || !*output_encoding) {
620 + if (commit_encoding)
621 + *commit_encoding =
622 + get_header(commit, msg, "encoding");
623 return msg;
624 + }
625 encoding = get_header(commit, msg, "encoding");
626 + if (commit_encoding)
627 + *commit_encoding = encoding;
628 use_encoding = encoding ? encoding : utf8;
629 if (same_encoding(use_encoding, output_encoding)) {
630 /*
@@ -658,7 +665,8 @@ char *logmsg_reencode(const struct commit *commit,
665 if (out)
666 out = replace_encoding_header(out, output_encoding);
667
661 - free(encoding);
668 + if (!commit_encoding)
669 + free(encoding);
670 /*
671 * If the re-encoding failed, out might be NULL here; in that
672 * case we just return the commit message verbatim.
@@ -1288,7 +1296,7 @@ void format_commit_message(const struct commit *commit,
1296 context.commit = commit;
1297 context.pretty_ctx = pretty_ctx;
1298 context.wrap_start = sb->len;
1291 - context.message = logmsg_reencode(commit, output_enc);
1299 + context.message = logmsg_reencode(commit, NULL, output_enc);
1300
1301 strbuf_expand(sb, format, format_commit_item, &context);
1302 rewrap_message_tail(sb, &context, 0, 0, 0);
@@ -1451,7 +1459,7 @@ void pretty_print_commit(const struct pretty_print_context *pp,
1459 }
1460
1461 encoding = get_log_output_encoding();
1454 - msg = reencoded = logmsg_reencode(commit, encoding);
1462 + msg = reencoded = logmsg_reencode(commit, NULL, encoding);
1463
1464 if (pp->fmt == CMIT_FMT_ONELINE || pp->fmt == CMIT_FMT_EMAIL)
1465 indent = 0;
revision.c
+1 -1
@@ -2291,7 +2291,7 @@ static int commit_match(struct commit *commit, struct rev_info *opt)
2291 * in it.
2292 */
2293 encoding = get_log_output_encoding();
2294 - message = logmsg_reencode(commit, encoding);
2294 + message = logmsg_reencode(commit, NULL, encoding);
2295
2296 /* Copy the commit to temporary if we are using "fake" headers */
2297 if (buf.len)