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