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