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