Code

sha1_file.c: fix typo
[git.git] / pretty.c
index 16bfb86cd3ce6d6b471cdc313114563ca78837dc..6cd91491d348b9aabea902c0ea60465074e9c06e 100644 (file)
--- a/pretty.c
+++ b/pretty.c
@@ -3,41 +3,52 @@
 #include "utf8.h"
 #include "diff.h"
 #include "revision.h"
-
-static struct cmt_fmt_map {
-       const char *n;
-       size_t cmp_len;
-       enum cmit_fmt v;
-} cmt_fmts[] = {
-       { "raw",        1,      CMIT_FMT_RAW },
-       { "medium",     1,      CMIT_FMT_MEDIUM },
-       { "short",      1,      CMIT_FMT_SHORT },
-       { "email",      1,      CMIT_FMT_EMAIL },
-       { "full",       5,      CMIT_FMT_FULL },
-       { "fuller",     5,      CMIT_FMT_FULLER },
-       { "oneline",    1,      CMIT_FMT_ONELINE },
-       { "format:",    7,      CMIT_FMT_USERFORMAT},
-};
+#include "string-list.h"
+#include "mailmap.h"
+#include "log-tree.h"
+#include "color.h"
 
 static char *user_format;
 
-enum cmit_fmt get_commit_format(const char *arg)
+void get_commit_format(const char *arg, struct rev_info *rev)
 {
        int i;
-
-       if (!arg || !*arg)
-               return CMIT_FMT_DEFAULT;
-       if (*arg == '=')
-               arg++;
-       if (!prefixcmp(arg, "format:")) {
+       static struct cmt_fmt_map {
+               const char *n;
+               size_t cmp_len;
+               enum cmit_fmt v;
+       } cmt_fmts[] = {
+               { "raw",        1,      CMIT_FMT_RAW },
+               { "medium",     1,      CMIT_FMT_MEDIUM },
+               { "short",      1,      CMIT_FMT_SHORT },
+               { "email",      1,      CMIT_FMT_EMAIL },
+               { "full",       5,      CMIT_FMT_FULL },
+               { "fuller",     5,      CMIT_FMT_FULLER },
+               { "oneline",    1,      CMIT_FMT_ONELINE },
+       };
+
+       rev->use_terminator = 0;
+       if (!arg || !*arg) {
+               rev->commit_format = CMIT_FMT_DEFAULT;
+               return;
+       }
+       if (!prefixcmp(arg, "format:") || !prefixcmp(arg, "tformat:")) {
+               const char *cp = strchr(arg, ':') + 1;
                free(user_format);
-               user_format = xstrdup(arg + 7);
-               return CMIT_FMT_USERFORMAT;
+               user_format = xstrdup(cp);
+               if (arg[0] == 't')
+                       rev->use_terminator = 1;
+               rev->commit_format = CMIT_FMT_USERFORMAT;
+               return;
        }
        for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
                if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len) &&
-                   !strncmp(arg, cmt_fmts[i].n, strlen(arg)))
-                       return cmt_fmts[i].v;
+                   !strncmp(arg, cmt_fmts[i].n, strlen(arg))) {
+                       if (cmt_fmts[i].v == CMIT_FMT_ONELINE)
+                               rev->use_terminator = 1;
+                       rev->commit_format = cmt_fmts[i].v;
+                       return;
+               }
        }
 
        die("invalid --pretty format: %s", arg);
@@ -171,6 +182,20 @@ static int is_empty_line(const char *line, int *len_p)
        return !len;
 }
 
+static const char *skip_empty_lines(const char *msg)
+{
+       for (;;) {
+               int linelen = get_one_line(msg);
+               int ll = linelen;
+               if (!linelen)
+                       break;
+               if (!is_empty_line(msg, &ll))
+                       break;
+               msg += linelen;
+       }
+       return msg;
+}
+
 static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
                        const struct commit *commit, int abbrev)
 {
@@ -185,15 +210,13 @@ static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
        while (parent) {
                struct commit *p = parent->item;
                const char *hex = NULL;
-               const char *dots;
                if (abbrev)
                        hex = find_unique_abbrev(p->object.sha1, abbrev);
                if (!hex)
                        hex = sha1_to_hex(p->object.sha1);
-               dots = (abbrev && strlen(hex) != 40) ?  "..." : "";
                parent = parent->next;
 
-               strbuf_addf(sb, " %s%s", hex, dots);
+               strbuf_addf(sb, " %s", hex);
        }
        strbuf_addch(sb, '\n');
 }
