X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=pretty.c;h=230fe1cc82e3a3bf7c0fef604350868d917acef4;hb=bd193f46b70fd3f8a55e2a8dcbb1fb2b4eec13d6;hp=34d668cc300b63650094202745850e45063a4d72;hpb=9cd7a92b97d0b4321c19e8952a3a9b1874e3abb5;p=git.git diff --git a/pretty.c b/pretty.c index 34d668cc3..230fe1cc8 100644 --- a/pretty.c +++ b/pretty.c @@ -208,6 +208,58 @@ int has_non_ascii(const char *s) return 0; } +static int is_rfc822_special(char ch) +{ + switch (ch) { + case '(': + case ')': + case '<': + case '>': + case '[': + case ']': + case ':': + case ';': + case '@': + case ',': + case '.': + case '"': + case '\\': + return 1; + default: + return 0; + } +} + +static int has_rfc822_specials(const char *s, int len) +{ + int i; + for (i = 0; i < len; i++) + if (is_rfc822_special(s[i])) + return 1; + return 0; +} + +static void add_rfc822_quoted(struct strbuf *out, const char *s, int len) +{ + int i; + + /* just a guess, we may have to also backslash-quote */ + strbuf_grow(out, len + 2); + + strbuf_addch(out, '"'); + for (i = 0; i < len; i++) { + switch (s[i]) { + case '"': + case '\\': + strbuf_addch(out, '\\'); + /* fall through */ + default: + strbuf_addch(out, s[i]); + } + } + strbuf_addch(out, '"'); +} + static int is_rfc2047_special(char ch) { return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_')); @@ -216,49 +268,66 @@ static int is_rfc2047_special(char ch) static void add_rfc2047(struct strbuf *sb, const char *line, int len, const char *encoding) { - int i, last; + static const int max_length = 78; /* per rfc2822 */ + int i; + int line_len; + + /* How many bytes are already used on the current line? */ + for (i = sb->len - 1; i >= 0; i--) + if (sb->buf[i] == '\n') + break; + line_len = sb->len - (i+1); for (i = 0; i < len; i++) { int ch = line[i]; - if (non_ascii(ch)) + if (non_ascii(ch) || ch == '\n') goto needquote; if ((i + 1 < len) && (ch == '=' && line[i+1] == '?')) goto needquote; } - strbuf_add(sb, line, len); + strbuf_add_wrapped_bytes(sb, line, len, 0, 1, max_length - line_len); return; needquote: strbuf_grow(sb, len * 3 + strlen(encoding) + 100); strbuf_addf(sb, "=?%s?q?", encoding); - for (i = last = 0; i < len; i++) { + line_len += strlen(encoding) + 5; /* 5 for =??q? */ + for (i = 0; i < len; i++) { unsigned ch = line[i] & 0xFF; + + if (line_len >= max_length - 2) { + strbuf_addf(sb, "?=\n =?%s?q?", encoding); + line_len = strlen(encoding) + 5 + 1; /* =??q? plus SP */ + } + /* * We encode ' ' using '=20' even though rfc2047 * allows using '_' for readability. Unfortunately, * many programs do not understand this and just * leave the underscore in place. */ - if (is_rfc2047_special(ch) || ch == ' ') { - strbuf_add(sb, line + last, i - last); + if (is_rfc2047_special(ch) || ch == ' ' || ch == '\n') { strbuf_addf(sb, "=%02X", ch); - last = i + 1; + line_len += 3; + } + else { + strbuf_addch(sb, ch); + line_len++; } } - strbuf_add(sb, line + last, len - last); strbuf_addstr(sb, "?="); } -void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb, - const char *line, enum date_mode dmode, - const char *encoding) +void pp_user_info(const struct pretty_print_context *pp, + const char *what, struct strbuf *sb, + const char *line, const char *encoding) { char *date; int namelen; unsigned long time; int tz; - if (fmt == CMIT_FMT_ONELINE) + if (pp->fmt == CMIT_FMT_ONELINE) return; date = strchr(line, '>'); if (!date) @@ -267,32 +336,48 @@ void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb, time = strtoul(date, &date, 10); tz = strtol(date, NULL, 10); - if (fmt == CMIT_FMT_EMAIL) { + if (pp->fmt == CMIT_FMT_EMAIL) { char *name_tail = strchr(line, '<'); int display_name_length; + int final_line; if (!name_tail) return; while (line < name_tail && isspace(name_tail[-1])) name_tail--; display_name_length = name_tail - line; strbuf_addstr(sb, "From: "); - add_rfc2047(sb, line, display_name_length, encoding); + if (!has_rfc822_specials(line, display_name_length)) { + add_rfc2047(sb, line, display_name_length, encoding); + } else { + struct strbuf quoted = STRBUF_INIT; + add_rfc822_quoted("ed, line, display_name_length); + add_rfc2047(sb, quoted.buf, quoted.len, encoding); + strbuf_release("ed); + } + for (final_line = 0; final_line < sb->len; final_line++) + if (sb->buf[sb->len - final_line - 1] == '\n') + break; + if (namelen - display_name_length + final_line > 78) { + strbuf_addch(sb, '\n'); + if (!isspace(name_tail[0])) + strbuf_addch(sb, ' '); + } strbuf_add(sb, name_tail, namelen - display_name_length); strbuf_addch(sb, '\n'); } else { strbuf_addf(sb, "%s: %.*s%.*s\n", what, - (fmt == CMIT_FMT_FULLER) ? 4 : 0, + (pp->fmt == CMIT_FMT_FULLER) ? 4 : 0, " ", namelen, line); } - switch (fmt) { + switch (pp->fmt) { case CMIT_FMT_MEDIUM: - strbuf_addf(sb, "Date: %s\n", show_date(time, tz, dmode)); + strbuf_addf(sb, "Date: %s\n", show_date(time, tz, pp->date_mode)); break; case CMIT_FMT_EMAIL: strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822)); break; case CMIT_FMT_FULLER: - strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, dmode)); + strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, pp->date_mode)); break; default: /* notin' */ @@ -323,12 +408,12 @@ static const char *skip_empty_lines(const char *msg) return msg; } -static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb, - const struct commit *commit, int abbrev) +static void add_merge_info(const struct pretty_print_context *pp, + struct strbuf *sb, const struct commit *commit) { struct commit_list *parent = commit->parents; - if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) || + if ((pp->fmt == CMIT_FMT_ONELINE) || (pp->fmt == CMIT_FMT_EMAIL) || !parent || !parent->next) return; @@ -337,8 +422,8 @@ static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb, while (parent) { struct commit *p = parent->item; const char *hex = NULL; - if (abbrev) - hex = find_unique_abbrev(p->object.sha1, abbrev); + if (pp->abbrev) + hex = find_unique_abbrev(p->object.sha1, pp->abbrev); if (!hex) hex = sha1_to_hex(p->object.sha1); parent = parent->next; @@ -859,11 +944,7 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder, c->abbrev_parent_hashes.off; return 1; case 'm': /* left/right/bottom */ - strbuf_addch(sb, (commit->object.flags & BOUNDARY) - ? '-' - : (commit->object.flags & SYMMETRIC_LEFT) - ? '<' - : '>'); + strbuf_addstr(sb, get_revision_mark(NULL, commit)); return 1; case 'd': format_decoration(sb, commit); @@ -1003,7 +1084,7 @@ void userformat_find_requirements(const char *fmt, struct userformat_want *w) return; fmt = user_format; } - strbuf_expand(&dummy, user_format, userformat_want_item, w); + strbuf_expand(&dummy, fmt, userformat_want_item, w); strbuf_release(&dummy); } @@ -1022,8 +1103,11 @@ void format_commit_message(const struct commit *commit, context.message = commit->buffer; if (output_enc) { char *enc = get_header(commit, "encoding"); - if (strcmp(enc ? enc : utf8, output_enc)) + if (strcmp(enc ? enc : utf8, output_enc)) { context.message = logmsg_reencode(commit, output_enc); + if (!context.message) + context.message = commit->buffer; + } free(enc); } @@ -1034,9 +1118,7 @@ void format_commit_message(const struct commit *commit, free(context.message); } -static void pp_header(enum cmit_fmt fmt, - int abbrev, - enum date_mode dmode, +static void pp_header(const struct pretty_print_context *pp, const char *encoding, const struct commit *commit, const char **msg_p, @@ -1056,7 +1138,7 @@ static void pp_header(enum cmit_fmt fmt, /* End of header */ return; - if (fmt == CMIT_FMT_RAW) { + if (pp->fmt == CMIT_FMT_RAW) { strbuf_add(sb, line, linelen); continue; } @@ -1076,7 +1158,7 @@ static void pp_header(enum cmit_fmt fmt, ; /* with enough slop */ strbuf_grow(sb, num * 50 + 20); - add_merge_info(fmt, sb, commit, abbrev); + add_merge_info(pp, sb, commit); parents_shown = 1; } @@ -1087,33 +1169,31 @@ static void pp_header(enum cmit_fmt fmt, */ if (!memcmp(line, "author ", 7)) { strbuf_grow(sb, linelen + 80); - pp_user_info("Author", fmt, sb, line + 7, dmode, encoding); + pp_user_info(pp, "Author", sb, line + 7, encoding); } if (!memcmp(line, "committer ", 10) && - (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) { + (pp->fmt == CMIT_FMT_FULL || pp->fmt == CMIT_FMT_FULLER)) { strbuf_grow(sb, linelen + 80); - pp_user_info("Commit", fmt, sb, line + 10, dmode, encoding); + pp_user_info(pp, "Commit", sb, line + 10, encoding); } } } -void pp_title_line(enum cmit_fmt fmt, +void pp_title_line(const struct pretty_print_context *pp, const char **msg_p, struct strbuf *sb, - const char *subject, - const char *after_subject, const char *encoding, int need_8bit_cte) { - const char *line_separator = (fmt == CMIT_FMT_EMAIL) ? "\n " : " "; struct strbuf title; strbuf_init(&title, 80); - *msg_p = format_subject(&title, *msg_p, line_separator); + *msg_p = format_subject(&title, *msg_p, + pp->preserve_subject ? "\n" : " "); strbuf_grow(sb, title.len + 1024); - if (subject) { - strbuf_addstr(sb, subject); + if (pp->subject) { + strbuf_addstr(sb, pp->subject); add_rfc2047(sb, title.buf, title.len, encoding); } else { strbuf_addbuf(sb, &title); @@ -1127,16 +1207,16 @@ void pp_title_line(enum cmit_fmt fmt, "Content-Transfer-Encoding: 8bit\n"; strbuf_addf(sb, header_fmt, encoding); } - if (after_subject) { - strbuf_addstr(sb, after_subject); + if (pp->after_subject) { + strbuf_addstr(sb, pp->after_subject); } - if (fmt == CMIT_FMT_EMAIL) { + if (pp->fmt == CMIT_FMT_EMAIL) { strbuf_addch(sb, '\n'); } strbuf_release(&title); } -void pp_remainder(enum cmit_fmt fmt, +void pp_remainder(const struct pretty_print_context *pp, const char **msg_p, struct strbuf *sb, int indent) @@ -1153,7 +1233,7 @@ void pp_remainder(enum cmit_fmt fmt, if (is_empty_line(line, &linelen)) { if (first) continue; - if (fmt == CMIT_FMT_SHORT) + if (pp->fmt == CMIT_FMT_SHORT) break; } first = 0; @@ -1178,19 +1258,19 @@ char *reencode_commit_message(const struct commit *commit, const char **encoding return logmsg_reencode(commit, encoding); } -void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit, - struct strbuf *sb, - const struct pretty_print_context *context) +void pretty_print_commit(const struct pretty_print_context *pp, + const struct commit *commit, + struct strbuf *sb) { unsigned long beginning_of_body; int indent = 4; const char *msg = commit->buffer; char *reencoded; const char *encoding; - int need_8bit_cte = context->need_8bit_cte; + int need_8bit_cte = pp->need_8bit_cte; - if (fmt == CMIT_FMT_USERFORMAT) { - format_commit_message(commit, user_format, sb, context); + if (pp->fmt == CMIT_FMT_USERFORMAT) { + format_commit_message(commit, user_format, sb, pp); return; } @@ -1199,14 +1279,14 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit, msg = reencoded; } - if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL) + if (pp->fmt == CMIT_FMT_ONELINE || pp->fmt == CMIT_FMT_EMAIL) indent = 0; /* * We need to check and emit Content-type: to mark it * as 8-bit if we haven't done so. */ - if (fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) { + if (pp->fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) { int i, ch, in_body; for (in_body = i = 0; (ch = msg[i]); i++) { @@ -1225,9 +1305,8 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit, } } - pp_header(fmt, context->abbrev, context->date_mode, encoding, - commit, &msg, sb); - if (fmt != CMIT_FMT_ONELINE && !context->subject) { + pp_header(pp, encoding, commit, &msg, sb); + if (pp->fmt != CMIT_FMT_ONELINE && !pp->subject) { strbuf_addch(sb, '\n'); } @@ -1235,17 +1314,16 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit, msg = skip_empty_lines(msg); /* These formats treat the title line specially. */ - if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL) - pp_title_line(fmt, &msg, sb, context->subject, - context->after_subject, encoding, need_8bit_cte); + if (pp->fmt == CMIT_FMT_ONELINE || pp->fmt == CMIT_FMT_EMAIL) + pp_title_line(pp, &msg, sb, encoding, need_8bit_cte); beginning_of_body = sb->len; - if (fmt != CMIT_FMT_ONELINE) - pp_remainder(fmt, &msg, sb, indent); + if (pp->fmt != CMIT_FMT_ONELINE) + pp_remainder(pp, &msg, sb, indent); strbuf_rtrim(sb); /* Make sure there is an EOLN for the non-oneline case */ - if (fmt != CMIT_FMT_ONELINE) + if (pp->fmt != CMIT_FMT_ONELINE) strbuf_addch(sb, '\n'); /* @@ -1253,12 +1331,20 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit, * format. Make sure we did not strip the blank line * between the header and the body. */ - if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body) + if (pp->fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body) strbuf_addch(sb, '\n'); - if (context->show_notes) + if (pp->show_notes) format_display_notes(commit->object.sha1, sb, encoding, NOTES_SHOW_HEADER | NOTES_INDENT); free(reencoded); } + +void pp_commit_easy(enum cmit_fmt fmt, const struct commit *commit, + struct strbuf *sb) +{ + struct pretty_print_context pp = {0}; + pp.fmt = fmt; + pretty_print_commit(&pp, commit, sb); +}