pretty.con commit git-notes: fix printing of multi-line notes (22a3d06)
   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 "notes.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 void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
 186                        const struct commit *commit, int abbrev)
 187{
 188        struct commit_list *parent = commit->parents;
 189
 190        if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
 191            !parent || !parent->next)
 192                return;
 193
 194        strbuf_addstr(sb, "Merge:");
 195
 196        while (parent) {
 197                struct commit *p = parent->item;
 198                const char *hex = NULL;
 199                const char *dots;
 200                if (abbrev)
 201                        hex = find_unique_abbrev(p->object.sha1, abbrev);
 202                if (!hex)
 203                        hex = sha1_to_hex(p->object.sha1);
 204                dots = (abbrev && strlen(hex) != 40) ?  "..." : "";
 205                parent = parent->next;
 206
 207                strbuf_addf(sb, " %s%s", hex, dots);
 208        }
 209        strbuf_addch(sb, '\n');
 210}
 211
 212static char *get_header(const struct commit *commit, const char *key)
 213{
 214        int key_len = strlen(key);
 215        const char *line = commit->buffer;
 216
 217        for (;;) {
 218                const char *eol = strchr(line, '\n'), *next;
 219
 220                if (line == eol)
 221                        return NULL;
 222                if (!eol) {
 223                        eol = line + strlen(line);
 224                        next = NULL;
 225                } else
 226                        next = eol + 1;
 227                if (eol - line > key_len &&
 228                    !strncmp(line, key, key_len) &&
 229                    line[key_len] == ' ') {
 230                        return xmemdupz(line + key_len + 1, eol - line - key_len - 1);
 231                }
 232                line = next;
 233        }
 234}
 235
 236static char *replace_encoding_header(char *buf, const char *encoding)
 237{
 238        struct strbuf tmp = STRBUF_INIT;
 239        size_t start, len;
 240        char *cp = buf;
 241
 242        /* guess if there is an encoding header before a \n\n */
 243        while (strncmp(cp, "encoding ", strlen("encoding "))) {
 244                cp = strchr(cp, '\n');
 245                if (!cp || *++cp == '\n')
 246                        return buf;
 247        }
 248        start = cp - buf;
 249        cp = strchr(cp, '\n');
 250        if (!cp)
 251                return buf; /* should not happen but be defensive */
 252        len = cp + 1 - (buf + start);
 253
 254        strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
 255        if (is_encoding_utf8(encoding)) {
 256                /* we have re-coded to UTF-8; drop the header */
 257                strbuf_remove(&tmp, start, len);
 258        } else {
 259                /* just replaces XXXX in 'encoding XXXX\n' */
 260                strbuf_splice(&tmp, start + strlen("encoding "),
 261                                          len - strlen("encoding \n"),
 262                                          encoding, strlen(encoding));
 263        }
 264        return strbuf_detach(&tmp, NULL);
 265}
 266
 267static char *logmsg_reencode(const struct commit *commit,
 268                             const char *output_encoding)
 269{
 270        static const char *utf8 = "utf-8";
 271        const char *use_encoding;
 272        char *encoding;
 273        char *out;
 274
 275        if (!*output_encoding)
 276                return NULL;
 277        encoding = get_header(commit, "encoding");
 278        use_encoding = encoding ? encoding : utf8;
 279        if (!strcmp(use_encoding, output_encoding))
 280                if (encoding) /* we'll strip encoding header later */
 281                        out = xstrdup(commit->buffer);
 282                else
 283                        return NULL; /* nothing to do */
 284        else
 285                out = reencode_string(commit->buffer,
 286                                      output_encoding, use_encoding);
 287        if (out)
 288                out = replace_encoding_header(out, output_encoding);
 289
 290        free(encoding);
 291        return out;
 292}
 293
 294static int mailmap_name(struct strbuf *sb, const char *email)
 295{
 296        static struct string_list *mail_map;
 297        char buffer[1024];
 298
 299        if (!mail_map) {
 300                mail_map = xcalloc(1, sizeof(*mail_map));
 301                read_mailmap(mail_map, ".mailmap", NULL);
 302        }
 303
 304        if (!mail_map->nr)
 305                return -1;
 306
 307        if (!map_email(mail_map, email, buffer, sizeof(buffer)))
 308                return -1;
 309        strbuf_addstr(sb, buffer);
 310        return 0;
 311}
 312
 313static size_t format_person_part(struct strbuf *sb, char part,
 314                                 const char *msg, int len, enum date_mode dmode)
 315{
 316        /* currently all placeholders have same length */
 317        const int placeholder_len = 2;
 318        int start, end, tz = 0;
 319        unsigned long date = 0;
 320        char *ep;
 321
 322        /* advance 'end' to point to email start delimiter */
 323        for (end = 0; end < len && msg[end] != '<'; end++)
 324                ; /* do nothing */
 325
 326        /*
 327         * When end points at the '<' that we found, it should have
 328         * matching '>' later, which means 'end' must be strictly
 329         * below len - 1.
 330         */
 331        if (end >= len - 2)
 332                goto skip;
 333
 334        if (part == 'n' || part == 'N') {       /* name */
 335                while (end > 0 && isspace(msg[end - 1]))
 336                        end--;
 337                if (part != 'N' || !msg[end] || !msg[end + 1] ||
 338                    mailmap_name(sb, msg + end + 2) < 0)
 339                        strbuf_add(sb, msg, end);
 340                return placeholder_len;
 341        }
 342        start = ++end; /* save email start position */
 343
 344        /* advance 'end' to point to email end delimiter */
 345        for ( ; end < len && msg[end] != '>'; end++)
 346                ; /* do nothing */
 347
 348        if (end >= len)
 349                goto skip;
 350
 351        if (part == 'e') {      /* email */
 352                strbuf_add(sb, msg + start, end - start);
 353                return placeholder_len;
 354        }
 355
 356        /* advance 'start' to point to date start delimiter */
 357        for (start = end + 1; start < len && isspace(msg[start]); start++)
 358                ; /* do nothing */
 359        if (start >= len)
 360                goto skip;
 361        date = strtoul(msg + start, &ep, 10);
 362        if (msg + start == ep)
 363                goto skip;
 364
 365        if (part == 't') {      /* date, UNIX timestamp */
 366                strbuf_add(sb, msg + start, ep - (msg + start));
 367                return placeholder_len;
 368        }
 369
 370        /* parse tz */
 371        for (start = ep - msg + 1; start < len && isspace(msg[start]); start++)
 372                ; /* do nothing */
 373        if (start + 1 < len) {
 374                tz = strtoul(msg + start + 1, NULL, 10);
 375                if (msg[start] == '-')
 376                        tz = -tz;
 377        }
 378
 379        switch (part) {
 380        case 'd':       /* date */
 381                strbuf_addstr(sb, show_date(date, tz, dmode));
 382                return placeholder_len;
 383        case 'D':       /* date, RFC2822 style */
 384                strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
 385                return placeholder_len;
 386        case 'r':       /* date, relative */
 387                strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE));
 388                return placeholder_len;
 389        case 'i':       /* date, ISO 8601 */
 390                strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601));
 391                return placeholder_len;
 392        }
 393
 394skip:
 395        /*
 396         * bogus commit, 'sb' cannot be updated, but we still need to
 397         * compute a valid return value.
 398         */
 399        if (part == 'n' || part == 'e' || part == 't' || part == 'd'
 400            || part == 'D' || part == 'r' || part == 'i')
 401                return placeholder_len;
 402
 403        return 0; /* unknown placeholder */
 404}
 405
 406struct chunk {
 407        size_t off;
 408        size_t len;
 409};
 410
 411struct format_commit_context {
 412        const struct commit *commit;
 413        enum date_mode dmode;
 414
 415        /* These offsets are relative to the start of the commit message. */
 416        int commit_header_parsed;
 417        struct chunk subject;
 418        struct chunk author;
 419        struct chunk committer;
 420        struct chunk encoding;
 421        size_t body_off;
 422
 423        /* The following ones are relative to the result struct strbuf. */
 424        struct chunk abbrev_commit_hash;
 425        struct chunk abbrev_tree_hash;
 426        struct chunk abbrev_parent_hashes;
 427};
 428
 429static int add_again(struct strbuf *sb, struct chunk *chunk)
 430{
 431        if (chunk->len) {
 432                strbuf_adddup(sb, chunk->off, chunk->len);
 433                return 1;
 434        }
 435
 436        /*
 437         * We haven't seen this chunk before.  Our caller is surely
 438         * going to add it the hard way now.  Remember the most likely
 439         * start of the to-be-added chunk: the current end of the
 440         * struct strbuf.
 441         */
 442        chunk->off = sb->len;
 443        return 0;
 444}
 445
 446static void parse_commit_header(struct format_commit_context *context)
 447{
 448        const char *msg = context->commit->buffer;
 449        int i;
 450        enum { HEADER, SUBJECT, BODY } state;
 451
 452        for (i = 0, state = HEADER; msg[i] && state < BODY; i++) {
 453                int eol;
 454                for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
 455                        ; /* do nothing */
 456
 457                if (state == SUBJECT) {
 458                        context->subject.off = i;
 459                        context->subject.len = eol - i;
 460                        i = eol;
 461                }
 462                if (i == eol) {
 463                        state++;
 464                        /* strip empty lines */
 465                        while (msg[eol] == '\n' && msg[eol + 1] == '\n')
 466                                eol++;
 467                } else if (!prefixcmp(msg + i, "author ")) {
 468                        context->author.off = i + 7;
 469                        context->author.len = eol - i - 7;
 470                } else if (!prefixcmp(msg + i, "committer ")) {
 471                        context->committer.off = i + 10;
 472                        context->committer.len = eol - i - 10;
 473                } else if (!prefixcmp(msg + i, "encoding ")) {
 474                        context->encoding.off = i + 9;
 475                        context->encoding.len = eol - i - 9;
 476                }
 477                i = eol;
 478                if (!msg[i])
 479                        break;
 480        }
 481        context->body_off = i;
 482        context->commit_header_parsed = 1;
 483}
 484
 485static void format_decoration(struct strbuf *sb, const struct commit *commit)
 486{
 487        struct name_decoration *d;
 488        const char *prefix = " (";
 489
 490        load_ref_decorations();
 491        d = lookup_decoration(&name_decoration, &commit->object);
 492        while (d) {
 493                strbuf_addstr(sb, prefix);
 494                prefix = ", ";
 495                strbuf_addstr(sb, d->name);
 496                d = d->next;
 497        }
 498        if (prefix[0] == ',')
 499                strbuf_addch(sb, ')');
 500}
 501
 502static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
 503                               void *context)
 504{
 505        struct format_commit_context *c = context;
 506        const struct commit *commit = c->commit;
 507        const char *msg = commit->buffer;
 508        struct commit_list *p;
 509        int h1, h2;
 510
 511        /* these are independent of the commit */
 512        switch (placeholder[0]) {
 513        case 'C':
 514                if (!prefixcmp(placeholder + 1, "red")) {
 515                        strbuf_addstr(sb, "\033[31m");
 516                        return 4;
 517                } else if (!prefixcmp(placeholder + 1, "green")) {
 518                        strbuf_addstr(sb, "\033[32m");
 519                        return 6;
 520                } else if (!prefixcmp(placeholder + 1, "blue")) {
 521                        strbuf_addstr(sb, "\033[34m");
 522                        return 5;
 523                } else if (!prefixcmp(placeholder + 1, "reset")) {
 524                        strbuf_addstr(sb, "\033[m");
 525                        return 6;
 526                } else
 527                        return 0;
 528        case 'n':               /* newline */
 529                strbuf_addch(sb, '\n');
 530                return 1;
 531        case 'x':
 532                /* %x00 == NUL, %x0a == LF, etc. */
 533                if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&
 534                    h1 <= 16 &&
 535                    0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&
 536                    h2 <= 16) {
 537                        strbuf_addch(sb, (h1<<4)|h2);
 538                        return 3;
 539                } else
 540                        return 0;
 541        }
 542
 543        /* these depend on the commit */
 544        if (!commit->object.parsed)
 545                parse_object(commit->object.sha1);
 546
 547        switch (placeholder[0]) {
 548        case 'H':               /* commit hash */
 549                strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
 550                return 1;
 551        case 'h':               /* abbreviated commit hash */
 552                if (add_again(sb, &c->abbrev_commit_hash))
 553                        return 1;
 554                strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
 555                                                     DEFAULT_ABBREV));
 556                c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
 557                return 1;
 558        case 'T':               /* tree hash */
 559                strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
 560                return 1;
 561        case 't':               /* abbreviated tree hash */
 562                if (add_again(sb, &c->abbrev_tree_hash))
 563                        return 1;
 564                strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
 565                                                     DEFAULT_ABBREV));
 566                c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
 567                return 1;
 568        case 'P':               /* parent hashes */
 569                for (p = commit->parents; p; p = p->next) {
 570                        if (p != commit->parents)
 571                                strbuf_addch(sb, ' ');
 572                        strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
 573                }
 574                return 1;
 575        case 'p':               /* abbreviated parent hashes */
 576                if (add_again(sb, &c->abbrev_parent_hashes))
 577                        return 1;
 578                for (p = commit->parents; p; p = p->next) {
 579                        if (p != commit->parents)
 580                                strbuf_addch(sb, ' ');
 581                        strbuf_addstr(sb, find_unique_abbrev(
 582                                        p->item->object.sha1, DEFAULT_ABBREV));
 583                }
 584                c->abbrev_parent_hashes.len = sb->len -
 585                                              c->abbrev_parent_hashes.off;
 586                return 1;
 587        case 'm':               /* left/right/bottom */
 588                strbuf_addch(sb, (commit->object.flags & BOUNDARY)
 589                                 ? '-'
 590                                 : (commit->object.flags & SYMMETRIC_LEFT)
 591                                 ? '<'
 592                                 : '>');
 593                return 1;
 594        case 'd':
 595                format_decoration(sb, commit);
 596                return 1;
 597        }
 598
 599        /* For the rest we have to parse the commit header. */
 600        if (!c->commit_header_parsed)
 601                parse_commit_header(c);
 602
 603        switch (placeholder[0]) {
 604        case 's':       /* subject */
 605                strbuf_add(sb, msg + c->subject.off, c->subject.len);
 606                return 1;
 607        case 'a':       /* author ... */
 608                return format_person_part(sb, placeholder[1],
 609                                   msg + c->author.off, c->author.len,
 610                                   c->dmode);
 611        case 'c':       /* committer ... */
 612                return format_person_part(sb, placeholder[1],
 613                                   msg + c->committer.off, c->committer.len,
 614                                   c->dmode);
 615        case 'e':       /* encoding */
 616                strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
 617                return 1;
 618        case 'b':       /* body */
 619                strbuf_addstr(sb, msg + c->body_off);
 620                return 1;
 621        }
 622        return 0;       /* unknown placeholder */
 623}
 624
 625void format_commit_message(const struct commit *commit,
 626                           const void *format, struct strbuf *sb,
 627                           enum date_mode dmode)
 628{
 629        struct format_commit_context context;
 630
 631        memset(&context, 0, sizeof(context));
 632        context.commit = commit;
 633        context.dmode = dmode;
 634        strbuf_expand(sb, format, format_commit_item, &context);
 635}
 636
 637static void pp_header(enum cmit_fmt fmt,
 638                      int abbrev,
 639                      enum date_mode dmode,
 640                      const char *encoding,
 641                      const struct commit *commit,
 642                      const char **msg_p,
 643                      struct strbuf *sb)
 644{
 645        int parents_shown = 0;
 646
 647        for (;;) {
 648                const char *line = *msg_p;
 649                int linelen = get_one_line(*msg_p);
 650
 651                if (!linelen)
 652                        return;
 653                *msg_p += linelen;
 654
 655                if (linelen == 1)
 656                        /* End of header */
 657                        return;
 658
 659                if (fmt == CMIT_FMT_RAW) {
 660                        strbuf_add(sb, line, linelen);
 661                        continue;
 662                }
 663
 664                if (!memcmp(line, "parent ", 7)) {
 665                        if (linelen != 48)
 666                                die("bad parent line in commit");
 667                        continue;
 668                }
 669
 670                if (!parents_shown) {
 671                        struct commit_list *parent;
 672                        int num;
 673                        for (parent = commit->parents, num = 0;
 674                             parent;
 675                             parent = parent->next, num++)
 676                                ;
 677                        /* with enough slop */
 678                        strbuf_grow(sb, num * 50 + 20);
 679                        add_merge_info(fmt, sb, commit, abbrev);
 680                        parents_shown = 1;
 681                }
 682
 683                /*
 684                 * MEDIUM == DEFAULT shows only author with dates.
 685                 * FULL shows both authors but not dates.
 686                 * FULLER shows both authors and dates.
 687                 */
 688                if (!memcmp(line, "author ", 7)) {
 689                        strbuf_grow(sb, linelen + 80);
 690                        pp_user_info("Author", fmt, sb, line + 7, dmode, encoding);
 691                }
 692                if (!memcmp(line, "committer ", 10) &&
 693                    (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) {
 694                        strbuf_grow(sb, linelen + 80);
 695                        pp_user_info("Commit", fmt, sb, line + 10, dmode, encoding);
 696                }
 697        }
 698}
 699
 700void pp_title_line(enum cmit_fmt fmt,
 701                   const char **msg_p,
 702                   struct strbuf *sb,
 703                   const char *subject,
 704                   const char *after_subject,
 705                   const char *encoding,
 706                   int need_8bit_cte)
 707{
 708        struct strbuf title;
 709
 710        strbuf_init(&title, 80);
 711
 712        for (;;) {
 713                const char *line = *msg_p;
 714                int linelen = get_one_line(line);
 715
 716                *msg_p += linelen;
 717                if (!linelen || is_empty_line(line, &linelen))
 718                        break;
 719
 720                strbuf_grow(&title, linelen + 2);
 721                if (title.len) {
 722                        if (fmt == CMIT_FMT_EMAIL) {
 723                                strbuf_addch(&title, '\n');
 724                        }
 725                        strbuf_addch(&title, ' ');
 726                }
 727                strbuf_add(&title, line, linelen);
 728        }
 729
 730        strbuf_grow(sb, title.len + 1024);
 731        if (subject) {
 732                strbuf_addstr(sb, subject);
 733                add_rfc2047(sb, title.buf, title.len, encoding);
 734        } else {
 735                strbuf_addbuf(sb, &title);
 736        }
 737        strbuf_addch(sb, '\n');
 738
 739        if (need_8bit_cte > 0) {
 740                const char *header_fmt =
 741                        "MIME-Version: 1.0\n"
 742                        "Content-Type: text/plain; charset=%s\n"
 743                        "Content-Transfer-Encoding: 8bit\n";
 744                strbuf_addf(sb, header_fmt, encoding);
 745        }
 746        if (after_subject) {
 747                strbuf_addstr(sb, after_subject);
 748        }
 749        if (fmt == CMIT_FMT_EMAIL) {
 750                strbuf_addch(sb, '\n');
 751        }
 752        strbuf_release(&title);
 753}
 754
 755void pp_remainder(enum cmit_fmt fmt,
 756                  const char **msg_p,
 757                  struct strbuf *sb,
 758                  int indent)
 759{
 760        int first = 1;
 761        for (;;) {
 762                const char *line = *msg_p;
 763                int linelen = get_one_line(line);
 764                *msg_p += linelen;
 765
 766                if (!linelen)
 767                        break;
 768
 769                if (is_empty_line(line, &linelen)) {
 770                        if (first)
 771                                continue;
 772                        if (fmt == CMIT_FMT_SHORT)
 773                                break;
 774                }
 775                first = 0;
 776
 777                strbuf_grow(sb, linelen + indent + 20);
 778                if (indent) {
 779                        memset(sb->buf + sb->len, ' ', indent);
 780                        strbuf_setlen(sb, sb->len + indent);
 781                }
 782                strbuf_add(sb, line, linelen);
 783                strbuf_addch(sb, '\n');
 784        }
 785}
 786
 787char *reencode_commit_message(const struct commit *commit, const char **encoding_p)
 788{
 789        const char *encoding;
 790
 791        encoding = (git_log_output_encoding
 792                    ? git_log_output_encoding
 793                    : git_commit_encoding);
 794        if (!encoding)
 795                encoding = "utf-8";
 796        if (encoding_p)
 797                *encoding_p = encoding;
 798        return logmsg_reencode(commit, encoding);
 799}
 800
 801void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
 802                         struct strbuf *sb, int abbrev,
 803                         const char *subject, const char *after_subject,
 804                         enum date_mode dmode, int need_8bit_cte)
 805{
 806        unsigned long beginning_of_body;
 807        int indent = 4;
 808        const char *msg = commit->buffer;
 809        char *reencoded;
 810        const char *encoding;
 811
 812        if (fmt == CMIT_FMT_USERFORMAT) {
 813                format_commit_message(commit, user_format, sb, dmode);
 814                return;
 815        }
 816
 817        reencoded = reencode_commit_message(commit, &encoding);
 818        if (reencoded) {
 819                msg = reencoded;
 820        }
 821
 822        if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
 823                indent = 0;
 824
 825        /*
 826         * We need to check and emit Content-type: to mark it
 827         * as 8-bit if we haven't done so.
 828         */
 829        if (fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
 830                int i, ch, in_body;
 831
 832                for (in_body = i = 0; (ch = msg[i]); i++) {
 833                        if (!in_body) {
 834                                /* author could be non 7-bit ASCII but
 835                                 * the log may be so; skip over the
 836                                 * header part first.
 837                                 */
 838                                if (ch == '\n' && msg[i+1] == '\n')
 839                                        in_body = 1;
 840                        }
 841                        else if (non_ascii(ch)) {
 842                                need_8bit_cte = 1;
 843                                break;
 844                        }
 845                }
 846        }
 847
 848        pp_header(fmt, abbrev, dmode, encoding, commit, &msg, sb);
 849        if (fmt != CMIT_FMT_ONELINE && !subject) {
 850                strbuf_addch(sb, '\n');
 851        }
 852
 853        /* Skip excess blank lines at the beginning of body, if any... */
 854        for (;;) {
 855                int linelen = get_one_line(msg);
 856                int ll = linelen;
 857                if (!linelen)
 858                        break;
 859                if (!is_empty_line(msg, &ll))
 860                        break;
 861                msg += linelen;
 862        }
 863
 864        /* These formats treat the title line specially. */
 865        if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
 866                pp_title_line(fmt, &msg, sb, subject,
 867                              after_subject, encoding, need_8bit_cte);
 868
 869        beginning_of_body = sb->len;
 870        if (fmt != CMIT_FMT_ONELINE)
 871                pp_remainder(fmt, &msg, sb, indent);
 872        strbuf_rtrim(sb);
 873
 874        /* Make sure there is an EOLN for the non-oneline case */
 875        if (fmt != CMIT_FMT_ONELINE)
 876                strbuf_addch(sb, '\n');
 877
 878        /*
 879         * The caller may append additional body text in e-mail
 880         * format.  Make sure we did not strip the blank line
 881         * between the header and the body.
 882         */
 883        if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
 884                strbuf_addch(sb, '\n');
 885
 886        if (fmt != CMIT_FMT_ONELINE)
 887                get_commit_notes(commit, sb, encoding);
 888
 889        free(reencoded);
 890}