pretty.con commit Merge branch 'sp/smart-http' (905bf77)
   1#include "cache.h"
   2#include "commit.h"
   3#include "utf8.h"
   4#include "diff.h"
   5#include "revision.h"
   6#include "string-list.h"
   7#include "mailmap.h"
   8#include "log-tree.h"
   9#include "color.h"
  10#include "reflog-walk.h"
  11
  12static char *user_format;
  13
  14static void save_user_format(struct rev_info *rev, const char *cp, int is_tformat)
  15{
  16        free(user_format);
  17        user_format = xstrdup(cp);
  18        if (is_tformat)
  19                rev->use_terminator = 1;
  20        rev->commit_format = CMIT_FMT_USERFORMAT;
  21}
  22
  23void get_commit_format(const char *arg, struct rev_info *rev)
  24{
  25        int i;
  26        static struct cmt_fmt_map {
  27                const char *n;
  28                size_t cmp_len;
  29                enum cmit_fmt v;
  30        } cmt_fmts[] = {
  31                { "raw",        1,      CMIT_FMT_RAW },
  32                { "medium",     1,      CMIT_FMT_MEDIUM },
  33                { "short",      1,      CMIT_FMT_SHORT },
  34                { "email",      1,      CMIT_FMT_EMAIL },
  35                { "full",       5,      CMIT_FMT_FULL },
  36                { "fuller",     5,      CMIT_FMT_FULLER },
  37                { "oneline",    1,      CMIT_FMT_ONELINE },
  38        };
  39
  40        rev->use_terminator = 0;
  41        if (!arg || !*arg) {
  42                rev->commit_format = CMIT_FMT_DEFAULT;
  43                return;
  44        }
  45        if (!prefixcmp(arg, "format:") || !prefixcmp(arg, "tformat:")) {
  46                save_user_format(rev, strchr(arg, ':') + 1, arg[0] == 't');
  47                return;
  48        }
  49        for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
  50                if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len) &&
  51                    !strncmp(arg, cmt_fmts[i].n, strlen(arg))) {
  52                        if (cmt_fmts[i].v == CMIT_FMT_ONELINE)
  53                                rev->use_terminator = 1;
  54                        rev->commit_format = cmt_fmts[i].v;
  55                        return;
  56                }
  57        }
  58        if (strchr(arg, '%')) {
  59                save_user_format(rev, arg, 1);
  60                return;
  61        }
  62
  63        die("invalid --pretty format: %s", arg);
  64}
  65
  66/*
  67 * Generic support for pretty-printing the header
  68 */
  69static int get_one_line(const char *msg)
  70{
  71        int ret = 0;
  72
  73        for (;;) {
  74                char c = *msg++;
  75                if (!c)
  76                        break;
  77                ret++;
  78                if (c == '\n')
  79                        break;
  80        }
  81        return ret;
  82}
  83
  84/* High bit set, or ISO-2022-INT */
  85int non_ascii(int ch)
  86{
  87        return !isascii(ch) || ch == '\033';
  88}
  89
  90int has_non_ascii(const char *s)
  91{
  92        int ch;
  93        if (!s)
  94                return 0;
  95        while ((ch = *s++) != '\0') {
  96                if (non_ascii(ch))
  97                        return 1;
  98        }
  99        return 0;
 100}
 101
 102static int is_rfc2047_special(char ch)
 103{
 104        return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_'));
 105}
 106
 107static void add_rfc2047(struct strbuf *sb, const char *line, int len,
 108                       const char *encoding)
 109{
 110        int i, last;
 111
 112        for (i = 0; i < len; i++) {
 113                int ch = line[i];
 114                if (non_ascii(ch))
 115                        goto needquote;
 116                if ((i + 1 < len) && (ch == '=' && line[i+1] == '?'))
 117                        goto needquote;
 118        }
 119        strbuf_add(sb, line, len);
 120        return;
 121
 122needquote:
 123        strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
 124        strbuf_addf(sb, "=?%s?q?", encoding);
 125        for (i = last = 0; i < len; i++) {
 126                unsigned ch = line[i] & 0xFF;
 127                /*
 128                 * We encode ' ' using '=20' even though rfc2047
 129                 * allows using '_' for readability.  Unfortunately,
 130                 * many programs do not understand this and just
 131                 * leave the underscore in place.
 132                 */
 133                if (is_rfc2047_special(ch) || ch == ' ') {
 134                        strbuf_add(sb, line + last, i - last);
 135                        strbuf_addf(sb, "=%02X", ch);
 136                        last = i + 1;
 137                }
 138        }
 139        strbuf_add(sb, line + last, len - last);
 140        strbuf_addstr(sb, "?=");
 141}
 142
 143void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
 144                  const char *line, enum date_mode dmode,
 145                  const char *encoding)
 146{
 147        char *date;
 148        int namelen;
 149        unsigned long time;
 150        int tz;
 151
 152        if (fmt == CMIT_FMT_ONELINE)
 153                return;
 154        date = strchr(line, '>');
 155        if (!date)
 156                return;
 157        namelen = ++date - line;
 158        time = strtoul(date, &date, 10);
 159        tz = strtol(date, NULL, 10);
 160
 161        if (fmt == CMIT_FMT_EMAIL) {
 162                char *name_tail = strchr(line, '<');
 163                int display_name_length;
 164                if (!name_tail)
 165                        return;
 166                while (line < name_tail && isspace(name_tail[-1]))
 167                        name_tail--;
 168                display_name_length = name_tail - line;
 169                strbuf_addstr(sb, "From: ");
 170                add_rfc2047(sb, line, display_name_length, encoding);
 171                strbuf_add(sb, name_tail, namelen - display_name_length);
 172                strbuf_addch(sb, '\n');
 173        } else {
 174                strbuf_addf(sb, "%s: %.*s%.*s\n", what,
 175                              (fmt == CMIT_FMT_FULLER) ? 4 : 0,
 176                              "    ", namelen, line);
 177        }
 178        switch (fmt) {
 179        case CMIT_FMT_MEDIUM:
 180                strbuf_addf(sb, "Date:   %s\n", show_date(time, tz, dmode));
 181                break;
 182        case CMIT_FMT_EMAIL:
 183                strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822));
 184                break;
 185        case CMIT_FMT_FULLER:
 186                strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, dmode));
 187                break;
 188        default:
 189                /* notin' */
 190                break;
 191        }
 192}
 193
 194static int is_empty_line(const char *line, int *len_p)
 195{
 196        int len = *len_p;
 197        while (len && isspace(line[len-1]))
 198                len--;
 199        *len_p = len;
 200        return !len;
 201}
 202
 203static const char *skip_empty_lines(const char *msg)
 204{
 205        for (;;) {
 206                int linelen = get_one_line(msg);
 207                int ll = linelen;
 208                if (!linelen)
 209                        break;
 210                if (!is_empty_line(msg, &ll))
 211                        break;
 212                msg += linelen;
 213        }
 214        return msg;
 215}
 216
 217static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
 218                        const struct commit *commit, int abbrev)
 219{
 220        struct commit_list *parent = commit->parents;
 221
 222        if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
 223            !parent || !parent->next)
 224                return;
 225
 226        strbuf_addstr(sb, "Merge:");
 227
 228        while (parent) {
 229                struct commit *p = parent->item;
 230                const char *hex = NULL;
 231                if (abbrev)
 232                        hex = find_unique_abbrev(p->object.sha1, abbrev);
 233                if (!hex)
 234                        hex = sha1_to_hex(p->object.sha1);
 235                parent = parent->next;
 236
 237                strbuf_addf(sb, " %s", hex);
 238        }
 239        strbuf_addch(sb, '\n');
 240}
 241
 242static char *get_header(const struct commit *commit, const char *key)
 243{
 244        int key_len = strlen(key);
 245        const char *line = commit->buffer;
 246
 247        for (;;) {
 248                const char *eol = strchr(line, '\n'), *next;
 249
 250                if (line == eol)
 251                        return NULL;
 252                if (!eol) {
 253                        eol = line + strlen(line);
 254                        next = NULL;
 255                } else
 256                        next = eol + 1;
 257                if (eol - line > key_len &&
 258                    !strncmp(line, key, key_len) &&
 259                    line[key_len] == ' ') {
 260                        return xmemdupz(line + key_len + 1, eol - line - key_len - 1);
 261                }
 262                line = next;
 263        }
 264}
 265
 266static char *replace_encoding_header(char *buf, const char *encoding)
 267{
 268        struct strbuf tmp = STRBUF_INIT;
 269        size_t start, len;
 270        char *cp = buf;
 271
 272        /* guess if there is an encoding header before a \n\n */
 273        while (strncmp(cp, "encoding ", strlen("encoding "))) {
 274                cp = strchr(cp, '\n');
 275                if (!cp || *++cp == '\n')
 276                        return buf;
 277        }
 278        start = cp - buf;
 279        cp = strchr(cp, '\n');
 280        if (!cp)
 281                return buf; /* should not happen but be defensive */
 282        len = cp + 1 - (buf + start);
 283
 284        strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
 285        if (is_encoding_utf8(encoding)) {
 286                /* we have re-coded to UTF-8; drop the header */
 287                strbuf_remove(&tmp, start, len);
 288        } else {
 289                /* just replaces XXXX in 'encoding XXXX\n' */
 290                strbuf_splice(&tmp, start + strlen("encoding "),
 291                                          len - strlen("encoding \n"),
 292                                          encoding, strlen(encoding));
 293        }
 294        return strbuf_detach(&tmp, NULL);
 295}
 296
 297static char *logmsg_reencode(const struct commit *commit,
 298                             const char *output_encoding)
 299{
 300        static const char *utf8 = "UTF-8";
 301        const char *use_encoding;
 302        char *encoding;
 303        char *out;
 304
 305        if (!*output_encoding)
 306                return NULL;
 307        encoding = get_header(commit, "encoding");
 308        use_encoding = encoding ? encoding : utf8;
 309        if (!strcmp(use_encoding, output_encoding))
 310                if (encoding) /* we'll strip encoding header later */
 311                        out = xstrdup(commit->buffer);
 312                else
 313                        return NULL; /* nothing to do */
 314        else
 315                out = reencode_string(commit->buffer,
 316                                      output_encoding, use_encoding);
 317        if (out)
 318                out = replace_encoding_header(out, output_encoding);
 319
 320        free(encoding);
 321        return out;
 322}
 323
 324static int mailmap_name(char *email, int email_len, char *name, int name_len)
 325{
 326        static struct string_list *mail_map;
 327        if (!mail_map) {
 328                mail_map = xcalloc(1, sizeof(*mail_map));
 329                read_mailmap(mail_map, NULL);
 330        }
 331        return mail_map->nr && map_user(mail_map, email, email_len, name, name_len);
 332}
 333
 334static size_t format_person_part(struct strbuf *sb, char part,
 335                                 const char *msg, int len, enum date_mode dmode)
 336{
 337        /* currently all placeholders have same length */
 338        const int placeholder_len = 2;
 339        int start, end, tz = 0;
 340        unsigned long date = 0;
 341        char *ep;
 342        const char *name_start, *name_end, *mail_start, *mail_end, *msg_end = msg+len;
 343        char person_name[1024];
 344        char person_mail[1024];
 345
 346        /* advance 'end' to point to email start delimiter */
 347        for (end = 0; end < len && msg[end] != '<'; end++)
 348                ; /* do nothing */
 349
 350        /*
 351         * When end points at the '<' that we found, it should have
 352         * matching '>' later, which means 'end' must be strictly
 353         * below len - 1.
 354         */
 355        if (end >= len - 2)
 356                goto skip;
 357
 358        /* Seek for both name and email part */
 359        name_start = msg;
 360        name_end = msg+end;
 361        while (name_end > name_start && isspace(*(name_end-1)))
 362                name_end--;
 363        mail_start = msg+end+1;
 364        mail_end = mail_start;
 365        while (mail_end < msg_end && *mail_end != '>')
 366                mail_end++;
 367        if (mail_end == msg_end)
 368                goto skip;
 369        end = mail_end-msg;
 370
 371        if (part == 'N' || part == 'E') { /* mailmap lookup */
 372                strlcpy(person_name, name_start, name_end-name_start+1);
 373                strlcpy(person_mail, mail_start, mail_end-mail_start+1);
 374                mailmap_name(person_mail, sizeof(person_mail), person_name, sizeof(person_name));
 375                name_start = person_name;
 376                name_end = name_start + strlen(person_name);
 377                mail_start = person_mail;
 378                mail_end = mail_start +  strlen(person_mail);
 379        }
 380        if (part == 'n' || part == 'N') {       /* name */
 381                strbuf_add(sb, name_start, name_end-name_start);
 382                return placeholder_len;
 383        }
 384        if (part == 'e' || part == 'E') {       /* email */
 385                strbuf_add(sb, mail_start, mail_end-mail_start);
 386                return placeholder_len;
 387        }
 388
 389        /* advance 'start' to point to date start delimiter */
 390        for (start = end + 1; start < len && isspace(msg[start]); start++)
 391                ; /* do nothing */
 392        if (start >= len)
 393                goto skip;
 394        date = strtoul(msg + start, &ep, 10);
 395        if (msg + start == ep)
 396                goto skip;
 397
 398        if (part == 't') {      /* date, UNIX timestamp */
 399                strbuf_add(sb, msg + start, ep - (msg + start));
 400                return placeholder_len;
 401        }
 402
 403        /* parse tz */
 404        for (start = ep - msg + 1; start < len && isspace(msg[start]); start++)
 405                ; /* do nothing */
 406        if (start + 1 < len) {
 407                tz = strtoul(msg + start + 1, NULL, 10);
 408                if (msg[start] == '-')
 409                        tz = -tz;
 410        }
 411
 412        switch (part) {
 413        case 'd':       /* date */
 414                strbuf_addstr(sb, show_date(date, tz, dmode));
 415                return placeholder_len;
 416        case 'D':       /* date, RFC2822 style */
 417                strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
 418                return placeholder_len;
 419        case 'r':       /* date, relative */
 420                strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE));
 421                return placeholder_len;
 422        case 'i':       /* date, ISO 8601 */
 423                strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601));
 424                return placeholder_len;
 425        }
 426
 427skip:
 428        /*
 429         * bogus commit, 'sb' cannot be updated, but we still need to
 430         * compute a valid return value.
 431         */
 432        if (part == 'n' || part == 'e' || part == 't' || part == 'd'
 433            || part == 'D' || part == 'r' || part == 'i')
 434                return placeholder_len;
 435
 436        return 0; /* unknown placeholder */
 437}
 438
 439struct chunk {
 440        size_t off;
 441        size_t len;
 442};
 443
 444struct format_commit_context {
 445        const struct commit *commit;
 446        const struct pretty_print_context *pretty_ctx;
 447        unsigned commit_header_parsed:1;
 448        unsigned commit_message_parsed:1;
 449        size_t width, indent1, indent2;
 450
 451        /* These offsets are relative to the start of the commit message. */
 452        struct chunk author;
 453        struct chunk committer;
 454        struct chunk encoding;
 455        size_t message_off;
 456        size_t subject_off;
 457        size_t body_off;
 458
 459        /* The following ones are relative to the result struct strbuf. */
 460        struct chunk abbrev_commit_hash;
 461        struct chunk abbrev_tree_hash;
 462        struct chunk abbrev_parent_hashes;
 463        size_t wrap_start;
 464};
 465
 466static int add_again(struct strbuf *sb, struct chunk *chunk)
 467{
 468        if (chunk->len) {
 469                strbuf_adddup(sb, chunk->off, chunk->len);
 470                return 1;
 471        }
 472
 473        /*
 474         * We haven't seen this chunk before.  Our caller is surely
 475         * going to add it the hard way now.  Remember the most likely
 476         * start of the to-be-added chunk: the current end of the
 477         * struct strbuf.
 478         */
 479        chunk->off = sb->len;
 480        return 0;
 481}
 482
 483static void parse_commit_header(struct format_commit_context *context)
 484{
 485        const char *msg = context->commit->buffer;
 486        int i;
 487
 488        for (i = 0; msg[i]; i++) {
 489                int eol;
 490                for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
 491                        ; /* do nothing */
 492
 493                if (i == eol) {
 494                        break;
 495                } else if (!prefixcmp(msg + i, "author ")) {
 496                        context->author.off = i + 7;
 497                        context->author.len = eol - i - 7;
 498                } else if (!prefixcmp(msg + i, "committer ")) {
 499                        context->committer.off = i + 10;
 500                        context->committer.len = eol - i - 10;
 501                } else if (!prefixcmp(msg + i, "encoding ")) {
 502                        context->encoding.off = i + 9;
 503                        context->encoding.len = eol - i - 9;
 504                }
 505                i = eol;
 506        }
 507        context->message_off = i;
 508        context->commit_header_parsed = 1;
 509}
 510
 511static int istitlechar(char c)
 512{
 513        return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
 514                (c >= '0' && c <= '9') || c == '.' || c == '_';
 515}
 516
 517static void format_sanitized_subject(struct strbuf *sb, const char *msg)
 518{
 519        size_t trimlen;
 520        size_t start_len = sb->len;
 521        int space = 2;
 522
 523        for (; *msg && *msg != '\n'; msg++) {
 524                if (istitlechar(*msg)) {
 525                        if (space == 1)
 526                                strbuf_addch(sb, '-');
 527                        space = 0;
 528                        strbuf_addch(sb, *msg);
 529                        if (*msg == '.')
 530                                while (*(msg+1) == '.')
 531                                        msg++;
 532                } else
 533                        space |= 1;
 534        }
 535
 536        /* trim any trailing '.' or '-' characters */
 537        trimlen = 0;
 538        while (sb->len - trimlen > start_len &&
 539                (sb->buf[sb->len - 1 - trimlen] == '.'
 540                || sb->buf[sb->len - 1 - trimlen] == '-'))
 541                trimlen++;
 542        strbuf_remove(sb, sb->len - trimlen, trimlen);
 543}
 544
 545const char *format_subject(struct strbuf *sb, const char *msg,
 546                           const char *line_separator)
 547{
 548        int first = 1;
 549
 550        for (;;) {
 551                const char *line = msg;
 552                int linelen = get_one_line(line);
 553
 554                msg += linelen;
 555                if (!linelen || is_empty_line(line, &linelen))
 556                        break;
 557
 558                if (!sb)
 559                        continue;
 560                strbuf_grow(sb, linelen + 2);
 561                if (!first)
 562                        strbuf_addstr(sb, line_separator);
 563                strbuf_add(sb, line, linelen);
 564                first = 0;
 565        }
 566        return msg;
 567}
 568
 569static void parse_commit_message(struct format_commit_context *c)
 570{
 571        const char *msg = c->commit->buffer + c->message_off;
 572        const char *start = c->commit->buffer;
 573
 574        msg = skip_empty_lines(msg);
 575        c->subject_off = msg - start;
 576
 577        msg = format_subject(NULL, msg, NULL);
 578        msg = skip_empty_lines(msg);
 579        c->body_off = msg - start;
 580
 581        c->commit_message_parsed = 1;
 582}
 583
 584static void format_decoration(struct strbuf *sb, const struct commit *commit)
 585{
 586        struct name_decoration *d;
 587        const char *prefix = " (";
 588
 589        load_ref_decorations(DECORATE_SHORT_REFS);
 590        d = lookup_decoration(&name_decoration, &commit->object);
 591        while (d) {
 592                strbuf_addstr(sb, prefix);
 593                prefix = ", ";
 594                strbuf_addstr(sb, d->name);
 595                d = d->next;
 596        }
 597        if (prefix[0] == ',')
 598                strbuf_addch(sb, ')');
 599}
 600
 601static void strbuf_wrap(struct strbuf *sb, size_t pos,
 602                        size_t width, size_t indent1, size_t indent2)
 603{
 604        struct strbuf tmp = STRBUF_INIT;
 605
 606        if (pos)
 607                strbuf_add(&tmp, sb->buf, pos);
 608        strbuf_add_wrapped_text(&tmp, sb->buf + pos,
 609                                (int) indent1, (int) indent2, (int) width);
 610        strbuf_swap(&tmp, sb);
 611        strbuf_release(&tmp);
 612}
 613
 614static void rewrap_message_tail(struct strbuf *sb,
 615                                struct format_commit_context *c,
 616                                size_t new_width, size_t new_indent1,
 617                                size_t new_indent2)
 618{
 619        if (c->width == new_width && c->indent1 == new_indent1 &&
 620            c->indent2 == new_indent2)
 621                return;
 622        if (c->wrap_start < sb->len)
 623                strbuf_wrap(sb, c->wrap_start, c->width, c->indent1, c->indent2);
 624        c->wrap_start = sb->len;
 625        c->width = new_width;
 626        c->indent1 = new_indent1;
 627        c->indent2 = new_indent2;
 628}
 629
 630static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
 631                               void *context)
 632{
 633        struct format_commit_context *c = context;
 634        const struct commit *commit = c->commit;
 635        const char *msg = commit->buffer;
 636        struct commit_list *p;
 637        int h1, h2;
 638
 639        /* these are independent of the commit */
 640        switch (placeholder[0]) {
 641        case 'C':
 642                if (placeholder[1] == '(') {
 643                        const char *end = strchr(placeholder + 2, ')');
 644                        char color[COLOR_MAXLEN];
 645                        if (!end)
 646                                return 0;
 647                        color_parse_mem(placeholder + 2,
 648                                        end - (placeholder + 2),
 649                                        "--pretty format", color);
 650                        strbuf_addstr(sb, color);
 651                        return end - placeholder + 1;
 652                }
 653                if (!prefixcmp(placeholder + 1, "red")) {
 654                        strbuf_addstr(sb, GIT_COLOR_RED);
 655                        return 4;
 656                } else if (!prefixcmp(placeholder + 1, "green")) {
 657                        strbuf_addstr(sb, GIT_COLOR_GREEN);
 658                        return 6;
 659                } else if (!prefixcmp(placeholder + 1, "blue")) {
 660                        strbuf_addstr(sb, GIT_COLOR_BLUE);
 661                        return 5;
 662                } else if (!prefixcmp(placeholder + 1, "reset")) {
 663                        strbuf_addstr(sb, GIT_COLOR_RESET);
 664                        return 6;
 665                } else
 666                        return 0;
 667        case 'n':               /* newline */
 668                strbuf_addch(sb, '\n');
 669                return 1;
 670        case 'x':
 671                /* %x00 == NUL, %x0a == LF, etc. */
 672                if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&
 673                    h1 <= 16 &&
 674                    0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&
 675                    h2 <= 16) {
 676                        strbuf_addch(sb, (h1<<4)|h2);
 677                        return 3;
 678                } else
 679                        return 0;
 680        case 'w':
 681                if (placeholder[1] == '(') {
 682                        unsigned long width = 0, indent1 = 0, indent2 = 0;
 683                        char *next;
 684                        const char *start = placeholder + 2;
 685                        const char *end = strchr(start, ')');
 686                        if (!end)
 687                                return 0;
 688                        if (end > start) {
 689                                width = strtoul(start, &next, 10);
 690                                if (*next == ',') {
 691                                        indent1 = strtoul(next + 1, &next, 10);
 692                                        if (*next == ',') {
 693                                                indent2 = strtoul(next + 1,
 694                                                                 &next, 10);
 695                                        }
 696                                }
 697                                if (*next != ')')
 698                                        return 0;
 699                        }
 700                        rewrap_message_tail(sb, c, width, indent1, indent2);
 701                        return end - placeholder + 1;
 702                } else
 703                        return 0;
 704        }
 705
 706        /* these depend on the commit */
 707        if (!commit->object.parsed)
 708                parse_object(commit->object.sha1);
 709
 710        switch (placeholder[0]) {
 711        case 'H':               /* commit hash */
 712                strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
 713                return 1;
 714        case 'h':               /* abbreviated commit hash */
 715                if (add_again(sb, &c->abbrev_commit_hash))
 716                        return 1;
 717                strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
 718                                                     DEFAULT_ABBREV));
 719                c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
 720                return 1;
 721        case 'T':               /* tree hash */
 722                strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
 723                return 1;
 724        case 't':               /* abbreviated tree hash */
 725                if (add_again(sb, &c->abbrev_tree_hash))
 726                        return 1;
 727                strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
 728                                                     DEFAULT_ABBREV));
 729                c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
 730                return 1;
 731        case 'P':               /* parent hashes */
 732                for (p = commit->parents; p; p = p->next) {
 733                        if (p != commit->parents)
 734                                strbuf_addch(sb, ' ');
 735                        strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
 736                }
 737                return 1;
 738        case 'p':               /* abbreviated parent hashes */
 739                if (add_again(sb, &c->abbrev_parent_hashes))
 740                        return 1;
 741                for (p = commit->parents; p; p = p->next) {
 742                        if (p != commit->parents)
 743                                strbuf_addch(sb, ' ');
 744                        strbuf_addstr(sb, find_unique_abbrev(
 745                                        p->item->object.sha1, DEFAULT_ABBREV));
 746                }
 747                c->abbrev_parent_hashes.len = sb->len -
 748                                              c->abbrev_parent_hashes.off;
 749                return 1;
 750        case 'm':               /* left/right/bottom */
 751                strbuf_addch(sb, (commit->object.flags & BOUNDARY)
 752                                 ? '-'
 753                                 : (commit->object.flags & SYMMETRIC_LEFT)
 754                                 ? '<'
 755                                 : '>');
 756                return 1;
 757        case 'd':
 758                format_decoration(sb, commit);
 759                return 1;
 760        case 'g':               /* reflog info */
 761                switch(placeholder[1]) {
 762                case 'd':       /* reflog selector */
 763                case 'D':
 764                        if (c->pretty_ctx->reflog_info)
 765                                get_reflog_selector(sb,
 766                                                    c->pretty_ctx->reflog_info,
 767                                                    c->pretty_ctx->date_mode,
 768                                                    (placeholder[1] == 'd'));
 769                        return 2;
 770                case 's':       /* reflog message */
 771                        if (c->pretty_ctx->reflog_info)
 772                                get_reflog_message(sb, c->pretty_ctx->reflog_info);
 773                        return 2;
 774                }
 775                return 0;       /* unknown %g placeholder */
 776        }
 777
 778        /* For the rest we have to parse the commit header. */
 779        if (!c->commit_header_parsed)
 780                parse_commit_header(c);
 781
 782        switch (placeholder[0]) {
 783        case 'a':       /* author ... */
 784                return format_person_part(sb, placeholder[1],
 785                                   msg + c->author.off, c->author.len,
 786                                   c->pretty_ctx->date_mode);
 787        case 'c':       /* committer ... */
 788                return format_person_part(sb, placeholder[1],
 789                                   msg + c->committer.off, c->committer.len,
 790                                   c->pretty_ctx->date_mode);
 791        case 'e':       /* encoding */
 792                strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
 793                return 1;
 794        }
 795
 796        /* Now we need to parse the commit message. */
 797        if (!c->commit_message_parsed)
 798                parse_commit_message(c);
 799
 800        switch (placeholder[0]) {
 801        case 's':       /* subject */
 802                format_subject(sb, msg + c->subject_off, " ");
 803                return 1;
 804        case 'f':       /* sanitized subject */
 805                format_sanitized_subject(sb, msg + c->subject_off);
 806                return 1;
 807        case 'b':       /* body */
 808                strbuf_addstr(sb, msg + c->body_off);
 809                return 1;
 810        }
 811        return 0;       /* unknown placeholder */
 812}
 813
 814void format_commit_message(const struct commit *commit,
 815                           const char *format, struct strbuf *sb,
 816                           const struct pretty_print_context *pretty_ctx)
 817{
 818        struct format_commit_context context;
 819
 820        memset(&context, 0, sizeof(context));
 821        context.commit = commit;
 822        context.pretty_ctx = pretty_ctx;
 823        context.wrap_start = sb->len;
 824        strbuf_expand(sb, format, format_commit_item, &context);
 825        rewrap_message_tail(sb, &context, 0, 0, 0);
 826}
 827
 828static void pp_header(enum cmit_fmt fmt,
 829                      int abbrev,
 830                      enum date_mode dmode,
 831                      const char *encoding,
 832                      const struct commit *commit,
 833                      const char **msg_p,
 834                      struct strbuf *sb)
 835{
 836        int parents_shown = 0;
 837
 838        for (;;) {
 839                const char *line = *msg_p;
 840                int linelen = get_one_line(*msg_p);
 841
 842                if (!linelen)
 843                        return;
 844                *msg_p += linelen;
 845
 846                if (linelen == 1)
 847                        /* End of header */
 848                        return;
 849
 850                if (fmt == CMIT_FMT_RAW) {
 851                        strbuf_add(sb, line, linelen);
 852                        continue;
 853                }
 854
 855                if (!memcmp(line, "parent ", 7)) {
 856                        if (linelen != 48)
 857                                die("bad parent line in commit");
 858                        continue;
 859                }
 860
 861                if (!parents_shown) {
 862                        struct commit_list *parent;
 863                        int num;
 864                        for (parent = commit->parents, num = 0;
 865                             parent;
 866                             parent = parent->next, num++)
 867                                ;
 868                        /* with enough slop */
 869                        strbuf_grow(sb, num * 50 + 20);
 870                        add_merge_info(fmt, sb, commit, abbrev);
 871                        parents_shown = 1;
 872                }
 873
 874                /*
 875                 * MEDIUM == DEFAULT shows only author with dates.
 876                 * FULL shows both authors but not dates.
 877                 * FULLER shows both authors and dates.
 878                 */
 879                if (!memcmp(line, "author ", 7)) {
 880                        strbuf_grow(sb, linelen + 80);
 881                        pp_user_info("Author", fmt, sb, line + 7, dmode, encoding);
 882                }
 883                if (!memcmp(line, "committer ", 10) &&
 884                    (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) {
 885                        strbuf_grow(sb, linelen + 80);
 886                        pp_user_info("Commit", fmt, sb, line + 10, dmode, encoding);
 887                }
 888        }
 889}
 890
 891void pp_title_line(enum cmit_fmt fmt,
 892                   const char **msg_p,
 893                   struct strbuf *sb,
 894                   const char *subject,
 895                   const char *after_subject,
 896                   const char *encoding,
 897                   int need_8bit_cte)
 898{
 899        const char *line_separator = (fmt == CMIT_FMT_EMAIL) ? "\n " : " ";
 900        struct strbuf title;
 901
 902        strbuf_init(&title, 80);
 903        *msg_p = format_subject(&title, *msg_p, line_separator);
 904
 905        strbuf_grow(sb, title.len + 1024);
 906        if (subject) {
 907                strbuf_addstr(sb, subject);
 908                add_rfc2047(sb, title.buf, title.len, encoding);
 909        } else {
 910                strbuf_addbuf(sb, &title);
 911        }
 912        strbuf_addch(sb, '\n');
 913
 914        if (need_8bit_cte > 0) {
 915                const char *header_fmt =
 916                        "MIME-Version: 1.0\n"
 917                        "Content-Type: text/plain; charset=%s\n"
 918                        "Content-Transfer-Encoding: 8bit\n";
 919                strbuf_addf(sb, header_fmt, encoding);
 920        }
 921        if (after_subject) {
 922                strbuf_addstr(sb, after_subject);
 923        }
 924        if (fmt == CMIT_FMT_EMAIL) {
 925                strbuf_addch(sb, '\n');
 926        }
 927        strbuf_release(&title);
 928}
 929
 930void pp_remainder(enum cmit_fmt fmt,
 931                  const char **msg_p,
 932                  struct strbuf *sb,
 933                  int indent)
 934{
 935        int first = 1;
 936        for (;;) {
 937                const char *line = *msg_p;
 938                int linelen = get_one_line(line);
 939                *msg_p += linelen;
 940
 941                if (!linelen)
 942                        break;
 943
 944                if (is_empty_line(line, &linelen)) {
 945                        if (first)
 946                                continue;
 947                        if (fmt == CMIT_FMT_SHORT)
 948                                break;
 949                }
 950                first = 0;
 951
 952                strbuf_grow(sb, linelen + indent + 20);
 953                if (indent) {
 954                        memset(sb->buf + sb->len, ' ', indent);
 955                        strbuf_setlen(sb, sb->len + indent);
 956                }
 957                strbuf_add(sb, line, linelen);
 958                strbuf_addch(sb, '\n');
 959        }
 960}
 961
 962char *reencode_commit_message(const struct commit *commit, const char **encoding_p)
 963{
 964        const char *encoding;
 965
 966        encoding = (git_log_output_encoding
 967                    ? git_log_output_encoding
 968                    : git_commit_encoding);
 969        if (!encoding)
 970                encoding = "UTF-8";
 971        if (encoding_p)
 972                *encoding_p = encoding;
 973        return logmsg_reencode(commit, encoding);
 974}
 975
 976void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
 977                         struct strbuf *sb,
 978                         const struct pretty_print_context *context)
 979{
 980        unsigned long beginning_of_body;
 981        int indent = 4;
 982        const char *msg = commit->buffer;
 983        char *reencoded;
 984        const char *encoding;
 985        int need_8bit_cte = context->need_8bit_cte;
 986
 987        if (fmt == CMIT_FMT_USERFORMAT) {
 988                format_commit_message(commit, user_format, sb, context);
 989                return;
 990        }
 991
 992        reencoded = reencode_commit_message(commit, &encoding);
 993        if (reencoded) {
 994                msg = reencoded;
 995        }
 996
 997        if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
 998                indent = 0;
 999
1000        /*
1001         * We need to check and emit Content-type: to mark it
1002         * as 8-bit if we haven't done so.
1003         */
1004        if (fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
1005                int i, ch, in_body;
1006
1007                for (in_body = i = 0; (ch = msg[i]); i++) {
1008                        if (!in_body) {
1009                                /* author could be non 7-bit ASCII but
1010                                 * the log may be so; skip over the
1011                                 * header part first.
1012                                 */
1013                                if (ch == '\n' && msg[i+1] == '\n')
1014                                        in_body = 1;
1015                        }
1016                        else if (non_ascii(ch)) {
1017                                need_8bit_cte = 1;
1018                                break;
1019                        }
1020                }
1021        }
1022
1023        pp_header(fmt, context->abbrev, context->date_mode, encoding,
1024                  commit, &msg, sb);
1025        if (fmt != CMIT_FMT_ONELINE && !context->subject) {
1026                strbuf_addch(sb, '\n');
1027        }
1028
1029        /* Skip excess blank lines at the beginning of body, if any... */
1030        msg = skip_empty_lines(msg);
1031
1032        /* These formats treat the title line specially. */
1033        if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
1034                pp_title_line(fmt, &msg, sb, context->subject,
1035                              context->after_subject, encoding, need_8bit_cte);
1036
1037        beginning_of_body = sb->len;
1038        if (fmt != CMIT_FMT_ONELINE)
1039                pp_remainder(fmt, &msg, sb, indent);
1040        strbuf_rtrim(sb);
1041
1042        /* Make sure there is an EOLN for the non-oneline case */
1043        if (fmt != CMIT_FMT_ONELINE)
1044                strbuf_addch(sb, '\n');
1045
1046        /*
1047         * The caller may append additional body text in e-mail
1048         * format.  Make sure we did not strip the blank line
1049         * between the header and the body.
1050         */
1051        if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
1052                strbuf_addch(sb, '\n');
1053        free(reencoded);
1054}