613e4eab0db8baeaf905ae825bb7294b4c23363b
   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#include "color.h"
  11#include "reflog-walk.h"
  12#include "gpg-interface.h"
  13
  14static char *user_format;
  15static struct cmt_fmt_map {
  16        const char *name;
  17        enum cmit_fmt format;
  18        int is_tformat;
  19        int is_alias;
  20        const char *user_format;
  21} *commit_formats;
  22static size_t builtin_formats_len;
  23static size_t commit_formats_len;
  24static size_t commit_formats_alloc;
  25static struct cmt_fmt_map *find_commit_format(const char *sought);
  26
  27static void save_user_format(struct rev_info *rev, const char *cp, int is_tformat)
  28{
  29        free(user_format);
  30        user_format = xstrdup(cp);
  31        if (is_tformat)
  32                rev->use_terminator = 1;
  33        rev->commit_format = CMIT_FMT_USERFORMAT;
  34}
  35
  36static int git_pretty_formats_config(const char *var, const char *value, void *cb)
  37{
  38        struct cmt_fmt_map *commit_format = NULL;
  39        const char *name;
  40        const char *fmt;
  41        int i;
  42
  43        if (prefixcmp(var, "pretty."))
  44                return 0;
  45
  46        name = var + strlen("pretty.");
  47        for (i = 0; i < builtin_formats_len; i++) {
  48                if (!strcmp(commit_formats[i].name, name))
  49                        return 0;
  50        }
  51
  52        for (i = builtin_formats_len; i < commit_formats_len; i++) {
  53                if (!strcmp(commit_formats[i].name, name)) {
  54                        commit_format = &commit_formats[i];
  55                        break;
  56                }
  57        }
  58
  59        if (!commit_format) {
  60                ALLOC_GROW(commit_formats, commit_formats_len+1,
  61                           commit_formats_alloc);
  62                commit_format = &commit_formats[commit_formats_len];
  63                memset(commit_format, 0, sizeof(*commit_format));
  64                commit_formats_len++;
  65        }
  66
  67        commit_format->name = xstrdup(name);
  68        commit_format->format = CMIT_FMT_USERFORMAT;
  69        git_config_string(&fmt, var, value);
  70        if (!prefixcmp(fmt, "format:") || !prefixcmp(fmt, "tformat:")) {
  71                commit_format->is_tformat = fmt[0] == 't';
  72                fmt = strchr(fmt, ':') + 1;
  73        } else if (strchr(fmt, '%'))
  74                commit_format->is_tformat = 1;
  75        else
  76                commit_format->is_alias = 1;
  77        commit_format->user_format = fmt;
  78
  79        return 0;
  80}
  81
  82static void setup_commit_formats(void)
  83{
  84        struct cmt_fmt_map builtin_formats[] = {
  85                { "raw",        CMIT_FMT_RAW,           0 },
  86                { "medium",     CMIT_FMT_MEDIUM,        0 },
  87                { "short",      CMIT_FMT_SHORT,         0 },
  88                { "email",      CMIT_FMT_EMAIL,         0 },
  89                { "fuller",     CMIT_FMT_FULLER,        0 },
  90                { "full",       CMIT_FMT_FULL,          0 },
  91                { "oneline",    CMIT_FMT_ONELINE,       1 }
  92        };
  93        commit_formats_len = ARRAY_SIZE(builtin_formats);
  94        builtin_formats_len = commit_formats_len;
  95        ALLOC_GROW(commit_formats, commit_formats_len, commit_formats_alloc);
  96        memcpy(commit_formats, builtin_formats,
  97               sizeof(*builtin_formats)*ARRAY_SIZE(builtin_formats));
  98
  99        git_config(git_pretty_formats_config, NULL);
 100}
 101
 102static struct cmt_fmt_map *find_commit_format_recursive(const char *sought,
 103                                                        const char *original,
 104                                                        int num_redirections)
 105{
 106        struct cmt_fmt_map *found = NULL;
 107        size_t found_match_len = 0;
 108        int i;
 109
 110        if (num_redirections >= commit_formats_len)
 111                die("invalid --pretty format: "
 112                    "'%s' references an alias which points to itself",
 113                    original);
 114
 115        for (i = 0; i < commit_formats_len; i++) {
 116                size_t match_len;
 117
 118                if (prefixcmp(commit_formats[i].name, sought))
 119                        continue;
 120
 121                match_len = strlen(commit_formats[i].name);
 122                if (found == NULL || found_match_len > match_len) {
 123                        found = &commit_formats[i];
 124                        found_match_len = match_len;
 125                }
 126        }
 127
 128        if (found && found->is_alias) {
 129                found = find_commit_format_recursive(found->user_format,
 130                                                     original,
 131                                                     num_redirections+1);
 132        }
 133
 134        return found;
 135}
 136
 137static struct cmt_fmt_map *find_commit_format(const char *sought)
 138{
 139        if (!commit_formats)
 140                setup_commit_formats();
 141
 142        return find_commit_format_recursive(sought, sought, 0);
 143}
 144
 145void get_commit_format(const char *arg, struct rev_info *rev)
 146{
 147        struct cmt_fmt_map *commit_format;
 148
 149        rev->use_terminator = 0;
 150        if (!arg || !*arg) {
 151                rev->commit_format = CMIT_FMT_DEFAULT;
 152                return;
 153        }
 154        if (!prefixcmp(arg, "format:") || !prefixcmp(arg, "tformat:")) {
 155                save_user_format(rev, strchr(arg, ':') + 1, arg[0] == 't');
 156                return;
 157        }
 158
 159        if (strchr(arg, '%')) {
 160                save_user_format(rev, arg, 1);
 161                return;
 162        }
 163
 164        commit_format = find_commit_format(arg);
 165        if (!commit_format)
 166                die("invalid --pretty format: %s", arg);
 167
 168        rev->commit_format = commit_format->format;
 169        rev->use_terminator = commit_format->is_tformat;
 170        if (commit_format->format == CMIT_FMT_USERFORMAT) {
 171                save_user_format(rev, commit_format->user_format,
 172                                 commit_format->is_tformat);
 173        }
 174}
 175
 176/*
 177 * Generic support for pretty-printing the header
 178 */
 179static int get_one_line(const char *msg)
 180{
 181        int ret = 0;
 182
 183        for (;;) {
 184                char c = *msg++;
 185                if (!c)
 186                        break;
 187                ret++;
 188                if (c == '\n')
 189                        break;
 190        }
 191        return ret;
 192}
 193
 194/* High bit set, or ISO-2022-INT */
 195static int non_ascii(int ch)
 196{
 197        return !isascii(ch) || ch == '\033';
 198}
 199
 200int has_non_ascii(const char *s)
 201{
 202        int ch;
 203        if (!s)
 204                return 0;
 205        while ((ch = *s++) != '\0') {
 206                if (non_ascii(ch))
 207                        return 1;
 208        }
 209        return 0;
 210}
 211
 212static int is_rfc822_special(char ch)
 213{
 214        switch (ch) {
 215        case '(':
 216        case ')':
 217        case '<':
 218        case '>':
 219        case '[':
 220        case ']':
 221        case ':':
 222        case ';':
 223        case '@':
 224        case ',':
 225        case '.':
 226        case '"':
 227        case '\\':
 228                return 1;
 229        default:
 230                return 0;
 231        }
 232}
 233
 234static int has_rfc822_specials(const char *s, int len)
 235{
 236        int i;
 237        for (i = 0; i < len; i++)
 238                if (is_rfc822_special(s[i]))
 239                        return 1;
 240        return 0;
 241}
 242
 243static int last_line_length(struct strbuf *sb)
 244{
 245        int i;
 246
 247        /* How many bytes are already used on the last line? */
 248        for (i = sb->len - 1; i >= 0; i--)
 249                if (sb->buf[i] == '\n')
 250                        break;
 251        return sb->len - (i + 1);
 252}
 253
 254static void add_rfc822_quoted(struct strbuf *out, const char *s, int len)
 255{
 256        int i;
 257
 258        /* just a guess, we may have to also backslash-quote */
 259        strbuf_grow(out, len + 2);
 260
 261        strbuf_addch(out, '"');
 262        for (i = 0; i < len; i++) {
 263                switch (s[i]) {
 264                case '"':
 265                case '\\':
 266                        strbuf_addch(out, '\\');
 267                        /* fall through */
 268                default:
 269                        strbuf_addch(out, s[i]);
 270                }
 271        }
 272        strbuf_addch(out, '"');
 273}
 274
 275enum rfc2047_type {
 276        RFC2047_SUBJECT,
 277        RFC2047_ADDRESS,
 278};
 279
 280static int is_rfc2047_special(char ch, enum rfc2047_type type)
 281{
 282        /*
 283         * rfc2047, section 4.2:
 284         *
 285         *    8-bit values which correspond to printable ASCII characters other
 286         *    than "=", "?", and "_" (underscore), MAY be represented as those
 287         *    characters.  (But see section 5 for restrictions.)  In
 288         *    particular, SPACE and TAB MUST NOT be represented as themselves
 289         *    within encoded words.
 290         */
 291
 292        /*
 293         * rule out non-ASCII characters and non-printable characters (the
 294         * non-ASCII check should be redundant as isprint() is not localized
 295         * and only knows about ASCII, but be defensive about that)
 296         */
 297        if (non_ascii(ch) || !isprint(ch))
 298                return 1;
 299
 300        /*
 301         * rule out special printable characters (' ' should be the only
 302         * whitespace character considered printable, but be defensive and use
 303         * isspace())
 304         */
 305        if (isspace(ch) || ch == '=' || ch == '?' || ch == '_')
 306                return 1;
 307
 308        /*
 309         * rfc2047, section 5.3:
 310         *
 311         *    As a replacement for a 'word' entity within a 'phrase', for example,
 312         *    one that precedes an address in a From, To, or Cc header.  The ABNF
 313         *    definition for 'phrase' from RFC 822 thus becomes:
 314         *
 315         *    phrase = 1*( encoded-word / word )
 316         *
 317         *    In this case the set of characters that may be used in a "Q"-encoded
 318         *    'encoded-word' is restricted to: <upper and lower case ASCII
 319         *    letters, decimal digits, "!", "*", "+", "-", "/", "=", and "_"
 320         *    (underscore, ASCII 95.)>.  An 'encoded-word' that appears within a
 321         *    'phrase' MUST be separated from any adjacent 'word', 'text' or
 322         *    'special' by 'linear-white-space'.
 323         */
 324
 325        if (type != RFC2047_ADDRESS)
 326                return 0;
 327
 328        /* '=' and '_' are special cases and have been checked above */
 329        return !(isalnum(ch) || ch == '!' || ch == '*' || ch == '+' || ch == '-' || ch == '/');
 330}
 331
 332static void add_rfc2047(struct strbuf *sb, const char *line, int len,
 333                       const char *encoding, enum rfc2047_type type)
 334{
 335        static const int max_length = 78; /* per rfc2822 */
 336        static const int max_encoded_length = 76; /* per rfc2047 */
 337        int i;
 338        int line_len = last_line_length(sb);
 339
 340        for (i = 0; i < len; i++) {
 341                int ch = line[i];
 342                if (non_ascii(ch) || ch == '\n')
 343                        goto needquote;
 344                if ((i + 1 < len) && (ch == '=' && line[i+1] == '?'))
 345                        goto needquote;
 346        }
 347        strbuf_add_wrapped_bytes(sb, line, len, -line_len, 1, max_length);
 348        return;
 349
 350needquote:
 351        strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
 352        strbuf_addf(sb, "=?%s?q?", encoding);
 353        line_len += strlen(encoding) + 5; /* 5 for =??q? */
 354        for (i = 0; i < len; i++) {
 355                unsigned ch = line[i] & 0xFF;
 356                int is_special = is_rfc2047_special(ch, type);
 357
 358                /*
 359                 * According to RFC 2047, we could encode the special character
 360                 * ' ' (space) with '_' (underscore) for readability. But many
 361                 * programs do not understand this and just leave the
 362                 * underscore in place. Thus, we do nothing special here, which
 363                 * causes ' ' to be encoded as '=20', avoiding this problem.
 364                 */
 365
 366                if (line_len + 2 + (is_special ? 3 : 1) > max_encoded_length) {
 367                        strbuf_addf(sb, "?=\n =?%s?q?", encoding);
 368                        line_len = strlen(encoding) + 5 + 1; /* =??q? plus SP */
 369                }
 370
 371                if (is_special) {
 372                        strbuf_addf(sb, "=%02X", ch);
 373                        line_len += 3;
 374                } else {
 375                        strbuf_addch(sb, ch);
 376                        line_len++;
 377                }
 378        }
 379        strbuf_addstr(sb, "?=");
 380}
 381
 382void pp_user_info(const struct pretty_print_context *pp,
 383                  const char *what, struct strbuf *sb,
 384                  const char *line, const char *encoding)
 385{
 386        char *date;
 387        int namelen;
 388        unsigned long time;
 389        int tz;
 390
 391        if (pp->fmt == CMIT_FMT_ONELINE)
 392                return;
 393        date = strchr(line, '>');
 394        if (!date)
 395                return;
 396        namelen = ++date - line;
 397        time = strtoul(date, &date, 10);
 398        tz = strtol(date, NULL, 10);
 399
 400        if (pp->fmt == CMIT_FMT_EMAIL) {
 401                char *name_tail = strchr(line, '<');
 402                int display_name_length;
 403                if (!name_tail)
 404                        return;
 405                while (line < name_tail && isspace(name_tail[-1]))
 406                        name_tail--;
 407                display_name_length = name_tail - line;
 408                strbuf_addstr(sb, "From: ");
 409                if (!has_rfc822_specials(line, display_name_length)) {
 410                        add_rfc2047(sb, line, display_name_length,
 411                                                encoding, RFC2047_ADDRESS);
 412                } else {
 413                        struct strbuf quoted = STRBUF_INIT;
 414                        add_rfc822_quoted(&quoted, line, display_name_length);
 415                        add_rfc2047(sb, quoted.buf, quoted.len,
 416                                                encoding, RFC2047_ADDRESS);
 417                        strbuf_release(&quoted);
 418                }
 419                if (namelen - display_name_length + last_line_length(sb) > 78) {
 420                        strbuf_addch(sb, '\n');
 421                        if (!isspace(name_tail[0]))
 422                                strbuf_addch(sb, ' ');
 423                }
 424                strbuf_add(sb, name_tail, namelen - display_name_length);
 425                strbuf_addch(sb, '\n');
 426        } else {
 427                strbuf_addf(sb, "%s: %.*s%.*s\n", what,
 428                              (pp->fmt == CMIT_FMT_FULLER) ? 4 : 0,
 429                              "    ", namelen, line);
 430        }
 431        switch (pp->fmt) {
 432        case CMIT_FMT_MEDIUM:
 433                strbuf_addf(sb, "Date:   %s\n", show_date(time, tz, pp->date_mode));
 434                break;
 435        case CMIT_FMT_EMAIL:
 436                strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822));
 437                break;
 438        case CMIT_FMT_FULLER:
 439                strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, pp->date_mode));
 440                break;
 441        default:
 442                /* notin' */
 443                break;
 444        }
 445}
 446
 447static int is_empty_line(const char *line, int *len_p)
 448{
 449        int len = *len_p;
 450        while (len && isspace(line[len-1]))
 451                len--;
 452        *len_p = len;
 453        return !len;
 454}
 455
 456static const char *skip_empty_lines(const char *msg)
 457{
 458        for (;;) {
 459                int linelen = get_one_line(msg);
 460                int ll = linelen;
 461                if (!linelen)
 462                        break;
 463                if (!is_empty_line(msg, &ll))
 464                        break;
 465                msg += linelen;
 466        }
 467        return msg;
 468}
 469
 470static void add_merge_info(const struct pretty_print_context *pp,
 471                           struct strbuf *sb, const struct commit *commit)
 472{
 473        struct commit_list *parent = commit->parents;
 474
 475        if ((pp->fmt == CMIT_FMT_ONELINE) || (pp->fmt == CMIT_FMT_EMAIL) ||
 476            !parent || !parent->next)
 477                return;
 478
 479        strbuf_addstr(sb, "Merge:");
 480
 481        while (parent) {
 482                struct commit *p = parent->item;
 483                const char *hex = NULL;
 484                if (pp->abbrev)
 485                        hex = find_unique_abbrev(p->object.sha1, pp->abbrev);
 486                if (!hex)
 487                        hex = sha1_to_hex(p->object.sha1);
 488                parent = parent->next;
 489
 490                strbuf_addf(sb, " %s", hex);
 491        }
 492        strbuf_addch(sb, '\n');
 493}
 494
 495static char *get_header(const struct commit *commit, const char *key)
 496{
 497        int key_len = strlen(key);
 498        const char *line = commit->buffer;
 499
 500        while (line) {
 501                const char *eol = strchr(line, '\n'), *next;
 502
 503                if (line == eol)
 504                        return NULL;
 505                if (!eol) {
 506                        warning("malformed commit (header is missing newline): %s",
 507                                sha1_to_hex(commit->object.sha1));
 508                        eol = line + strlen(line);
 509                        next = NULL;
 510                } else
 511                        next = eol + 1;
 512                if (eol - line > key_len &&
 513                    !strncmp(line, key, key_len) &&
 514                    line[key_len] == ' ') {
 515                        return xmemdupz(line + key_len + 1, eol - line - key_len - 1);
 516                }
 517                line = next;
 518        }
 519        return NULL;
 520}
 521
 522static char *replace_encoding_header(char *buf, const char *encoding)
 523{
 524        struct strbuf tmp = STRBUF_INIT;
 525        size_t start, len;
 526        char *cp = buf;
 527
 528        /* guess if there is an encoding header before a \n\n */
 529        while (strncmp(cp, "encoding ", strlen("encoding "))) {
 530                cp = strchr(cp, '\n');
 531                if (!cp || *++cp == '\n')
 532                        return buf;
 533        }
 534        start = cp - buf;
 535        cp = strchr(cp, '\n');
 536        if (!cp)
 537                return buf; /* should not happen but be defensive */
 538        len = cp + 1 - (buf + start);
 539
 540        strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
 541        if (is_encoding_utf8(encoding)) {
 542                /* we have re-coded to UTF-8; drop the header */
 543                strbuf_remove(&tmp, start, len);
 544        } else {
 545                /* just replaces XXXX in 'encoding XXXX\n' */
 546                strbuf_splice(&tmp, start + strlen("encoding "),
 547                                          len - strlen("encoding \n"),
 548                                          encoding, strlen(encoding));
 549        }
 550        return strbuf_detach(&tmp, NULL);
 551}
 552
 553char *logmsg_reencode(const struct commit *commit,
 554                      const char *output_encoding)
 555{
 556        static const char *utf8 = "UTF-8";
 557        const char *use_encoding;
 558        char *encoding;
 559        char *out;
 560
 561        if (!*output_encoding)
 562                return NULL;
 563        encoding = get_header(commit, "encoding");
 564        use_encoding = encoding ? encoding : utf8;
 565        if (!strcmp(use_encoding, output_encoding))
 566                if (encoding) /* we'll strip encoding header later */
 567                        out = xstrdup(commit->buffer);
 568                else
 569                        return NULL; /* nothing to do */
 570        else
 571                out = reencode_string(commit->buffer,
 572                                      output_encoding, use_encoding);
 573        if (out)
 574                out = replace_encoding_header(out, output_encoding);
 575
 576        free(encoding);
 577        return out;
 578}
 579
 580static int mailmap_name(char *email, int email_len, char *name, int name_len)
 581{
 582        static struct string_list *mail_map;
 583        if (!mail_map) {
 584                mail_map = xcalloc(1, sizeof(*mail_map));
 585                read_mailmap(mail_map, NULL);
 586        }
 587        return mail_map->nr && map_user(mail_map, email, email_len, name, name_len);
 588}
 589
 590static size_t format_person_part(struct strbuf *sb, char part,
 591                                 const char *msg, int len, enum date_mode dmode)
 592{
 593        /* currently all placeholders have same length */
 594        const int placeholder_len = 2;
 595        int tz;
 596        unsigned long date = 0;
 597        char person_name[1024];
 598        char person_mail[1024];
 599        struct ident_split s;
 600        const char *name_start, *name_end, *mail_start, *mail_end;
 601
 602        if (split_ident_line(&s, msg, len) < 0)
 603                goto skip;
 604
 605        name_start = s.name_begin;
 606        name_end = s.name_end;
 607        mail_start = s.mail_begin;
 608        mail_end = s.mail_end;
 609
 610        if (part == 'N' || part == 'E') { /* mailmap lookup */
 611                snprintf(person_name, sizeof(person_name), "%.*s",
 612                         (int)(name_end - name_start), name_start);
 613                snprintf(person_mail, sizeof(person_mail), "%.*s",
 614                         (int)(mail_end - mail_start), mail_start);
 615                mailmap_name(person_mail, sizeof(person_mail), person_name, sizeof(person_name));
 616                name_start = person_name;
 617                name_end = name_start + strlen(person_name);
 618                mail_start = person_mail;
 619                mail_end = mail_start +  strlen(person_mail);
 620        }
 621        if (part == 'n' || part == 'N') {       /* name */
 622                strbuf_add(sb, name_start, name_end-name_start);
 623                return placeholder_len;
 624        }
 625        if (part == 'e' || part == 'E') {       /* email */
 626                strbuf_add(sb, mail_start, mail_end-mail_start);
 627                return placeholder_len;
 628        }
 629
 630        if (!s.date_begin)
 631                goto skip;
 632
 633        date = strtoul(s.date_begin, NULL, 10);
 634
 635        if (part == 't') {      /* date, UNIX timestamp */
 636                strbuf_add(sb, s.date_begin, s.date_end - s.date_begin);
 637                return placeholder_len;
 638        }
 639
 640        /* parse tz */
 641        tz = strtoul(s.tz_begin + 1, NULL, 10);
 642        if (*s.tz_begin == '-')
 643                tz = -tz;
 644
 645        switch (part) {
 646        case 'd':       /* date */
 647                strbuf_addstr(sb, show_date(date, tz, dmode));
 648                return placeholder_len;
 649        case 'D':       /* date, RFC2822 style */
 650                strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
 651                return placeholder_len;
 652        case 'r':       /* date, relative */
 653                strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE));
 654                return placeholder_len;
 655        case 'i':       /* date, ISO 8601 */
 656                strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601));
 657                return placeholder_len;
 658        }
 659
 660skip:
 661        /*
 662         * reading from either a bogus commit, or a reflog entry with
 663         * %gn, %ge, etc.; 'sb' cannot be updated, but we still need
 664         * to compute a valid return value.
 665         */
 666        if (part == 'n' || part == 'e' || part == 't' || part == 'd'
 667            || part == 'D' || part == 'r' || part == 'i')
 668                return placeholder_len;
 669
 670        return 0; /* unknown placeholder */
 671}
 672
 673struct chunk {
 674        size_t off;
 675        size_t len;
 676};
 677
 678struct format_commit_context {
 679        const struct commit *commit;
 680        const struct pretty_print_context *pretty_ctx;
 681        unsigned commit_header_parsed:1;
 682        unsigned commit_message_parsed:1;
 683        unsigned commit_signature_parsed:1;
 684        struct {
 685                char *gpg_output;
 686                char good_bad;
 687                char *signer;
 688        } signature;
 689        char *message;
 690        size_t width, indent1, indent2;
 691
 692        /* These offsets are relative to the start of the commit message. */
 693        struct chunk author;
 694        struct chunk committer;
 695        struct chunk encoding;
 696        size_t message_off;
 697        size_t subject_off;
 698        size_t body_off;
 699
 700        /* The following ones are relative to the result struct strbuf. */
 701        struct chunk abbrev_commit_hash;
 702        struct chunk abbrev_tree_hash;
 703        struct chunk abbrev_parent_hashes;
 704        size_t wrap_start;
 705};
 706
 707static int add_again(struct strbuf *sb, struct chunk *chunk)
 708{
 709        if (chunk->len) {
 710                strbuf_adddup(sb, chunk->off, chunk->len);
 711                return 1;
 712        }
 713
 714        /*
 715         * We haven't seen this chunk before.  Our caller is surely
 716         * going to add it the hard way now.  Remember the most likely
 717         * start of the to-be-added chunk: the current end of the
 718         * struct strbuf.
 719         */
 720        chunk->off = sb->len;
 721        return 0;
 722}
 723
 724static void parse_commit_header(struct format_commit_context *context)
 725{
 726        const char *msg = context->message;
 727        int i;
 728
 729        for (i = 0; msg[i]; i++) {
 730                int eol;
 731                for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
 732                        ; /* do nothing */
 733
 734                if (i == eol) {
 735                        break;
 736                } else if (!prefixcmp(msg + i, "author ")) {
 737                        context->author.off = i + 7;
 738                        context->author.len = eol - i - 7;
 739                } else if (!prefixcmp(msg + i, "committer ")) {
 740                        context->committer.off = i + 10;
 741                        context->committer.len = eol - i - 10;
 742                } else if (!prefixcmp(msg + i, "encoding ")) {
 743                        context->encoding.off = i + 9;
 744                        context->encoding.len = eol - i - 9;
 745                }
 746                i = eol;
 747        }
 748        context->message_off = i;
 749        context->commit_header_parsed = 1;
 750}
 751
 752static int istitlechar(char c)
 753{
 754        return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
 755                (c >= '0' && c <= '9') || c == '.' || c == '_';
 756}
 757
 758static void format_sanitized_subject(struct strbuf *sb, const char *msg)
 759{
 760        size_t trimlen;
 761        size_t start_len = sb->len;
 762        int space = 2;
 763
 764        for (; *msg && *msg != '\n'; msg++) {
 765                if (istitlechar(*msg)) {
 766                        if (space == 1)
 767                                strbuf_addch(sb, '-');
 768                        space = 0;
 769                        strbuf_addch(sb, *msg);
 770                        if (*msg == '.')
 771                                while (*(msg+1) == '.')
 772                                        msg++;
 773                } else
 774                        space |= 1;
 775        }
 776
 777        /* trim any trailing '.' or '-' characters */
 778        trimlen = 0;
 779        while (sb->len - trimlen > start_len &&
 780                (sb->buf[sb->len - 1 - trimlen] == '.'
 781                || sb->buf[sb->len - 1 - trimlen] == '-'))
 782                trimlen++;
 783        strbuf_remove(sb, sb->len - trimlen, trimlen);
 784}
 785
 786const char *format_subject(struct strbuf *sb, const char *msg,
 787                           const char *line_separator)
 788{
 789        int first = 1;
 790
 791        for (;;) {
 792                const char *line = msg;
 793                int linelen = get_one_line(line);
 794
 795                msg += linelen;
 796                if (!linelen || is_empty_line(line, &linelen))
 797                        break;
 798
 799                if (!sb)
 800                        continue;
 801                strbuf_grow(sb, linelen + 2);
 802                if (!first)
 803                        strbuf_addstr(sb, line_separator);
 804                strbuf_add(sb, line, linelen);
 805                first = 0;
 806        }
 807        return msg;
 808}
 809
 810static void parse_commit_message(struct format_commit_context *c)
 811{
 812        const char *msg = c->message + c->message_off;
 813        const char *start = c->message;
 814
 815        msg = skip_empty_lines(msg);
 816        c->subject_off = msg - start;
 817
 818        msg = format_subject(NULL, msg, NULL);
 819        msg = skip_empty_lines(msg);
 820        c->body_off = msg - start;
 821
 822        c->commit_message_parsed = 1;
 823}
 824
 825static void format_decoration(struct strbuf *sb, const struct commit *commit)
 826{
 827        struct name_decoration *d;
 828        const char *prefix = " (";
 829
 830        load_ref_decorations(DECORATE_SHORT_REFS);
 831        d = lookup_decoration(&name_decoration, &commit->object);
 832        while (d) {
 833                strbuf_addstr(sb, prefix);
 834                prefix = ", ";
 835                strbuf_addstr(sb, d->name);
 836                d = d->next;
 837        }
 838        if (prefix[0] == ',')
 839                strbuf_addch(sb, ')');
 840}
 841
 842static void strbuf_wrap(struct strbuf *sb, size_t pos,
 843                        size_t width, size_t indent1, size_t indent2)
 844{
 845        struct strbuf tmp = STRBUF_INIT;
 846
 847        if (pos)
 848                strbuf_add(&tmp, sb->buf, pos);
 849        strbuf_add_wrapped_text(&tmp, sb->buf + pos,
 850                                (int) indent1, (int) indent2, (int) width);
 851        strbuf_swap(&tmp, sb);
 852        strbuf_release(&tmp);
 853}
 854
 855static void rewrap_message_tail(struct strbuf *sb,
 856                                struct format_commit_context *c,
 857                                size_t new_width, size_t new_indent1,
 858                                size_t new_indent2)
 859{
 860        if (c->width == new_width && c->indent1 == new_indent1 &&
 861            c->indent2 == new_indent2)
 862                return;
 863        if (c->wrap_start < sb->len)
 864                strbuf_wrap(sb, c->wrap_start, c->width, c->indent1, c->indent2);
 865        c->wrap_start = sb->len;
 866        c->width = new_width;
 867        c->indent1 = new_indent1;
 868        c->indent2 = new_indent2;
 869}
 870
 871static struct {
 872        char result;
 873        const char *check;
 874} signature_check[] = {
 875        { 'G', ": Good signature from " },
 876        { 'B', ": BAD signature from " },
 877};
 878
 879static void parse_signature_lines(struct format_commit_context *ctx)
 880{
 881        const char *buf = ctx->signature.gpg_output;
 882        int i;
 883
 884        for (i = 0; i < ARRAY_SIZE(signature_check); i++) {
 885                const char *found = strstr(buf, signature_check[i].check);
 886                const char *next;
 887                if (!found)
 888                        continue;
 889                ctx->signature.good_bad = signature_check[i].result;
 890                found += strlen(signature_check[i].check);
 891                next = strchrnul(found, '\n');
 892                ctx->signature.signer = xmemdupz(found, next - found);
 893                break;
 894        }
 895}
 896
 897static void parse_commit_signature(struct format_commit_context *ctx)
 898{
 899        struct strbuf payload = STRBUF_INIT;
 900        struct strbuf signature = STRBUF_INIT;
 901        struct strbuf gpg_output = STRBUF_INIT;
 902        int status;
 903
 904        ctx->commit_signature_parsed = 1;
 905
 906        if (parse_signed_commit(ctx->commit->object.sha1,
 907                                &payload, &signature) <= 0)
 908                goto out;
 909        status = verify_signed_buffer(payload.buf, payload.len,
 910                                      signature.buf, signature.len,
 911                                      &gpg_output);
 912        if (status && !gpg_output.len)
 913                goto out;
 914        ctx->signature.gpg_output = strbuf_detach(&gpg_output, NULL);
 915        parse_signature_lines(ctx);
 916
 917 out:
 918        strbuf_release(&gpg_output);
 919        strbuf_release(&payload);
 920        strbuf_release(&signature);
 921}
 922
 923
 924static int format_reflog_person(struct strbuf *sb,
 925                                char part,
 926                                struct reflog_walk_info *log,
 927                                enum date_mode dmode)
 928{
 929        const char *ident;
 930
 931        if (!log)
 932                return 2;
 933
 934        ident = get_reflog_ident(log);
 935        if (!ident)
 936                return 2;
 937
 938        return format_person_part(sb, part, ident, strlen(ident), dmode);
 939}
 940
 941static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
 942                                void *context)
 943{
 944        struct format_commit_context *c = context;
 945        const struct commit *commit = c->commit;
 946        const char *msg = c->message;
 947        struct commit_list *p;
 948        int h1, h2;
 949
 950        /* these are independent of the commit */
 951        switch (placeholder[0]) {
 952        case 'C':
 953                if (placeholder[1] == '(') {
 954                        const char *end = strchr(placeholder + 2, ')');
 955                        char color[COLOR_MAXLEN];
 956                        if (!end)
 957                                return 0;
 958                        color_parse_mem(placeholder + 2,
 959                                        end - (placeholder + 2),
 960                                        "--pretty format", color);
 961                        strbuf_addstr(sb, color);
 962                        return end - placeholder + 1;
 963                }
 964                if (!prefixcmp(placeholder + 1, "red")) {
 965                        strbuf_addstr(sb, GIT_COLOR_RED);
 966                        return 4;
 967                } else if (!prefixcmp(placeholder + 1, "green")) {
 968                        strbuf_addstr(sb, GIT_COLOR_GREEN);
 969                        return 6;
 970                } else if (!prefixcmp(placeholder + 1, "blue")) {
 971                        strbuf_addstr(sb, GIT_COLOR_BLUE);
 972                        return 5;
 973                } else if (!prefixcmp(placeholder + 1, "reset")) {
 974                        strbuf_addstr(sb, GIT_COLOR_RESET);
 975                        return 6;
 976                } else
 977                        return 0;
 978        case 'n':               /* newline */
 979                strbuf_addch(sb, '\n');
 980                return 1;
 981        case 'x':
 982                /* %x00 == NUL, %x0a == LF, etc. */
 983                if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&
 984                    h1 <= 16 &&
 985                    0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&
 986                    h2 <= 16) {
 987                        strbuf_addch(sb, (h1<<4)|h2);
 988                        return 3;
 989                } else
 990                        return 0;
 991        case 'w':
 992                if (placeholder[1] == '(') {
 993                        unsigned long width = 0, indent1 = 0, indent2 = 0;
 994                        char *next;
 995                        const char *start = placeholder + 2;
 996                        const char *end = strchr(start, ')');
 997                        if (!end)
 998                                return 0;
 999                        if (end > start) {
1000                                width = strtoul(start, &next, 10);
1001                                if (*next == ',') {
1002                                        indent1 = strtoul(next + 1, &next, 10);
1003                                        if (*next == ',') {
1004                                                indent2 = strtoul(next + 1,
1005                                                                 &next, 10);
1006                                        }
1007                                }
1008                                if (*next != ')')
1009                                        return 0;
1010                        }
1011                        rewrap_message_tail(sb, c, width, indent1, indent2);
1012                        return end - placeholder + 1;
1013                } else
1014                        return 0;
1015        }
1016
1017        /* these depend on the commit */
1018        if (!commit->object.parsed)
1019                parse_object(commit->object.sha1);
1020
1021        switch (placeholder[0]) {
1022        case 'H':               /* commit hash */
1023                strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
1024                return 1;
1025        case 'h':               /* abbreviated commit hash */
1026                if (add_again(sb, &c->abbrev_commit_hash))
1027                        return 1;
1028                strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
1029                                                     c->pretty_ctx->abbrev));
1030                c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
1031                return 1;
1032        case 'T':               /* tree hash */
1033                strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
1034                return 1;
1035        case 't':               /* abbreviated tree hash */
1036                if (add_again(sb, &c->abbrev_tree_hash))
1037                        return 1;
1038                strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
1039                                                     c->pretty_ctx->abbrev));
1040                c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
1041                return 1;
1042        case 'P':               /* parent hashes */
1043                for (p = commit->parents; p; p = p->next) {
1044                        if (p != commit->parents)
1045                                strbuf_addch(sb, ' ');
1046                        strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
1047                }
1048                return 1;
1049        case 'p':               /* abbreviated parent hashes */
1050                if (add_again(sb, &c->abbrev_parent_hashes))
1051                        return 1;
1052                for (p = commit->parents; p; p = p->next) {
1053                        if (p != commit->parents)
1054                                strbuf_addch(sb, ' ');
1055                        strbuf_addstr(sb, find_unique_abbrev(
1056                                        p->item->object.sha1,
1057                                        c->pretty_ctx->abbrev));
1058                }
1059                c->abbrev_parent_hashes.len = sb->len -
1060                                              c->abbrev_parent_hashes.off;
1061                return 1;
1062        case 'm':               /* left/right/bottom */
1063                strbuf_addstr(sb, get_revision_mark(NULL, commit));
1064                return 1;
1065        case 'd':
1066                format_decoration(sb, commit);
1067                return 1;
1068        case 'g':               /* reflog info */
1069                switch(placeholder[1]) {
1070                case 'd':       /* reflog selector */
1071                case 'D':
1072                        if (c->pretty_ctx->reflog_info)
1073                                get_reflog_selector(sb,
1074                                                    c->pretty_ctx->reflog_info,
1075                                                    c->pretty_ctx->date_mode,
1076                                                    c->pretty_ctx->date_mode_explicit,
1077                                                    (placeholder[1] == 'd'));
1078                        return 2;
1079                case 's':       /* reflog message */
1080                        if (c->pretty_ctx->reflog_info)
1081                                get_reflog_message(sb, c->pretty_ctx->reflog_info);
1082                        return 2;
1083                case 'n':
1084                case 'N':
1085                case 'e':
1086                case 'E':
1087                        return format_reflog_person(sb,
1088                                                    placeholder[1],
1089                                                    c->pretty_ctx->reflog_info,
1090                                                    c->pretty_ctx->date_mode);
1091                }
1092                return 0;       /* unknown %g placeholder */
1093        case 'N':
1094                if (c->pretty_ctx->show_notes) {
1095                        format_display_notes(commit->object.sha1, sb,
1096                                    get_log_output_encoding(), 0);
1097                        return 1;
1098                }
1099                return 0;
1100        }
1101
1102        if (placeholder[0] == 'G') {
1103                if (!c->commit_signature_parsed)
1104                        parse_commit_signature(c);
1105                switch (placeholder[1]) {
1106                case 'G':
1107                        if (c->signature.gpg_output)
1108                                strbuf_addstr(sb, c->signature.gpg_output);
1109                        break;
1110                case '?':
1111                        switch (c->signature.good_bad) {
1112                        case 'G':
1113                        case 'B':
1114                                strbuf_addch(sb, c->signature.good_bad);
1115                        }
1116                        break;
1117                case 'S':
1118                        if (c->signature.signer)
1119                                strbuf_addstr(sb, c->signature.signer);
1120                        break;
1121                }
1122                return 2;
1123        }
1124
1125
1126        /* For the rest we have to parse the commit header. */
1127        if (!c->commit_header_parsed)
1128                parse_commit_header(c);
1129
1130        switch (placeholder[0]) {
1131        case 'a':       /* author ... */
1132                return format_person_part(sb, placeholder[1],
1133                                   msg + c->author.off, c->author.len,
1134                                   c->pretty_ctx->date_mode);
1135        case 'c':       /* committer ... */
1136                return format_person_part(sb, placeholder[1],
1137                                   msg + c->committer.off, c->committer.len,
1138                                   c->pretty_ctx->date_mode);
1139        case 'e':       /* encoding */
1140                strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
1141                return 1;
1142        case 'B':       /* raw body */
1143                /* message_off is always left at the initial newline */
1144                strbuf_addstr(sb, msg + c->message_off + 1);
1145                return 1;
1146        }
1147
1148        /* Now we need to parse the commit message. */
1149        if (!c->commit_message_parsed)
1150                parse_commit_message(c);
1151
1152        switch (placeholder[0]) {
1153        case 's':       /* subject */
1154                format_subject(sb, msg + c->subject_off, " ");
1155                return 1;
1156        case 'f':       /* sanitized subject */
1157                format_sanitized_subject(sb, msg + c->subject_off);
1158                return 1;
1159        case 'b':       /* body */
1160                strbuf_addstr(sb, msg + c->body_off);
1161                return 1;
1162        }
1163        return 0;       /* unknown placeholder */
1164}
1165
1166static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
1167                                 void *context)
1168{
1169        int consumed;
1170        size_t orig_len;
1171        enum {
1172                NO_MAGIC,
1173                ADD_LF_BEFORE_NON_EMPTY,
1174                DEL_LF_BEFORE_EMPTY,
1175                ADD_SP_BEFORE_NON_EMPTY
1176        } magic = NO_MAGIC;
1177
1178        switch (placeholder[0]) {
1179        case '-':
1180                magic = DEL_LF_BEFORE_EMPTY;
1181                break;
1182        case '+':
1183                magic = ADD_LF_BEFORE_NON_EMPTY;
1184                break;
1185        case ' ':
1186                magic = ADD_SP_BEFORE_NON_EMPTY;
1187                break;
1188        default:
1189                break;
1190        }
1191        if (magic != NO_MAGIC)
1192                placeholder++;
1193
1194        orig_len = sb->len;
1195        consumed = format_commit_one(sb, placeholder, context);
1196        if (magic == NO_MAGIC)
1197                return consumed;
1198
1199        if ((orig_len == sb->len) && magic == DEL_LF_BEFORE_EMPTY) {
1200                while (sb->len && sb->buf[sb->len - 1] == '\n')
1201                        strbuf_setlen(sb, sb->len - 1);
1202        } else if (orig_len != sb->len) {
1203                if (magic == ADD_LF_BEFORE_NON_EMPTY)
1204                        strbuf_insert(sb, orig_len, "\n", 1);
1205                else if (magic == ADD_SP_BEFORE_NON_EMPTY)
1206                        strbuf_insert(sb, orig_len, " ", 1);
1207        }
1208        return consumed + 1;
1209}
1210
1211static size_t userformat_want_item(struct strbuf *sb, const char *placeholder,
1212                                   void *context)
1213{
1214        struct userformat_want *w = context;
1215
1216        if (*placeholder == '+' || *placeholder == '-' || *placeholder == ' ')
1217                placeholder++;
1218
1219        switch (*placeholder) {
1220        case 'N':
1221                w->notes = 1;
1222                break;
1223        }
1224        return 0;
1225}
1226
1227void userformat_find_requirements(const char *fmt, struct userformat_want *w)
1228{
1229        struct strbuf dummy = STRBUF_INIT;
1230
1231        if (!fmt) {
1232                if (!user_format)
1233                        return;
1234                fmt = user_format;
1235        }
1236        strbuf_expand(&dummy, fmt, userformat_want_item, w);
1237        strbuf_release(&dummy);
1238}
1239
1240void format_commit_message(const struct commit *commit,
1241                           const char *format, struct strbuf *sb,
1242                           const struct pretty_print_context *pretty_ctx)
1243{
1244        struct format_commit_context context;
1245        static const char utf8[] = "UTF-8";
1246        const char *output_enc = pretty_ctx->output_encoding;
1247
1248        memset(&context, 0, sizeof(context));
1249        context.commit = commit;
1250        context.pretty_ctx = pretty_ctx;
1251        context.wrap_start = sb->len;
1252        context.message = commit->buffer;
1253        if (output_enc) {
1254                char *enc = get_header(commit, "encoding");
1255                if (strcmp(enc ? enc : utf8, output_enc)) {
1256                        context.message = logmsg_reencode(commit, output_enc);
1257                        if (!context.message)
1258                                context.message = commit->buffer;
1259                }
1260                free(enc);
1261        }
1262
1263        strbuf_expand(sb, format, format_commit_item, &context);
1264        rewrap_message_tail(sb, &context, 0, 0, 0);
1265
1266        if (context.message != commit->buffer)
1267                free(context.message);
1268        free(context.signature.gpg_output);
1269        free(context.signature.signer);
1270}
1271
1272static void pp_header(const struct pretty_print_context *pp,
1273                      const char *encoding,
1274                      const struct commit *commit,
1275                      const char **msg_p,
1276                      struct strbuf *sb)
1277{
1278        int parents_shown = 0;
1279
1280        for (;;) {
1281                const char *line = *msg_p;
1282                int linelen = get_one_line(*msg_p);
1283
1284                if (!linelen)
1285                        return;
1286                *msg_p += linelen;
1287
1288                if (linelen == 1)
1289                        /* End of header */
1290                        return;
1291
1292                if (pp->fmt == CMIT_FMT_RAW) {
1293                        strbuf_add(sb, line, linelen);
1294                        continue;
1295                }
1296
1297                if (!memcmp(line, "parent ", 7)) {
1298                        if (linelen != 48)
1299                                die("bad parent line in commit");
1300                        continue;
1301                }
1302
1303                if (!parents_shown) {
1304                        struct commit_list *parent;
1305                        int num;
1306                        for (parent = commit->parents, num = 0;
1307                             parent;
1308                             parent = parent->next, num++)
1309                                ;
1310                        /* with enough slop */
1311                        strbuf_grow(sb, num * 50 + 20);
1312                        add_merge_info(pp, sb, commit);
1313                        parents_shown = 1;
1314                }
1315
1316                /*
1317                 * MEDIUM == DEFAULT shows only author with dates.
1318                 * FULL shows both authors but not dates.
1319                 * FULLER shows both authors and dates.
1320                 */
1321                if (!memcmp(line, "author ", 7)) {
1322                        strbuf_grow(sb, linelen + 80);
1323                        pp_user_info(pp, "Author", sb, line + 7, encoding);
1324                }
1325                if (!memcmp(line, "committer ", 10) &&
1326                    (pp->fmt == CMIT_FMT_FULL || pp->fmt == CMIT_FMT_FULLER)) {
1327                        strbuf_grow(sb, linelen + 80);
1328                        pp_user_info(pp, "Commit", sb, line + 10, encoding);
1329                }
1330        }
1331}
1332
1333void pp_title_line(const struct pretty_print_context *pp,
1334                   const char **msg_p,
1335                   struct strbuf *sb,
1336                   const char *encoding,
1337                   int need_8bit_cte)
1338{
1339        struct strbuf title;
1340
1341        strbuf_init(&title, 80);
1342        *msg_p = format_subject(&title, *msg_p,
1343                                pp->preserve_subject ? "\n" : " ");
1344
1345        strbuf_grow(sb, title.len + 1024);
1346        if (pp->subject) {
1347                strbuf_addstr(sb, pp->subject);
1348                add_rfc2047(sb, title.buf, title.len, encoding, RFC2047_SUBJECT);
1349        } else {
1350                strbuf_addbuf(sb, &title);
1351        }
1352        strbuf_addch(sb, '\n');
1353
1354        if (need_8bit_cte > 0) {
1355                const char *header_fmt =
1356                        "MIME-Version: 1.0\n"
1357                        "Content-Type: text/plain; charset=%s\n"
1358                        "Content-Transfer-Encoding: 8bit\n";
1359                strbuf_addf(sb, header_fmt, encoding);
1360        }
1361        if (pp->after_subject) {
1362                strbuf_addstr(sb, pp->after_subject);
1363        }
1364        if (pp->fmt == CMIT_FMT_EMAIL) {
1365                strbuf_addch(sb, '\n');
1366        }
1367        strbuf_release(&title);
1368}
1369
1370void pp_remainder(const struct pretty_print_context *pp,
1371                  const char **msg_p,
1372                  struct strbuf *sb,
1373                  int indent)
1374{
1375        int first = 1;
1376        for (;;) {
1377                const char *line = *msg_p;
1378                int linelen = get_one_line(line);
1379                *msg_p += linelen;
1380
1381                if (!linelen)
1382                        break;
1383
1384                if (is_empty_line(line, &linelen)) {
1385                        if (first)
1386                                continue;
1387                        if (pp->fmt == CMIT_FMT_SHORT)
1388                                break;
1389                }
1390                first = 0;
1391
1392                strbuf_grow(sb, linelen + indent + 20);
1393                if (indent) {
1394                        memset(sb->buf + sb->len, ' ', indent);
1395                        strbuf_setlen(sb, sb->len + indent);
1396                }
1397                strbuf_add(sb, line, linelen);
1398                strbuf_addch(sb, '\n');
1399        }
1400}
1401
1402char *reencode_commit_message(const struct commit *commit, const char **encoding_p)
1403{
1404        const char *encoding;
1405
1406        encoding = get_log_output_encoding();
1407        if (encoding_p)
1408                *encoding_p = encoding;
1409        return logmsg_reencode(commit, encoding);
1410}
1411
1412void pretty_print_commit(const struct pretty_print_context *pp,
1413                         const struct commit *commit,
1414                         struct strbuf *sb)
1415{
1416        unsigned long beginning_of_body;
1417        int indent = 4;
1418        const char *msg = commit->buffer;
1419        char *reencoded;
1420        const char *encoding;
1421        int need_8bit_cte = pp->need_8bit_cte;
1422
1423        if (pp->fmt == CMIT_FMT_USERFORMAT) {
1424                format_commit_message(commit, user_format, sb, pp);
1425                return;
1426        }
1427
1428        reencoded = reencode_commit_message(commit, &encoding);
1429        if (reencoded) {
1430                msg = reencoded;
1431        }
1432
1433        if (pp->fmt == CMIT_FMT_ONELINE || pp->fmt == CMIT_FMT_EMAIL)
1434                indent = 0;
1435
1436        /*
1437         * We need to check and emit Content-type: to mark it
1438         * as 8-bit if we haven't done so.
1439         */
1440        if (pp->fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
1441                int i, ch, in_body;
1442
1443                for (in_body = i = 0; (ch = msg[i]); i++) {
1444                        if (!in_body) {
1445                                /* author could be non 7-bit ASCII but
1446                                 * the log may be so; skip over the
1447                                 * header part first.
1448                                 */
1449                                if (ch == '\n' && msg[i+1] == '\n')
1450                                        in_body = 1;
1451                        }
1452                        else if (non_ascii(ch)) {
1453                                need_8bit_cte = 1;
1454                                break;
1455                        }
1456                }
1457        }
1458
1459        pp_header(pp, encoding, commit, &msg, sb);
1460        if (pp->fmt != CMIT_FMT_ONELINE && !pp->subject) {
1461                strbuf_addch(sb, '\n');
1462        }
1463
1464        /* Skip excess blank lines at the beginning of body, if any... */
1465        msg = skip_empty_lines(msg);
1466
1467        /* These formats treat the title line specially. */
1468        if (pp->fmt == CMIT_FMT_ONELINE || pp->fmt == CMIT_FMT_EMAIL)
1469                pp_title_line(pp, &msg, sb, encoding, need_8bit_cte);
1470
1471        beginning_of_body = sb->len;
1472        if (pp->fmt != CMIT_FMT_ONELINE)
1473                pp_remainder(pp, &msg, sb, indent);
1474        strbuf_rtrim(sb);
1475
1476        /* Make sure there is an EOLN for the non-oneline case */
1477        if (pp->fmt != CMIT_FMT_ONELINE)
1478                strbuf_addch(sb, '\n');
1479
1480        /*
1481         * The caller may append additional body text in e-mail
1482         * format.  Make sure we did not strip the blank line
1483         * between the header and the body.
1484         */
1485        if (pp->fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
1486                strbuf_addch(sb, '\n');
1487
1488        if (pp->show_notes)
1489                format_display_notes(commit->object.sha1, sb, encoding,
1490                                     NOTES_SHOW_HEADER | NOTES_INDENT);
1491
1492        free(reencoded);
1493}
1494
1495void pp_commit_easy(enum cmit_fmt fmt, const struct commit *commit,
1496                    struct strbuf *sb)
1497{
1498        struct pretty_print_context pp = {0};
1499        pp.fmt = fmt;
1500        pretty_print_commit(&pp, commit, sb);
1501}