Imported Upstream version 2.14.6
[platform/upstream/git.git] / pretty.c
index f3e8b6c..39cad51 100644 (file)
--- a/pretty.c
+++ b/pretty.c
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "commit.h"
 #include "utf8.h"
 #include "diff.h"
 #include "color.h"
 #include "reflog-walk.h"
 #include "gpg-interface.h"
+#include "trailer.h"
 
 static char *user_format;
 static struct cmt_fmt_map {
        const char *name;
        enum cmit_fmt format;
        int is_tformat;
+       int expand_tabs_in_log;
        int is_alias;
        const char *user_format;
 } *commit_formats;
@@ -24,6 +27,11 @@ static size_t commit_formats_len;
 static size_t commit_formats_alloc;
 static struct cmt_fmt_map *find_commit_format(const char *sought);
 
+int commit_format_is_empty(enum cmit_fmt fmt)
+{
+       return fmt == CMIT_FMT_USERFORMAT && !*user_format;
+}
+
 static void save_user_format(struct rev_info *rev, const char *cp, int is_tformat)
 {
        free(user_format);
@@ -40,10 +48,9 @@ static int git_pretty_formats_config(const char *var, const char *value, void *c
        const char *fmt;
        int i;
 
-       if (!starts_with(var, "pretty."))
+       if (!skip_prefix(var, "pretty.", &name))
                return 0;
 
-       name = var + strlen("pretty.");
        for (i = 0; i < builtin_formats_len; i++) {
                if (!strcmp(commit_formats[i].name, name))
                        return 0;
@@ -66,11 +73,12 @@ static int git_pretty_formats_config(const char *var, const char *value, void *c
 
        commit_format->name = xstrdup(name);
        commit_format->format = CMIT_FMT_USERFORMAT;
-       git_config_string(&fmt, var, value);
-       if (starts_with(fmt, "format:") || starts_with(fmt, "tformat:")) {
-               commit_format->is_tformat = fmt[0] == 't';
-               fmt = strchr(fmt, ':') + 1;
-       } else if (strchr(fmt, '%'))
+       if (git_config_string(&fmt, var, value))
+               return -1;
+
+       if (skip_prefix(fmt, "format:", &fmt))
+               commit_format->is_tformat = 0;
+       else if (skip_prefix(fmt, "tformat:", &fmt) || strchr(fmt, '%'))
                commit_format->is_tformat = 1;
        else
                commit_format->is_alias = 1;
@@ -82,13 +90,14 @@ static int git_pretty_formats_config(const char *var, const char *value, void *c
 static void setup_commit_formats(void)
 {
        struct cmt_fmt_map builtin_formats[] = {
-               { "raw",        CMIT_FMT_RAW,           0 },
-               { "medium",     CMIT_FMT_MEDIUM,        0 },
-               { "short",      CMIT_FMT_SHORT,         0 },
-               { "email",      CMIT_FMT_EMAIL,         0 },
-               { "fuller",     CMIT_FMT_FULLER,        0 },
-               { "full",       CMIT_FMT_FULL,          0 },
-               { "oneline",    CMIT_FMT_ONELINE,       1 }
+               { "raw",        CMIT_FMT_RAW,           0,      0 },
+               { "medium",     CMIT_FMT_MEDIUM,        0,      8 },
+               { "short",      CMIT_FMT_SHORT,         0,      0 },
+               { "email",      CMIT_FMT_EMAIL,         0,      0 },
+               { "mboxrd",     CMIT_FMT_MBOXRD,        0,      0 },
+               { "fuller",     CMIT_FMT_FULLER,        0,      8 },
+               { "full",       CMIT_FMT_FULL,          0,      8 },
+               { "oneline",    CMIT_FMT_ONELINE,       1,      0 }
        };
        commit_formats_len = ARRAY_SIZE(builtin_formats);
        builtin_formats_len = commit_formats_len;
@@ -147,16 +156,16 @@ void get_commit_format(const char *arg, struct rev_info *rev)
        struct cmt_fmt_map *commit_format;
 
        rev->use_terminator = 0;
-       if (!arg || !*arg) {
+       if (!arg) {
                rev->commit_format = CMIT_FMT_DEFAULT;
                return;
        }
-       if (starts_with(arg, "format:") || starts_with(arg, "tformat:")) {
-               save_user_format(rev, strchr(arg, ':') + 1, arg[0] == 't');
+       if (skip_prefix(arg, "format:", &arg)) {
+               save_user_format(rev, arg, 0);
                return;
        }
 
-       if (strchr(arg, '%')) {
+       if (!*arg || skip_prefix(arg, "tformat:", &arg) || strchr(arg, '%')) {
                save_user_format(rev, arg, 1);
                return;
        }
@@ -167,6 +176,7 @@ void get_commit_format(const char *arg, struct rev_info *rev)
 
        rev->commit_format = commit_format->format;
        rev->use_terminator = commit_format->is_tformat;
+       rev->expand_tabs_in_log_default = commit_format->expand_tabs_in_log;
        if (commit_format->format == CMIT_FMT_USERFORMAT) {
                save_user_format(rev, commit_format->user_format,
                                 commit_format->is_tformat);
@@ -393,14 +403,14 @@ static void add_rfc2047(struct strbuf *sb, const char *line, size_t len,
        strbuf_addstr(sb, "?=");
 }
 
-static const char *show_ident_date(const struct ident_split *ident,
-                                  enum date_mode mode)
+const char *show_ident_date(const struct ident_split *ident,
+                           const struct date_mode *mode)
 {
-       unsigned long date = 0;
+       timestamp_t date = 0;
        long tz = 0;
 
        if (ident->date_begin && ident->date_end)
-               date = strtoul(ident->date_begin, NULL, 10);
+               date = parse_timestamp(ident->date_begin, NULL, 10);
        if (date_overflows(date))
                date = 0;
        else {
@@ -437,7 +447,7 @@ void pp_user_info(struct pretty_print_context *pp,
        if (pp->mailmap)
                map_user(pp->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
 
-       if (pp->fmt == CMIT_FMT_EMAIL) {
+       if (cmit_fmt_is_mail(pp->fmt)) {
                if (pp->from_ident && ident_cmp(pp->from_ident, &ident)) {
                        struct strbuf buf = STRBUF_INIT;
 
@@ -484,15 +494,16 @@ void pp_user_info(struct pretty_print_context *pp,
        switch (pp->fmt) {
        case CMIT_FMT_MEDIUM:
                strbuf_addf(sb, "Date:   %s\n",
-                           show_ident_date(&ident, pp->date_mode));
+                           show_ident_date(&ident, &pp->date_mode));
                break;
        case CMIT_FMT_EMAIL:
+       case CMIT_FMT_MBOXRD:
                strbuf_addf(sb, "Date: %s\n",
-                           show_ident_date(&ident, DATE_RFC2822));
+                           show_ident_date(&ident, DATE_MODE(RFC2822)));
                break;
        case CMIT_FMT_FULLER:
                strbuf_addf(sb, "%sDate: %s\n", what,
-                           show_ident_date(&ident, pp->date_mode));
+                           show_ident_date(&ident, &pp->date_mode));
                break;
        default:
                /* notin' */
@@ -500,7 +511,7 @@ void pp_user_info(struct pretty_print_context *pp,
        }
 }
 
-static int is_empty_line(const char *line, int *len_p)
+static int is_blank_line(const char *line, int *len_p)
 {
        int len = *len_p;
        while (len && isspace(line[len - 1]))
@@ -509,14 +520,14 @@ static int is_empty_line(const char *line, int *len_p)
        return !len;
 }
 
-static const char *skip_empty_lines(const char *msg)
+const char *skip_blank_lines(const char *msg)
 {
        for (;;) {
                int linelen = get_one_line(msg);
                int ll = linelen;
                if (!linelen)
                        break;
-               if (!is_empty_line(msg, &ll))
+               if (!is_blank_line(msg, &ll))
                        break;
                msg += linelen;
        }
@@ -528,51 +539,29 @@ static void add_merge_info(const struct pretty_print_context *pp,
 {
        struct commit_list *parent = commit->parents;
 
-       if ((pp->fmt == CMIT_FMT_ONELINE) || (pp->fmt == CMIT_FMT_EMAIL) ||
+       if ((pp->fmt == CMIT_FMT_ONELINE) || (cmit_fmt_is_mail(pp->fmt)) ||
            !parent || !parent->next)
                return;
 
        strbuf_addstr(sb, "Merge:");
 
        while (parent) {
-               struct commit *p = parent->item;
-               const char *hex = NULL;
+               struct object_id *oidp = &parent->item->object.oid;
+               strbuf_addch(sb, ' ');
                if (pp->abbrev)
-                       hex = find_unique_abbrev(p->object.sha1, pp->abbrev);
-               if (!hex)
-                       hex = sha1_to_hex(p->object.sha1);
+                       strbuf_add_unique_abbrev(sb, oidp->hash, pp->abbrev);
+               else
+                       strbuf_addstr(sb, oid_to_hex(oidp));
                parent = parent->next;
-
-               strbuf_addf(sb, " %s", hex);
        }
        strbuf_addch(sb, '\n');
 }
 
-static char *get_header(const struct commit *commit, const char *msg,
-                       const char *key)
+static char *get_header(const char *msg, const char *key)
 {
-       int key_len = strlen(key);
-       const char *line = msg;
-
-       while (line) {
-               const char *eol = strchrnul(line, '\n'), *next;
-
-               if (line == eol)
-                       return NULL;
-               if (!*eol) {
-                       warning("malformed commit (header is missing newline): %s",
-                               sha1_to_hex(commit->object.sha1));
-                       next = NULL;
-               } else
-                       next = eol + 1;
-               if (eol - line > key_len &&
-                   !strncmp(line, key, key_len) &&
-                   line[key_len] == ' ') {
-                       return xmemdupz(line + key_len + 1, eol - line - key_len - 1);
-               }
-               line = next;
-       }
-       return NULL;
+       size_t len;
+       const char *v = find_commit_header(msg, key, &len);
+       return v ? xmemdupz(v, len) : NULL;
 }
 
 static char *replace_encoding_header(char *buf, const char *encoding)
@@ -582,7 +571,7 @@ static char *replace_encoding_header(char *buf, const char *encoding)
        char *cp = buf;
 
        /* guess if there is an encoding header before a \n\n */
-       while (strncmp(cp, "encoding ", strlen("encoding "))) {
+       while (!starts_with(cp, "encoding ")) {
                cp = strchr(cp, '\n');
                if (!cp || *++cp == '\n')
                        return buf;
@@ -618,11 +607,10 @@ const char *logmsg_reencode(const struct commit *commit,
 
        if (!output_encoding || !*output_encoding) {
                if (commit_encoding)
-                       *commit_encoding =
-                               get_header(commit, msg, "encoding");
+                       *commit_encoding = get_header(msg, "encoding");
                return msg;
        }
-       encoding = get_header(commit, msg, "encoding");
+       encoding = get_header(msg, "encoding");
        if (commit_encoding)
                *commit_encoding = encoding;
        use_encoding = encoding ? encoding : utf8;
@@ -687,7 +675,8 @@ static int mailmap_name(const char **email, size_t *email_len,
 }
 
 static size_t format_person_part(struct strbuf *sb, char part,
-                                const char *msg, int len, enum date_mode dmode)
+                                const char *msg, int len,
+                                const struct date_mode *dmode)
 {
        /* currently all placeholders have same length */
        const int placeholder_len = 2;
@@ -727,13 +716,16 @@ static size_t format_person_part(struct strbuf *sb, char part,
                strbuf_addstr(sb, show_ident_date(&s, dmode));
                return placeholder_len;
        case 'D':       /* date, RFC2822 style */
-               strbuf_addstr(sb, show_ident_date(&s, DATE_RFC2822));
+               strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(RFC2822)));
                return placeholder_len;
        case 'r':       /* date, relative */
-               strbuf_addstr(sb, show_ident_date(&s, DATE_RELATIVE));
+               strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(RELATIVE)));
+               return placeholder_len;
+       case 'i':       /* date, ISO 8601-like */
+               strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(ISO8601)));
                return placeholder_len;
-       case 'i':       /* date, ISO 8601 */
-               strbuf_addstr(sb, show_ident_date(&s, DATE_ISO8601));
+       case 'I':       /* date, ISO 8601 strict */
+               strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(ISO8601_STRICT)));
                return placeholder_len;
        }
 
@@ -792,47 +784,28 @@ struct format_commit_context {
        size_t body_off;
 
        /* The following ones are relative to the result struct strbuf. */
-       struct chunk abbrev_commit_hash;
-       struct chunk abbrev_tree_hash;
-       struct chunk abbrev_parent_hashes;
        size_t wrap_start;
 };
 
-static int add_again(struct strbuf *sb, struct chunk *chunk)
-{
-       if (chunk->len) {
-               strbuf_adddup(sb, chunk->off, chunk->len);
-               return 1;
-       }
-
-       /*
-        * We haven't seen this chunk before.  Our caller is surely
-        * going to add it the hard way now.  Remember the most likely
-        * start of the to-be-added chunk: the current end of the
-        * struct strbuf.
-        */
-       chunk->off = sb->len;
-       return 0;
-}
-
 static void parse_commit_header(struct format_commit_context *context)
 {
        const char *msg = context->message;
        int i;
 
        for (i = 0; msg[i]; i++) {
+               const char *name;
                int eol;
                for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
                        ; /* do nothing */
 
                if (i == eol) {
                        break;
-               } else if (starts_with(msg + i, "author ")) {
-                       context->author.off = i + 7;
-                       context->author.len = eol - i - 7;
-               } else if (starts_with(msg + i, "committer ")) {
-                       context->committer.off = i + 10;
-                       context->committer.len = eol - i - 10;
+               } else if (skip_prefix(msg + i, "author ", &name)) {
+                       context->author.off = name - msg;
+                       context->author.len = msg + eol - name;
+               } else if (skip_prefix(msg + i, "committer ", &name)) {
+                       context->committer.off = name - msg;
+                       context->committer.len = msg + eol - name;
                }
                i = eol;
        }
@@ -884,7 +857,7 @@ const char *format_subject(struct strbuf *sb, const char *msg,
                int linelen = get_one_line(line);
 
                msg += linelen;
-               if (!linelen || is_empty_line(line, &linelen))
+               if (!linelen || is_blank_line(line, &linelen))
                        break;
 
                if (!sb)
@@ -898,16 +871,26 @@ const char *format_subject(struct strbuf *sb, const char *msg,
        return msg;
 }
 
+static void format_trailers(struct strbuf *sb, const char *msg)
+{
+       struct trailer_info info;
+
+       trailer_info_get(&info, msg);
+       strbuf_add(sb, info.trailer_start,
+                  info.trailer_end - info.trailer_start);
+       trailer_info_release(&info);
+}
+
 static void parse_commit_message(struct format_commit_context *c)
 {
        const char *msg = c->message + c->message_off;
        const char *start = c->message;
 
-       msg = skip_empty_lines(msg);
+       msg = skip_blank_lines(msg);
        c->subject_off = msg - start;
 
        msg = format_subject(NULL, msg, NULL);
-       msg = skip_empty_lines(msg);
+       msg = skip_blank_lines(msg);
        c->body_off = msg - start;
 
        c->commit_message_parsed = 1;
@@ -945,7 +928,7 @@ static void rewrap_message_tail(struct strbuf *sb,
 static int format_reflog_person(struct strbuf *sb,
                                char part,
                                struct reflog_walk_info *log,
-                               enum date_mode dmode)
+                               const struct date_mode *dmode)
 {
        const char *ident;
 
@@ -963,6 +946,9 @@ static size_t parse_color(struct strbuf *sb, /* in UTF-8 */
                          const char *placeholder,
                          struct format_commit_context *c)
 {
+       const char *rest = placeholder;
+       const char *basic_color = NULL;
+
        if (placeholder[1] == '(') {
                const char *begin = placeholder + 2;
                const char *end = strchr(begin, ')');
@@ -970,31 +956,42 @@ static size_t parse_color(struct strbuf *sb, /* in UTF-8 */
 
                if (!end)
                        return 0;
-               if (starts_with(begin, "auto,")) {
+
+               if (skip_prefix(begin, "auto,", &begin)) {
+                       if (!want_color(c->pretty_ctx->color))
+                               return end - placeholder + 1;
+               } else if (skip_prefix(begin, "always,", &begin)) {
+                       /* nothing to do; we do not respect want_color at all */
+               } else {
+                       /* the default is the same as "auto" */
                        if (!want_color(c->pretty_ctx->color))
                                return end - placeholder + 1;
-                       begin += 5;
                }
-               color_parse_mem(begin,
-                               end - begin,
-                               "--pretty format", color);
+
+               if (color_parse_mem(begin, end - begin, color) < 0)
+                       die(_("unable to parse --pretty format"));
                strbuf_addstr(sb, color);
                return end - placeholder + 1;
        }
-       if (starts_with(placeholder + 1, "red")) {
-               strbuf_addstr(sb, GIT_COLOR_RED);
-               return 4;
-       } else if (starts_with(placeholder + 1, "green")) {
-               strbuf_addstr(sb, GIT_COLOR_GREEN);
-               return 6;
-       } else if (starts_with(placeholder + 1, "blue")) {
-               strbuf_addstr(sb, GIT_COLOR_BLUE);
-               return 5;
-       } else if (starts_with(placeholder + 1, "reset")) {
-               strbuf_addstr(sb, GIT_COLOR_RESET);
-               return 6;
-       } else
-               return 0;
+
+       /*
+        * We handle things like "%C(red)" above; for historical reasons, there
+        * are a few colors that can be specified without parentheses (and
+        * they cannot support things like "auto" or "always" at all).
+        */
+       if (skip_prefix(placeholder + 1, "red", &rest))
+               basic_color = GIT_COLOR_RED;
+       else if (skip_prefix(placeholder + 1, "green", &rest))
+               basic_color = GIT_COLOR_GREEN;
+       else if (skip_prefix(placeholder + 1, "blue", &rest))
+               basic_color = GIT_COLOR_BLUE;
+       else if (skip_prefix(placeholder + 1, "reset", &rest))
+               basic_color = GIT_COLOR_RESET;
+
+       if (basic_color && want_color(c->pretty_ctx->color))
+               strbuf_addstr(sb, basic_color);
+
+       return rest - placeholder;
 }
 
 static size_t parse_padding_placeholder(struct strbuf *sb,
@@ -1036,9 +1033,15 @@ static size_t parse_padding_placeholder(struct strbuf *sb,
                int width;
                if (!end || end == start)
                        return 0;
-               width = strtoul(start, &next, 10);
+               width = strtol(start, &next, 10);
                if (next == start || width == 0)
                        return 0;
+               if (width < 0) {
+                       if (to_column)
+                               width += term_columns();
+                       if (width < 0)
+                               return 0;
+               }
                c->padding = to_column ? -width : width;
                c->flush_type = flush_type;
 
@@ -1071,13 +1074,15 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
        const struct commit *commit = c->commit;
        const char *msg = c->message;
        struct commit_list *p;
-       int h1, h2;
+       int ch;
 
        /* these are independent of the commit */
        switch (placeholder[0]) {
        case 'C':
                if (starts_with(placeholder + 1, "(auto)")) {
-                       c->auto_color = 1;
+                       c->auto_color = want_color(c->pretty_ctx->color);
+                       if (c->auto_color && sb->len)
+                               strbuf_addstr(sb, GIT_COLOR_RESET);
                        return 7; /* consumed 7 bytes, "C(auto)" */
                } else {
                        int ret = parse_color(sb, placeholder, c);
@@ -1095,14 +1100,11 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
                return 1;
        case 'x':
                /* %x00 == NUL, %x0a == LF, etc. */
-               if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&
-                   h1 <= 16 &&
-                   0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&
-                   h2 <= 16) {
-                       strbuf_addch(sb, (h1<<4)|h2);
-                       return 3;
-               } else
+               ch = hex2chr(placeholder + 1);
+               if (ch < 0)
                        return 0;
+               strbuf_addch(sb, ch);
+               return 3;
        case 'w':
                if (placeholder[1] == '(') {
                        unsigned long width = 0, indent1 = 0, indent2 = 0;
@@ -1135,54 +1137,41 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
 
        /* these depend on the commit */
        if (!commit->object.parsed)
-               parse_object(commit->object.sha1);
+               parse_object(&commit->object.oid);
 
        switch (placeholder[0]) {
        case 'H':               /* commit hash */
                strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_COMMIT));
-               strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
+               strbuf_addstr(sb, oid_to_hex(&commit->object.oid));
                strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_RESET));
                return 1;
        case 'h':               /* abbreviated commit hash */
                strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_COMMIT));
-               if (add_again(sb, &c->abbrev_commit_hash)) {
-                       strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_RESET));
-                       return 1;
-               }
-               strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
-                                                    c->pretty_ctx->abbrev));
+               strbuf_add_unique_abbrev(sb, commit->object.oid.hash,
+                                        c->pretty_ctx->abbrev);
                strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_RESET));
-               c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
                return 1;
        case 'T':               /* tree hash */
-               strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
+               strbuf_addstr(sb, oid_to_hex(&commit->tree->object.oid));
                return 1;
        case 't':               /* abbreviated tree hash */
-               if (add_again(sb, &c->abbrev_tree_hash))
-                       return 1;
-               strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
-                                                    c->pretty_ctx->abbrev));
-               c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
+               strbuf_add_unique_abbrev(sb, commit->tree->object.oid.hash,
+                                        c->pretty_ctx->abbrev);
                return 1;
        case 'P':               /* parent hashes */
                for (p = commit->parents; p; p = p->next) {
                        if (p != commit->parents)
                                strbuf_addch(sb, ' ');
-                       strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
+                       strbuf_addstr(sb, oid_to_hex(&p->item->object.oid));
                }
                return 1;
        case 'p':               /* abbreviated parent hashes */
-               if (add_again(sb, &c->abbrev_parent_hashes))
-                       return 1;
                for (p = commit->parents; p; p = p->next) {
                        if (p != commit->parents)
                                strbuf_addch(sb, ' ');
-                       strbuf_addstr(sb, find_unique_abbrev(
-                                       p->item->object.sha1,
-                                       c->pretty_ctx->abbrev));
+                       strbuf_add_unique_abbrev(sb, p->item->object.oid.hash,
+                                                c->pretty_ctx->abbrev);
                }
-               c->abbrev_parent_hashes.len = sb->len -
-                                             c->abbrev_parent_hashes.off;
                return 1;
        case 'm':               /* left/right/bottom */
                strbuf_addstr(sb, get_revision_mark(NULL, commit));
@@ -1191,6 +1180,10 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
                load_ref_decorations(DECORATE_SHORT_REFS);
                format_decorations(sb, commit, c->auto_color);
                return 1;
+       case 'D':
+               load_ref_decorations(DECORATE_SHORT_REFS);
+               format_decorations_extended(sb, commit, c->auto_color, "", ", ", "");
+               return 1;
        case 'g':               /* reflog info */
                switch(placeholder[1]) {
                case 'd':       /* reflog selector */
@@ -1198,7 +1191,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
                        if (c->pretty_ctx->reflog_info)
                                get_reflog_selector(sb,
                                                    c->pretty_ctx->reflog_info,
-                                                   c->pretty_ctx->date_mode,
+                                                   &c->pretty_ctx->date_mode,
                                                    c->pretty_ctx->date_mode_explicit,
                                                    (placeholder[1] == 'd'));
                        return 2;
@@ -1213,7 +1206,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
                        return format_reflog_person(sb,
                                                    placeholder[1],
                                                    c->pretty_ctx->reflog_info,
-                                                   c->pretty_ctx->date_mode);
+                                                   &c->pretty_ctx->date_mode);
                }
                return 0;       /* unknown %g placeholder */
        case 'N':
@@ -1236,8 +1229,12 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
                        switch (c->signature_check.result) {
                        case 'G':
                        case 'B':
+                       case 'E':
                        case 'U':
                        case 'N':
+                       case 'X':
+                       case 'Y':
+                       case 'R':
                                strbuf_addch(sb, c->signature_check.result);
                        }
                        break;
@@ -1264,11 +1261,11 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
        case 'a':       /* author ... */
                return format_person_part(sb, placeholder[1],
                                   msg + c->author.off, c->author.len,
-                                  c->pretty_ctx->date_mode);
+                                  &c->pretty_ctx->date_mode);
        case 'c':       /* committer ... */
                return format_person_part(sb, placeholder[1],
                                   msg + c->committer.off, c->committer.len,
-                                  c->pretty_ctx->date_mode);
+                                  &c->pretty_ctx->date_mode);
        case 'e':       /* encoding */
                if (c->commit_encoding)
                        strbuf_addstr(sb, c->commit_encoding);
@@ -1294,6 +1291,12 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
                strbuf_addstr(sb, msg + c->body_off);
                return 1;
        }
+
+       if (starts_with(placeholder, "(trailers)")) {
+               format_trailers(sb, msg + c->subject_off);
+               return strlen("(trailers)");
+       }
+
        return 0;       /* unknown placeholder */
 }
 
@@ -1309,6 +1312,7 @@ static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */
                if (!start)
                        start = sb->buf;
                occupied = utf8_strnwidth(start, -1, 1);
+               occupied += c->pretty_ctx->graph_width;
                padding = (-padding) - occupied;
        }
        while (1) {
@@ -1377,7 +1381,7 @@ static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */
                case trunc_none:
                        break;
                }
-               strbuf_addstr(sb, local_sb.buf);
+               strbuf_addbuf(sb, &local_sb);
        } else {
                int sb_len = sb->len, offset = 0;
                if (c->flush_type == flush_left)
@@ -1389,9 +1393,7 @@ static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */
                 * convert it back to chars
                 */
                padding = padding - len + local_sb.len;
-               strbuf_grow(sb, padding);
-               strbuf_setlen(sb, sb_len + padding);
-               memset(sb->buf + sb_len, ' ', sb->len - sb_len);
+               strbuf_addchars(sb, ' ', padding);
                memcpy(sb->buf + sb_len + offset, local_sb.buf,
                       local_sb.len);
        }
@@ -1521,8 +1523,6 @@ void format_commit_message(const struct commit *commit,
 
        free(context.commit_encoding);
        unuse_commit_buffer(commit, context.message);
-       free(context.signature_check.gpg_output);
-       free(context.signature_check.signer);
 }
 
 static void pp_header(struct pretty_print_context *pp,
@@ -1534,7 +1534,7 @@ static void pp_header(struct pretty_print_context *pp,
        int parents_shown = 0;
 
        for (;;) {
-               const char *line = *msg_p;
+               const char *name, *line = *msg_p;
                int linelen = get_one_line(*msg_p);
 
                if (!linelen)
@@ -1557,12 +1557,7 @@ static void pp_header(struct pretty_print_context *pp,
                }
 
                if (!parents_shown) {
-                       struct commit_list *parent;
-                       int num;
-                       for (parent = commit->parents, num = 0;
-                            parent;
-                            parent = parent->next, num++)
-                               ;
+                       unsigned num = commit_list_count(commit->parents);
                        /* with enough slop */
                        strbuf_grow(sb, num * 50 + 20);
                        add_merge_info(pp, sb, commit);
@@ -1574,14 +1569,14 @@ static void pp_header(struct pretty_print_context *pp,
                 * FULL shows both authors but not dates.
                 * FULLER shows both authors and dates.
                 */
-               if (starts_with(line, "author ")) {
+               if (skip_prefix(line, "author ", &name)) {
                        strbuf_grow(sb, linelen + 80);
-                       pp_user_info(pp, "Author", sb, line + 7, encoding);
+                       pp_user_info(pp, "Author", sb, name, encoding);
                }
-               if (starts_with(line, "committer ") &&
+               if (skip_prefix(line, "committer ", &name) &&
                    (pp->fmt == CMIT_FMT_FULL || pp->fmt == CMIT_FMT_FULLER)) {
                        strbuf_grow(sb, linelen + 80);
-                       pp_user_info(pp, "Commit", sb, line + 10, encoding);
+                       pp_user_info(pp, "Commit", sb, name, encoding);
                }
        }
 }
@@ -1600,8 +1595,9 @@ void pp_title_line(struct pretty_print_context *pp,
                                pp->preserve_subject ? "\n" : " ");
 
        strbuf_grow(sb, title.len + 1024);
-       if (pp->subject) {
-               strbuf_addstr(sb, pp->subject);
+       if (pp->print_email_subject) {
+               if (pp->rev)
+                       fmt_output_email_subject(sb, pp->rev);
                if (needs_rfc2047_encoding(title.buf, title.len, RFC2047_SUBJECT))
                        add_rfc2047(sb, title.buf, title.len,
                                                encoding, RFC2047_SUBJECT);
@@ -1633,7 +1629,7 @@ void pp_title_line(struct pretty_print_context *pp,
        if (pp->after_subject) {
                strbuf_addstr(sb, pp->after_subject);
        }
-       if (pp->fmt == CMIT_FMT_EMAIL) {
+       if (cmit_fmt_is_mail(pp->fmt)) {
                strbuf_addch(sb, '\n');
        }
 
@@ -1650,6 +1646,82 @@ void pp_title_line(struct pretty_print_context *pp,
        strbuf_release(&title);
 }
 
+static int pp_utf8_width(const char *start, const char *end)
+{
+       int width = 0;
+       size_t remain = end - start;
+
+       while (remain) {
+               int n = utf8_width(&start, &remain);
+               if (n < 0 || !start)
+                       return -1;
+               width += n;
+       }
+       return width;
+}
+
+static void strbuf_add_tabexpand(struct strbuf *sb, int tabwidth,
+                                const char *line, int linelen)
+{
+       const char *tab;
+
+       while ((tab = memchr(line, '\t', linelen)) != NULL) {
+               int width = pp_utf8_width(line, tab);
+
+               /*
+                * If it wasn't well-formed utf8, or it
+                * had characters with badly defined
+                * width (control characters etc), just
+                * give up on trying to align things.
+                */
+               if (width < 0)
+                       break;
+
+               /* Output the data .. */
+               strbuf_add(sb, line, tab - line);
+
+               /* .. and the de-tabified tab */
+               strbuf_addchars(sb, ' ', tabwidth - (width % tabwidth));
+
+               /* Skip over the printed part .. */
+               linelen -= tab + 1 - line;
+               line = tab + 1;
+       }
+
+       /*
+        * Print out everything after the last tab without
+        * worrying about width - there's nothing more to
+        * align.
+        */
+       strbuf_add(sb, line, linelen);
+}
+
+/*
+ * pp_handle_indent() prints out the intendation, and
+ * the whole line (without the final newline), after
+ * de-tabifying.
+ */
+static void pp_handle_indent(struct pretty_print_context *pp,
+                            struct strbuf *sb, int indent,
+                            const char *line, int linelen)
+{
+       strbuf_addchars(sb, ' ', indent);
+       if (pp->expand_tabs_in_log)
+               strbuf_add_tabexpand(sb, pp->expand_tabs_in_log, line, linelen);
+       else
+               strbuf_add(sb, line, linelen);
+}
+
+static int is_mboxrd_from(const char *line, int len)
+{
+       /*
+        * a line matching /^From $/ here would only have len == 4
+        * at this point because is_empty_line would've trimmed all
+        * trailing space
+        */
+       return len > 4 && starts_with(line + strspn(line, ">"), "From ");
+}
+
 void pp_remainder(struct pretty_print_context *pp,
                  const char **msg_p,
                  struct strbuf *sb,
@@ -1664,7 +1736,7 @@ void pp_remainder(struct pretty_print_context *pp,
                if (!linelen)
                        break;
 
-               if (is_empty_line(line, &linelen)) {
+               if (is_blank_line(line, &linelen)) {
                        if (first)
                                continue;
                        if (pp->fmt == CMIT_FMT_SHORT)
@@ -1673,11 +1745,18 @@ void pp_remainder(struct pretty_print_context *pp,
                first = 0;
 
                strbuf_grow(sb, linelen + indent + 20);
-               if (indent) {
-                       memset(sb->buf + sb->len, ' ', indent);
-                       strbuf_setlen(sb, sb->len + indent);
+               if (indent)
+                       pp_handle_indent(pp, sb, indent, line, linelen);
+               else if (pp->expand_tabs_in_log)
+                       strbuf_add_tabexpand(sb, pp->expand_tabs_in_log,
+                                            line, linelen);
+               else {
+                       if (pp->fmt == CMIT_FMT_MBOXRD &&
+                                       is_mboxrd_from(line, linelen))
+                               strbuf_addch(sb, '>');
+
+                       strbuf_add(sb, line, linelen);
                }
-               strbuf_add(sb, line, linelen);
                strbuf_addch(sb, '\n');
        }
 }
@@ -1701,14 +1780,14 @@ void pretty_print_commit(struct pretty_print_context *pp,
        encoding = get_log_output_encoding();
        msg = reencoded = logmsg_reencode(commit, NULL, encoding);
 
-       if (pp->fmt == CMIT_FMT_ONELINE || pp->fmt == CMIT_FMT_EMAIL)
+       if (pp->fmt == CMIT_FMT_ONELINE || cmit_fmt_is_mail(pp->fmt))
                indent = 0;
 
        /*
         * We need to check and emit Content-type: to mark it
         * as 8-bit if we haven't done so.
         */
-       if (pp->fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
+       if (cmit_fmt_is_mail(pp->fmt) && need_8bit_cte == 0) {
                int i, ch, in_body;
 
                for (in_body = i = 0; (ch = msg[i]); i++) {
@@ -1728,15 +1807,15 @@ void pretty_print_commit(struct pretty_print_context *pp,
        }
 
        pp_header(pp, encoding, commit, &msg, sb);
-       if (pp->fmt != CMIT_FMT_ONELINE && !pp->subject) {
+       if (pp->fmt != CMIT_FMT_ONELINE && !pp->print_email_subject) {
                strbuf_addch(sb, '\n');
        }
 
        /* Skip excess blank lines at the beginning of body, if any... */
-       msg = skip_empty_lines(msg);
+       msg = skip_blank_lines(msg);
 
        /* These formats treat the title line specially. */
-       if (pp->fmt == CMIT_FMT_ONELINE || pp->fmt == CMIT_FMT_EMAIL)
+       if (pp->fmt == CMIT_FMT_ONELINE || cmit_fmt_is_mail(pp->fmt))
                pp_title_line(pp, &msg, sb, encoding, need_8bit_cte);
 
        beginning_of_body = sb->len;
@@ -1753,7 +1832,7 @@ void pretty_print_commit(struct pretty_print_context *pp,
         * format.  Make sure we did not strip the blank line
         * between the header and the body.
         */
-       if (pp->fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
+       if (cmit_fmt_is_mail(pp->fmt) && sb->len <= beginning_of_body)
                strbuf_addch(sb, '\n');
 
        unuse_commit_buffer(commit, reencoded);