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