blob: fca29d4799da27164394a8c59da067a0522241fe [file] [log] [blame]
Junio C Hamano5f1c3f02006-04-09 01:11:11 -07001#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Junio C Hamano5f1c3f02006-04-09 01:11:11 -07003#include "diff.h"
4#include "commit.h"
René Scharfecab4feb2008-09-04 23:39:21 +02005#include "tag.h"
Adam Simpkins7fefda52008-05-04 03:36:54 -07006#include "graph.h"
Junio C Hamano5f1c3f02006-04-09 01:11:11 -07007#include "log-tree.h"
Johannes Schindelin8860fd42007-01-11 11:47:48 +01008#include "reflog-walk.h"
René Scharfecab4feb2008-09-04 23:39:21 +02009#include "refs.h"
Thomas Rastb079c502009-02-19 22:26:31 +010010#include "string-list.h"
Nazri Ramliy67a4b582010-06-19 09:37:35 +080011#include "color.h"
Junio C Hamano0c37f1f2011-10-18 15:53:23 -070012#include "gpg-interface.h"
Brandon Casey959a2622013-02-12 02:17:39 -080013#include "sequencer.h"
Thomas Rast12da1d12013-03-28 17:47:32 +010014#include "line-log.h"
Junio C Hamano5f1c3f02006-04-09 01:11:11 -070015
Jeff King2608c242014-08-26 06:23:54 -040016static struct decoration name_decoration = { "object names" };
Junio C Hamano76c61fb2015-05-13 10:25:18 -070017static int decoration_loaded;
18static int decoration_flags;
Nazri Ramliya7524122010-06-19 09:37:34 +080019
Nazri Ramliy67a4b582010-06-19 09:37:35 +080020static 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 */
Nguyễn Thái Ngọc Duy76f5df32011-08-18 19:29:37 +070027 GIT_COLOR_BOLD_BLUE, /* GRAFTED */
Nazri Ramliy67a4b582010-06-19 09:37:35 +080028};
29
30static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
31{
Jeff Kingdaa0c3d2011-08-17 22:04:23 -070032 if (want_color(decorate_use_color))
Nazri Ramliy67a4b582010-06-19 09:37:35 +080033 return decoration_colors[ix];
34 return "";
35}
36
Nazri Ramliy5e11bee2010-06-24 08:21:16 +080037static 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
Jonathan Nieder88521172014-10-07 15:16:57 -040062int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
Nazri Ramliy5e11bee2010-06-24 08:21:16 +080063{
Jonathan Nieder88521172014-10-07 15:16:57 -040064 int slot = parse_decorate_color_slot(slot_name);
Nazri Ramliy5e11bee2010-06-24 08:21:16 +080065 if (slot < 0)
66 return 0;
67 if (!value)
68 return config_error_nonbool(var);
Jeff Kingf6c5a292014-10-07 15:33:09 -040069 return color_parse(value, decoration_colors[slot]);
Nazri Ramliy5e11bee2010-06-24 08:21:16 +080070}
71
Nazri Ramliy67a4b582010-06-19 09:37:35 +080072/*
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) \
Jeff Kingf1c96262011-08-17 22:03:12 -070077 decorate_get_color((o)->use_color, ix)
Nazri Ramliy67a4b582010-06-19 09:37:35 +080078
Jeff King662174d2014-08-26 06:23:36 -040079void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
René Scharfecab4feb2008-09-04 23:39:21 +020080{
Jeff King96ffc062016-02-22 17:44:32 -050081 struct name_decoration *res;
82 FLEX_ALLOC_STR(res, name, name);
Nazri Ramliya7524122010-06-19 09:37:34 +080083 res->type = type;
René Scharfecab4feb2008-09-04 23:39:21 +020084 res->next = add_decoration(&name_decoration, obj, res);
85}
86
Jeff King2608c242014-08-26 06:23:54 -040087const struct name_decoration *get_name_decoration(const struct object *obj)
88{
89 return lookup_decoration(&name_decoration, obj);
90}
91
Michael Haggertyf124b732015-05-25 18:38:57 +000092static int add_ref_decoration(const char *refname, const struct object_id *oid,
93 int flags, void *cb_data)
René Scharfecab4feb2008-09-04 23:39:21 +020094{
Nguyễn Thái Ngọc Duy5267d292011-08-19 19:43:50 +070095 struct object *obj;
Nazri Ramliya7524122010-06-19 09:37:34 +080096 enum decoration_type type = DECORATION_NONE;
Rafael Ascensão65516f52017-11-21 21:33:41 +000097 struct decoration_filter *filter = (struct decoration_filter *)cb_data;
Nguyễn Thái Ngọc Duy5267d292011-08-19 19:43:50 +070098
Rafael Ascensão65516f52017-11-21 21:33:41 +000099 if (filter && !ref_filter_match(refname,
100 filter->include_ref_pattern,
101 filter->exclude_ref_pattern))
102 return 0;
Junio C Hamano429ad202015-05-13 12:40:35 -0700103
Mike Hommey58d121b2015-06-12 06:34:59 +0900104 if (starts_with(refname, git_replace_ref_base)) {
Michael Haggerty3d79f652015-05-25 18:38:58 +0000105 struct object_id original_oid;
Michael Haggertyafc711b2014-02-18 12:24:55 +0100106 if (!check_replace_refs)
Michael J Gruberb9ad5002011-08-25 17:09:30 +0200107 return 0;
Junio C Hamano31a0ad52015-08-03 11:01:10 -0700108 if (get_oid_hex(refname + strlen(git_replace_ref_base),
109 &original_oid)) {
Nguyễn Thái Ngọc Duy5267d292011-08-19 19:43:50 +0700110 warning("invalid replace ref %s", refname);
111 return 0;
112 }
brian m. carlsonc251c832017-05-06 22:10:38 +0000113 obj = parse_object(&original_oid);
Nguyễn Thái Ngọc Duy5267d292011-08-19 19:43:50 +0700114 if (obj)
115 add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
116 return 0;
117 }
118
brian m. carlsonc251c832017-05-06 22:10:38 +0000119 obj = parse_object(oid);
René Scharfecab4feb2008-09-04 23:39:21 +0200120 if (!obj)
121 return 0;
Nazri Ramliya7524122010-06-19 09:37:34 +0800122
Christian Couder59556542013-11-30 21:55:40 +0100123 if (starts_with(refname, "refs/heads/"))
Nazri Ramliya7524122010-06-19 09:37:34 +0800124 type = DECORATION_REF_LOCAL;
Christian Couder59556542013-11-30 21:55:40 +0100125 else if (starts_with(refname, "refs/remotes/"))
Nazri Ramliya7524122010-06-19 09:37:34 +0800126 type = DECORATION_REF_REMOTE;
Christian Couder59556542013-11-30 21:55:40 +0100127 else if (starts_with(refname, "refs/tags/"))
Nazri Ramliya7524122010-06-19 09:37:34 +0800128 type = DECORATION_REF_TAG;
Nguyễn Thái Ngọc Duy97ba6422012-01-05 19:39:40 +0700129 else if (!strcmp(refname, "refs/stash"))
Nazri Ramliya7524122010-06-19 09:37:34 +0800130 type = DECORATION_REF_STASH;
Nguyễn Thái Ngọc Duy97ba6422012-01-05 19:39:40 +0700131 else if (!strcmp(refname, "HEAD"))
Nazri Ramliya7524122010-06-19 09:37:34 +0800132 type = DECORATION_REF_HEAD;
133
Nazri Ramliya7524122010-06-19 09:37:34 +0800134 add_name_decoration(type, refname, obj);
René Scharfecab4feb2008-09-04 23:39:21 +0200135 while (obj->type == OBJ_TAG) {
136 obj = ((struct tag *)obj)->tagged;
137 if (!obj)
138 break;
brian m. carlson5e1361c2013-12-17 04:28:21 +0000139 if (!obj->parsed)
brian m. carlsonc251c832017-05-06 22:10:38 +0000140 parse_object(&obj->oid);
Nazri Ramliya7524122010-06-19 09:37:34 +0800141 add_name_decoration(DECORATION_REF_TAG, refname, obj);
René Scharfecab4feb2008-09-04 23:39:21 +0200142 }
143 return 0;
144}
145
Nguyễn Thái Ngọc Duy76f5df32011-08-18 19:29:37 +0700146static int add_graft_decoration(const struct commit_graft *graft, void *cb_data)
147{
brian m. carlsonbc832662017-05-06 22:10:10 +0000148 struct commit *commit = lookup_commit(&graft->oid);
Nguyễn Thái Ngọc Duy76f5df32011-08-18 19:29:37 +0700149 if (!commit)
150 return 0;
151 add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
152 return 0;
153}
154
Rafael Ascensão65516f52017-11-21 21:33:41 +0000155void load_ref_decorations(struct decoration_filter *filter, int flags)
René Scharfecab4feb2008-09-04 23:39:21 +0200156{
Junio C Hamano76c61fb2015-05-13 10:25:18 -0700157 if (!decoration_loaded) {
Rafael Ascensão65516f52017-11-21 21:33:41 +0000158 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 }
Junio C Hamano76c61fb2015-05-13 10:25:18 -0700167 decoration_loaded = 1;
168 decoration_flags = flags;
Rafael Ascensão65516f52017-11-21 21:33:41 +0000169 for_each_ref(add_ref_decoration, filter);
170 head_ref(add_ref_decoration, filter);
171 for_each_commit_graft(add_graft_decoration, filter);
René Scharfecab4feb2008-09-04 23:39:21 +0200172 }
173}
174
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200175static void show_parents(struct commit *commit, int abbrev, FILE *file)
Linus Torvaldsc8c893c2006-05-03 07:59:00 -0700176{
177 struct commit_list *p;
178 for (p = commit->parents; p ; p = p->next) {
179 struct commit *parent = p->item;
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200180 fprintf(file, " %s", find_unique_abbrev(parent->object.oid.hash, abbrev));
Linus Torvaldsc8c893c2006-05-03 07:59:00 -0700181 }
182}
183
Jay Soffian91b849b2011-10-04 10:02:03 -0400184static 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) {
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200188 fprintf(opt->diffopt.file, " %s", find_unique_abbrev(p->item->object.oid.hash, abbrev));
Jay Soffian91b849b2011-10-04 10:02:03 -0400189 }
190}
191
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000192/*
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100193 * 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;
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100200 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 */
René Scharfe744c0402017-09-23 11:45:04 +0200212 branch_name = resolve_ref_unsafe("HEAD", 0, NULL, &rru_flags);
Jeff Kingd79be492017-10-19 13:49:01 -0400213 if (!branch_name || !(rru_flags & REF_ISSYMREF))
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100214 return NULL;
Junio C Hamano76c61fb2015-05-13 10:25:18 -0700215
Junio C Hamano429ad202015-05-13 12:40:35 -0700216 if (!starts_with(branch_name, "refs/"))
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100217 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
Junio C Hamano429ad202015-05-13 12:40:35 -0700229static 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
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100237/*
Harry Jeffery92710952014-09-18 21:53:53 +0100238 * The caller makes sure there is no funny color before calling.
239 * format_decorations_extended makes sure the same after return.
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000240 */
Harry Jeffery92710952014-09-18 21:53:53 +0100241void format_decorations_extended(struct strbuf *sb,
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000242 const struct commit *commit,
Harry Jeffery92710952014-09-18 21:53:53 +0100243 int use_color,
244 const char *prefix,
245 const char *separator,
246 const char *suffix)
Linus Torvaldsca135e72007-04-16 16:05:10 -0700247{
Jeff King2608c242014-08-26 06:23:54 -0400248 const struct name_decoration *decoration;
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100249 const struct name_decoration *current_and_HEAD;
Nazri Ramliy67a4b582010-06-19 09:37:35 +0800250 const char *color_commit =
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000251 diff_get_color(use_color, DIFF_COMMIT);
Nazri Ramliy67a4b582010-06-19 09:37:35 +0800252 const char *color_reset =
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000253 decorate_get_color(use_color, DECORATION_NONE);
Linus Torvaldsca135e72007-04-16 16:05:10 -0700254
Jeff King2608c242014-08-26 06:23:54 -0400255 decoration = get_name_decoration(&commit->object);
Linus Torvaldsca135e72007-04-16 16:05:10 -0700256 if (!decoration)
257 return;
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100258
259 current_and_HEAD = current_pointed_by_HEAD(decoration);
Linus Torvaldsca135e72007-04-16 16:05:10 -0700260 while (decoration) {
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100261 /*
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
Junio C Hamano429ad202015-05-13 12:40:35 -0700274 show_name(sb, decoration);
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100275
276 if (current_and_HEAD &&
277 decoration->type == DECORATION_REF_HEAD) {
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100278 strbuf_addstr(sb, " -> ");
279 strbuf_addstr(sb, color_reset);
280 strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
Junio C Hamano429ad202015-05-13 12:40:35 -0700281 show_name(sb, current_and_HEAD);
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100282 }
283 strbuf_addstr(sb, color_reset);
284
285 prefix = separator;
286 }
Linus Torvaldsca135e72007-04-16 16:05:10 -0700287 decoration = decoration->next;
288 }
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000289 strbuf_addstr(sb, color_commit);
Harry Jeffery92710952014-09-18 21:53:53 +0100290 strbuf_addstr(sb, suffix);
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000291 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)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200299 fprintf(opt->diffopt.file, "\t%s", (char *) commit->util);
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000300 if (!opt->show_decorations)
301 return;
302 format_decorations(&sb, commit, opt->diffopt.use_color);
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200303 fputs(sb.buf, opt->diffopt.file);
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000304 strbuf_release(&sb);
Linus Torvaldsca135e72007-04-16 16:05:10 -0700305}
306
Johannes Schindeline00de242007-02-09 01:43:54 +0100307static 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
Junio C Hamanod28b5d42012-12-21 22:06:01 -0800317void fmt_output_subject(struct strbuf *filename,
318 const char *subject,
Junio C Hamano021f2f42012-12-21 23:26:16 -0800319 struct rev_info *info)
Stephen Boyd6fa8e622009-03-22 19:14:04 -0700320{
Junio C Hamano021f2f42012-12-21 23:26:16 -0800321 const char *suffix = info->patch_suffix;
322 int nr = info->nr;
Junio C Hamanod28b5d42012-12-21 22:06:01 -0800323 int start_len = filename->len;
324 int max_len = start_len + FORMAT_PATCH_NAME_MAX - (strlen(suffix) + 1);
Stephen Boyd6fa8e622009-03-22 19:14:04 -0700325
Junio C Hamano5fe10fe2012-12-22 00:21:23 -0800326 if (0 < info->reroll_count)
327 strbuf_addf(filename, "v%d-", info->reroll_count);
Junio C Hamanod28b5d42012-12-21 22:06:01 -0800328 strbuf_addf(filename, "%04d-%s", nr, subject);
Christian Couderb09b8682009-03-27 01:13:01 +0100329
Junio C Hamanod28b5d42012-12-21 22:06:01 -0800330 if (max_len < filename->len)
331 strbuf_setlen(filename, max_len);
332 strbuf_addstr(filename, suffix);
333}
Jeff Kinga21c2f92012-05-21 19:10:32 -0400334
Junio C Hamanod28b5d42012-12-21 22:06:01 -0800335void 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);
Stephen Boyd6fa8e622009-03-22 19:14:04 -0700345}
346
René Scharfe8ffc8dc2017-03-01 12:36:38 +0100347void 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
Stephen Boyd108dab22009-03-22 19:14:05 -0700363void log_write_email_headers(struct rev_info *opt, struct commit *commit,
Junio C Hamano267123b2008-03-15 00:09:20 -0700364 const char **extra_headers_p,
365 int *need_8bit_cte_p)
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500366{
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500367 const char *extra_headers = opt->extra_headers;
brian m. carlson3a30aa12015-12-15 01:52:04 +0000368 const char *name = oid_to_hex(opt->zero_commit ?
369 &null_oid : &commit->object.oid);
Junio C Hamano267123b2008-03-15 00:09:20 -0700370
371 *need_8bit_cte_p = 0; /* unknown */
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500372
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200373 fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700374 graph_show_oneline(opt->graph);
375 if (opt->message_id) {
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200376 fprintf(opt->diffopt.file, "Message-Id: <%s>\n", opt->message_id);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700377 graph_show_oneline(opt->graph);
378 }
Thomas Rastb079c502009-02-19 22:26:31 +0100379 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
380 int i, n;
381 n = opt->ref_message_ids->nr;
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200382 fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
Thomas Rastb079c502009-02-19 22:26:31 +0100383 for (i = 0; i < n; i++)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200384 fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
Thomas Rastb079c502009-02-19 22:26:31 +0100385 opt->ref_message_ids->items[i].string);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700386 graph_show_oneline(opt->graph);
387 }
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500388 if (opt->mime_boundary) {
389 static char subject_buffer[1024];
390 static char buffer[1024];
Stephen Boyd108dab22009-03-22 19:14:05 -0700391 struct strbuf filename = STRBUF_INIT;
Junio C Hamano267123b2008-03-15 00:09:20 -0700392 *need_8bit_cte_p = -1; /* NEVER */
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500393 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
Junio C Hamano38ec23a2012-12-21 21:39:37 -0800410 if (opt->numbered_files)
411 strbuf_addf(&filename, "%d", opt->nr);
412 else
Junio C Hamanod28b5d42012-12-21 22:06:01 -0800413 fmt_output_commit(&filename, commit, opt);
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500414 snprintf(buffer, sizeof(buffer) - 1,
Kevin Ballard6b2fbaa2008-07-29 22:49:33 -0700415 "\n--%s%s\n"
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500416 "Content-Type: text/x-patch;"
Stephen Boyd108dab22009-03-22 19:14:05 -0700417 " name=\"%s\"\n"
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500418 "Content-Transfer-Encoding: 8bit\n"
419 "Content-Disposition: %s;"
Stephen Boyd108dab22009-03-22 19:14:05 -0700420 " filename=\"%s\"\n\n",
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500421 mime_boundary_leader, opt->mime_boundary,
Stephen Boyd108dab22009-03-22 19:14:05 -0700422 filename.buf,
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500423 opt->no_inline ? "attachment" : "inline",
Stephen Boyd108dab22009-03-22 19:14:05 -0700424 filename.buf);
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500425 opt->diffopt.stat_sep = buffer;
Stephen Boyd108dab22009-03-22 19:14:05 -0700426 strbuf_release(&filename);
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500427 }
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500428 *extra_headers_p = extra_headers;
429}
430
Junio C Hamanoc6b3ec42012-01-04 13:48:45 -0800431static 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');
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200440 fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
Junio C Hamanoc6b3ec42012-01-04 13:48:45 -0800441 *eol ? "\n" : "");
Zoltan Klingercf3983d2014-07-09 12:10:21 +1000442 graph_show_oneline(opt->graph);
Junio C Hamanoc6b3ec42012-01-04 13:48:45 -0800443 bol = (*eol) ? (eol + 1) : eol;
444 }
445}
446
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700447static 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;
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700453
Jeff King218aa3a2014-06-13 02:32:11 -0400454 if (parse_signed_commit(commit, &payload, &signature) <= 0)
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700455 goto out;
456
457 status = verify_signed_buffer(payload.buf, payload.len,
458 signature.buf, signature.len,
Michael J Gruber9cc4ac82013-02-14 17:04:44 +0100459 &gpg_output, NULL);
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700460 if (status && !gpg_output.len)
461 strbuf_addstr(&gpg_output, "No signature\n");
462
Junio C Hamanoc6b3ec42012-01-04 13:48:45 -0800463 show_sig_lines(opt, status, gpg_output.buf);
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700464
465 out:
466 strbuf_release(&gpg_output);
467 strbuf_release(&payload);
468 strbuf_release(&signature);
469}
470
brian m. carlsona92ea682017-05-06 22:10:18 +0000471static int which_parent(const struct object_id *oid, const struct commit *commit)
Junio C Hamano824958e2012-01-04 13:51:28 -0800472{
473 int nth;
474 const struct commit_list *parent;
475
476 for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
brian m. carlsona92ea682017-05-06 22:10:18 +0000477 if (!oidcmp(&parent->item->object.oid, oid))
Junio C Hamano824958e2012-01-04 13:51:28 -0800478 return nth;
479 nth++;
480 }
481 return -1;
482}
483
Junio C Hamanod041ffa2012-01-04 16:23:12 -0800484static int is_common_merge(const struct commit *commit)
485{
486 return (commit->parents
487 && commit->parents->next
488 && !commit->parents->next->next);
489}
490
Christian Couder063da622014-07-07 08:35:37 +0200491static void show_one_mergetag(struct commit *commit,
Junio C Hamano824958e2012-01-04 13:51:28 -0800492 struct commit_extra_header *extra,
Christian Couder063da622014-07-07 08:35:37 +0200493 void *data)
Junio C Hamano824958e2012-01-04 13:51:28 -0800494{
Christian Couder063da622014-07-07 08:35:37 +0200495 struct rev_info *opt = (struct rev_info *)data;
brian m. carlsona92ea682017-05-06 22:10:18 +0000496 struct object_id oid;
Junio C Hamano824958e2012-01-04 13:51:28 -0800497 struct tag *tag;
498 struct strbuf verify_message;
499 int status, nth;
500 size_t payload_size, gpg_message_offset;
501
brian m. carlsona92ea682017-05-06 22:10:18 +0000502 hash_sha1_file(extra->value, extra->len, typename(OBJ_TAG), oid.hash);
brian m. carlsond3101b52017-05-06 22:10:19 +0000503 tag = lookup_tag(&oid);
Junio C Hamano824958e2012-01-04 13:51:28 -0800504 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");
Junio C Hamanod041ffa2012-01-04 16:23:12 -0800510 else if (is_common_merge(commit) &&
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000511 !oidcmp(&tag->tagged->oid,
512 &commit->parents->next->item->object.oid))
Junio C Hamanod041ffa2012-01-04 16:23:12 -0800513 strbuf_addf(&verify_message,
514 "merged tag '%s'\n", tag->tag);
brian m. carlsona92ea682017-05-06 22:10:18 +0000515 else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0)
Junio C Hamano824958e2012-01-04 13:51:28 -0800516 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
brian m. carlsoned1c9972015-11-10 02:22:29 +0000517 tag->tag, tag->tagged->oid.hash);
Junio C Hamano824958e2012-01-04 13:51:28 -0800518 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);
Michael J Gruber13150932013-02-14 17:04:43 +0100524 status = -1;
Michael J Gruber42c55ce2014-06-27 15:18:36 +0200525 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 }
Junio C Hamano824958e2012-01-04 13:51:28 -0800536
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{
Christian Couder063da622014-07-07 08:35:37 +0200543 for_each_mergetag(show_one_mergetag, commit, opt);
Junio C Hamano824958e2012-01-04 13:51:28 -0800544}
545
Adam Simpkins02865652008-04-29 01:32:59 -0700546void show_log(struct rev_info *opt)
Linus Torvalds91539832006-04-17 11:59:32 -0700547{
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500548 struct strbuf msgbuf = STRBUF_INIT;
Timo Hirvonen39bc9a62006-06-25 13:54:14 +0300549 struct log_info *log = opt->loginfo;
Linus Torvalds91539832006-04-17 11:59:32 -0700550 struct commit *commit = log->commit, *parent = log->parent;
brian m. carlsona92ea682017-05-06 22:10:18 +0000551 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : GIT_SHA1_HEXSZ;
Thomas Rastdd2e7942009-10-19 17:48:08 +0200552 const char *extra_headers = opt->extra_headers;
553 struct pretty_print_context ctx = {0};
Linus Torvalds91539832006-04-17 11:59:32 -0700554
555 opt->loginfo = NULL;
556 if (!opt->verbose_header) {
Adam Simpkins7fefda52008-05-04 03:36:54 -0700557 graph_show_commit(opt->graph);
558
Michael J Gruber1df2d652011-03-07 13:31:39 +0100559 if (!opt->graph)
Michael J Gruberb1b47552011-03-10 15:45:03 +0100560 put_revision_mark(opt, commit);
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200561 fputs(find_unique_abbrev(commit->object.oid.hash, abbrev_commit), opt->diffopt.file);
Adam Simpkins885cf802008-05-04 03:36:52 -0700562 if (opt->print_parents)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200563 show_parents(commit, abbrev_commit, opt->diffopt.file);
Jay Soffian91b849b2011-10-04 10:02:03 -0400564 if (opt->children.name)
565 show_children(opt, commit, abbrev_commit);
Linus Torvalds0f3a2902008-10-27 12:51:59 -0700566 show_decorations(opt, commit);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700567 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200568 putc('\n', opt->diffopt.file);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700569 graph_show_remainder(opt->graph);
570 }
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200571 putc(opt->diffopt.line_termination, opt->diffopt.file);
Linus Torvalds91539832006-04-17 11:59:32 -0700572 return;
573 }
574
575 /*
Jeff King87152272009-07-01 03:26:28 -0400576 * If use_terminator is set, we already handled any record termination
577 * at the end of the last record.
Adam Simpkins02865652008-04-29 01:32:59 -0700578 * Otherwise, add a diffopt.line_termination character before all
579 * entries but the first. (IOW, as a separator between entries)
Linus Torvalds91539832006-04-17 11:59:32 -0700580 */
Adam Simpkins7fefda52008-05-04 03:36:54 -0700581 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);
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200597 putc(opt->diffopt.line_termination, opt->diffopt.file);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700598 }
Linus Torvalds91539832006-04-17 11:59:32 -0700599 opt->shown_one = 1;
600
601 /*
Adam Simpkins7fefda52008-05-04 03:36:54 -0700602 * 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 /*
Linus Torvalds91539832006-04-17 11:59:32 -0700608 * Print header line of header..
609 */
Junio C Hamano3eefc182006-04-18 16:45:27 -0700610
Eric Wong9f23e042016-06-05 04:46:39 +0000611 if (cmit_fmt_is_mail(opt->commit_format)) {
René Scharfe6d167fd2017-03-01 12:37:07 +0100612 log_write_email_headers(opt, commit, &extra_headers,
Thomas Rastdd2e7942009-10-19 17:48:08 +0200613 &ctx.need_8bit_cte);
René Scharfe6d167fd2017-03-01 12:37:07 +0100614 ctx.rev = opt;
615 ctx.print_email_subject = 1;
Johannes Schindeline52a5de2007-02-23 01:35:03 +0100616 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200617 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
Junio C Hamano74bd9022006-12-16 15:31:25 -0800618 if (opt->commit_format != CMIT_FMT_ONELINE)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200619 fputs("commit ", opt->diffopt.file);
Adam Simpkins7528f272008-05-25 00:07:21 -0700620
Michael J Gruber1df2d652011-03-07 13:31:39 +0100621 if (!opt->graph)
Michael J Gruberb1b47552011-03-10 15:45:03 +0100622 put_revision_mark(opt, commit);
brian m. carlsoned1c9972015-11-10 02:22:29 +0000623 fputs(find_unique_abbrev(commit->object.oid.hash, abbrev_commit),
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200624 opt->diffopt.file);
Adam Simpkins885cf802008-05-04 03:36:52 -0700625 if (opt->print_parents)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200626 show_parents(commit, abbrev_commit, opt->diffopt.file);
Jay Soffian91b849b2011-10-04 10:02:03 -0400627 if (opt->children.name)
628 show_children(opt, commit, abbrev_commit);
Junio C Hamano73f0a152006-05-24 12:19:47 -0700629 if (parent)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200630 fprintf(opt->diffopt.file, " (from %s)",
brian m. carlsoned1c9972015-11-10 02:22:29 +0000631 find_unique_abbrev(parent->object.oid.hash,
Junio C Hamano3eefc182006-04-18 16:45:27 -0700632 abbrev_commit));
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200633 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
Linus Torvalds0f3a2902008-10-27 12:51:59 -0700634 show_decorations(opt, commit);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700635 if (opt->commit_format == CMIT_FMT_ONELINE) {
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200636 putc(' ', opt->diffopt.file);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700637 } else {
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200638 putc('\n', opt->diffopt.file);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700639 graph_show_oneline(opt->graph);
640 }
Nicolas Pitre903b45f2007-01-27 22:40:36 -0500641 if (opt->reflog_info) {
Adam Simpkins7fefda52008-05-04 03:36:54 -0700642 /*
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 */
Junio C Hamano4d12a472007-01-20 00:51:41 -0800648 show_reflog_message(opt->reflog_info,
Junio C Hamano55ccf852012-05-07 14:11:32 -0700649 opt->commit_format == CMIT_FMT_ONELINE,
Jeff Kinga5481a62015-06-25 12:55:02 -0400650 &opt->date_mode,
Junio C Hamano55ccf852012-05-07 14:11:32 -0700651 opt->date_mode_explicit);
Adam Simpkins02865652008-04-29 01:32:59 -0700652 if (opt->commit_format == CMIT_FMT_ONELINE)
Nicolas Pitre903b45f2007-01-27 22:40:36 -0500653 return;
Nicolas Pitre903b45f2007-01-27 22:40:36 -0500654 }
Junio C Hamano3eefc182006-04-18 16:45:27 -0700655 }
Linus Torvalds91539832006-04-17 11:59:32 -0700656
Junio C Hamano824958e2012-01-04 13:51:28 -0800657 if (opt->show_signature) {
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700658 show_signature(opt, commit);
Junio C Hamano824958e2012-01-04 13:51:28 -0800659 show_mergetag(opt, commit);
660 }
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700661
Jeff King8597ea32014-06-10 17:44:13 -0400662 if (!get_cached_commit_buffer(commit, NULL))
Linus Torvalds3131b712008-02-09 14:02:07 -0800663 return;
664
Junio C Hamanoddf333f2012-10-17 18:51:47 -0700665 if (opt->show_notes) {
666 int raw;
667 struct strbuf notebuf = STRBUF_INIT;
668
669 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
brian m. carlsonfb61e4d2017-05-30 10:30:41 -0700670 format_display_notes(&commit->object.oid, &notebuf,
Junio C Hamanoddf333f2012-10-17 18:51:47 -0700671 get_log_output_encoding(), raw);
672 ctx.notes_message = notebuf.len
673 ? strbuf_detach(&notebuf, NULL)
674 : xcalloc(1, 1);
675 }
676
Linus Torvalds91539832006-04-17 11:59:32 -0700677 /*
678 * And then the pretty-printed message itself
679 */
Nguyễn Thái Ngọc Duy5289c562013-02-12 02:17:38 -0800680 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")));
Thomas Rastdd2e7942009-10-19 17:48:08 +0200684 ctx.date_mode = opt->date_mode;
Jeff Kingf026c752012-05-04 01:25:18 -0400685 ctx.date_mode_explicit = opt->date_mode_explicit;
Thomas Rastdd2e7942009-10-19 17:48:08 +0200686 ctx.abbrev = opt->diffopt.abbrev;
687 ctx.after_subject = extra_headers;
Jeff King9553d2b2011-05-26 18:28:17 -0400688 ctx.preserve_subject = opt->preserve_subject;
Thomas Rast8f8f5472009-10-19 17:48:10 +0200689 ctx.reflog_info = opt->reflog_info;
Jeff King6bf13942011-05-26 18:27:49 -0400690 ctx.fmt = opt->commit_format;
Antoine Pelisse0e2913b2013-01-05 22:26:41 +0100691 ctx.mailmap = opt->mailmap;
Junio C Hamano30825172012-12-17 17:56:49 -0500692 ctx.color = opt->diffopt.use_color;
Linus Torvalds7cc13c72016-03-16 09:15:53 -0700693 ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
Alexey Shumkinecaee802013-06-26 14:19:50 +0400694 ctx.output_encoding = get_log_output_encoding();
Jeff Kinga9080472013-07-03 03:08:22 -0400695 if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
696 ctx.from_ident = &opt->from_ident;
Josef Kufner3ad87c82016-06-16 20:18:37 +0700697 if (opt->graph)
698 ctx.graph_width = graph_width(opt->graph);
Jeff King6bf13942011-05-26 18:27:49 -0400699 pretty_print_commit(&ctx, commit, &msgbuf);
Junio C Hamano212620f2012-10-17 20:48:25 -0700700
701 if (opt->add_signoff)
Nguyễn Thái Ngọc Duy5289c562013-02-12 02:17:38 -0800702 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
Junio C Hamano212620f2012-10-17 20:48:25 -0700703
Junio C Hamano5a664cf2012-10-17 19:02:46 -0700704 if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
Junio C Hamanobd1470b2012-10-17 21:27:22 -0700705 ctx.notes_message && *ctx.notes_message) {
Eric Wong9f23e042016-06-05 04:46:39 +0000706 if (cmit_fmt_is_mail(ctx.fmt)) {
Junio C Hamanobd1470b2012-10-17 21:27:22 -0700707 strbuf_addstr(&msgbuf, "---\n");
708 opt->shown_dashes = 1;
709 }
Junio C Hamano5a664cf2012-10-17 19:02:46 -0700710 strbuf_addstr(&msgbuf, ctx.notes_message);
Junio C Hamanobd1470b2012-10-17 21:27:22 -0700711 }
Junio C Hamanocf2251b2006-05-31 15:11:49 -0700712
Adam Simpkins7fefda52008-05-04 03:36:54 -0700713 if (opt->show_log_size) {
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200714 fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700715 graph_show_oneline(opt->graph);
716 }
Marco Costalba9fa34652007-07-20 20:15:13 +0200717
Adam Simpkins7fefda52008-05-04 03:36:54 -0700718 /*
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
Jacob Keller660e1132016-08-31 16:27:20 -0700727 graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf);
Jeff Kingb9c7d6e2014-07-29 13:56:48 -0400728 if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
Adam Simpkins7fefda52008-05-04 03:36:54 -0700729 if (!opt->missing_newline)
730 graph_show_padding(opt->graph);
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200731 putc(opt->diffopt.line_termination, opt->diffopt.file);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700732 }
733
Pierre Habouzit674d1722007-09-10 12:35:06 +0200734 strbuf_release(&msgbuf);
Junio C Hamanoddf333f2012-10-17 18:51:47 -0700735 free(ctx.notes_message);
Linus Torvalds91539832006-04-17 11:59:32 -0700736}
737
Linus Torvaldscd2bdc52006-04-14 16:52:13 -0700738int log_tree_diff_flush(struct rev_info *opt)
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700739{
Junio C Hamanobd1470b2012-10-17 21:27:22 -0700740 opt->shown_dashes = 0;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700741 diffcore_std(&opt->diffopt);
Linus Torvalds91539832006-04-17 11:59:32 -0700742
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700743 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 }
Linus Torvalds91539832006-04-17 11:59:32 -0700750
Junio C Hamano3969cf72006-06-27 15:08:19 -0700751 if (opt->loginfo && !opt->no_commit_id) {
Adam Simpkins02865652008-04-29 01:32:59 -0700752 show_log(opt);
Linus Torvalds304b5af2007-10-09 09:35:22 -0700753 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
754 opt->verbose_header &&
Jeff Kingb9c7d6e2014-07-29 13:56:48 -0400755 opt->commit_format != CMIT_FMT_ONELINE &&
756 !commit_format_is_empty(opt->commit_format)) {
Junio C Hamano1d34c502012-11-13 10:09:07 -0800757 /*
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 */
Junio C Hamano3969cf72006-06-27 15:08:19 -0700763 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
Bo Yang81bd1b22010-05-26 15:08:03 +0800764 if (opt->diffopt.output_prefix) {
765 struct strbuf *msg = NULL;
766 msg = opt->diffopt.output_prefix(&opt->diffopt,
767 opt->diffopt.output_prefix_data);
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200768 fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
Bo Yang81bd1b22010-05-26 15:08:03 +0800769 }
Junio C Hamano1d34c502012-11-13 10:09:07 -0800770
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)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200783 fprintf(opt->diffopt.file, "---");
784 putc('\n', opt->diffopt.file);
Junio C Hamano3969cf72006-06-27 15:08:19 -0700785 }
786 }
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700787 diff_flush(&opt->diffopt);
788 return 1;
789}
790
Linus Torvaldscd2bdc52006-04-14 16:52:13 -0700791static int do_diff_combined(struct rev_info *opt, struct commit *commit)
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700792{
René Scharfe82889292011-12-17 11:20:07 +0100793 diff_tree_combined_merge(commit, opt->dense_combined_merges, opt);
Linus Torvalds91539832006-04-17 11:59:32 -0700794 return !opt->loginfo;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700795}
796
Linus Torvalds91539832006-04-17 11:59:32 -0700797/*
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)
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700803{
Linus Torvalds91539832006-04-17 11:59:32 -0700804 int showed_log;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700805 struct commit_list *parents;
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000806 struct object_id *oid;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700807
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700808 if (!opt->diff && !opt->diffopt.flags.exit_with_status)
Linus Torvalds91539832006-04-17 11:59:32 -0700809 return 0;
810
Jeff King7059dcc2013-10-24 04:52:36 -0400811 parse_commit_or_die(commit);
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000812 oid = &commit->tree->object.oid;
Thomas Rastd1b9b762013-03-28 09:19:34 +0100813
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700814 /* Root commit? */
Thomas Rast53d00b32013-07-31 22:13:20 +0200815 parents = get_saved_parents(opt, commit);
Linus Torvalds91539832006-04-17 11:59:32 -0700816 if (!parents) {
Rene Scharfe2b603562006-10-26 18:52:39 +0200817 if (opt->show_root_diff) {
Brandon Williams7b8dea02017-05-30 10:30:57 -0700818 diff_root_tree_oid(oid, "", &opt->diffopt);
Rene Scharfe2b603562006-10-26 18:52:39 +0200819 log_tree_diff_flush(opt);
820 }
Linus Torvalds91539832006-04-17 11:59:32 -0700821 return !opt->loginfo;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700822 }
823
824 /* More than one parent? */
Linus Torvalds91539832006-04-17 11:59:32 -0700825 if (parents && parents->next) {
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700826 if (opt->ignore_merges)
827 return 0;
828 else if (opt->combine_merges)
829 return do_diff_combined(opt, commit);
Petr Baudis88d9d452010-02-10 02:11:49 +0100830 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 */
Jeff King7059dcc2013-10-24 04:52:36 -0400836 parse_commit_or_die(parents->item);
Brandon Williams66f414f2017-05-30 10:31:03 -0700837 diff_tree_oid(&parents->item->tree->object.oid,
838 oid, "", &opt->diffopt);
Petr Baudis88d9d452010-02-10 02:11:49 +0100839 log_tree_diff_flush(opt);
840 return !opt->loginfo;
841 }
Linus Torvalds91539832006-04-17 11:59:32 -0700842
843 /* If we show individual diffs, show the parent info */
844 log->parent = parents->item;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700845 }
846
Linus Torvalds91539832006-04-17 11:59:32 -0700847 showed_log = 0;
848 for (;;) {
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700849 struct commit *parent = parents->item;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700850
Jeff King7059dcc2013-10-24 04:52:36 -0400851 parse_commit_or_die(parent);
Brandon Williams66f414f2017-05-30 10:31:03 -0700852 diff_tree_oid(&parent->tree->object.oid,
853 oid, "", &opt->diffopt);
Linus Torvalds91539832006-04-17 11:59:32 -0700854 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;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700864 }
Linus Torvalds91539832006-04-17 11:59:32 -0700865 return showed_log;
866}
867
868int log_tree_commit(struct rev_info *opt, struct commit *commit)
869{
870 struct log_info log;
Johannes Schindelin6ea57702016-06-22 17:01:28 +0200871 int shown, close_file = opt->diffopt.close_file;
Linus Torvalds91539832006-04-17 11:59:32 -0700872
873 log.commit = commit;
874 log.parent = NULL;
875 opt->loginfo = &log;
Johannes Schindelin6ea57702016-06-22 17:01:28 +0200876 opt->diffopt.close_file = 0;
Linus Torvalds91539832006-04-17 11:59:32 -0700877
Thomas Rast12da1d12013-03-28 17:47:32 +0100878 if (opt->line_level_traverse)
879 return line_log_print(opt, commit);
880
Nguyễn Thái Ngọc Duy1b32dec2014-03-25 20:23:27 +0700881 if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200882 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
Junio C Hamano3eefc182006-04-18 16:45:27 -0700883 shown = log_tree_diff(opt, commit, &log);
884 if (!shown && opt->loginfo && opt->always_show_header) {
Linus Torvalds91539832006-04-17 11:59:32 -0700885 log.parent = NULL;
Adam Simpkins02865652008-04-29 01:32:59 -0700886 show_log(opt);
Junio C Hamano3eefc182006-04-18 16:45:27 -0700887 shown = 1;
Linus Torvalds91539832006-04-17 11:59:32 -0700888 }
Nguyễn Thái Ngọc Duy1b32dec2014-03-25 20:23:27 +0700889 if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200890 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
Linus Torvalds91539832006-04-17 11:59:32 -0700891 opt->loginfo = NULL;
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200892 maybe_flush_or_die(opt->diffopt.file, "stdout");
Johannes Schindelin6ea57702016-06-22 17:01:28 +0200893 if (close_file)
894 fclose(opt->diffopt.file);
Junio C Hamano3eefc182006-04-18 16:45:27 -0700895 return shown;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700896}