git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: git@vger.kernel.org
Subject: [PATCH 15/26] mailinfo: move metainfo_charset to struct mailinfo
Date: Tue, 13 Oct 2015 16:16:36 -0700	[thread overview]
Message-ID: <1444778207-859-16-git-send-email-gitster@pobox.com> (raw)
In-Reply-To: <1444778207-859-1-git-send-email-gitster@pobox.com>

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>
---
 builtin/mailinfo.c | 40 +++++++++++++++++++---------------------
 1 file changed, 19 insertions(+), 21 deletions(-)

diff --git a/builtin/mailinfo.c b/builtin/mailinfo.c
index 5302c03..7e01efa 100644
--- a/builtin/mailinfo.c
+++ b/builtin/mailinfo.c
@@ -9,8 +9,6 @@
 
 static FILE *cmitmsg, *patchfile;
 
-static const char *metainfo_charset;
-
 struct mailinfo {
 	FILE *input;
 	FILE *output;
@@ -22,6 +20,7 @@ struct mailinfo {
 	int add_message_id;
 	int use_scissors;
 	int use_inbody_headers; /* defaults to 1 */
+	const char *metainfo_charset;
 
 	char *message_id;
 	int patch_lines;
@@ -293,7 +292,7 @@ static void cleanup_space(struct strbuf *sb)
 	}
 }
 
-static void decode_header(struct strbuf *line);
+static void decode_header(struct mailinfo *mi, struct strbuf *line);
 static const char *header[MAX_HDR_PARSED] = {
 	"From","Subject","Date",
 };
