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