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>
This commit is contained in:
Junio C Hamano
2005-11-27 16:22:16 -08:00
parent 4e72dcec89
commit 650e4be59b

View File

@ -16,7 +16,7 @@ extern char *gitstrcasestr(const char *haystack, const char *needle);
static FILE *cmitmsg, *patchfile; static FILE *cmitmsg, *patchfile;
static int keep_subject = 0; static int keep_subject = 0;
static int metainfo_utf8 = 0; static char *metainfo_charset = NULL;
static char line[1000]; static char line[1000];
static char date[1000]; static char date[1000];
static char name[1000]; static char name[1000];
@ -441,16 +441,26 @@ static int decode_b_segment(char *in, char *ot, char *ep)
static void convert_to_utf8(char *line, char *charset) static void convert_to_utf8(char *line, char *charset)
{ {
if (*charset) {
char *in, *out; char *in, *out;
size_t insize, outsize, nrc; size_t insize, outsize, nrc;
char outbuf[4096]; /* cheat */ char outbuf[4096]; /* cheat */
iconv_t conv = iconv_open("utf-8", charset); 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) { if (conv == (iconv_t) -1) {
fprintf(stderr, "cannot convert from %s to utf-8\n", static int warned_latin1_once = 0;
charset); if (input_charset != latin_one) {
fprintf(stderr, "cannot convert from %s to %s\n",
input_charset, metainfo_charset);
*charset = 0; *charset = 0;
}
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; return;
} }
in = line; in = line;
@ -463,7 +473,6 @@ static void convert_to_utf8(char *line, char *charset)
return; return;
*out = 0; *out = 0;
strcpy(line, outbuf); strcpy(line, outbuf);
}
} }
static void decode_header_bq(char *it) static void decode_header_bq(char *it)
@ -511,7 +520,7 @@ static void decode_header_bq(char *it)
} }
if (sz < 0) if (sz < 0)
return; return;
if (metainfo_utf8) if (metainfo_charset)
convert_to_utf8(piecebuf, charset_q); convert_to_utf8(piecebuf, charset_q);
strcpy(out, piecebuf); strcpy(out, piecebuf);
out += strlen(out); out += strlen(out);
@ -590,7 +599,7 @@ static int handle_commit_msg(void)
* normalize the log message to UTF-8. * normalize the log message to UTF-8.
*/ */
decode_transfer_encoding(line); decode_transfer_encoding(line);
if (metainfo_utf8) if (metainfo_charset)
convert_to_utf8(line, charset); convert_to_utf8(line, charset);
fputs(line, cmitmsg); fputs(line, cmitmsg);
} while (fgets(line, sizeof(line), stdin) != NULL); } while (fgets(line, sizeof(line), stdin) != NULL);
@ -720,7 +729,9 @@ int main(int argc, char **argv)
if (!strcmp(argv[1], "-k")) if (!strcmp(argv[1], "-k"))
keep_subject = 1; keep_subject = 1;
else if (!strcmp(argv[1], "-u")) 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 else
usage(); usage();
argc--; argv++; argc--; argv++;