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