git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/3] mailinfo: allow -u to fall back on latin1 to utf8 conversion.

From
Junio C Hamano <junkio@cox.net>
Date
Nov 28, 2005, 00:39 UTC
Message-ID
<7v7jat61xq.fsf@assigned-by-dhcp.cox.net>
In-Reply-To
<Pine.LNX.4.64.0511262006350.13959@g5.osdl.org>

When the message body does not identify what encoding it is in, -u assumes it is in latin-1 and converts it to utf8, which is the recommended encoding for git commit log messages.

With -u=<encoding>, the conversion is made into the specified one, instead of utf8, to allow project-local policies.

Signed-off-by: Junio C Hamano <junkio@cox.net>
---
 * This says [2/3] but does not use the first one in the series,
   to keep mailinfo less dependent on git.  [3/3] integrates it
   to git a bit further by using the configuration file.
 mailinfo.c |   59 +++++++++++++++++++++++++++++++++++------------------------
 1 files changed, 35 insertions(+), 24 deletions(-)

applies-to: dfac5ab58034e7129ba0d8096ca2bb6857df2242 650e4be59b9f385f56e5829d97d09e8440f174b8

diff --git a/mailinfo.c b/mailinfo.c
index cb853df..6d8c933 100644
--- a/mailinfo.c
+++ b/mailinfo.c
@@ -16,7 +16,7 @@ extern char *gitstrcasestr(const char *h
 static FILE *cmitmsg, *patchfile;
 
 static int keep_subject = 0;
-static int metainfo_utf8 = 0;
+static char *metainfo_charset = NULL;
 static char line[1000];
 static char date[1000];
 static char name[1000];
@@ -441,29 +441,38 @@ static int decode_b_segment(char *in, ch
 
 static void convert_to_utf8(char *line, char *charset)
 {
-	if (*charset) {
-		char *in, *out;
-		size_t insize, outsize, nrc;
-		char outbuf[4096]; /* cheat */
-		iconv_t conv = iconv_open("utf-8", charset);
-
-		if (conv == (iconv_t) -1) {
-			fprintf(stderr, "cannot convert from %s to utf-8\n",
-				charset);
+	char *in, *out;
+	size_t insize, outsize, nrc;
+	char outbuf[4096]; /* cheat */
+	static char latin_one[] = "latin-1";
+	char *input_charset = *charset ? charset : latin_one;
+	iconv_t conv = iconv_open(metainfo_charset, input_charset);
+
+	if (conv == (iconv_t) -1) {
+		static int warned_latin1_once = 0;
+		if (input_charset != latin_one) {
+			fprintf(stderr, "cannot convert from %s to %s\n",
+				input_charset, metainfo_charset);
 			*charset = 0;
-			return;
 		}
-		in = line;
-		insize = strlen(in);
-		out = outbuf;
-		outsize = sizeof(outbuf);
-		nrc = iconv(conv, &in, &insize, &out, &outsize);
-		iconv_close(conv);
-		if (nrc == (size_t) -1)
-			return;
-		*out = 0;
-		strcpy(line, outbuf);
+		else if (!warned_latin1_once) {
+			warned_latin1_once = 1;
+			fprintf(stderr, "tried to convert from %s to %s, "
+				"but your iconv does not work with it.\n",
+				input_charset, metainfo_charset);
+		}
+		return;
 	}
+	in = line;
+	insize = strlen(in);
+	out = outbuf;
+	outsize = sizeof(outbuf);
+	nrc = iconv(conv, &in, &insize, &out, &outsize);
+	iconv_close(conv);
+	if (nrc == (size_t) -1)
+		return;
+	*out = 0;
+	strcpy(line, outbuf);
 }
 
 static void decode_header_bq(char *it)
@@ -511,7 +520,7 @@ static void decode_header_bq(char *it)
 		}
 		if (sz < 0)
 			return;
-		if (metainfo_utf8)
+		if (metainfo_charset)
 			convert_to_utf8(piecebuf, charset_q);
 		strcpy(out, piecebuf);
 		out += strlen(out);
@@ -590,7 +599,7 @@ static int handle_commit_msg(void)
 		 * normalize the log message to UTF-8.
 		 */
 		decode_transfer_encoding(line);
-		if (metainfo_utf8)
+		if (metainfo_charset)
 			convert_to_utf8(line, charset);
 		fputs(line, cmitmsg);
 	} while (fgets(line, sizeof(line), stdin) != NULL);
@@ -720,7 +729,9 @@ int main(int argc, char **argv)
 		if (!strcmp(argv[1], "-k"))
 			keep_subject = 1;
 		else if (!strcmp(argv[1], "-u"))
-			metainfo_utf8 = 1;
+			metainfo_charset = "utf-8";
+		else if (!strncmp(argv[1], "-u=", 3))
+			metainfo_charset = argv[1] + 3;
 		else
 			usage();
 		argc--; argv++;
---
0.99.9.GIT
Previous: Linus TorvaldsNext: H. Peter Anvin
Message 49 of 52 in “Rss produced by git is not valid xml?”
  1. Ismail DonmezNov 18, 2005
  2. Ismail DonmezNov 18, 2005
  3. Ismail DonmezNov 18, 2005
  4. Kay SieversNov 18, 2005
  5. Ismail DonmezNov 18, 2005
  6. Linus TorvaldsNov 18, 2005
  7. H. Peter AnvinNov 18, 2005
  8. Linus TorvaldsNov 18, 2005
  9. H. Peter AnvinNov 18, 2005
  10. Josef WeidendorferNov 18, 2005
  11. Kay SieversNov 18, 2005
  12. Ismail DonmezNov 18, 2005
  13. Linus TorvaldsNov 18, 2005
  14. Ismail DonmezNov 18, 2005
  15. Junio C HamanoNov 18, 2005
  16. Ismail DonmezNov 18, 2005
  17. Junio C HamanoNov 19, 2005
  18. Kay SieversNov 18, 2005
  19. Linus TorvaldsNov 18, 2005
  20. Ismail DonmezNov 18, 2005
  21. Linus TorvaldsNov 18, 2005
  22. H. Peter AnvinNov 18, 2005
  23. Linus TorvaldsNov 18, 2005
  24. H. Peter AnvinNov 18, 2005
  25. Andreas EricssonNov 18, 2005
  26. H. Peter AnvinNov 19, 2005
  27. Andreas EricssonNov 19, 2005
  28. Johannes SchindelinNov 19, 2005
  29. Linus TorvaldsNov 18, 2005
  30. H. Peter AnvinNov 18, 2005
  31. Johannes SchindelinNov 19, 2005
  32. Linus TorvaldsNov 18, 2005
  33. Johannes SchindelinNov 19, 2005
  34. Junio C HamanoNov 19, 2005
  35. Linus TorvaldsNov 19, 2005
  36. Junio C HamanoNov 19, 2005
  37. Linus TorvaldsNov 19, 2005
  38. Johannes SchindelinNov 20, 2005
  39. Linus TorvaldsNov 20, 2005
  40. Johannes SchindelinNov 20, 2005
  41. Johannes SchindelinNov 19, 2005
  42. H. Peter AnvinNov 20, 2005
  43. Johannes SchindelinNov 21, 2005
  44. H. Peter AnvinNov 21, 2005
  45. Junio C HamanoNov 19, 2005
  46. H. Peter AnvinNov 19, 2005
  47. Junio C HamanoNov 27, 2005
  48. Linus TorvaldsNov 27, 2005
  49. 2/3 mailinfo: allow -u to fall back on latin1 to utf8 conversion.Junio C Hamano, Nov 28, 2005
  50. H. Peter AnvinNov 28, 2005
  51. Junio C HamanoNov 28, 2005
  52. Kay SieversNov 27, 2005

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.