log-tree.con commit Merge branch 'cl/t9001-cleanup' into maint (b1bdf46)
   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.hash, 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.hash, 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->show_source && commit->util)
 299                fprintf(opt->diffopt.file, "\t%s", (char *) commit->util);
 300        if (!opt->show_decorations)
 301                return;
 302        format_decorations(&sb, commit, opt->diffopt.use_color);
 303        fputs(sb.buf, opt->diffopt.file);
 304        strbuf_release(&sb);
 305}
 306
 307static unsigned int digits_in_number(unsigned int number)
 308{
 309        unsigned int i = 10, result = 1;
 310        while (i <= number) {
 311                i *= 10;
 312                result++;
 313        }
 314        return result;
 315}
 316
 317void fmt_output_subject(struct strbuf *filename,
 318                        const char *subject,
 319                        struct rev_info *info)
 320{
 321        const char *suffix = info->patch_suffix;
 322        int nr = info->nr;
 323        int start_len = filename->len;
 324        int max_len = start_len + FORMAT_PATCH_NAME_MAX - (strlen(suffix) + 1);
 325
 326        if (0 < info->reroll_count)
 327                strbuf_addf(filename, "v%d-", info->reroll_count);
 328        strbuf_addf(filename, "%04d-%s", nr, subject);
 329
 330        if (max_len < filename->len)
 331                strbuf_setlen(filename, max_len);
 332        strbuf_addstr(filename, suffix);
 333}
 334
 335void fmt_output_commit(struct strbuf *filename,
 336                       struct commit *commit,
 337                       struct rev_info *info)
 338{
 339        struct pretty_print_context ctx = {0};
 340        struct strbuf subject = STRBUF_INIT;
 341
 342        format_commit_message(commit, "%f", &subject, &ctx);
 343        fmt_output_subject(filename, subject.buf, info);
 344        strbuf_release(&subject);
 345}
 346
 347void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
 348{
 349        if (opt->total > 0) {
 350                strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ",
 351                            opt->subject_prefix,
 352                            *opt->subject_prefix ? " " : "",
 353                            digits_in_number(opt->total),
 354                            opt->nr, opt->total);
 355        } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
 356                strbuf_addf(sb, "Subject: [%s] ",
 357                            opt->subject_prefix);
 358        } else {
 359                strbuf_addstr(sb, "Subject: ");
 360        }
 361}
 362
 363void log_write_email_headers(struct rev_info *opt, struct commit *commit,
 364                             const char **extra_headers_p,
 365                             int *need_8bit_cte_p)
 366{
 367        const char *extra_headers = opt->extra_headers;
 368        const char *name = oid_to_hex(opt->zero_commit ?
 369                                      &null_oid : &commit->object.oid);
 370
 371        *need_8bit_cte_p = 0; /* unknown */
 372
 373        fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
 374        graph_show_oneline(opt->graph);
 375        if (opt->message_id) {
 376                fprintf(opt->diffopt.file, "Message-Id: <%s>\n", opt->message_id);
 377                graph_show_oneline(opt->graph);
 378        }
 379        if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
 380                int i, n;
 381                n = opt->ref_message_ids->nr;
 382                fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
 383                for (i = 0; i < n; i++)
 384                        fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
 385                               opt->ref_message_ids->items[i].string);
 386                graph_show_oneline(opt->graph);
 387        }
 388        if (opt->mime_boundary) {
 389                static char subject_buffer[1024];
 390                static char buffer[1024];
 391                struct strbuf filename =  STRBUF_INIT;
 392                *need_8bit_cte_p = -1; /* NEVER */
 393                snprintf(subject_buffer, sizeof(subject_buffer) - 1,
 394                         "%s"
 395                         "MIME-Version: 1.0\n"
 396                         "Content-Type: multipart/mixed;"
 397                         " boundary=\"%s%s\"\n"
 398                         "\n"
 399                         "This is a multi-part message in MIME "
 400                         "format.\n"
 401                         "--%s%s\n"
 402                         "Content-Type: text/plain; "
 403                         "charset=UTF-8; format=fixed\n"
 404                         "Content-Transfer-Encoding: 8bit\n\n",
 405                         extra_headers ? extra_headers : "",
 406                         mime_boundary_leader, opt->mime_boundary,
 407                         mime_boundary_leader, opt->mime_boundary);
 408                extra_headers = subject_buffer;
 409
 410                if (opt->numbered_files)
 411                        strbuf_addf(&filename, "%d", opt->nr);
 412                else
 413                        fmt_output_commit(&filename, commit, opt);
 414                snprintf(buffer, sizeof(buffer) - 1,
 415                         "\n--%s%s\n"
 416                         "Content-Type: text/x-patch;"
 417                         " name=\"%s\"\n"
 418                         "Content-Transfer-Encoding: 8bit\n"
 419                         "Content-Disposition: %s;"
 420                         " filename=\"%s\"\n\n",
 421                         mime_boundary_leader, opt->mime_boundary,
 422                         filename.buf,
 423                         opt->no_inline ? "attachment" : "inline",
 424                         filename.buf);
 425                opt->diffopt.stat_sep = buffer;
 426                strbuf_release(&filename);
 427        }
 428        *extra_headers_p = extra_headers;
 429}
 430
 431static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
 432{
 433        const char *color, *reset, *eol;
 434
 435        color = diff_get_color_opt(&opt->diffopt,
 436                                   status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
 437        reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
 438        while (*bol) {
 439                eol = strchrnul(bol, '\n');
 440                fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
 441                       *eol ? "\n" : "");
 442                graph_show_oneline(opt->graph);
 443                bol = (*eol) ? (eol + 1) : eol;
 444        }
 445}
 446
 447static void show_signature(struct rev_info *opt, struct commit *commit)
 448{
 449        struct strbuf payload = STRBUF_INIT;
 450        struct strbuf signature = STRBUF_INIT;
 451        struct strbuf gpg_output = STRBUF_INIT;
 452        int status;
 453
 454        if (parse_signed_commit(commit, &payload, &signature) <= 0)
 455                goto out;
 456
 457        status = verify_signed_buffer(payload.buf, payload.len,
 458                                      signature.buf, signature.len,
 459                                      &gpg_output, NULL);
 460        if (status && !gpg_output.len)
 461                strbuf_addstr(&gpg_output, "No signature\n");
 462
 463        show_sig_lines(opt, status, gpg_output.buf);
 464
 465 out:
 466        strbuf_release(&gpg_output);
 467        strbuf_release(&payload);
 468        strbuf_release(&signature);
 469}
 470
 471static int which_parent(const struct object_id *oid, const struct commit *commit)
 472{
 473        int nth;
 474        const struct commit_list *parent;
 475
 476        for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
 477                if (!oidcmp(&parent->item->object.oid, oid))
 478                        return nth;
 479                nth++;
 480        }
 481        return -1;
 482}
 483
 484static int is_common_merge(const struct commit *commit)
 485{
 486        return (commit->parents
 487                && commit->parents->next
 488                && !commit->parents->next->next);
 489}
 490
 491static void show_one_mergetag(struct commit *commit,
 492                              struct commit_extra_header *extra,
 493                              void *data)
 494{
 495        struct rev_info *opt = (struct rev_info *)data;
 496        struct object_id oid;
 497        struct tag *tag;
 498        struct strbuf verify_message;
 499        int status, nth;
 500        size_t payload_size, gpg_message_offset;
 501
 502        hash_sha1_file(extra->value, extra->len, typename(OBJ_TAG), oid.hash);
 503        tag = lookup_tag(&oid);
 504        if (!tag)
 505                return; /* error message already given */
 506
 507        strbuf_init(&verify_message, 256);
 508        if (parse_tag_buffer(tag, extra->value, extra->len))
 509                strbuf_addstr(&verify_message, "malformed mergetag\n");
 510        else if (is_common_merge(commit) &&
 511                 !oidcmp(&tag->tagged->oid,
 512                          &commit->parents->next->item->object.oid))
 513                strbuf_addf(&verify_message,
 514                            "merged tag '%s'\n", tag->tag);
 515        else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0)
 516                strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
 517                                    tag->tag, tag->tagged->oid.hash);
 518        else
 519                strbuf_addf(&verify_message,
 520                            "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
 521        gpg_message_offset = verify_message.len;
 522
 523        payload_size = parse_signature(extra->value, extra->len);
 524        status = -1;
 525        if (extra->len > payload_size) {
 526                /* could have a good signature */
 527                if (!verify_signed_buffer(extra->value, payload_size,
 528                                          extra->value + payload_size,
 529                                          extra->len - payload_size,
 530                                          &verify_message, NULL))
 531                        status = 0; /* good */
 532                else if (verify_message.len <= gpg_message_offset)
 533                        strbuf_addstr(&verify_message, "No signature\n");
 534                /* otherwise we couldn't verify, which is shown as bad */
 535        }
 536
 537        show_sig_lines(opt, status, verify_message.buf);
 538        strbuf_release(&verify_message);
 539}
 540
 541static void show_mergetag(struct rev_info *opt, struct commit *commit)
 542{
 543        for_each_mergetag(show_one_mergetag, commit, opt);
 544}
 545
 546void show_log(struct rev_info *opt)
 547{
 548        struct strbuf msgbuf = STRBUF_INIT;
 549        struct log_info *log = opt->loginfo;
 550        struct commit *commit = log->commit, *parent = log->parent;
 551        int abbrev_commit = opt->abbrev_commit ? opt->abbrev : GIT_SHA1_HEXSZ;
 552        const char *extra_headers = opt->extra_headers;
 553        struct pretty_print_context ctx = {0};
 554
 555        opt->loginfo = NULL;
 556        if (!opt->verbose_header) {
 557                graph_show_commit(opt->graph);
 558
 559                if (!opt->graph)
 560                        put_revision_mark(opt, commit);
 561                fputs(find_unique_abbrev(commit->object.oid.hash, abbrev_commit), opt->diffopt.file);
 562                if (opt->print_parents)
 563                        show_parents(commit, abbrev_commit, opt->diffopt.file);
 564                if (opt->children.name)
 565                        show_children(opt, commit, abbrev_commit);
 566                show_decorations(opt, commit);
 567                if (opt->graph && !graph_is_commit_finished(opt->graph)) {
 568                        putc('\n', opt->diffopt.file);
 569                        graph_show_remainder(opt->graph);
 570                }
 571                putc(opt->diffopt.line_termination, opt->diffopt.file);
 572                return;
 573        }
 574
 575        /*
 576         * If use_terminator is set, we already handled any record termination
 577         * at the end of the last record.
 578         * Otherwise, add a diffopt.line_termination character before all
 579         * entries but the first.  (IOW, as a separator between entries)
 580         */
 581        if (opt->shown_one && !opt->use_terminator) {
 582                /*
 583                 * If entries are separated by a newline, the output
 584                 * should look human-readable.  If the last entry ended
 585                 * with a newline, print the graph output before this
 586                 * newline.  Otherwise it will end up as a completely blank
 587                 * line and will look like a gap in the graph.
 588                 *
 589                 * If the entry separator is not a newline, the output is
 590                 * primarily intended for programmatic consumption, and we
 591                 * never want the extra graph output before the entry
 592                 * separator.
 593                 */
 594                if (opt->diffopt.line_termination == '\n' &&
 595                    !opt->missing_newline)
 596                        graph_show_padding(opt->graph);
 597                putc(opt->diffopt.line_termination, opt->diffopt.file);
 598        }
 599        opt->shown_one = 1;
 600
 601        /*
 602         * If the history graph was requested,
 603         * print the graph, up to this commit's line
 604         */
 605        graph_show_commit(opt->graph);
 606
 607        /*
 608         * Print header line of header..
 609         */
 610
 611        if (cmit_fmt_is_mail(opt->commit_format)) {
 612                log_write_email_headers(opt, commit, &extra_headers,
 613                                        &ctx.need_8bit_cte);
 614                ctx.rev = opt;
 615                ctx.print_email_subject = 1;
 616        } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
 617                fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
 618                if (opt->commit_format != CMIT_FMT_ONELINE)
 619                        fputs("commit ", opt->diffopt.file);
 620
 621                if (!opt->graph)
 622                        put_revision_mark(opt, commit);
 623                fputs(find_unique_abbrev(commit->object.oid.hash, abbrev_commit),
 624                      opt->diffopt.file);
 625                if (opt->print_parents)
 626                        show_parents(commit, abbrev_commit, opt->diffopt.file);
 627                if (opt->children.name)
 628                        show_children(opt, commit, abbrev_commit);
 629                if (parent)
 630                        fprintf(opt->diffopt.file, " (from %s)",
 631                               find_unique_abbrev(parent->object.oid.hash,
 632                                                  abbrev_commit));
 633                fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
 634                show_decorations(opt, commit);
 635                if (opt->commit_format == CMIT_FMT_ONELINE) {
 636                        putc(' ', opt->diffopt.file);
 637                } else {
 638                        putc('\n', opt->diffopt.file);
 639                        graph_show_oneline(opt->graph);
 640                }
 641                if (opt->reflog_info) {
 642                        /*
 643                         * setup_revisions() ensures that opt->reflog_info
 644                         * and opt->graph cannot both be set,
 645                         * so we don't need to worry about printing the
 646                         * graph info here.
 647                         */
 648                        show_reflog_message(opt->reflog_info,
 649                                            opt->commit_format == CMIT_FMT_ONELINE,
 650                                            &opt->date_mode,
 651                                            opt->date_mode_explicit);
 652                        if (opt->commit_format == CMIT_FMT_ONELINE)
 653                                return;
 654                }
 655        }
 656
 657        if (opt->show_signature) {
 658                show_signature(opt, commit);
 659                show_mergetag(opt, commit);
 660        }
 661
 662        if (!get_cached_commit_buffer(commit, NULL))
 663                return;
 664
 665        if (opt->show_notes) {
 666                int raw;
 667                struct strbuf notebuf = STRBUF_INIT;
 668
 669                raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
 670                format_display_notes(&commit->object.oid, &notebuf,
 671                                     get_log_output_encoding(), raw);
 672                ctx.notes_message = notebuf.len
 673                        ? strbuf_detach(&notebuf, NULL)
 674                        : xcalloc(1, 1);
 675        }
 676
 677        /*
 678         * And then the pretty-printed message itself
 679         */
 680        if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
 681                ctx.need_8bit_cte =
 682                        has_non_ascii(fmt_name(getenv("GIT_COMMITTER_NAME"),
 683                                               getenv("GIT_COMMITTER_EMAIL")));
 684        ctx.date_mode = opt->date_mode;
 685        ctx.date_mode_explicit = opt->date_mode_explicit;
 686        ctx.abbrev = opt->diffopt.abbrev;
 687        ctx.after_subject = extra_headers;
 688        ctx.preserve_subject = opt->preserve_subject;
 689        ctx.reflog_info = opt->reflog_info;
 690        ctx.fmt = opt->commit_format;
 691        ctx.mailmap = opt->mailmap;
 692        ctx.color = opt->diffopt.use_color;
 693        ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
 694        ctx.output_encoding = get_log_output_encoding();
 695        if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
 696                ctx.from_ident = &opt->from_ident;
 697        if (opt->graph)
 698                ctx.graph_width = graph_width(opt->graph);
 699        pretty_print_commit(&ctx, commit, &msgbuf);
 700
 701        if (opt->add_signoff)
 702                append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
 703
 704        if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
 705            ctx.notes_message && *ctx.notes_message) {
 706                if (cmit_fmt_is_mail(ctx.fmt)) {
 707                        strbuf_addstr(&msgbuf, "---\n");
 708                        opt->shown_dashes = 1;
 709                }
 710                strbuf_addstr(&msgbuf, ctx.notes_message);
 711        }
 712
 713        if (opt->show_log_size) {
 714                fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
 715                graph_show_oneline(opt->graph);
 716        }
 717
 718        /*
 719         * Set opt->missing_newline if msgbuf doesn't
 720         * end in a newline (including if it is empty)
 721         */
 722        if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
 723                opt->missing_newline = 1;
 724        else
 725                opt->missing_newline = 0;
 726
 727        graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf);
 728        if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
 729                if (!opt->missing_newline)
 730                        graph_show_padding(opt->graph);
 731                putc(opt->diffopt.line_termination, opt->diffopt.file);
 732        }
 733
 734        strbuf_release(&msgbuf);
 735        free(ctx.notes_message);
 736}
 737
 738int log_tree_diff_flush(struct rev_info *opt)
 739{
 740        opt->shown_dashes = 0;
 741        diffcore_std(&opt->diffopt);
 742
 743        if (diff_queue_is_empty()) {
 744                int saved_fmt = opt->diffopt.output_format;
 745                opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
 746                diff_flush(&opt->diffopt);
 747                opt->diffopt.output_format = saved_fmt;
 748                return 0;
 749        }
 750
 751        if (opt->loginfo && !opt->no_commit_id) {
 752                show_log(opt);
 753                if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
 754                    opt->verbose_header &&
 755                    opt->commit_format != CMIT_FMT_ONELINE &&
 756                    !commit_format_is_empty(opt->commit_format)) {
 757                        /*
 758                         * When showing a verbose header (i.e. log message),
 759                         * and not in --pretty=oneline format, we would want
 760                         * an extra newline between the end of log and the
 761                         * diff/diffstat output for readability.
 762                         */
 763                        int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
 764                        if (opt->diffopt.output_prefix) {
 765                                struct strbuf *msg = NULL;
 766                                msg = opt->diffopt.output_prefix(&opt->diffopt,
 767                                        opt->diffopt.output_prefix_data);
 768                                fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
 769                        }
 770
 771                        /*
 772                         * We may have shown three-dashes line early
 773                         * between notes and the log message, in which
 774                         * case we only want a blank line after the
 775                         * notes without (an extra) three-dashes line.
 776                         * Otherwise, we show the three-dashes line if
 777                         * we are showing the patch with diffstat, but
 778                         * in that case, there is no extra blank line
 779                         * after the three-dashes line.
 780                         */
 781                        if (!opt->shown_dashes &&
 782                            (pch & opt->diffopt.output_format) == pch)
 783                                fprintf(opt->diffopt.file, "---");
 784                        putc('\n', opt->diffopt.file);
 785                }
 786        }
 787        diff_flush(&opt->diffopt);
 788        return 1;
 789}
 790
 791static int do_diff_combined(struct rev_info *opt, struct commit *commit)
 792{
 793        diff_tree_combined_merge(commit, opt->dense_combined_merges, opt);
 794        return !opt->loginfo;
 795}
 796
 797/*
 798 * Show the diff of a commit.
 799 *
 800 * Return true if we printed any log info messages
 801 */
 802static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
 803{
 804        int showed_log;
 805        struct commit_list *parents;
 806        struct object_id *oid;
 807
 808        if (!opt->diff && !opt->diffopt.flags.exit_with_status)
 809                return 0;
 810
 811        parse_commit_or_die(commit);
 812        oid = &commit->tree->object.oid;
 813
 814        /* Root commit? */
 815        parents = get_saved_parents(opt, commit);
 816        if (!parents) {
 817                if (opt->show_root_diff) {
 818                        diff_root_tree_oid(oid, "", &opt->diffopt);
 819                        log_tree_diff_flush(opt);
 820                }
 821                return !opt->loginfo;
 822        }
 823
 824        /* More than one parent? */
 825        if (parents && parents->next) {
 826                if (opt->ignore_merges)
 827                        return 0;
 828                else if (opt->combine_merges)
 829                        return do_diff_combined(opt, commit);
 830                else if (opt->first_parent_only) {
 831                        /*
 832                         * Generate merge log entry only for the first
 833                         * parent, showing summary diff of the others
 834                         * we merged _in_.
 835                         */
 836                        parse_commit_or_die(parents->item);
 837                        diff_tree_oid(&parents->item->tree->object.oid,
 838                                      oid, "", &opt->diffopt);
 839                        log_tree_diff_flush(opt);
 840                        return !opt->loginfo;
 841                }
 842
 843                /* If we show individual diffs, show the parent info */
 844                log->parent = parents->item;
 845        }
 846
 847        showed_log = 0;
 848        for (;;) {
 849                struct commit *parent = parents->item;
 850
 851                parse_commit_or_die(parent);
 852                diff_tree_oid(&parent->tree->object.oid,
 853                              oid, "", &opt->diffopt);
 854                log_tree_diff_flush(opt);
 855
 856                showed_log |= !opt->loginfo;
 857
 858                /* Set up the log info for the next parent, if any.. */
 859                parents = parents->next;
 860                if (!parents)
 861                        break;
 862                log->parent = parents->item;
 863                opt->loginfo = log;
 864        }
 865        return showed_log;
 866}
 867
 868int log_tree_commit(struct rev_info *opt, struct commit *commit)
 869{
 870        struct log_info log;
 871        int shown, close_file = opt->diffopt.close_file;
 872
 873        log.commit = commit;
 874        log.parent = NULL;
 875        opt->loginfo = &log;
 876        opt->diffopt.close_file = 0;
 877
 878        if (opt->line_level_traverse)
 879                return line_log_print(opt, commit);
 880
 881        if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
 882                fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
 883        shown = log_tree_diff(opt, commit, &log);
 884        if (!shown && opt->loginfo && opt->always_show_header) {
 885                log.parent = NULL;
 886                show_log(opt);
 887                shown = 1;
 888        }
 889        if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
 890                fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
 891        opt->loginfo = NULL;
 892        maybe_flush_or_die(opt->diffopt.file, "stdout");
 893        if (close_file)
 894                fclose(opt->diffopt.file);
 895        return shown;
 896}