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