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