@matheusbsbs / matheus-bagliano / commits / 650e4be59b
Fork 0

mailinfo: allow -u to fall back on latin1 to utf8 conversion.

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>

Junio C Hamano committed Nov 27, 2005 at 16:22 UTC 650e4be59b9f385f56e5829d97d09e8440f174b8
1 file changed +35 -24
mailinfo.c
+35 -24
@@ -16,7 +16,7 @@ extern char *gitstrcasestr(const char *haystack, const char *needle);
16 static FILE *cmitmsg, *patchfile;
17
18 static int keep_subject = 0;
19 -static int metainfo_utf8 = 0;
19 +static char *metainfo_charset = NULL;
20 static char line[1000];
21 static char date[1000];
22 static char name[1000];
@@ -441,29 +441,38 @@ static int decode_b_segment(char *in, char *ot, char *ep)
441
442 static void convert_to_utf8(char *line, char *charset)
443 {
444 - if (*charset) {
445 - char *in, *out;
446 - size_t insize, outsize, nrc;
447 - char outbuf[4096]; /* cheat */
448 - iconv_t conv = iconv_open("utf-8", charset);
449 -
450 - if (conv == (iconv_t) -1) {
451 - fprintf(stderr, "cannot convert from %s to utf-8\n",
452 - charset);
444 + char *in, *out;
445 + size_t insize, outsize, nrc;
446 + char outbuf[4096]; /* cheat */
447 + static char latin_one[] = "latin-1";
448 + char *input_charset = *charset ? charset : latin_one;
449 + iconv_t conv = iconv_open(metainfo_charset, input_charset);
450 +
451 + if (conv == (iconv_t) -1) {
452 + static int warned_latin1_once = 0;
453 + if (input_charset != latin_one) {
454 + fprintf(stderr, "cannot convert from %s to %s\n",
455 + input_charset, metainfo_charset);
456 *charset = 0;
454 - return;
457 }
456 - in = line;
457 - insize = strlen(in);
458 - out = outbuf;
459 - outsize = sizeof(outbuf);
460 - nrc = iconv(conv, &in, &insize, &out, &outsize);
461 - iconv_close(conv);
462 - if (nrc == (size_t) -1)
463 - return;
464 - *out = 0;
465 - strcpy(line, outbuf);
458 + else if (!warned_latin1_once) {
459 + warned_latin1_once = 1;
460 + fprintf(stderr, "tried to convert from %s to %s, "
461 + "but your iconv does not work with it.\n",
462 + input_charset, metainfo_charset);
463 + }
464 + return;
465 }
466 + in = line;
467 + insize = strlen(in);
468 + out = outbuf;
469 + outsize = sizeof(outbuf);
470 + nrc = iconv(conv, &in, &insize, &out, &outsize);
471 + iconv_close(conv);
472 + if (nrc == (size_t) -1)
473 + return;
474 + *out = 0;
475 + strcpy(line, outbuf);
476 }
477
478 static void decode_header_bq(char *it)
@@ -511,7 +520,7 @@ static void decode_header_bq(char *it)
520 }
521 if (sz < 0)
522 return;
514 - if (metainfo_utf8)
523 + if (metainfo_charset)
524 convert_to_utf8(piecebuf, charset_q);
525 strcpy(out, piecebuf);
526 out += strlen(out);
@@ -590,7 +599,7 @@ static int handle_commit_msg(void)
599 * normalize the log message to UTF-8.
600 */
601 decode_transfer_encoding(line);
593 - if (metainfo_utf8)
602 + if (metainfo_charset)
603 convert_to_utf8(line, charset);
604 fputs(line, cmitmsg);
605 } while (fgets(line, sizeof(line), stdin) != NULL);
@@ -720,7 +729,9 @@ int main(int argc, char **argv)
729 if (!strcmp(argv[1], "-k"))
730 keep_subject = 1;
731 else if (!strcmp(argv[1], "-u"))
723 - metainfo_utf8 = 1;
732 + metainfo_charset = "utf-8";
733 + else if (!strncmp(argv[1], "-u=", 3))
734 + metainfo_charset = argv[1] + 3;
735 else
736 usage();
737 argc--; argv++;