@matheusbsbs / matheus-bagliano / commits / 177b29dcab
Fork 0

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 <patnotz@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Pat Notz committed Nov 2, 2010 at 13:59 UTC 177b29dcab4c6a1b291b718c1fb192d151356f3f
2 files changed +24 -6
commit.h
+3
@@ -77,6 +77,7 @@ struct pretty_print_context
77 int need_8bit_cte;
78 int show_notes;
79 struct reflog_walk_info *reflog_info;
80 + const char *output_encoding;
81 };
82
83 struct userformat_want {
@@ -85,6 +86,8 @@ struct userformat_want {
86
87 extern int has_non_ascii(const char *text);
88 struct rev_info; /* in revision.h, it circularly uses enum cmit_fmt */
89 +extern char *logmsg_reencode(const struct commit *commit,
90 + const char *output_encoding);
91 extern char *reencode_commit_message(const struct commit *commit,
92 const char **encoding_p);
93 extern void get_commit_format(const char *arg, struct rev_info *);
pretty.c
+21 -6
@@ -403,8 +403,8 @@ static char *replace_encoding_header(char *buf, const char *encoding)
403 return strbuf_detach(&tmp, NULL);
404 }
405
406 -static char *logmsg_reencode(const struct commit *commit,
407 - const char *output_encoding)
406 +char *logmsg_reencode(const struct commit *commit,
407 + const char *output_encoding)
408 {
409 static const char *utf8 = "UTF-8";
410 const char *use_encoding;
@@ -555,6 +555,7 @@ struct format_commit_context {
555 const struct pretty_print_context *pretty_ctx;
556 unsigned commit_header_parsed:1;
557 unsigned commit_message_parsed:1;
558 + char *message;
559 size_t width, indent1, indent2;
560
561 /* 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)
592
593 static void parse_commit_header(struct format_commit_context *context)
594 {
594 - const char *msg = context->commit->buffer;
595 + const char *msg = context->message;
596 int i;
597
598 for (i = 0; msg[i]; i++) {
@@ -677,8 +678,8 @@ const char *format_subject(struct strbuf *sb, const char *msg,
678
679 static void parse_commit_message(struct format_commit_context *c)
680 {
680 - const char *msg = c->commit->buffer + c->message_off;
681 - const char *start = c->commit->buffer;
681 + const char *msg = c->message + c->message_off;
682 + const char *start = c->message;
683
684 msg = skip_empty_lines(msg);
685 c->subject_off = msg - start;
@@ -741,7 +742,7 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
742 {
743 struct format_commit_context *c = context;
744 const struct commit *commit = c->commit;
744 - const char *msg = commit->buffer;
745 + const char *msg = c->message;
746 struct commit_list *p;
747 int h1, h2;
748
@@ -1011,13 +1012,27 @@ void format_commit_message(const struct commit *commit,
1012 const struct pretty_print_context *pretty_ctx)
1013 {
1014 struct format_commit_context context;
1015 + static const char utf8[] = "UTF-8";
1016 + const char *enc;
1017 + const char *output_enc = pretty_ctx->output_encoding;
1018
1019 memset(&context, 0, sizeof(context));
1020 context.commit = commit;
1021 context.pretty_ctx = pretty_ctx;
1022 context.wrap_start = sb->len;
1023 + context.message = commit->buffer;
1024 + if (output_enc) {
1025 + enc = get_header(commit, "encoding");
1026 + enc = enc ? enc : utf8;
1027 + if (strcmp(enc, output_enc))
1028 + context.message = logmsg_reencode(commit, output_enc);
1029 + }
1030 +
1031 strbuf_expand(sb, format, format_commit_item, &context);
1032 rewrap_message_tail(sb, &context, 0, 0, 0);
1033 +
1034 + if (context.message != commit->buffer)
1035 + free(context.message);
1036 }
1037
1038 static void pp_header(enum cmit_fmt fmt,