@matheusbsbs / matheus-bagliano / commits / cdd406e389
Fork 0

CMIT_FMT_EMAIL: Q-encode Subject: and display-name part of From: fields.

By convention, the commit message and the author/committer names in the commit objects are UTF-8 encoded. When formatting for e-mails, Q-encode them according to RFC 2047. While we are at it, generate the content-type and content-transfer-encoding headers as well. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed May 16, 2006 at 02:29 UTC cdd406e389bd6e0b2ad9394340a366c0c5ae27fc
1 file changed +72 -7
commit.c
+72 -7
@@ -422,6 +422,46 @@ static int get_one_line(const char *msg, unsigned long len)
422 return ret;
423 }
424
425 +static int is_rfc2047_special(char ch)
426 +{
427 + return ((ch & 0x80) || (ch == '=') || (ch == '?') || (ch == '_'));
428 +}
429 +
430 +static int add_rfc2047(char *buf, const char *line, int len)
431 +{
432 + char *bp = buf;
433 + int i, needquote;
434 + static const char q_utf8[] = "=?utf-8?q?";
435 +
436 + for (i = needquote = 0; !needquote && i < len; i++) {
437 + unsigned ch = line[i];
438 + if (ch & 0x80)
439 + needquote++;
440 + if ((i + 1 < len) &&
441 + (ch == '=' && line[i+1] == '?'))
442 + needquote++;
443 + }
444 + if (!needquote)
445 + return sprintf(buf, "%.*s", len, line);
446 +
447 + memcpy(bp, q_utf8, sizeof(q_utf8)-1);
448 + bp += sizeof(q_utf8)-1;
449 + for (i = 0; i < len; i++) {
450 + unsigned ch = line[i];
451 + if (is_rfc2047_special(ch)) {
452 + sprintf(bp, "=%02X", ch);
453 + bp += 3;
454 + }
455 + else if (ch == ' ')
456 + *bp++ = '_';
457 + else
458 + *bp++ = ch;
459 + }
460 + memcpy(bp, "?=", 2);
461 + bp += 2;
462 + return bp - buf;
463 +}
464 +
465 static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const char *line)
466 {
467 char *date;
@@ -440,12 +480,26 @@ static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const c
480 tz = strtol(date, NULL, 10);
481
482 if (fmt == CMIT_FMT_EMAIL) {
443 - what = "From";
483 + char *name_tail = strchr(line, '<');
484 + int display_name_length;
485 + if (!name_tail)
486 + return 0;
487 + while (line < name_tail && isspace(name_tail[-1]))
488 + name_tail--;
489 + display_name_length = name_tail - line;
490 filler = "";
491 + strcpy(buf, "From: ");
492 + ret = strlen(buf);
493 + ret += add_rfc2047(buf + ret, line, display_name_length);
494 + memcpy(buf + ret, name_tail, namelen - display_name_length);
495 + ret += namelen - display_name_length;
496 + buf[ret++] = '\n';
497 + }
498 + else {
499 + ret = sprintf(buf, "%s: %.*s%.*s\n", what,
500 + (fmt == CMIT_FMT_FULLER) ? 4 : 0,
501 + filler, namelen, line);
502 }
446 - ret = sprintf(buf, "%s: %.*s%.*s\n", what,
447 - (fmt == CMIT_FMT_FULLER) ? 4 : 0,
448 - filler, namelen, line);
503 switch (fmt) {
504 case CMIT_FMT_MEDIUM:
505 ret += sprintf(buf + ret, "Date: %s\n", show_date(time, tz));
@@ -584,13 +638,24 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit
638 int slen = strlen(subject);
639 memcpy(buf + offset, subject, slen);
640 offset += slen;
641 + offset += add_rfc2047(buf + offset, line, linelen);
642 + }
643 + else {
644 + memset(buf + offset, ' ', indent);
645 + memcpy(buf + offset + indent, line, linelen);
646 + offset += linelen + indent;
647 }
588 - memset(buf + offset, ' ', indent);
589 - memcpy(buf + offset + indent, line, linelen);
590 - offset += linelen + indent;
648 buf[offset++] = '\n';
649 if (fmt == CMIT_FMT_ONELINE)
650 break;
651 + if (subject) {
652 + static const char header[] =
653 + "Content-Type: text/plain; charset=UTF-8\n"
654 + "Content-Transfer-Encoding: 8bit\n";
655 + memcpy(buf + offset, header, sizeof(header)-1);
656 + offset += sizeof(header)-1;
657 + subject = NULL;
658 + }
659 if (after_subject) {
660 int slen = strlen(after_subject);
661 if (slen > space - offset - 1)