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