@@ -336,7 +335,7 @@ static int check_header(struct mailinfo *mi,
 			 * normalize the meta information to utf8.
 			 */
 			strbuf_add(&sb, line->buf + len + 2, line->len - len - 2);
-			decode_header(&sb);
+			decode_header(mi, &sb);
 			handle_header(&hdr_data[i], &sb);
 			ret = 1;
 			goto check_header_out;
@@ -347,7 +346,7 @@ static int check_header(struct mailinfo *mi,
 	if (cmp_header(line, "Content-Type")) {
 		len = strlen("Content-Type: ");
 		strbuf_add(&sb, line->buf + len, line->len - len);
-		decode_header(&sb);
+		decode_header(mi, &sb);
 		strbuf_insert(&sb, 0, "Content-Type: ", len);
 		handle_content_type(&sb);
 		ret = 1;
@@ -356,7 +355,7 @@ static int check_header(struct mailinfo *mi,
 	if (cmp_header(line, "Content-Transfer-Encoding")) {
 		len = strlen("Content-Transfer-Encoding: ");
 		strbuf_add(&sb, line->buf + len, line->len - len);
-		decode_header(&sb);
+		decode_header(mi, &sb);
 		handle_content_transfer_encoding(&sb);
 		ret = 1;
 		goto check_header_out;
@@ -364,7 +363,7 @@ static int check_header(struct mailinfo *mi,
 	if (cmp_header(line, "Message-Id")) {
 		len = strlen("Message-Id: ");
 		strbuf_add(&sb, line->buf + len, line->len - len);
-		decode_header(&sb);
+		decode_header(mi, &sb);
 		handle_message_id(mi, &sb);
 		ret = 1;
 		goto check_header_out;
@@ -520,23 +519,24 @@ static struct strbuf *decode_b_segment(const struct strbuf *b_seg)
 	return out;
 }
 
-static void convert_to_utf8(struct strbuf *line, const char *charset)
+static void convert_to_utf8(struct mailinfo *mi,
+			    struct strbuf *line, const char *charset)
 {
 	char *out;
 
-	if (!charset || !*charset)
+	if (!mi->metainfo_charset || !charset || !*charset)
 		return;
 
-	if (same_encoding(metainfo_charset, charset))
+	if (same_encoding(mi->metainfo_charset, charset))
 		return;
-	out = reencode_string(line->buf, metainfo_charset, charset);
+	out = reencode_string(line->buf, mi->metainfo_charset, charset);
 	if (!out)
 		die("cannot convert from %s to %s",
-		    charset, metainfo_charset);
+		    charset, mi->metainfo_charset);
 	strbuf_attach(line, out, strlen(out), strlen(out));
 }
 
-static void decode_header(struct strbuf *it)
+static void decode_header(struct mailinfo *mi, struct strbuf *it)
 {
 	char *in, *ep, *cp;
 	struct strbuf outbuf = STRBUF_INIT, *dec;
@@ -599,8 +599,7 @@ static void decode_header(struct strbuf *it)
 			dec = decode_q_segment(&piecebuf, 1);
 			break;
 		}
-		if (metainfo_charset)
-			convert_to_utf8(dec, charset_q.buf);
+		convert_to_utf8(mi, dec, charset_q.buf);
 
 		strbuf_addbuf(&outbuf, dec);
 		strbuf_release(dec);
@@ -774,8 +773,7 @@ static int handle_commit_msg(struct mailinfo *mi, struct strbuf *line)
 		mi->header_stage = 0;
 
 	/* normalize the log message to UTF-8. */
-	if (metainfo_charset)
-		convert_to_utf8(line, charset.buf);
+	convert_to_utf8(mi, line, charset.buf);
 
 	if (mi->use_scissors && is_scissors_line(line)) {
 		int i;
@@ -1074,7 +1072,7 @@ int cmd_mailinfo(int argc, const char **argv, const char *prefix)
 	setup_mailinfo(&mi);
 
 	def_charset = get_commit_output_encoding();
-	metainfo_charset = def_charset;
+	mi.metainfo_charset = def_charset;
 
 	while (1 < argc && argv[1][0] == '-') {
 		if (!strcmp(argv[1], "-k"))
@@ -1084,11 +1082,11 @@ int cmd_mailinfo(int argc, const char **argv, const char *prefix)
 		else if (!strcmp(argv[1], "-m") || !strcmp(argv[1], "--message-id"))
 			mi.add_message_id = 1;
 		else if (!strcmp(argv[1], "-u"))
-			metainfo_charset = def_charset;
+			mi.metainfo_charset = def_charset;
 		else if (!strcmp(argv[1], "-n"))
-			metainfo_charset = NULL;
+			mi.metainfo_charset = NULL;
 		else if (starts_with(argv[1], "--encoding="))
-			metainfo_charset = argv[1] + 11;
+			mi.metainfo_charset = argv[1] + 11;
 		else if (!strcmp(argv[1], "--scissors"))
 			mi.use_scissors = 1;
 		else if (!strcmp(argv[1], "--no-scissors"))
-- 
2.6.1-320-g86a1181

  parent reply	other threads:[~2015-10-13 23:17 UTC|newest]

Thread overview: 124+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-13 23:16 [PATCH 00/26] mailinfo libification Junio C Hamano
2015-10-13 23:16 ` [PATCH 01/26] mailinfo: remove a no-op call convert_to_utf8(it, "") Junio C Hamano
2015-10-13 23:16 ` [PATCH 02/26] mailinfo: fix for off-by-one error in boundary stack Junio C Hamano
2015-10-14 20:12   ` Stefan Beller
2015-10-14 20:28     ` Junio C Hamano
2015-10-13 23:16 ` [PATCH 03/26] mailinfo: fold decode_header_bq() into decode_header() Junio C Hamano
2015-10-13 23:16 ` [PATCH 04/26] mailinfo: move handle_boundary() lower Junio C Hamano
2015-10-13 23:16 ` [PATCH 05/26] mailinfo: get rid of function-local static states Junio C Hamano
2015-10-13 23:16 ` [PATCH 06/26] mailinfo: always pass "line" as an argument Junio C Hamano
2015-10-14 20:22   ` Stefan Beller
2015-10-14 20:27     ` Junio C Hamano
2015-10-13 23:16 ` [PATCH 07/26] mailinfo: move global "line" into mailinfo() function Junio C Hamano
2015-10-14 20:27   ` Stefan Beller
2015-10-13 23:16 ` [PATCH 08/26] mailinfo: introduce "struct mailinfo" to hold globals Junio C Hamano
2015-10-13 23:16 ` [PATCH 09/26] mailinfo: move keep_subject & keep_non_patch_bracket to struct mailinfo Junio C Hamano
2015-10-13 23:16 ` [PATCH 10/26] mailinfo: move global "FILE *fin, *fout" " Junio C Hamano
2015-10-13 23:16 ` [PATCH 11/26] mailinfo: move filter/header stage " Junio C Hamano
2015-10-13 23:16 ` [PATCH 12/26] mailinfo: move patch_lines " Junio C Hamano
2015-10-13 23:16 ` [PATCH 13/26] mailinfo: move add_message_id and message_id " Junio C Hamano
2015-10-13 23:16 ` [PATCH 14/26] mailinfo: move use_scissors and use_inbody_headers " Junio C Hamano
2015-10-13 23:16 ` Junio C Hamano [this message]
2015-10-13 23:16 ` [PATCH 16/26] mailinfo: move transfer_encoding " Junio C Hamano
2015-10-13 23:16 ` [PATCH 17/26] mailinfo: move charset " Junio C Hamano
2015-10-13 23:16 ` [PATCH 18/26] mailinfo: handle_commit_msg() shouldn't be called after finding patchbreak Junio C Hamano
2015-10-13 23:16 ` [PATCH 19/26] mailinfo: move cmitmsg and patchfile to struct mailinfo Junio C Hamano
2015-10-14  1:37   ` [PATCH 27/26] mailinfo: close patchfile Junio C Hamano
2015-10-14  1:46     ` [PATCH 28/26] am: make direct call to mailinfo Junio C Hamano
2015-10-13 23:16 ` [PATCH 20/26] mailinfo: move [ps]_hdr_data to struct mailinfo Junio C Hamano
2015-10-13 23:16 ` [PATCH 21/26] mailinfo: keep the parsed log message in a strbuf Junio C Hamano
2015-10-13 23:16 ` [PATCH 22/26] mailinfo: move content/content_top to struct mailinfo Junio C Hamano
2015-10-13 23:16 ` [PATCH 23/26] mailinfo: handle errors found in decode_header() better Junio C Hamano
2015-10-13 23:16 ` [PATCH 24/26] mailinfo: handle charset conversion errors in the caller Junio C Hamano
2015-10-13 23:16 ` [PATCH 25/26] mailinfo: remove calls to exit() and die() deep in the callchain Junio C Hamano
2015-10-13 23:16 ` [PATCH 26/26] mailinfo: libify the whole thing Junio C Hamano
2015-10-14 20:45 ` [PATCH v2 00/31] libify mailinfo and call it directly from am Junio C Hamano
2015-10-14 20:45   ` [PATCH v2 01/31] mailinfo: remove a no-op call convert_to_utf8(it, "") Junio C Hamano
2015-10-14 20:45   ` [PATCH v2 02/31] mailinfo: fix for off-by-one error in boundary stack Junio C Hamano
2015-10-14 20:45   ` [PATCH v2 03/31] mailinfo: explicitly close file handle to the patch output Junio C Hamano
2015-10-14 20:45   ` [PATCH v2 04/31] mailinfo: fold decode_header_bq() into decode_header() Junio C Hamano
2015-10-14 20:45   ` [PATCH v2 05/31] mailinfo: move handle_boundary() lower Junio C Hamano
2015-10-14 20:45   ` [PATCH v2 06/31] mailinfo: get rid of function-local static states Junio C Hamano
2015-10-14 20:45   ` [PATCH v2 07/31] mailinfo: always pass "line" as an argument Junio C Hamano
2015-10-14 20:45   ` [PATCH v2 08/31] mailinfo: move global "line" into mailinfo() function Junio C Hamano
2015-10-14 20:45   ` [PATCH v2 09/31] mailinfo: introduce "struct mailinfo" to hold globals Junio C Hamano
2015-10-14 20:45   ` [PATCH v2 10/31] mailinfo: move keep_subject & keep_non_patch_bracket to struct mailinfo Junio C Hamano
2015-10-14 20:45   ` [PATCH v2 11/31] mailinfo: move global "FILE *fin, *fout" " Junio C Hamano
2015-10-14 20:45   ` [PATCH v2 12/31] mailinfo: move filter/header stage " Junio C Hamano
2015-10-14 20:45   ` [PATCH v2 13/31] mailinfo: move patch_lines " Junio C Hamano
2015-10-14 20:45   ` [PATCH v2 14/31] mailinfo: move add_message_id and message_id " Junio C Hamano
2015-10-14 20:45   ` [PATCH v2 15/31] mailinfo: move use_scissors and use_inbody_headers " Junio C Hamano
2015-10-14 20:45   ` [PATCH v2 16/31] mailinfo: move metainfo_charset " Junio C Hamano
2015-10-15 20:47     ` Eric Sunshine
2015-10-14 20:45   ` [PATCH v2 17/31] mailinfo: move transfer_encoding " Junio C Hamano
2015-10-14 20:45   ` [PATCH v2 18/31] mailinfo: move charset " Junio C Hamano
2015-10-14 20:45   ` [PATCH v2 19/31] mailinfo: handle_commit_msg() shouldn't be called after finding patchbreak Junio C Hamano
2015-10-14 20:45   ` [PATCH v2 20/31] mailinfo: move cmitmsg and patchfile to struct mailinfo Junio C Hamano
2015-10-14 22:55     ` Junio C Hamano
2015-10-14 20:45   ` [PATCH v2 21/31] mailinfo: move [ps]_hdr_data " Junio C Hamano
2015-10-14 22:57     ` Junio C Hamano
2015-10-14 20:45   ` [PATCH v2 22/31] mailinfo: keep the parsed log message in a strbuf Junio C Hamano
2015-10-14 20:45   ` [PATCH v2 23/31] mailinfo: move content/content_top to struct mailinfo Junio C Hamano
2015-10-14 20:45   ` [PATCH v2 24/31] mailinfo: move read_one_header_line() closer to its callers Junio C Hamano
2015-10-14 20:45   ` [PATCH v2 25/31] mailinfo: move check_header() after the helpers it uses Junio C Hamano
2015-10-14 20:45   ` [PATCH v2 26/31] mailinfo: move cleanup_space() before its users Junio C Hamano
2015-10-14 20:45   ` [PATCH v2 27/31] mailinfo: move definition of MAX_HDR_PARSED to closer to its use Junio C Hamano
2015-10-14 20:45   ` [PATCH v2 28/31] mailinfo: libify Junio C Hamano
2015-10-14 20:45   ` [PATCH v2 29/31] mailinfo: handle charset conversion errors in the caller Junio C Hamano
2015-10-14 20:45   ` [PATCH v2 30/31] mailinfo: remove calls to exit() and die() deep in the callchain Junio C Hamano
2015-10-14 20:45   ` [PATCH v2 31/31] am: make direct call to mailinfo Junio C Hamano
2015-10-19  7:28   ` [PATCH v3 00/34] libify mailinfo and call it directly from am Junio C Hamano
2015-10-19  7:28     ` [PATCH v3 01/34] mailinfo: remove a no-op call convert_to_utf8(it, "") Junio C Hamano
2015-10-19  7:28     ` [PATCH v3 02/34] mailinfo: fold decode_header_bq() into decode_header() Junio C Hamano
2015-10-19  7:28     ` [PATCH v3 03/34] mailinfo: fix an off-by-one error in the boundary stack Junio C Hamano
2015-10-19  7:28     ` [PATCH v3 04/34] mailinfo: explicitly close file handle to the patch output Junio C Hamano
2015-10-19  7:28     ` [PATCH v3 05/34] mailinfo: move handle_boundary() lower Junio C Hamano
2015-10-19  7:28     ` [PATCH v3 06/34] mailinfo: get rid of function-local static states Junio C Hamano
2015-10-19  7:28     ` [PATCH v3 07/34] mailinfo: do not let handle_body() touch global "line" directly Junio C Hamano
2015-10-19  7:28     ` [PATCH v3 08/34] mailinfo: do not let handle_boundary() " Junio C Hamano
2015-10-19  7:28     ` [PATCH v3 09/34] mailinfo: do not let find_boundary() " Junio C Hamano
2015-10-19  7:28     ` [PATCH v3 10/34] mailinfo: move global "line" into mailinfo() function Junio C Hamano
2015-10-19 22:57       ` Eric Sunshine
2015-10-20  5:19         ` Junio C Hamano
2015-10-19  7:28     ` [PATCH v3 11/34] mailinfo: introduce "struct mailinfo" to hold globals Junio C Hamano
2015-10-19  7:28     ` [PATCH v3 12/34] mailinfo: move keep_subject & keep_non_patch_bracket to struct mailinfo Junio C Hamano
2015-10-19  7:28     ` [PATCH v3 13/34] mailinfo: move global "FILE *fin, *fout" " Junio C Hamano
2015-10-19  7:28     ` [PATCH v3 14/34] mailinfo: move filter/header stage " Junio C Hamano
2015-10-21 20:20       ` Stefan Beller
2015-10-19  7:28     ` [PATCH v3 15/34] mailinfo: move patch_lines " Junio C Hamano
2015-10-19  7:28     ` [PATCH v3 16/34] mailinfo: move add_message_id and message_id " Junio C Hamano
2015-10-19  7:28     ` [PATCH v3 17/34] mailinfo: move use_scissors and use_inbody_headers " Junio C Hamano
2015-10-21 20:24       ` Stefan Beller
2015-10-21 21:02         ` Junio C Hamano
2015-10-19  7:28     ` [PATCH v3 18/34] mailinfo: move metainfo_charset " Junio C Hamano
2015-10-19  7:28     ` [PATCH v3 19/34] mailinfo: move check for metainfo_charset to convert_to_utf8() Junio C Hamano
2015-10-19 22:57       ` Eric Sunshine
2015-10-19  7:28     ` [PATCH v3 20/34] mailinfo: move transfer_encoding to struct mailinfo Junio C Hamano
2015-10-19  7:28     ` [PATCH v3 21/34] mailinfo: move charset " Junio C Hamano
2015-10-19  7:28     ` [PATCH v3 22/34] mailinfo: move cmitmsg and patchfile " Junio C Hamano
2015-10-19  7:28     ` [PATCH v3 23/34] mailinfo: move [ps]_hdr_data " Junio C Hamano
2015-10-21 20:30       ` Stefan Beller
2015-10-19  7:28     ` [PATCH v3 24/34] mailinfo: move content/content_top " Junio C Hamano
2015-10-21 20:36       ` Stefan Beller
2015-10-21 21:04         ` Junio C Hamano
2015-10-21 21:08           ` Stefan Beller
2015-10-19  7:28     ` [PATCH v3 25/34] mailinfo: handle_commit_msg() shouldn't be called after finding patchbreak Junio C Hamano
2015-10-19  7:28     ` [PATCH v3 26/34] mailinfo: keep the parsed log message in a strbuf Junio C Hamano
2015-10-19  7:28     ` [PATCH v3 27/34] mailinfo: move read_one_header_line() closer to its callers Junio C Hamano
2015-10-19  7:28     ` [PATCH v3 28/34] mailinfo: move check_header() after the helpers it uses Junio C Hamano
2015-10-19  7:28     ` [PATCH v3 29/34] mailinfo: move cleanup_space() before its users Junio C Hamano
2015-10-19  7:28     ` [PATCH v3 30/34] mailinfo: move definition of MAX_HDR_PARSED closer to its use Junio C Hamano
2015-10-19  7:28     ` [PATCH v3 31/34] mailinfo: libify Junio C Hamano
2015-10-19  7:28     ` [PATCH v3 32/34] mailinfo: handle charset conversion errors in the caller Junio C Hamano
2015-10-19  7:28     ` [PATCH v3 33/34] mailinfo: remove calls to exit() and die() deep in the callchain Junio C Hamano
2015-10-19  7:28     ` [PATCH v3 34/34] am: make direct call to mailinfo Junio C Hamano
2015-10-20 21:24     ` [PATCH v3 00/34] libify mailinfo and call it directly from am Junio C Hamano
2015-10-20 22:01       ` Stefan Beller
2015-10-20 22:06         ` Junio C Hamano
2015-10-20 22:08           ` Stefan Beller
2015-10-21 15:51       ` Ramsay Jones
2015-10-21 20:04         ` Johannes Sixt
2015-10-21 20:07           ` Junio C Hamano
2015-10-26 14:25           ` Johannes Schindelin
2015-10-26 18:42             ` Junio C Hamano
2015-10-21 23:15     ` [PATCH v4 00/35] " Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1444778207-859-16-git-send-email-gitster@pobox.com \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).