git
/
log-tree.c
1174 строки · 32.7 Кб
1#define USE_THE_REPOSITORY_VARIABLE2
3#include "git-compat-util.h"4#include "commit-reach.h"5#include "config.h"6#include "diff.h"7#include "diffcore.h"8#include "environment.h"9#include "hex.h"10#include "object-name.h"11#include "object-store-ll.h"12#include "repository.h"13#include "tmp-objdir.h"14#include "commit.h"15#include "tag.h"16#include "graph.h"17#include "log-tree.h"18#include "merge-ort.h"19#include "reflog-walk.h"20#include "refs.h"21#include "replace-object.h"22#include "revision.h"23#include "string-list.h"24#include "color.h"25#include "gpg-interface.h"26#include "sequencer.h"27#include "line-log.h"28#include "help.h"29#include "range-diff.h"30#include "strmap.h"31#include "tree.h"32#include "wildmatch.h"33#include "write-or-die.h"34#include "pager.h"35
36static struct decoration name_decoration = { "object names" };37static int decoration_loaded;38static int decoration_flags;39
40static char decoration_colors[][COLOR_MAXLEN] = {41GIT_COLOR_RESET,42GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */43GIT_COLOR_BOLD_RED, /* REF_REMOTE */44GIT_COLOR_BOLD_YELLOW, /* REF_TAG */45GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */46GIT_COLOR_BOLD_CYAN, /* REF_HEAD */47GIT_COLOR_BOLD_BLUE, /* GRAFTED */48};49
50static const char *color_decorate_slots[] = {51[DECORATION_REF_LOCAL] = "branch",52[DECORATION_REF_REMOTE] = "remoteBranch",53[DECORATION_REF_TAG] = "tag",54[DECORATION_REF_STASH] = "stash",55[DECORATION_REF_HEAD] = "HEAD",56[DECORATION_GRAFTED] = "grafted",57};58
59static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)60{
61if (want_color(decorate_use_color))62return decoration_colors[ix];63return "";64}
65
66define_list_config_array(color_decorate_slots);67
68int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)69{
70int slot = LOOKUP_CONFIG(color_decorate_slots, slot_name);71if (slot < 0)72return 0;73if (!value)74return config_error_nonbool(var);75return color_parse(value, decoration_colors[slot]);76}
77
78/*
79* log-tree.c uses DIFF_OPT_TST for determining whether to use color
80* for showing the commit sha1, use the same check for --decorate
81*/
82#define decorate_get_color_opt(o, ix) \83decorate_get_color((o)->use_color, ix)84
85void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)86{
87struct name_decoration *res;88FLEX_ALLOC_STR(res, name, name);89res->type = type;90res->next = add_decoration(&name_decoration, obj, res);91}
92
93const struct name_decoration *get_name_decoration(const struct object *obj)94{
95load_ref_decorations(NULL, DECORATE_SHORT_REFS);96return lookup_decoration(&name_decoration, obj);97}
98
99static int match_ref_pattern(const char *refname,100const struct string_list_item *item)101{
102int matched = 0;103if (!item->util) {104if (!wildmatch(item->string, refname, 0))105matched = 1;106} else {107const char *rest;108if (skip_prefix(refname, item->string, &rest) &&109(!*rest || *rest == '/'))110matched = 1;111}112return matched;113}
114
115static int ref_filter_match(const char *refname,116const struct decoration_filter *filter)117{
118struct string_list_item *item;119const struct string_list *exclude_patterns = filter->exclude_ref_pattern;120const struct string_list *include_patterns = filter->include_ref_pattern;121const struct string_list *exclude_patterns_config =122filter->exclude_ref_config_pattern;123
124if (exclude_patterns && exclude_patterns->nr) {125for_each_string_list_item(item, exclude_patterns) {126if (match_ref_pattern(refname, item))127return 0;128}129}130
131if (include_patterns && include_patterns->nr) {132for_each_string_list_item(item, include_patterns) {133if (match_ref_pattern(refname, item))134return 1;135}136return 0;137}138
139if (exclude_patterns_config && exclude_patterns_config->nr) {140for_each_string_list_item(item, exclude_patterns_config) {141if (match_ref_pattern(refname, item))142return 0;143}144}145
146return 1;147}
148
149static int add_ref_decoration(const char *refname, const char *referent UNUSED, const struct object_id *oid,150int flags UNUSED,151void *cb_data)152{
153int i;154struct object *obj;155enum object_type objtype;156enum decoration_type deco_type = DECORATION_NONE;157struct decoration_filter *filter = (struct decoration_filter *)cb_data;158const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;159
160if (filter && !ref_filter_match(refname, filter))161return 0;162
163if (starts_with(refname, git_replace_ref_base)) {164struct object_id original_oid;165if (!replace_refs_enabled(the_repository))166return 0;167if (get_oid_hex(refname + strlen(git_replace_ref_base),168&original_oid)) {169warning("invalid replace ref %s", refname);170return 0;171}172obj = parse_object(the_repository, &original_oid);173if (obj)174add_name_decoration(DECORATION_GRAFTED, "replaced", obj);175return 0;176}177
178objtype = oid_object_info(the_repository, oid, NULL);179if (objtype < 0)180return 0;181obj = lookup_object_by_type(the_repository, oid, objtype);182
183for (i = 0; i < ARRAY_SIZE(ref_namespace); i++) {184struct ref_namespace_info *info = &ref_namespace[i];185
186if (!info->decoration)187continue;188if (info->exact) {189if (!strcmp(refname, info->ref)) {190deco_type = info->decoration;191break;192}193} else if (starts_with(refname, info->ref)) {194deco_type = info->decoration;195break;196}197}198
199add_name_decoration(deco_type, refname, obj);200while (obj->type == OBJ_TAG) {201if (!obj->parsed)202parse_object(the_repository, &obj->oid);203obj = ((struct tag *)obj)->tagged;204if (!obj)205break;206add_name_decoration(DECORATION_REF_TAG, refname, obj);207}208return 0;209}
210
211static int add_graft_decoration(const struct commit_graft *graft,212void *cb_data UNUSED)213{
214struct commit *commit = lookup_commit(the_repository, &graft->oid);215if (!commit)216return 0;217add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);218return 0;219}
220
221void load_ref_decorations(struct decoration_filter *filter, int flags)222{
223if (!decoration_loaded) {224if (filter) {225struct string_list_item *item;226for_each_string_list_item(item, filter->exclude_ref_pattern) {227normalize_glob_ref(item, NULL, item->string);228}229for_each_string_list_item(item, filter->include_ref_pattern) {230normalize_glob_ref(item, NULL, item->string);231}232for_each_string_list_item(item, filter->exclude_ref_config_pattern) {233normalize_glob_ref(item, NULL, item->string);234}235}236decoration_loaded = 1;237decoration_flags = flags;238refs_for_each_ref(get_main_ref_store(the_repository),239add_ref_decoration, filter);240refs_head_ref(get_main_ref_store(the_repository),241add_ref_decoration, filter);242for_each_commit_graft(add_graft_decoration, filter);243}244}
245
246static void show_parents(struct commit *commit, int abbrev, FILE *file)247{
248struct commit_list *p;249for (p = commit->parents; p ; p = p->next) {250struct commit *parent = p->item;251fprintf(file, " %s",252repo_find_unique_abbrev(the_repository, &parent->object.oid, abbrev));253}254}
255
256static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)257{
258struct commit_list *p = lookup_decoration(&opt->children, &commit->object);259for ( ; p; p = p->next) {260fprintf(opt->diffopt.file, " %s",261repo_find_unique_abbrev(the_repository, &p->item->object.oid, abbrev));262}263}
264
265/*
266* Do we have HEAD in the output, and also the branch it points at?
267* If so, find that decoration entry for that current branch.
268*/
269static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration)270{
271const struct name_decoration *list, *head = NULL;272const char *branch_name = NULL;273int rru_flags;274
275/* First find HEAD */276for (list = decoration; list; list = list->next)277if (list->type == DECORATION_REF_HEAD) {278head = list;279break;280}281if (!head)282return NULL;283
284/* Now resolve and find the matching current branch */285branch_name = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),286"HEAD", 0, NULL, &rru_flags);287if (!branch_name || !(rru_flags & REF_ISSYMREF))288return NULL;289
290if (!starts_with(branch_name, "refs/"))291return NULL;292
293/* OK, do we have that ref in the list? */294for (list = decoration; list; list = list->next)295if ((list->type == DECORATION_REF_LOCAL) &&296!strcmp(branch_name, list->name)) {297return list;298}299
300return NULL;301}
302
303static void show_name(struct strbuf *sb, const struct name_decoration *decoration)304{
305if (decoration_flags == DECORATE_SHORT_REFS)306strbuf_addstr(sb, prettify_refname(decoration->name));307else308strbuf_addstr(sb, decoration->name);309}
310
311/*
312* The caller makes sure there is no funny color before calling.
313* format_decorations ensures the same after return.
314*/
315void format_decorations(struct strbuf *sb,316const struct commit *commit,317int use_color,318const struct decoration_options *opts)319{
320const struct name_decoration *decoration;321const struct name_decoration *current_and_HEAD;322const char *color_commit, *color_reset;323
324const char *prefix = " (";325const char *suffix = ")";326const char *separator = ", ";327const char *pointer = " -> ";328const char *tag = "tag: ";329
330decoration = get_name_decoration(&commit->object);331if (!decoration)332return;333
334if (opts) {335if (opts->prefix)336prefix = opts->prefix;337if (opts->suffix)338suffix = opts->suffix;339if (opts->separator)340separator = opts->separator;341if (opts->pointer)342pointer = opts->pointer;343if (opts->tag)344tag = opts->tag;345}346
347color_commit = diff_get_color(use_color, DIFF_COMMIT);348color_reset = decorate_get_color(use_color, DECORATION_NONE);349
350current_and_HEAD = current_pointed_by_HEAD(decoration);351while (decoration) {352/*353* When both current and HEAD are there, only
354* show HEAD->current where HEAD would have
355* appeared, skipping the entry for current.
356*/
357if (decoration != current_and_HEAD) {358const char *color =359decorate_get_color(use_color, decoration->type);360
361if (*prefix) {362strbuf_addstr(sb, color_commit);363strbuf_addstr(sb, prefix);364strbuf_addstr(sb, color_reset);365}366
367if (*tag && decoration->type == DECORATION_REF_TAG) {368strbuf_addstr(sb, color);369strbuf_addstr(sb, tag);370strbuf_addstr(sb, color_reset);371}372
373strbuf_addstr(sb, color);374show_name(sb, decoration);375strbuf_addstr(sb, color_reset);376
377if (current_and_HEAD &&378decoration->type == DECORATION_REF_HEAD) {379strbuf_addstr(sb, color_commit);380strbuf_addstr(sb, pointer);381strbuf_addstr(sb, color_reset);382strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));383show_name(sb, current_and_HEAD);384strbuf_addstr(sb, color_reset);385}386
387prefix = separator;388}389decoration = decoration->next;390}391if (*suffix) {392strbuf_addstr(sb, color_commit);393strbuf_addstr(sb, suffix);394strbuf_addstr(sb, color_reset);395}396}
397
398void show_decorations(struct rev_info *opt, struct commit *commit)399{
400struct strbuf sb = STRBUF_INIT;401
402if (opt->sources) {403char **slot = revision_sources_peek(opt->sources, commit);404
405if (slot && *slot)406fprintf(opt->diffopt.file, "\t%s", *slot);407}408if (!opt->show_decorations)409return;410format_decorations(&sb, commit, opt->diffopt.use_color, NULL);411fputs(sb.buf, opt->diffopt.file);412strbuf_release(&sb);413}
414
415void fmt_output_subject(struct strbuf *filename,416const char *subject,417struct rev_info *info)418{
419const char *suffix = info->patch_suffix;420int nr = info->nr;421int start_len = filename->len;422int max_len = start_len + info->patch_name_max - (strlen(suffix) + 1);423
424if (info->reroll_count) {425struct strbuf temp = STRBUF_INIT;426
427strbuf_addf(&temp, "v%s", info->reroll_count);428format_sanitized_subject(filename, temp.buf, temp.len);429strbuf_addstr(filename, "-");430strbuf_release(&temp);431}432strbuf_addf(filename, "%04d-%s", nr, subject);433
434if (max_len < filename->len)435strbuf_setlen(filename, max_len);436strbuf_addstr(filename, suffix);437}
438
439void fmt_output_commit(struct strbuf *filename,440struct commit *commit,441struct rev_info *info)442{
443struct pretty_print_context ctx = {0};444struct strbuf subject = STRBUF_INIT;445
446repo_format_commit_message(the_repository, commit, "%f", &subject,447&ctx);448fmt_output_subject(filename, subject.buf, info);449strbuf_release(&subject);450}
451
452void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)453{
454if (opt->total > 0) {455strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ",456opt->subject_prefix,457*opt->subject_prefix ? " " : "",458decimal_width(opt->total),459opt->nr, opt->total);460} else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {461strbuf_addf(sb, "Subject: [%s] ",462opt->subject_prefix);463} else {464strbuf_addstr(sb, "Subject: ");465}466}
467
468void log_write_email_headers(struct rev_info *opt, struct commit *commit,469char **extra_headers_p,470int *need_8bit_cte_p,471int maybe_multipart)472{
473struct strbuf headers = STRBUF_INIT;474const char *name = oid_to_hex(opt->zero_commit ?475null_oid() : &commit->object.oid);476
477*need_8bit_cte_p = 0; /* unknown */478
479if (opt->extra_headers && *opt->extra_headers)480strbuf_addstr(&headers, opt->extra_headers);481
482fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);483graph_show_oneline(opt->graph);484if (opt->message_id) {485fprintf(opt->diffopt.file, "Message-ID: <%s>\n", opt->message_id);486graph_show_oneline(opt->graph);487}488if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {489int i, n;490n = opt->ref_message_ids->nr;491fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);492for (i = 0; i < n; i++)493fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),494opt->ref_message_ids->items[i].string);495graph_show_oneline(opt->graph);496}497if (opt->mime_boundary && maybe_multipart) {498static struct strbuf buffer = STRBUF_INIT;499struct strbuf filename = STRBUF_INIT;500*need_8bit_cte_p = -1; /* NEVER */501
502strbuf_reset(&buffer);503
504strbuf_addf(&headers,505"MIME-Version: 1.0\n"506"Content-Type: multipart/mixed;"507" boundary=\"%s%s\"\n"508"\n"509"This is a multi-part message in MIME "510"format.\n"511"--%s%s\n"512"Content-Type: text/plain; "513"charset=UTF-8; format=fixed\n"514"Content-Transfer-Encoding: 8bit\n\n",515mime_boundary_leader, opt->mime_boundary,516mime_boundary_leader, opt->mime_boundary);517
518if (opt->numbered_files)519strbuf_addf(&filename, "%d", opt->nr);520else521fmt_output_commit(&filename, commit, opt);522strbuf_addf(&buffer,523"\n--%s%s\n"524"Content-Type: text/x-patch;"525" name=\"%s\"\n"526"Content-Transfer-Encoding: 8bit\n"527"Content-Disposition: %s;"528" filename=\"%s\"\n\n",529mime_boundary_leader, opt->mime_boundary,530filename.buf,531opt->no_inline ? "attachment" : "inline",532filename.buf);533opt->diffopt.stat_sep = buffer.buf;534strbuf_release(&filename);535}536*extra_headers_p = headers.len ? strbuf_detach(&headers, NULL) : NULL;537}
538
539static void show_sig_lines(struct rev_info *opt, int status, const char *bol)540{
541const char *color, *reset, *eol;542
543color = diff_get_color_opt(&opt->diffopt,544status ? DIFF_WHITESPACE : DIFF_FRAGINFO);545reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);546while (*bol) {547eol = strchrnul(bol, '\n');548fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,549*eol ? "\n" : "");550graph_show_oneline(opt->graph);551bol = (*eol) ? (eol + 1) : eol;552}553}
554
555static void show_signature(struct rev_info *opt, struct commit *commit)556{
557struct strbuf payload = STRBUF_INIT;558struct strbuf signature = STRBUF_INIT;559struct signature_check sigc = { 0 };560int status;561
562if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0)563goto out;564
565sigc.payload_type = SIGNATURE_PAYLOAD_COMMIT;566sigc.payload = strbuf_detach(&payload, &sigc.payload_len);567status = check_signature(&sigc, signature.buf, signature.len);568if (status && !sigc.output)569show_sig_lines(opt, status, "No signature\n");570else571show_sig_lines(opt, status, sigc.output);572signature_check_clear(&sigc);573
574out:575strbuf_release(&payload);576strbuf_release(&signature);577}
578
579static int which_parent(const struct object_id *oid, const struct commit *commit)580{
581int nth;582const struct commit_list *parent;583
584for (nth = 0, parent = commit->parents; parent; parent = parent->next) {585if (oideq(&parent->item->object.oid, oid))586return nth;587nth++;588}589return -1;590}
591
592static int is_common_merge(const struct commit *commit)593{
594return (commit->parents595&& commit->parents->next596&& !commit->parents->next->next);597}
598
599static int show_one_mergetag(struct commit *commit,600struct commit_extra_header *extra,601void *data)602{
603struct rev_info *opt = (struct rev_info *)data;604struct object_id oid;605struct tag *tag;606struct strbuf verify_message;607struct signature_check sigc = { 0 };608int status, nth;609struct strbuf payload = STRBUF_INIT;610struct strbuf signature = STRBUF_INIT;611
612hash_object_file(the_hash_algo, extra->value, extra->len,613OBJ_TAG, &oid);614tag = lookup_tag(the_repository, &oid);615if (!tag)616return -1; /* error message already given */617
618strbuf_init(&verify_message, 256);619if (parse_tag_buffer(the_repository, tag, extra->value, extra->len))620strbuf_addstr(&verify_message, "malformed mergetag\n");621else if (is_common_merge(commit) &&622oideq(&tag->tagged->oid,623&commit->parents->next->item->object.oid))624strbuf_addf(&verify_message,625"merged tag '%s'\n", tag->tag);626else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0)627strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",628tag->tag, oid_to_hex(&tag->tagged->oid));629else630strbuf_addf(&verify_message,631"parent #%d, tagged '%s'\n", nth + 1, tag->tag);632
633status = -1;634if (parse_signature(extra->value, extra->len, &payload, &signature)) {635/* could have a good signature */636sigc.payload_type = SIGNATURE_PAYLOAD_TAG;637sigc.payload = strbuf_detach(&payload, &sigc.payload_len);638status = check_signature(&sigc, signature.buf, signature.len);639if (sigc.output)640strbuf_addstr(&verify_message, sigc.output);641else642strbuf_addstr(&verify_message, "No signature\n");643signature_check_clear(&sigc);644/* otherwise we couldn't verify, which is shown as bad */645}646
647show_sig_lines(opt, status, verify_message.buf);648strbuf_release(&verify_message);649strbuf_release(&payload);650strbuf_release(&signature);651return 0;652}
653
654static int show_mergetag(struct rev_info *opt, struct commit *commit)655{
656return for_each_mergetag(show_one_mergetag, commit, opt);657}
658
659static void next_commentary_block(struct rev_info *opt, struct strbuf *sb)660{
661const char *x = opt->shown_dashes ? "\n" : "---\n";662if (sb)663strbuf_addstr(sb, x);664else665fputs(x, opt->diffopt.file);666opt->shown_dashes = 1;667}
668
669static void show_diff_of_diff(struct rev_info *opt)670{
671if (!cmit_fmt_is_mail(opt->commit_format))672return;673
674if (opt->idiff_oid1) {675struct diff_queue_struct dq;676
677memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));678DIFF_QUEUE_CLEAR(&diff_queued_diff);679
680fprintf_ln(opt->diffopt.file, "\n%s", opt->idiff_title);681show_interdiff(opt->idiff_oid1, opt->idiff_oid2, 2,682&opt->diffopt);683
684memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));685}686
687if (opt->rdiff1) {688struct diff_queue_struct dq;689struct diff_options opts;690struct range_diff_options range_diff_opts = {691.creation_factor = opt->creation_factor,692.dual_color = 1,693.diffopt = &opts694};695
696memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));697DIFF_QUEUE_CLEAR(&diff_queued_diff);698
699fprintf_ln(opt->diffopt.file, "\n%s", opt->rdiff_title);700/*701* Pass minimum required diff-options to range-diff; others
702* can be added later if deemed desirable.
703*/
704repo_diff_setup(the_repository, &opts);705opts.file = opt->diffopt.file;706opts.use_color = opt->diffopt.use_color;707diff_setup_done(&opts);708show_range_diff(opt->rdiff1, opt->rdiff2, &range_diff_opts);709
710memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));711}712}
713
714void show_log(struct rev_info *opt)715{
716struct strbuf msgbuf = STRBUF_INIT;717struct log_info *log = opt->loginfo;718struct commit *commit = log->commit, *parent = log->parent;719int abbrev_commit = opt->abbrev_commit ? opt->abbrev : the_hash_algo->hexsz;720struct pretty_print_context ctx = {0};721
722opt->loginfo = NULL;723if (!opt->verbose_header) {724graph_show_commit(opt->graph);725
726if (!opt->graph)727put_revision_mark(opt, commit);728fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid, abbrev_commit),729opt->diffopt.file);730if (opt->print_parents)731show_parents(commit, abbrev_commit, opt->diffopt.file);732if (opt->children.name)733show_children(opt, commit, abbrev_commit);734show_decorations(opt, commit);735if (opt->graph && !graph_is_commit_finished(opt->graph)) {736putc('\n', opt->diffopt.file);737graph_show_remainder(opt->graph);738}739putc(opt->diffopt.line_termination, opt->diffopt.file);740return;741}742
743/*744* If use_terminator is set, we already handled any record termination
745* at the end of the last record.
746* Otherwise, add a diffopt.line_termination character before all
747* entries but the first. (IOW, as a separator between entries)
748*/
749if (opt->shown_one && !opt->use_terminator) {750/*751* If entries are separated by a newline, the output
752* should look human-readable. If the last entry ended
753* with a newline, print the graph output before this
754* newline. Otherwise it will end up as a completely blank
755* line and will look like a gap in the graph.
756*
757* If the entry separator is not a newline, the output is
758* primarily intended for programmatic consumption, and we
759* never want the extra graph output before the entry
760* separator.
761*/
762if (opt->diffopt.line_termination == '\n' &&763!opt->missing_newline)764graph_show_padding(opt->graph);765putc(opt->diffopt.line_termination, opt->diffopt.file);766}767opt->shown_one = 1;768
769/*770* If the history graph was requested,
771* print the graph, up to this commit's line
772*/
773graph_show_commit(opt->graph);774
775/*776* Print header line of header..
777*/
778
779if (cmit_fmt_is_mail(opt->commit_format)) {780log_write_email_headers(opt, commit, &ctx.after_subject,781&ctx.need_8bit_cte, 1);782ctx.rev = opt;783} else if (opt->commit_format != CMIT_FMT_USERFORMAT) {784fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);785if (opt->commit_format != CMIT_FMT_ONELINE)786fputs("commit ", opt->diffopt.file);787
788if (!opt->graph)789put_revision_mark(opt, commit);790fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid,791abbrev_commit),792opt->diffopt.file);793if (opt->print_parents)794show_parents(commit, abbrev_commit, opt->diffopt.file);795if (opt->children.name)796show_children(opt, commit, abbrev_commit);797if (parent)798fprintf(opt->diffopt.file, " (from %s)",799repo_find_unique_abbrev(the_repository, &parent->object.oid, abbrev_commit));800fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);801show_decorations(opt, commit);802if (opt->commit_format == CMIT_FMT_ONELINE) {803putc(' ', opt->diffopt.file);804} else {805putc('\n', opt->diffopt.file);806graph_show_oneline(opt->graph);807}808if (opt->reflog_info) {809/*810* setup_revisions() ensures that opt->reflog_info
811* and opt->graph cannot both be set,
812* so we don't need to worry about printing the
813* graph info here.
814*/
815show_reflog_message(opt->reflog_info,816opt->commit_format == CMIT_FMT_ONELINE,817opt->date_mode,818opt->date_mode_explicit);819if (opt->commit_format == CMIT_FMT_ONELINE)820return;821}822}823
824if (opt->show_signature) {825show_signature(opt, commit);826show_mergetag(opt, commit);827}828
829if (opt->show_notes) {830int raw;831struct strbuf notebuf = STRBUF_INIT;832
833raw = (opt->commit_format == CMIT_FMT_USERFORMAT);834format_display_notes(&commit->object.oid, ¬ebuf,835get_log_output_encoding(), raw);836ctx.notes_message = strbuf_detach(¬ebuf, NULL);837}838
839/*840* And then the pretty-printed message itself
841*/
842if (ctx.need_8bit_cte >= 0 && opt->add_signoff)843ctx.need_8bit_cte =844has_non_ascii(fmt_name(WANT_COMMITTER_IDENT));845ctx.date_mode = opt->date_mode;846ctx.date_mode_explicit = opt->date_mode_explicit;847ctx.abbrev = opt->diffopt.abbrev;848ctx.preserve_subject = opt->preserve_subject;849ctx.encode_email_headers = opt->encode_email_headers;850ctx.reflog_info = opt->reflog_info;851ctx.fmt = opt->commit_format;852ctx.mailmap = opt->mailmap;853ctx.color = opt->diffopt.use_color;854ctx.expand_tabs_in_log = opt->expand_tabs_in_log;855ctx.output_encoding = get_log_output_encoding();856ctx.rev = opt;857if (opt->from_ident.mail_begin && opt->from_ident.name_begin)858ctx.from_ident = &opt->from_ident;859if (opt->graph)860ctx.graph_width = graph_width(opt->graph);861pretty_print_commit(&ctx, commit, &msgbuf);862
863if (opt->add_signoff)864append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);865
866if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&867ctx.notes_message && *ctx.notes_message) {868if (cmit_fmt_is_mail(ctx.fmt))869next_commentary_block(opt, &msgbuf);870strbuf_addstr(&msgbuf, ctx.notes_message);871}872
873if (opt->show_log_size) {874fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);875graph_show_oneline(opt->graph);876}877
878/*879* Set opt->missing_newline if msgbuf doesn't
880* end in a newline (including if it is empty)
881*/
882if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')883opt->missing_newline = 1;884else885opt->missing_newline = 0;886
887graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf);888if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {889if (!opt->missing_newline)890graph_show_padding(opt->graph);891putc(opt->diffopt.line_termination, opt->diffopt.file);892}893
894strbuf_release(&msgbuf);895free(ctx.notes_message);896free(ctx.after_subject);897}
898
899int log_tree_diff_flush(struct rev_info *opt)900{
901opt->shown_dashes = 0;902diffcore_std(&opt->diffopt);903
904if (diff_queue_is_empty(&opt->diffopt)) {905int saved_fmt = opt->diffopt.output_format;906opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;907diff_flush(&opt->diffopt);908opt->diffopt.output_format = saved_fmt;909return 0;910}911
912if (opt->loginfo && !opt->no_commit_id) {913show_log(opt);914if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&915opt->verbose_header &&916opt->commit_format != CMIT_FMT_ONELINE &&917!commit_format_is_empty(opt->commit_format)) {918/*919* When showing a verbose header (i.e. log message),
920* and not in --pretty=oneline format, we would want
921* an extra newline between the end of log and the
922* diff/diffstat output for readability.
923*/
924int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;925if (opt->diffopt.output_prefix) {926struct strbuf *msg = NULL;927msg = opt->diffopt.output_prefix(&opt->diffopt,928opt->diffopt.output_prefix_data);929fwrite(msg->buf, msg->len, 1, opt->diffopt.file);930}931
932/*933* We may have shown three-dashes line early
934* between generated commentary (notes, etc.)
935* and the log message, in which case we only
936* want a blank line after the commentary
937* without (an extra) three-dashes line.
938* Otherwise, we show the three-dashes line if
939* we are showing the patch with diffstat, but
940* in that case, there is no extra blank line
941* after the three-dashes line.
942*/
943if (!opt->shown_dashes &&944(pch & opt->diffopt.output_format) == pch)945fprintf(opt->diffopt.file, "---");946putc('\n', opt->diffopt.file);947}948}949diff_flush(&opt->diffopt);950return 1;951}
952
953static int do_diff_combined(struct rev_info *opt, struct commit *commit)954{
955diff_tree_combined_merge(commit, opt);956return !opt->loginfo;957}
958
959static void setup_additional_headers(struct diff_options *o,960struct strmap *all_headers)961{
962struct hashmap_iter iter;963struct strmap_entry *entry;964
965/*966* Make o->additional_path_headers contain the subset of all_headers
967* that match o->pathspec. If there aren't any that match o->pathspec,
968* then make o->additional_path_headers be NULL.
969*/
970
971if (!o->pathspec.nr) {972o->additional_path_headers = all_headers;973return;974}975
976o->additional_path_headers = xmalloc(sizeof(struct strmap));977strmap_init_with_options(o->additional_path_headers, NULL, 0);978strmap_for_each_entry(all_headers, &iter, entry) {979if (match_pathspec(the_repository->index, &o->pathspec,980entry->key, strlen(entry->key),9810 /* prefix */, NULL /* seen */,9820 /* is_dir */))983strmap_put(o->additional_path_headers,984entry->key, entry->value);985}986if (!strmap_get_size(o->additional_path_headers)) {987strmap_clear(o->additional_path_headers, 0);988FREE_AND_NULL(o->additional_path_headers);989}990}
991
992static void cleanup_additional_headers(struct diff_options *o)993{
994if (!o->pathspec.nr) {995o->additional_path_headers = NULL;996return;997}998if (!o->additional_path_headers)999return;1000
1001strmap_clear(o->additional_path_headers, 0);1002FREE_AND_NULL(o->additional_path_headers);1003}
1004
1005static int do_remerge_diff(struct rev_info *opt,1006struct commit_list *parents,1007struct object_id *oid)1008{
1009struct merge_options o;1010struct commit_list *bases = NULL;1011struct merge_result res = {0};1012struct pretty_print_context ctx = {0};1013struct commit *parent1 = parents->item;1014struct commit *parent2 = parents->next->item;1015struct strbuf parent1_desc = STRBUF_INIT;1016struct strbuf parent2_desc = STRBUF_INIT;1017
1018/* Setup merge options */1019init_ui_merge_options(&o, the_repository);1020o.show_rename_progress = 0;1021o.record_conflict_msgs_as_headers = 1;1022o.msg_header_prefix = "remerge";1023
1024ctx.abbrev = DEFAULT_ABBREV;1025repo_format_commit_message(the_repository, parent1, "%h (%s)",1026&parent1_desc, &ctx);1027repo_format_commit_message(the_repository, parent2, "%h (%s)",1028&parent2_desc, &ctx);1029o.branch1 = parent1_desc.buf;1030o.branch2 = parent2_desc.buf;1031
1032/* Parse the relevant commits and get the merge bases */1033parse_commit_or_die(parent1);1034parse_commit_or_die(parent2);1035if (repo_get_merge_bases(the_repository, parent1, parent2, &bases) < 0)1036exit(128);1037
1038/* Re-merge the parents */1039merge_incore_recursive(&o, bases, parent1, parent2, &res);1040
1041/* Show the diff */1042setup_additional_headers(&opt->diffopt, res.path_messages);1043diff_tree_oid(&res.tree->object.oid, oid, "", &opt->diffopt);1044log_tree_diff_flush(opt);1045
1046/* Cleanup */1047free_commit_list(bases);1048cleanup_additional_headers(&opt->diffopt);1049strbuf_release(&parent1_desc);1050strbuf_release(&parent2_desc);1051merge_finalize(&o, &res);1052
1053/* Clean up the contents of the temporary object directory */1054if (opt->remerge_objdir)1055tmp_objdir_discard_objects(opt->remerge_objdir);1056else1057BUG("did a remerge diff without remerge_objdir?!?");1058
1059return !opt->loginfo;1060}
1061
1062/*
1063* Show the diff of a commit.
1064*
1065* Return true if we printed any log info messages
1066*/
1067static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)1068{
1069int showed_log;1070struct commit_list *parents;1071struct object_id *oid;1072int is_merge;1073int all_need_diff = opt->diff || opt->diffopt.flags.exit_with_status;1074
1075if (!all_need_diff && !opt->merges_need_diff)1076return 0;1077
1078parse_commit_or_die(commit);1079oid = get_commit_tree_oid(commit);1080
1081parents = get_saved_parents(opt, commit);1082is_merge = parents && parents->next;1083if (!is_merge && !all_need_diff)1084return 0;1085
1086/* Root commit? */1087if (!parents) {1088if (opt->show_root_diff) {1089diff_root_tree_oid(oid, "", &opt->diffopt);1090log_tree_diff_flush(opt);1091}1092return !opt->loginfo;1093}1094
1095if (is_merge) {1096int octopus = (parents->next->next != NULL);1097
1098if (opt->remerge_diff) {1099if (octopus) {1100show_log(opt);1101fprintf(opt->diffopt.file,1102"diff: warning: Skipping remerge-diff "1103"for octopus merges.\n");1104return 1;1105}1106return do_remerge_diff(opt, parents, oid);1107}1108if (opt->combine_merges)1109return do_diff_combined(opt, commit);1110if (opt->separate_merges) {1111if (!opt->first_parent_merges) {1112/* Show parent info for multiple diffs */1113log->parent = parents->item;1114}1115} else1116return 0;1117}1118
1119showed_log = 0;1120for (;;) {1121struct commit *parent = parents->item;1122
1123parse_commit_or_die(parent);1124diff_tree_oid(get_commit_tree_oid(parent),1125oid, "", &opt->diffopt);1126log_tree_diff_flush(opt);1127
1128showed_log |= !opt->loginfo;1129
1130/* Set up the log info for the next parent, if any.. */1131parents = parents->next;1132if (!parents || opt->first_parent_merges)1133break;1134log->parent = parents->item;1135opt->loginfo = log;1136}1137return showed_log;1138}
1139
1140int log_tree_commit(struct rev_info *opt, struct commit *commit)1141{
1142struct log_info log;1143int shown;1144/* maybe called by e.g. cmd_log_walk(), maybe stand-alone */1145int no_free = opt->diffopt.no_free;1146
1147log.commit = commit;1148log.parent = NULL;1149opt->loginfo = &log;1150opt->diffopt.no_free = 1;1151
1152/* NEEDSWORK: no restoring of no_free? Why? */1153if (opt->line_level_traverse)1154return line_log_print(opt, commit);1155
1156if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)1157fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);1158shown = log_tree_diff(opt, commit, &log);1159if (!shown && opt->loginfo && opt->always_show_header) {1160log.parent = NULL;1161show_log(opt);1162shown = 1;1163}1164if (opt->track_linear && !opt->linear && opt->reverse_output_stage)1165fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);1166if (shown)1167show_diff_of_diff(opt);1168opt->loginfo = NULL;1169maybe_flush_or_die(opt->diffopt.file, "stdout");1170opt->diffopt.no_free = no_free;1171
1172diff_free(&opt->diffopt);1173return shown;1174}
1175