mailinfo: move metainfo_charset to struct mailinfo

This requires us to pass the struct down to decode_header() and convert_to_utf8() callchain. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Oct 14, 2015 at 16:15 UTC 28be2d083cfce464dc898dfb78f2b957b7116277
1 file changed +19 -19
builtin/mailinfo.c
+19 -19
@@ -9,8 +9,6 @@
9
10 static FILE *cmitmsg, *patchfile;
11
12 -static const char *metainfo_charset;
13 -
12 struct mailinfo {
13 FILE *input;
14 FILE *output;
@@ -22,6 +20,7 @@ struct mailinfo {
20 int add_message_id;
21 int use_scissors;
22 int use_inbody_headers;
23 + const char *metainfo_charset;
24
25 char *message_id;
26 int patch_lines;
@@ -380,23 +379,24 @@ static struct strbuf *decode_b_segment(const struct strbuf *b_seg)
379 return out;
380 }
381
383 -static void convert_to_utf8(struct strbuf *line, const char *charset)
382 +static void convert_to_utf8(struct mailinfo *mi,
383 + struct strbuf *line, const char *charset)
384 {
385 char *out;
386
387 if (!charset || !*charset)
388 return;
389
390 - if (same_encoding(metainfo_charset, charset))
390 + if (same_encoding(mi->metainfo_charset, charset))
391 return;
392 - out = reencode_string(line->buf, metainfo_charset, charset);
392 + out = reencode_string(line->buf, mi->metainfo_charset, charset);
393 if (!out)
394 die("cannot convert from %s to %s",
395 - charset, metainfo_charset);
395 + charset, mi->metainfo_charset);
396 strbuf_attach(line, out, strlen(out), strlen(out));
397 }
398
399 -static void decode_header(struct strbuf *it)
399 +static void decode_header(struct mailinfo *mi, struct strbuf *it)
400 {
401 char *in, *ep, *cp;
402 struct strbuf outbuf = STRBUF_INIT, *dec;
@@ -459,8 +459,8 @@ static void decode_header(struct strbuf *it)
459 dec = decode_q_segment(&piecebuf, 1);
460 break;
461 }
462 - if (metainfo_charset)
463 - convert_to_utf8(dec, charset_q.buf);
462 + if (mi->metainfo_charset)
463 + convert_to_utf8(mi, dec, charset_q.buf);
464
465 strbuf_addbuf(&outbuf, dec);
466 strbuf_release(dec);
@@ -491,7 +491,7 @@ static int check_header(struct mailinfo *mi,
491 * normalize the meta information to utf8.
492 */
493 strbuf_add(&sb, line->buf + len + 2, line->len - len - 2);
494 - decode_header(&sb);
494 + decode_header(mi, &sb);
495 handle_header(&hdr_data[i], &sb);
496 ret = 1;
497 goto check_header_out;
@@ -502,7 +502,7 @@ static int check_header(struct mailinfo *mi,
502 if (cmp_header(line, "Content-Type")) {
503 len = strlen("Content-Type: ");
504 strbuf_add(&sb, line->buf + len, line->len - len);
505 - decode_header(&sb);
505 + decode_header(mi, &sb);
506 strbuf_insert(&sb, 0, "Content-Type: ", len);
507 handle_content_type(&sb);
508 ret = 1;
@@ -511,7 +511,7 @@ static int check_header(struct mailinfo *mi,
511 if (cmp_header(line, "Content-Transfer-Encoding")) {
512 len = strlen("Content-Transfer-Encoding: ");
513 strbuf_add(&sb, line->buf + len, line->len - len);
514 - decode_header(&sb);
514 + decode_header(mi, &sb);
515 handle_content_transfer_encoding(&sb);
516 ret = 1;
517 goto check_header_out;
@@ -519,7 +519,7 @@ static int check_header(struct mailinfo *mi,
519 if (cmp_header(line, "Message-Id")) {
520 len = strlen("Message-Id: ");
521 strbuf_add(&sb, line->buf + len, line->len - len);
522 - decode_header(&sb);
522 + decode_header(mi, &sb);
523 handle_message_id(mi, &sb);
524 ret = 1;
525 goto check_header_out;
@@ -674,8 +674,8 @@ static int handle_commit_msg(struct mailinfo *mi, struct strbuf *line)
674 mi->header_stage = 0;
675
676 /* normalize the log message to UTF-8. */
677 - if (metainfo_charset)
678 - convert_to_utf8(line, charset.buf);
677 + if (mi->metainfo_charset)
678 + convert_to_utf8(mi, line, charset.buf);
679
680 if (mi->use_scissors && is_scissors_line(line)) {
681 int i;
@@ -1052,7 +1052,7 @@ int cmd_mailinfo(int argc, const char **argv, const char *prefix)
1052 setup_mailinfo(&mi);
1053
1054 def_charset = get_commit_output_encoding();
1055 - metainfo_charset = def_charset;
1055 + mi.metainfo_charset = def_charset;
1056
1057 while (1 < argc && argv[1][0] == '-') {
1058 if (!strcmp(argv[1], "-k"))
@@ -1062,11 +1062,11 @@ int cmd_mailinfo(int argc, const char **argv, const char *prefix)
1062 else if (!strcmp(argv[1], "-m") || !strcmp(argv[1], "--message-id"))
1063 mi.add_message_id = 1;
1064 else if (!strcmp(argv[1], "-u"))
1065 - metainfo_charset = def_charset;
1065 + mi.metainfo_charset = def_charset;
1066 else if (!strcmp(argv[1], "-n"))
1067 - metainfo_charset = NULL;
1067 + mi.metainfo_charset = NULL;
1068 else if (starts_with(argv[1], "--encoding="))
1069 - metainfo_charset = argv[1] + 11;
1069 + mi.metainfo_charset = argv[1] + 11;
1070 else if (!strcmp(argv[1], "--scissors"))
1071 mi.use_scissors = 1;
1072 else if (!strcmp(argv[1], "--no-scissors"))