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