@@ -224,7 +247,7 @@ static char *get_header(const struct commit *commit, const char *key)
 
 static char *replace_encoding_header(char *buf, const char *encoding)
 {
-       struct strbuf tmp;
+       struct strbuf tmp = STRBUF_INIT;
        size_t start, len;
        char *cp = buf;
 
@@ -240,7 +263,6 @@ static char *replace_encoding_header(char *buf, const char *encoding)
                return buf; /* should not happen but be defensive */
        len = cp + 1 - (buf + start);
 
-       strbuf_init(&tmp, 0);
        strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
        if (is_encoding_utf8(encoding)) {
                /* we have re-coded to UTF-8; drop the header */
@@ -281,14 +303,27 @@ static char *logmsg_reencode(const struct commit *commit,
        return out;
 }
 
+static int mailmap_name(char *email, int email_len, char *name, int name_len)
+{
+       static struct string_list *mail_map;
+       if (!mail_map) {
+               mail_map = xcalloc(1, sizeof(*mail_map));
+               read_mailmap(mail_map, NULL);
+       }
+       return mail_map->nr && map_user(mail_map, email, email_len, name, name_len);
+}
+
 static size_t format_person_part(struct strbuf *sb, char part,
-                               const char *msg, int len)
+                                const char *msg, int len, enum date_mode dmode)
 {
        /* currently all placeholders have same length */
        const int placeholder_len = 2;
        int start, end, tz = 0;
        unsigned long date = 0;
        char *ep;
+       const char *name_start, *name_end, *mail_start, *mail_end, *msg_end = msg+len;
+       char person_name[1024];
+       char person_mail[1024];
 
        /* advance 'end' to point to email start delimiter */
        for (end = 0; end < len && msg[end] != '<'; end++)
@@ -302,23 +337,34 @@ static size_t format_person_part(struct strbuf *sb, char part,
        if (end >= len - 2)
                goto skip;
 
-       if (part == 'n') {      /* name */
-               while (end > 0 && isspace(msg[end - 1]))
-                       end--;
-               strbuf_add(sb, msg, end);
+       /* Seek for both name and email part */
+       name_start = msg;
+       name_end = msg+end;
+       while (name_end > name_start && isspace(*(name_end-1)))
+               name_end--;
+       mail_start = msg+end+1;
+       mail_end = mail_start;
+       while (mail_end < msg_end && *mail_end != '>')
+               mail_end++;
+       if (mail_end == msg_end)
+               goto skip;
+       end = mail_end-msg;
+
+       if (part == 'N' || part == 'E') { /* mailmap lookup */
+               strlcpy(person_name, name_start, name_end-name_start+1);
+               strlcpy(person_mail, mail_start, mail_end-mail_start+1);
+               mailmap_name(person_mail, sizeof(person_mail), person_name, sizeof(person_name));
+               name_start = person_name;
+               name_end = name_start + strlen(person_name);
+               mail_start = person_mail;
+               mail_end = mail_start +  strlen(person_mail);
+       }
+       if (part == 'n' || part == 'N') {       /* name */
+               strbuf_add(sb, name_start, name_end-name_start);
                return placeholder_len;
        }
-       start = ++end; /* save email start position */
-
-       /* advance 'end' to point to email end delimiter */
-       for ( ; end < len && msg[end] != '>'; end++)
-               ; /* do nothing */
-
-       if (end >= len)
-               goto skip;
-
-       if (part == 'e') {      /* email */
-               strbuf_add(sb, msg + start, end - start);
+       if (part == 'e' || part == 'E') {       /* email */
+               strbuf_add(sb, mail_start, mail_end-mail_start);
                return placeholder_len;
        }
 
@@ -347,7 +393,7 @@ static size_t format_person_part(struct strbuf *sb, char part,
 
        switch (part) {
        case 'd':       /* date */
-               strbuf_addstr(sb, show_date(date, tz, DATE_NORMAL));
+               strbuf_addstr(sb, show_date(date, tz, dmode));
                return placeholder_len;
        case 'D':       /* date, RFC2822 style */
                strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
@@ -379,13 +425,16 @@ struct chunk {
 
 struct format_commit_context {
        const struct commit *commit;
+       enum date_mode dmode;
+       unsigned commit_header_parsed:1;
+       unsigned commit_message_parsed:1;
 
        /* These offsets are relative to the start of the commit message. */
-       int commit_header_parsed;
-       struct chunk subject;
        struct chunk author;
        struct chunk committer;
        struct chunk encoding;
+       size_t message_off;
+       size_t subject_off;
        size_t body_off;
 
        /* The following ones are relative to the result struct strbuf. */
@@ -415,23 +464,14 @@ static void parse_commit_header(struct format_commit_context *context)
 {
        const char *msg = context->commit->buffer;
        int i;
-       enum { HEADER, SUBJECT, BODY } state;
 
-       for (i = 0, state = HEADER; msg[i] && state < BODY; i++) {
+       for (i = 0; msg[i]; i++) {
                int eol;
                for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
                        ; /* do nothing */
 
-               if (state == SUBJECT) {
-                       context->subject.off = i;
-                       context->subject.len = eol - i;
-                       i = eol;
-               }
                if (i == eol) {
-                       state++;
-                       /* strip empty lines */
-                       while (msg[eol] == '\n' && msg[eol + 1] == '\n')
-                               eol++;
+                       break;
                } else if (!prefixcmp(msg + i, "author ")) {
                        context->author.off = i + 7;
                        context->author.len = eol - i - 7;
@@ -443,13 +483,67 @@ static void parse_commit_header(struct format_commit_context *context)
                        context->encoding.len = eol - i - 9;
                }
                i = eol;
-               if (!msg[i])
-                       break;
        }
-       context->body_off = i;
+       context->message_off = i;
        context->commit_header_parsed = 1;
 }
 
+const char *format_subject(struct strbuf *sb, const char *msg,
+                          const char *line_separator)
+{
+       int first = 1;
+
+       for (;;) {
+               const char *line = msg;
+               int linelen = get_one_line(line);
+
+               msg += linelen;
+               if (!linelen || is_empty_line(line, &linelen))
+                       break;
+
+               if (!sb)
+                       continue;
+               strbuf_grow(sb, linelen + 2);
+               if (!first)
+                       strbuf_addstr(sb, line_separator);
+               strbuf_add(sb, line, linelen);
+               first = 0;
+       }
+       return msg;
+}
+
+static void parse_commit_message(struct format_commit_context *c)
+{
+       const char *msg = c->commit->buffer + c->message_off;
+       const char *start = c->commit->buffer;
+
+       msg = skip_empty_lines(msg);
+       c->subject_off = msg - start;
+
+       msg = format_subject(NULL, msg, NULL);
+       msg = skip_empty_lines(msg);
+       c->body_off = msg - start;
+
+       c->commit_message_parsed = 1;
+}
+
+static void format_decoration(struct strbuf *sb, const struct commit *commit)
+{
+       struct name_decoration *d;
+       const char *prefix = " (";
+
+       load_ref_decorations();
+       d = lookup_decoration(&name_decoration, &commit->object);
+       while (d) {
+               strbuf_addstr(sb, prefix);
+               prefix = ", ";
+               strbuf_addstr(sb, d->name);
+               d = d->next;
+       }
+       if (prefix[0] == ',')
+               strbuf_addch(sb, ')');
+}
+
 static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
                                void *context)
 {
@@ -457,10 +551,22 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
        const struct commit *commit = c->commit;
        const char *msg = commit->buffer;
        struct commit_list *p;
+       int h1, h2;
 
        /* these are independent of the commit */
        switch (placeholder[0]) {
        case 'C':
+               if (placeholder[1] == '(') {
+                       const char *end = strchr(placeholder + 2, ')');
+                       char color[COLOR_MAXLEN];
+                       if (!end)
+                               return 0;
+                       color_parse_mem(placeholder + 2,
+                                       end - (placeholder + 2),
+                                       "--pretty format", color);
+                       strbuf_addstr(sb, color);
+                       return end - placeholder + 1;
+               }
                if (!prefixcmp(placeholder + 1, "red")) {
                        strbuf_addstr(sb, "\033[31m");
                        return 4;
@@ -478,6 +584,16 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
        case 'n':               /* newline */
                strbuf_addch(sb, '\n');
                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
+                       return 0;
        }
 
        /* these depend on the commit */
@@ -531,6 +647,9 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
                                 ? '<'
                                 : '>');
                return 1;
+       case 'd':
+               format_decoration(sb, commit);
+               return 1;
        }
 
        /* For the rest we have to parse the commit header. */
@@ -538,18 +657,27 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
                parse_commit_header(c);
 
        switch (placeholder[0]) {
-       case 's':       /* subject */
-               strbuf_add(sb, msg + c->subject.off, c->subject.len);
-               return 1;
        case 'a':       /* author ... */
                return format_person_part(sb, placeholder[1],
-                                  msg + c->author.off, c->author.len);
+                                  msg + c->author.off, c->author.len,
+                                  c->dmode);
        case 'c':       /* committer ... */
                return format_person_part(sb, placeholder[1],
-                                  msg + c->committer.off, c->committer.len);
+                                  msg + c->committer.off, c->committer.len,
+                                  c->dmode);
        case 'e':       /* encoding */
                strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
                return 1;
+       }
+
+       /* Now we need to parse the commit message. */
+       if (!c->commit_message_parsed)
+               parse_commit_message(c);
+
+       switch (placeholder[0]) {
+       case 's':       /* subject */
+               format_subject(sb, msg + c->subject_off, " ");
+               return 1;
        case 'b':       /* body */
                strbuf_addstr(sb, msg + c->body_off);
                return 1;
@@ -558,12 +686,14 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
 }
 
 void format_commit_message(const struct commit *commit,
-                           const void *format, struct strbuf *sb)
+                          const void *format, struct strbuf *sb,
+                          enum date_mode dmode)
 {
        struct format_commit_context context;
 
        memset(&context, 0, sizeof(context));
        context.commit = commit;
+       context.dmode = dmode;
        strbuf_expand(sb, format, format_commit_item, &context);
 }
 
@@ -638,27 +768,11 @@ void pp_title_line(enum cmit_fmt fmt,
                   const char *encoding,
                   int need_8bit_cte)
 {
+       const char *line_separator = (fmt == CMIT_FMT_EMAIL) ? "\n " : " ";
        struct strbuf title;
 
        strbuf_init(&title, 80);
-
-       for (;;) {
-               const char *line = *msg_p;
-               int linelen = get_one_line(line);
-
-               *msg_p += linelen;
-               if (!linelen || is_empty_line(line, &linelen))
-                       break;
-
-               strbuf_grow(&title, linelen + 2);
-               if (title.len) {
-                       if (fmt == CMIT_FMT_EMAIL) {
-                               strbuf_addch(&title, '\n');
-                       }
-                       strbuf_addch(&title, ' ');
-               }
-               strbuf_add(&title, line, linelen);
-       }
+       *msg_p = format_subject(&title, *msg_p, line_separator);
 
        strbuf_grow(sb, title.len + 1024);
        if (subject) {
@@ -717,6 +831,20 @@ void pp_remainder(enum cmit_fmt fmt,
        }
 }
 
+char *reencode_commit_message(const struct commit *commit, const char **encoding_p)
+{
+       const char *encoding;
+
+       encoding = (git_log_output_encoding
+                   ? git_log_output_encoding
+                   : git_commit_encoding);
+       if (!encoding)
+               encoding = "utf-8";
+       if (encoding_p)
+               *encoding_p = encoding;
+       return logmsg_reencode(commit, encoding);
+}
+
 void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
                         struct strbuf *sb, int abbrev,
                         const char *subject, const char *after_subject,
@@ -729,16 +857,11 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
        const char *encoding;
 
        if (fmt == CMIT_FMT_USERFORMAT) {
-               format_commit_message(commit, user_format, sb);
+               format_commit_message(commit, user_format, sb, dmode);
                return;
        }
 
-       encoding = (git_log_output_encoding
-                   ? git_log_output_encoding
-                   : git_commit_encoding);
-       if (!encoding)
-               encoding = "utf-8";
-       reencoded = logmsg_reencode(commit, encoding);
+       reencoded = reencode_commit_message(commit, &encoding);
        if (reencoded) {
                msg = reencoded;
        }
@@ -775,15 +898,7 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
        }
 
        /* Skip excess blank lines at the beginning of body, if any... */
-       for (;;) {
-               int linelen = get_one_line(msg);
-               int ll = linelen;
-               if (!linelen)
-                       break;
-               if (!is_empty_line(msg, &ll))
-                       break;
-               msg += linelen;
-       }
+       msg = skip_empty_lines(msg);
 
        /* These formats treat the title line specially. */
        if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)