log-tree.con commit get_patch_filename(): drop "just-numbers" hack (38ec23a)
   1#include "cache.h"
   2#include "diff.h"
   3#include "commit.h"
   4#include "tag.h"
   5#include "graph.h"
   6#include "log-tree.h"
   7#include "reflog-walk.h"
   8#include "refs.h"
   9#include "string-list.h"
  10#include "color.h"
  11#include "gpg-interface.h"
  12
  13struct decoration name_decoration = { "object names" };
  14
  15enum decoration_type {
  16        DECORATION_NONE = 0,
  17        DECORATION_REF_LOCAL,
  18        DECORATION_REF_REMOTE,
  19        DECORATION_REF_TAG,
  20        DECORATION_REF_STASH,
  21        DECORATION_REF_HEAD,
  22        DECORATION_GRAFTED,
  23};
  24
  25static char decoration_colors[][COLOR_MAXLEN] = {
  26        GIT_COLOR_RESET,
  27        GIT_COLOR_BOLD_GREEN,   /* REF_LOCAL */
  28        GIT_COLOR_BOLD_RED,     /* REF_REMOTE */
  29        GIT_COLOR_BOLD_YELLOW,  /* REF_TAG */
  30        GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */
  31        GIT_COLOR_BOLD_CYAN,    /* REF_HEAD */
  32        GIT_COLOR_BOLD_BLUE,    /* GRAFTED */
  33};
  34
  35static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
  36{
  37        if (want_color(decorate_use_color))
  38                return decoration_colors[ix];
  39        return "";
  40}
  41
  42static int parse_decorate_color_slot(const char *slot)
  43{
  44        /*
  45         * We're comparing with 'ignore-case' on
  46         * (because config.c sets them all tolower),
  47         * but let's match the letters in the literal
  48         * string values here with how they are
  49         * documented in Documentation/config.txt, for
  50         * consistency.
  51         *
  52         * We love being consistent, don't we?
  53         */
  54        if (!strcasecmp(slot, "branch"))
  55                return DECORATION_REF_LOCAL;
  56        if (!strcasecmp(slot, "remoteBranch"))
  57                return DECORATION_REF_REMOTE;
  58        if (!strcasecmp(slot, "tag"))
  59                return DECORATION_REF_TAG;
  60        if (!strcasecmp(slot, "stash"))
  61                return DECORATION_REF_STASH;
  62        if (!strcasecmp(slot, "HEAD"))
  63                return DECORATION_REF_HEAD;
  64        return -1;
  65}
  66
  67int parse_decorate_color_config(const char *var, const int ofs, const char *value)
  68{
  69        int slot = parse_decorate_color_slot(var + ofs);
  70        if (slot < 0)
  71                return 0;
  72        if (!value)
  73                return config_error_nonbool(var);
  74        color_parse(value, var, decoration_colors[slot]);
  75        return 0;
  76}
  77
  78/*
  79 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
  80 * for showing the commit sha1, use the same check for --decorate
  81 */
  82#define decorate_get_color_opt(o, ix) \
  83        decorate_get_color((o)->use_color, ix)
  84
  85static void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
  86{
  87        int nlen = strlen(name);
  88        struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + nlen);
  89        memcpy(res->name, name, nlen + 1);
  90        res->type = type;
  91        res->next = add_decoration(&name_decoration, obj, res);
  92}
  93
  94static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
  95{
  96        struct object *obj;
  97        enum decoration_type type = DECORATION_NONE;
  98
  99        if (!prefixcmp(refname, "refs/replace/")) {
 100                unsigned char original_sha1[20];
 101                if (!read_replace_refs)
 102                        return 0;
 103                if (get_sha1_hex(refname + 13, original_sha1)) {
 104                        warning("invalid replace ref %s", refname);
 105                        return 0;
 106                }
 107                obj = parse_object(original_sha1);
 108                if (obj)
 109                        add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
 110                return 0;
 111        }
 112
 113        obj = parse_object(sha1);
 114        if (!obj)
 115                return 0;
 116
 117        if (!prefixcmp(refname, "refs/heads/"))
 118                type = DECORATION_REF_LOCAL;
 119        else if (!prefixcmp(refname, "refs/remotes/"))
 120                type = DECORATION_REF_REMOTE;
 121        else if (!prefixcmp(refname, "refs/tags/"))
 122                type = DECORATION_REF_TAG;
 123        else if (!strcmp(refname, "refs/stash"))
 124                type = DECORATION_REF_STASH;
 125        else if (!strcmp(refname, "HEAD"))
 126                type = DECORATION_REF_HEAD;
 127
 128        if (!cb_data || *(int *)cb_data == DECORATE_SHORT_REFS)
 129                refname = prettify_refname(refname);
 130        add_name_decoration(type, refname, obj);
 131        while (obj->type == OBJ_TAG) {
 132                obj = ((struct tag *)obj)->tagged;
 133                if (!obj)
 134                        break;
 135                add_name_decoration(DECORATION_REF_TAG, refname, obj);
 136        }
 137        return 0;
 138}
 139
 140static int add_graft_decoration(const struct commit_graft *graft, void *cb_data)
 141{
 142        struct commit *commit = lookup_commit(graft->sha1);
 143        if (!commit)
 144                return 0;
 145        add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
 146        return 0;
 147}
 148
 149void load_ref_decorations(int flags)
 150{
 151        static int loaded;
 152        if (!loaded) {
 153                loaded = 1;
 154                for_each_ref(add_ref_decoration, &flags);
 155                head_ref(add_ref_decoration, &flags);
 156                for_each_commit_graft(add_graft_decoration, NULL);
 157        }
 158}
 159
 160static void show_parents(struct commit *commit, int abbrev)
 161{
 162        struct commit_list *p;
 163        for (p = commit->parents; p ; p = p->next) {
 164                struct commit *parent = p->item;
 165                printf(" %s", find_unique_abbrev(parent->object.sha1, abbrev));
 166        }
 167}
 168
 169static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
 170{
 171        struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
 172        for ( ; p; p = p->next) {
 173                printf(" %s", find_unique_abbrev(p->item->object.sha1, abbrev));
 174        }
 175}
 176
 177void show_decorations(struct rev_info *opt, struct commit *commit)
 178{
 179        const char *prefix;
 180        struct name_decoration *decoration;
 181        const char *color_commit =
 182                diff_get_color_opt(&opt->diffopt, DIFF_COMMIT);
 183        const char *color_reset =
 184                decorate_get_color_opt(&opt->diffopt, DECORATION_NONE);
 185
 186        if (opt->show_source && commit->util)
 187                printf("\t%s", (char *) commit->util);
 188        if (!opt->show_decorations)
 189                return;
 190        decoration = lookup_decoration(&name_decoration, &commit->object);
 191        if (!decoration)
 192                return;
 193        prefix = " (";
 194        while (decoration) {
 195                printf("%s", prefix);
 196                fputs(decorate_get_color_opt(&opt->diffopt, decoration->type),
 197                      stdout);
 198                if (decoration->type == DECORATION_REF_TAG)
 199                        fputs("tag: ", stdout);
 200                printf("%s", decoration->name);
 201                fputs(color_reset, stdout);
 202                fputs(color_commit, stdout);
 203                prefix = ", ";
 204                decoration = decoration->next;
 205        }
 206        putchar(')');
 207}
 208
 209/*
 210 * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
 211 * Signed-off-by: and Acked-by: lines.
 212 */
 213static int detect_any_signoff(char *letter, int size)
 214{
 215        char *cp;
 216        int seen_colon = 0;
 217        int seen_at = 0;
 218        int seen_name = 0;
 219        int seen_head = 0;
 220
 221        cp = letter + size;
 222        while (letter <= --cp && *cp == '\n')
 223                continue;
 224
 225        while (letter <= cp) {
 226                char ch = *cp--;
 227                if (ch == '\n')
 228                        break;
 229
 230                if (!seen_at) {
 231                        if (ch == '@')
 232                                seen_at = 1;
 233                        continue;
 234                }
 235                if (!seen_colon) {
 236                        if (ch == '@')
 237                                return 0;
 238                        else if (ch == ':')
 239                                seen_colon = 1;
 240                        else
 241                                seen_name = 1;
 242                        continue;
 243                }
 244                if (('A' <= ch && ch <= 'Z') ||
 245                    ('a' <= ch && ch <= 'z') ||
 246                    ch == '-') {
 247                        seen_head = 1;
 248                        continue;
 249                }
 250                /* no empty last line doesn't match */
 251                return 0;
 252        }
 253        return seen_head && seen_name;
 254}
 255
 256static void append_signoff(struct strbuf *sb, const char *signoff)
 257{
 258        static const char signed_off_by[] = "Signed-off-by: ";
 259        size_t signoff_len = strlen(signoff);
 260        int has_signoff = 0;
 261        char *cp;
 262
 263        cp = sb->buf;
 264
 265        /* First see if we already have the sign-off by the signer */
 266        while ((cp = strstr(cp, signed_off_by))) {
 267
 268                has_signoff = 1;
 269
 270                cp += strlen(signed_off_by);
 271                if (cp + signoff_len >= sb->buf + sb->len)
 272                        break;
 273                if (strncmp(cp, signoff, signoff_len))
 274                        continue;
 275                if (!isspace(cp[signoff_len]))
 276                        continue;
 277                /* we already have him */
 278                return;
 279        }
 280
 281        if (!has_signoff)
 282                has_signoff = detect_any_signoff(sb->buf, sb->len);
 283
 284        if (!has_signoff)
 285                strbuf_addch(sb, '\n');
 286
 287        strbuf_addstr(sb, signed_off_by);
 288        strbuf_add(sb, signoff, signoff_len);
 289        strbuf_addch(sb, '\n');
 290}
 291
 292static unsigned int digits_in_number(unsigned int number)
 293{
 294        unsigned int i = 10, result = 1;
 295        while (i <= number) {
 296                i *= 10;
 297                result++;
 298        }
 299        return result;
 300}
 301
 302void get_patch_filename(struct strbuf *buf,
 303                        struct commit *commit, const char *subject,
 304                        struct rev_info *info)
 305{
 306        const char *suffix = info->patch_suffix;
 307        int nr = info->nr;
 308        int suffix_len = strlen(suffix) + 1;
 309        int start_len = buf->len;
 310        int max_len = start_len + FORMAT_PATCH_NAME_MAX - suffix_len;
 311
 312        strbuf_addf(buf, "%04d-", nr);
 313        if (subject)
 314                strbuf_addstr(buf, subject);
 315        else if (commit) {
 316                struct pretty_print_context ctx = {0};
 317                format_commit_message(commit, "%f", buf, &ctx);
 318        }
 319        if (max_len < buf->len)
 320                strbuf_setlen(buf, max_len);
 321        strbuf_addstr(buf, suffix);
 322}
 323
 324void log_write_email_headers(struct rev_info *opt, struct commit *commit,
 325                             const char **subject_p,
 326                             const char **extra_headers_p,
 327                             int *need_8bit_cte_p)
 328{
 329        const char *subject = NULL;
 330        const char *extra_headers = opt->extra_headers;
 331        const char *name = sha1_to_hex(commit->object.sha1);
 332
 333        *need_8bit_cte_p = 0; /* unknown */
 334        if (opt->total > 0) {
 335                static char buffer[64];
 336                snprintf(buffer, sizeof(buffer),
 337                         "Subject: [%s%s%0*d/%d] ",
 338                         opt->subject_prefix,
 339                         *opt->subject_prefix ? " " : "",
 340                         digits_in_number(opt->total),
 341                         opt->nr, opt->total);
 342                subject = buffer;
 343        } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
 344                static char buffer[256];
 345                snprintf(buffer, sizeof(buffer),
 346                         "Subject: [%s] ",
 347                         opt->subject_prefix);
 348                subject = buffer;
 349        } else {
 350                subject = "Subject: ";
 351        }
 352
 353        printf("From %s Mon Sep 17 00:00:00 2001\n", name);
 354        graph_show_oneline(opt->graph);
 355        if (opt->message_id) {
 356                printf("Message-Id: <%s>\n", opt->message_id);
 357                graph_show_oneline(opt->graph);
 358        }
 359        if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
 360                int i, n;
 361                n = opt->ref_message_ids->nr;
 362                printf("In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
 363                for (i = 0; i < n; i++)
 364                        printf("%s<%s>\n", (i > 0 ? "\t" : "References: "),
 365                               opt->ref_message_ids->items[i].string);
 366                graph_show_oneline(opt->graph);
 367        }
 368        if (opt->mime_boundary) {
 369                static char subject_buffer[1024];
 370                static char buffer[1024];
 371                struct strbuf filename =  STRBUF_INIT;
 372                *need_8bit_cte_p = -1; /* NEVER */
 373                snprintf(subject_buffer, sizeof(subject_buffer) - 1,
 374                         "%s"
 375                         "MIME-Version: 1.0\n"
 376                         "Content-Type: multipart/mixed;"
 377                         " boundary=\"%s%s\"\n"
 378                         "\n"
 379                         "This is a multi-part message in MIME "
 380                         "format.\n"
 381                         "--%s%s\n"
 382                         "Content-Type: text/plain; "
 383                         "charset=UTF-8; format=fixed\n"
 384                         "Content-Transfer-Encoding: 8bit\n\n",
 385                         extra_headers ? extra_headers : "",
 386                         mime_boundary_leader, opt->mime_boundary,
 387                         mime_boundary_leader, opt->mime_boundary);
 388                extra_headers = subject_buffer;
 389
 390                if (opt->numbered_files)
 391                        strbuf_addf(&filename, "%d", opt->nr);
 392                else
 393                        get_patch_filename(&filename, commit, NULL,
 394                                           opt);
 395                snprintf(buffer, sizeof(buffer) - 1,
 396                         "\n--%s%s\n"
 397                         "Content-Type: text/x-patch;"
 398                         " name=\"%s\"\n"
 399                         "Content-Transfer-Encoding: 8bit\n"
 400                         "Content-Disposition: %s;"
 401                         " filename=\"%s\"\n\n",
 402                         mime_boundary_leader, opt->mime_boundary,
 403                         filename.buf,
 404                         opt->no_inline ? "attachment" : "inline",
 405                         filename.buf);
 406                opt->diffopt.stat_sep = buffer;
 407                strbuf_release(&filename);
 408        }
 409        *subject_p = subject;
 410        *extra_headers_p = extra_headers;
 411}
 412
 413static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
 414{
 415        const char *color, *reset, *eol;
 416
 417        color = diff_get_color_opt(&opt->diffopt,
 418                                   status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
 419        reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
 420        while (*bol) {
 421                eol = strchrnul(bol, '\n');
 422                printf("%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
 423                       *eol ? "\n" : "");
 424                bol = (*eol) ? (eol + 1) : eol;
 425        }
 426}
 427
 428static void show_signature(struct rev_info *opt, struct commit *commit)
 429{
 430        struct strbuf payload = STRBUF_INIT;
 431        struct strbuf signature = STRBUF_INIT;
 432        struct strbuf gpg_output = STRBUF_INIT;
 433        int status;
 434
 435        if (parse_signed_commit(commit->object.sha1, &payload, &signature) <= 0)
 436                goto out;
 437
 438        status = verify_signed_buffer(payload.buf, payload.len,
 439                                      signature.buf, signature.len,
 440                                      &gpg_output);
 441        if (status && !gpg_output.len)
 442                strbuf_addstr(&gpg_output, "No signature\n");
 443
 444        show_sig_lines(opt, status, gpg_output.buf);
 445
 446 out:
 447        strbuf_release(&gpg_output);
 448        strbuf_release(&payload);
 449        strbuf_release(&signature);
 450}
 451
 452static int which_parent(const unsigned char *sha1, const struct commit *commit)
 453{
 454        int nth;
 455        const struct commit_list *parent;
 456
 457        for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
 458                if (!hashcmp(parent->item->object.sha1, sha1))
 459                        return nth;
 460                nth++;
 461        }
 462        return -1;
 463}
 464
 465static int is_common_merge(const struct commit *commit)
 466{
 467        return (commit->parents
 468                && commit->parents->next
 469                && !commit->parents->next->next);
 470}
 471
 472static void show_one_mergetag(struct rev_info *opt,
 473                              struct commit_extra_header *extra,
 474                              struct commit *commit)
 475{
 476        unsigned char sha1[20];
 477        struct tag *tag;
 478        struct strbuf verify_message;
 479        int status, nth;
 480        size_t payload_size, gpg_message_offset;
 481
 482        hash_sha1_file(extra->value, extra->len, typename(OBJ_TAG), sha1);
 483        tag = lookup_tag(sha1);
 484        if (!tag)
 485                return; /* error message already given */
 486
 487        strbuf_init(&verify_message, 256);
 488        if (parse_tag_buffer(tag, extra->value, extra->len))
 489                strbuf_addstr(&verify_message, "malformed mergetag\n");
 490        else if (is_common_merge(commit) &&
 491                 !hashcmp(tag->tagged->sha1,
 492                          commit->parents->next->item->object.sha1))
 493                strbuf_addf(&verify_message,
 494                            "merged tag '%s'\n", tag->tag);
 495        else if ((nth = which_parent(tag->tagged->sha1, commit)) < 0)
 496                strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
 497                                    tag->tag, tag->tagged->sha1);
 498        else
 499                strbuf_addf(&verify_message,
 500                            "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
 501        gpg_message_offset = verify_message.len;
 502
 503        payload_size = parse_signature(extra->value, extra->len);
 504        if ((extra->len <= payload_size) ||
 505            (verify_signed_buffer(extra->value, payload_size,
 506                                  extra->value + payload_size,
 507                                  extra->len - payload_size,
 508                                  &verify_message) &&
 509             verify_message.len <= gpg_message_offset)) {
 510                strbuf_addstr(&verify_message, "No signature\n");
 511                status = -1;
 512        }
 513        else if (strstr(verify_message.buf + gpg_message_offset,
 514                        ": Good signature from "))
 515                status = 0;
 516        else
 517                status = -1;
 518
 519        show_sig_lines(opt, status, verify_message.buf);
 520        strbuf_release(&verify_message);
 521}
 522
 523static void show_mergetag(struct rev_info *opt, struct commit *commit)
 524{
 525        struct commit_extra_header *extra, *to_free;
 526
 527        to_free = read_commit_extra_headers(commit, NULL);
 528        for (extra = to_free; extra; extra = extra->next) {
 529                if (strcmp(extra->key, "mergetag"))
 530                        continue; /* not a merge tag */
 531                show_one_mergetag(opt, extra, commit);
 532        }
 533        free_commit_extra_headers(to_free);
 534}
 535
 536void show_log(struct rev_info *opt)
 537{
 538        struct strbuf msgbuf = STRBUF_INIT;
 539        struct log_info *log = opt->loginfo;
 540        struct commit *commit = log->commit, *parent = log->parent;
 541        int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
 542        const char *extra_headers = opt->extra_headers;
 543        struct pretty_print_context ctx = {0};
 544
 545        opt->loginfo = NULL;
 546        ctx.show_notes = opt->show_notes;
 547        if (!opt->verbose_header) {
 548                graph_show_commit(opt->graph);
 549
 550                if (!opt->graph)
 551                        put_revision_mark(opt, commit);
 552                fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
 553                if (opt->print_parents)
 554                        show_parents(commit, abbrev_commit);
 555                if (opt->children.name)
 556                        show_children(opt, commit, abbrev_commit);
 557                show_decorations(opt, commit);
 558                if (opt->graph && !graph_is_commit_finished(opt->graph)) {
 559                        putchar('\n');
 560                        graph_show_remainder(opt->graph);
 561                }
 562                putchar(opt->diffopt.line_termination);
 563                return;
 564        }
 565
 566        /*
 567         * If use_terminator is set, we already handled any record termination
 568         * at the end of the last record.
 569         * Otherwise, add a diffopt.line_termination character before all
 570         * entries but the first.  (IOW, as a separator between entries)
 571         */
 572        if (opt->shown_one && !opt->use_terminator) {
 573                /*
 574                 * If entries are separated by a newline, the output
 575                 * should look human-readable.  If the last entry ended
 576                 * with a newline, print the graph output before this
 577                 * newline.  Otherwise it will end up as a completely blank
 578                 * line and will look like a gap in the graph.
 579                 *
 580                 * If the entry separator is not a newline, the output is
 581                 * primarily intended for programmatic consumption, and we
 582                 * never want the extra graph output before the entry
 583                 * separator.
 584                 */
 585                if (opt->diffopt.line_termination == '\n' &&
 586                    !opt->missing_newline)
 587                        graph_show_padding(opt->graph);
 588                putchar(opt->diffopt.line_termination);
 589        }
 590        opt->shown_one = 1;
 591
 592        /*
 593         * If the history graph was requested,
 594         * print the graph, up to this commit's line
 595         */
 596        graph_show_commit(opt->graph);
 597
 598        /*
 599         * Print header line of header..
 600         */
 601
 602        if (opt->commit_format == CMIT_FMT_EMAIL) {
 603                log_write_email_headers(opt, commit, &ctx.subject, &extra_headers,
 604                                        &ctx.need_8bit_cte);
 605        } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
 606                fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
 607                if (opt->commit_format != CMIT_FMT_ONELINE)
 608                        fputs("commit ", stdout);
 609
 610                if (!opt->graph)
 611                        put_revision_mark(opt, commit);
 612                fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit),
 613                      stdout);
 614                if (opt->print_parents)
 615                        show_parents(commit, abbrev_commit);
 616                if (opt->children.name)
 617                        show_children(opt, commit, abbrev_commit);
 618                if (parent)
 619                        printf(" (from %s)",
 620                               find_unique_abbrev(parent->object.sha1,
 621                                                  abbrev_commit));
 622                show_decorations(opt, commit);
 623                printf("%s", diff_get_color_opt(&opt->diffopt, DIFF_RESET));
 624                if (opt->commit_format == CMIT_FMT_ONELINE) {
 625                        putchar(' ');
 626                } else {
 627                        putchar('\n');
 628                        graph_show_oneline(opt->graph);
 629                }
 630                if (opt->reflog_info) {
 631                        /*
 632                         * setup_revisions() ensures that opt->reflog_info
 633                         * and opt->graph cannot both be set,
 634                         * so we don't need to worry about printing the
 635                         * graph info here.
 636                         */
 637                        show_reflog_message(opt->reflog_info,
 638                                            opt->commit_format == CMIT_FMT_ONELINE,
 639                                            opt->date_mode,
 640                                            opt->date_mode_explicit);
 641                        if (opt->commit_format == CMIT_FMT_ONELINE)
 642                                return;
 643                }
 644        }
 645
 646        if (opt->show_signature) {
 647                show_signature(opt, commit);
 648                show_mergetag(opt, commit);
 649        }
 650
 651        if (!commit->buffer)
 652                return;
 653
 654        /*
 655         * And then the pretty-printed message itself
 656         */
 657        if (ctx.need_8bit_cte >= 0)
 658                ctx.need_8bit_cte = has_non_ascii(opt->add_signoff);
 659        ctx.date_mode = opt->date_mode;
 660        ctx.date_mode_explicit = opt->date_mode_explicit;
 661        ctx.abbrev = opt->diffopt.abbrev;
 662        ctx.after_subject = extra_headers;
 663        ctx.preserve_subject = opt->preserve_subject;
 664        ctx.reflog_info = opt->reflog_info;
 665        ctx.fmt = opt->commit_format;
 666        pretty_print_commit(&ctx, commit, &msgbuf);
 667
 668        if (opt->add_signoff)
 669                append_signoff(&msgbuf, opt->add_signoff);
 670        if (opt->show_log_size) {
 671                printf("log size %i\n", (int)msgbuf.len);
 672                graph_show_oneline(opt->graph);
 673        }
 674
 675        /*
 676         * Set opt->missing_newline if msgbuf doesn't
 677         * end in a newline (including if it is empty)
 678         */
 679        if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
 680                opt->missing_newline = 1;
 681        else
 682                opt->missing_newline = 0;
 683
 684        if (opt->graph)
 685                graph_show_commit_msg(opt->graph, &msgbuf);
 686        else
 687                fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
 688        if (opt->use_terminator) {
 689                if (!opt->missing_newline)
 690                        graph_show_padding(opt->graph);
 691                putchar(opt->diffopt.line_termination);
 692        }
 693
 694        strbuf_release(&msgbuf);
 695}
 696
 697int log_tree_diff_flush(struct rev_info *opt)
 698{
 699        diffcore_std(&opt->diffopt);
 700
 701        if (diff_queue_is_empty()) {
 702                int saved_fmt = opt->diffopt.output_format;
 703                opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
 704                diff_flush(&opt->diffopt);
 705                opt->diffopt.output_format = saved_fmt;
 706                return 0;
 707        }
 708
 709        if (opt->loginfo && !opt->no_commit_id) {
 710                /* When showing a verbose header (i.e. log message),
 711                 * and not in --pretty=oneline format, we would want
 712                 * an extra newline between the end of log and the
 713                 * output for readability.
 714                 */
 715                show_log(opt);
 716                if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
 717                    opt->verbose_header &&
 718                    opt->commit_format != CMIT_FMT_ONELINE) {
 719                        int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
 720                        if (opt->diffopt.output_prefix) {
 721                                struct strbuf *msg = NULL;
 722                                msg = opt->diffopt.output_prefix(&opt->diffopt,
 723                                        opt->diffopt.output_prefix_data);
 724                                fwrite(msg->buf, msg->len, 1, stdout);
 725                        }
 726                        if ((pch & opt->diffopt.output_format) == pch) {
 727                                printf("---");
 728                        }
 729                        putchar('\n');
 730                }
 731        }
 732        diff_flush(&opt->diffopt);
 733        return 1;
 734}
 735
 736static int do_diff_combined(struct rev_info *opt, struct commit *commit)
 737{
 738        diff_tree_combined_merge(commit, opt->dense_combined_merges, opt);
 739        return !opt->loginfo;
 740}
 741
 742/*
 743 * Show the diff of a commit.
 744 *
 745 * Return true if we printed any log info messages
 746 */
 747static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
 748{
 749        int showed_log;
 750        struct commit_list *parents;
 751        unsigned const char *sha1 = commit->object.sha1;
 752
 753        if (!opt->diff && !DIFF_OPT_TST(&opt->diffopt, EXIT_WITH_STATUS))
 754                return 0;
 755
 756        /* Root commit? */
 757        parents = commit->parents;
 758        if (!parents) {
 759                if (opt->show_root_diff) {
 760                        diff_root_tree_sha1(sha1, "", &opt->diffopt);
 761                        log_tree_diff_flush(opt);
 762                }
 763                return !opt->loginfo;
 764        }
 765
 766        /* More than one parent? */
 767        if (parents && parents->next) {
 768                if (opt->ignore_merges)
 769                        return 0;
 770                else if (opt->combine_merges)
 771                        return do_diff_combined(opt, commit);
 772                else if (opt->first_parent_only) {
 773                        /*
 774                         * Generate merge log entry only for the first
 775                         * parent, showing summary diff of the others
 776                         * we merged _in_.
 777                         */
 778                        diff_tree_sha1(parents->item->object.sha1, sha1, "", &opt->diffopt);
 779                        log_tree_diff_flush(opt);
 780                        return !opt->loginfo;
 781                }
 782
 783                /* If we show individual diffs, show the parent info */
 784                log->parent = parents->item;
 785        }
 786
 787        showed_log = 0;
 788        for (;;) {
 789                struct commit *parent = parents->item;
 790
 791                diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
 792                log_tree_diff_flush(opt);
 793
 794                showed_log |= !opt->loginfo;
 795
 796                /* Set up the log info for the next parent, if any.. */
 797                parents = parents->next;
 798                if (!parents)
 799                        break;
 800                log->parent = parents->item;
 801                opt->loginfo = log;
 802        }
 803        return showed_log;
 804}
 805
 806int log_tree_commit(struct rev_info *opt, struct commit *commit)
 807{
 808        struct log_info log;
 809        int shown;
 810
 811        log.commit = commit;
 812        log.parent = NULL;
 813        opt->loginfo = &log;
 814
 815        shown = log_tree_diff(opt, commit, &log);
 816        if (!shown && opt->loginfo && opt->always_show_header) {
 817                log.parent = NULL;
 818                show_log(opt);
 819                shown = 1;
 820        }
 821        opt->loginfo = NULL;
 822        maybe_flush_or_die(stdout, "stdout");
 823        return shown;
 824}