blob: 8c2415747a2606aefb29fd6e0092c86da3c72589 [file] [log] [blame]
Junio C Hamano5f1c3f02006-04-09 01:11:11 -07001#include "cache.h"
2#include "diff.h"
3#include "commit.h"
René Scharfecab4feb2008-09-04 23:39:21 +02004#include "tag.h"
Adam Simpkins7fefda52008-05-04 03:36:54 -07005#include "graph.h"
Junio C Hamano5f1c3f02006-04-09 01:11:11 -07006#include "log-tree.h"
Johannes Schindelin8860fd42007-01-11 11:47:48 +01007#include "reflog-walk.h"
René Scharfecab4feb2008-09-04 23:39:21 +02008#include "refs.h"
Thomas Rastb079c502009-02-19 22:26:31 +01009#include "string-list.h"
Nazri Ramliy67a4b582010-06-19 09:37:35 +080010#include "color.h"
Junio C Hamano0c37f1f2011-10-18 15:53:23 -070011#include "gpg-interface.h"
Brandon Casey959a2622013-02-12 02:17:39 -080012#include "sequencer.h"
Thomas Rast12da1d12013-03-28 17:47:32 +010013#include "line-log.h"
Junio C Hamano5f1c3f02006-04-09 01:11:11 -070014
Jeff King2608c242014-08-26 06:23:54 -040015static struct decoration name_decoration = { "object names" };
Junio C Hamano76c61fb2015-05-13 10:25:18 -070016static int decoration_loaded;
17static int decoration_flags;
Nazri Ramliya7524122010-06-19 09:37:34 +080018
Nazri Ramliy67a4b582010-06-19 09:37:35 +080019static char decoration_colors[][COLOR_MAXLEN] = {
20 GIT_COLOR_RESET,
21 GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */
22 GIT_COLOR_BOLD_RED, /* REF_REMOTE */
23 GIT_COLOR_BOLD_YELLOW, /* REF_TAG */
24 GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */
25 GIT_COLOR_BOLD_CYAN, /* REF_HEAD */
Nguyễn Thái Ngọc Duy76f5df32011-08-18 19:29:37 +070026 GIT_COLOR_BOLD_BLUE, /* GRAFTED */
Nazri Ramliy67a4b582010-06-19 09:37:35 +080027};
28
29static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
30{
Jeff Kingdaa0c3d2011-08-17 22:04:23 -070031 if (want_color(decorate_use_color))
Nazri Ramliy67a4b582010-06-19 09:37:35 +080032 return decoration_colors[ix];
33 return "";
34}
35
Nazri Ramliy5e11bee2010-06-24 08:21:16 +080036static int parse_decorate_color_slot(const char *slot)
37{
38 /*
39 * We're comparing with 'ignore-case' on
40 * (because config.c sets them all tolower),
41 * but let's match the letters in the literal
42 * string values here with how they are
43 * documented in Documentation/config.txt, for
44 * consistency.
45 *
46 * We love being consistent, don't we?
47 */
48 if (!strcasecmp(slot, "branch"))
49 return DECORATION_REF_LOCAL;
50 if (!strcasecmp(slot, "remoteBranch"))
51 return DECORATION_REF_REMOTE;
52 if (!strcasecmp(slot, "tag"))
53 return DECORATION_REF_TAG;
54 if (!strcasecmp(slot, "stash"))
55 return DECORATION_REF_STASH;
56 if (!strcasecmp(slot, "HEAD"))
57 return DECORATION_REF_HEAD;
58 return -1;
59}
60
Jonathan Nieder88521172014-10-07 15:16:57 -040061int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
Nazri Ramliy5e11bee2010-06-24 08:21:16 +080062{
Jonathan Nieder88521172014-10-07 15:16:57 -040063 int slot = parse_decorate_color_slot(slot_name);
Nazri Ramliy5e11bee2010-06-24 08:21:16 +080064 if (slot < 0)
65 return 0;
66 if (!value)
67 return config_error_nonbool(var);
Jeff Kingf6c5a292014-10-07 15:33:09 -040068 return color_parse(value, decoration_colors[slot]);
Nazri Ramliy5e11bee2010-06-24 08:21:16 +080069}
70
Nazri Ramliy67a4b582010-06-19 09:37:35 +080071/*
72 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
73 * for showing the commit sha1, use the same check for --decorate
74 */
75#define decorate_get_color_opt(o, ix) \
Jeff Kingf1c96262011-08-17 22:03:12 -070076 decorate_get_color((o)->use_color, ix)
Nazri Ramliy67a4b582010-06-19 09:37:35 +080077
Jeff King662174d2014-08-26 06:23:36 -040078void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
René Scharfecab4feb2008-09-04 23:39:21 +020079{
Jeff King96ffc062016-02-22 17:44:32 -050080 struct name_decoration *res;
81 FLEX_ALLOC_STR(res, name, name);
Nazri Ramliya7524122010-06-19 09:37:34 +080082 res->type = type;
René Scharfecab4feb2008-09-04 23:39:21 +020083 res->next = add_decoration(&name_decoration, obj, res);
84}
85
Jeff King2608c242014-08-26 06:23:54 -040086const struct name_decoration *get_name_decoration(const struct object *obj)
87{
88 return lookup_decoration(&name_decoration, obj);
89}
90
Michael Haggertyf124b732015-05-25 18:38:57 +000091static int add_ref_decoration(const char *refname, const struct object_id *oid,
92 int flags, void *cb_data)
René Scharfecab4feb2008-09-04 23:39:21 +020093{
Nguyễn Thái Ngọc Duy5267d292011-08-19 19:43:50 +070094 struct object *obj;
Nazri Ramliya7524122010-06-19 09:37:34 +080095 enum decoration_type type = DECORATION_NONE;
Nguyễn Thái Ngọc Duy5267d292011-08-19 19:43:50 +070096
Junio C Hamano429ad202015-05-13 12:40:35 -070097 assert(cb_data == NULL);
98
Mike Hommey58d121b2015-06-12 06:34:59 +090099 if (starts_with(refname, git_replace_ref_base)) {
Michael Haggerty3d79f652015-05-25 18:38:58 +0000100 struct object_id original_oid;
Michael Haggertyafc711b2014-02-18 12:24:55 +0100101 if (!check_replace_refs)
Michael J Gruberb9ad5002011-08-25 17:09:30 +0200102 return 0;
Junio C Hamano31a0ad52015-08-03 11:01:10 -0700103 if (get_oid_hex(refname + strlen(git_replace_ref_base),
104 &original_oid)) {
Nguyễn Thái Ngọc Duy5267d292011-08-19 19:43:50 +0700105 warning("invalid replace ref %s", refname);
106 return 0;
107 }
Michael Haggerty3d79f652015-05-25 18:38:58 +0000108 obj = parse_object(original_oid.hash);
Nguyễn Thái Ngọc Duy5267d292011-08-19 19:43:50 +0700109 if (obj)
110 add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
111 return 0;
112 }
113
Michael Haggertyf124b732015-05-25 18:38:57 +0000114 obj = parse_object(oid->hash);
René Scharfecab4feb2008-09-04 23:39:21 +0200115 if (!obj)
116 return 0;
Nazri Ramliya7524122010-06-19 09:37:34 +0800117
Christian Couder59556542013-11-30 21:55:40 +0100118 if (starts_with(refname, "refs/heads/"))
Nazri Ramliya7524122010-06-19 09:37:34 +0800119 type = DECORATION_REF_LOCAL;
Christian Couder59556542013-11-30 21:55:40 +0100120 else if (starts_with(refname, "refs/remotes/"))
Nazri Ramliya7524122010-06-19 09:37:34 +0800121 type = DECORATION_REF_REMOTE;
Christian Couder59556542013-11-30 21:55:40 +0100122 else if (starts_with(refname, "refs/tags/"))
Nazri Ramliya7524122010-06-19 09:37:34 +0800123 type = DECORATION_REF_TAG;
Nguyễn Thái Ngọc Duy97ba6422012-01-05 19:39:40 +0700124 else if (!strcmp(refname, "refs/stash"))
Nazri Ramliya7524122010-06-19 09:37:34 +0800125 type = DECORATION_REF_STASH;
Nguyễn Thái Ngọc Duy97ba6422012-01-05 19:39:40 +0700126 else if (!strcmp(refname, "HEAD"))
Nazri Ramliya7524122010-06-19 09:37:34 +0800127 type = DECORATION_REF_HEAD;
128
Nazri Ramliya7524122010-06-19 09:37:34 +0800129 add_name_decoration(type, refname, obj);
René Scharfecab4feb2008-09-04 23:39:21 +0200130 while (obj->type == OBJ_TAG) {
131 obj = ((struct tag *)obj)->tagged;
132 if (!obj)
133 break;
brian m. carlson5e1361c2013-12-17 04:28:21 +0000134 if (!obj->parsed)
brian m. carlsoned1c9972015-11-10 02:22:29 +0000135 parse_object(obj->oid.hash);
Nazri Ramliya7524122010-06-19 09:37:34 +0800136 add_name_decoration(DECORATION_REF_TAG, refname, obj);
René Scharfecab4feb2008-09-04 23:39:21 +0200137 }
138 return 0;
139}
140
Nguyễn Thái Ngọc Duy76f5df32011-08-18 19:29:37 +0700141static int add_graft_decoration(const struct commit_graft *graft, void *cb_data)
142{
brian m. carlson7683e2e2015-03-13 23:39:34 +0000143 struct commit *commit = lookup_commit(graft->oid.hash);
Nguyễn Thái Ngọc Duy76f5df32011-08-18 19:29:37 +0700144 if (!commit)
145 return 0;
146 add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
147 return 0;
148}
149
Lars Hjemli33e70182009-08-15 16:23:12 +0200150void load_ref_decorations(int flags)
René Scharfecab4feb2008-09-04 23:39:21 +0200151{
Junio C Hamano76c61fb2015-05-13 10:25:18 -0700152 if (!decoration_loaded) {
Michael Haggerty2b2a5be2015-05-25 18:38:28 +0000153
Junio C Hamano76c61fb2015-05-13 10:25:18 -0700154 decoration_loaded = 1;
155 decoration_flags = flags;
Michael Haggertyf124b732015-05-25 18:38:57 +0000156 for_each_ref(add_ref_decoration, NULL);
157 head_ref(add_ref_decoration, NULL);
Nguyễn Thái Ngọc Duy76f5df32011-08-18 19:29:37 +0700158 for_each_commit_graft(add_graft_decoration, NULL);
René Scharfecab4feb2008-09-04 23:39:21 +0200159 }
160}
161
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200162static void show_parents(struct commit *commit, int abbrev, FILE *file)
Linus Torvaldsc8c893c2006-05-03 07:59:00 -0700163{
164 struct commit_list *p;
165 for (p = commit->parents; p ; p = p->next) {
166 struct commit *parent = p->item;
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200167 fprintf(file, " %s", find_unique_abbrev(parent->object.oid.hash, abbrev));
Linus Torvaldsc8c893c2006-05-03 07:59:00 -0700168 }
169}
170
Jay Soffian91b849b2011-10-04 10:02:03 -0400171static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
172{
173 struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
174 for ( ; p; p = p->next) {
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200175 fprintf(opt->diffopt.file, " %s", find_unique_abbrev(p->item->object.oid.hash, abbrev));
Jay Soffian91b849b2011-10-04 10:02:03 -0400176 }
177}
178
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000179/*
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100180 * Do we have HEAD in the output, and also the branch it points at?
181 * If so, find that decoration entry for that current branch.
182 */
183static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration)
184{
185 const struct name_decoration *list, *head = NULL;
186 const char *branch_name = NULL;
187 unsigned char unused[20];
188 int rru_flags;
189
190 /* First find HEAD */
191 for (list = decoration; list; list = list->next)
192 if (list->type == DECORATION_REF_HEAD) {
193 head = list;
194 break;
195 }
196 if (!head)
197 return NULL;
198
199 /* Now resolve and find the matching current branch */
200 branch_name = resolve_ref_unsafe("HEAD", 0, unused, &rru_flags);
201 if (!(rru_flags & REF_ISSYMREF))
202 return NULL;
Junio C Hamano76c61fb2015-05-13 10:25:18 -0700203
Junio C Hamano429ad202015-05-13 12:40:35 -0700204 if (!starts_with(branch_name, "refs/"))
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100205 return NULL;
206
207 /* OK, do we have that ref in the list? */
208 for (list = decoration; list; list = list->next)
209 if ((list->type == DECORATION_REF_LOCAL) &&
210 !strcmp(branch_name, list->name)) {
211 return list;
212 }
213
214 return NULL;
215}
216
Junio C Hamano429ad202015-05-13 12:40:35 -0700217static void show_name(struct strbuf *sb, const struct name_decoration *decoration)
218{
219 if (decoration_flags == DECORATE_SHORT_REFS)
220 strbuf_addstr(sb, prettify_refname(decoration->name));
221 else
222 strbuf_addstr(sb, decoration->name);
223}
224
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100225/*
Harry Jeffery92710952014-09-18 21:53:53 +0100226 * The caller makes sure there is no funny color before calling.
227 * format_decorations_extended makes sure the same after return.
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000228 */
Harry Jeffery92710952014-09-18 21:53:53 +0100229void format_decorations_extended(struct strbuf *sb,
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000230 const struct commit *commit,
Harry Jeffery92710952014-09-18 21:53:53 +0100231 int use_color,
232 const char *prefix,
233 const char *separator,
234 const char *suffix)
Linus Torvaldsca135e72007-04-16 16:05:10 -0700235{
Jeff King2608c242014-08-26 06:23:54 -0400236 const struct name_decoration *decoration;
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100237 const struct name_decoration *current_and_HEAD;
Nazri Ramliy67a4b582010-06-19 09:37:35 +0800238 const char *color_commit =
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000239 diff_get_color(use_color, DIFF_COMMIT);
Nazri Ramliy67a4b582010-06-19 09:37:35 +0800240 const char *color_reset =
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000241 decorate_get_color(use_color, DECORATION_NONE);
Linus Torvaldsca135e72007-04-16 16:05:10 -0700242
Jeff King2608c242014-08-26 06:23:54 -0400243 decoration = get_name_decoration(&commit->object);
Linus Torvaldsca135e72007-04-16 16:05:10 -0700244 if (!decoration)
245 return;
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100246
247 current_and_HEAD = current_pointed_by_HEAD(decoration);
Linus Torvaldsca135e72007-04-16 16:05:10 -0700248 while (decoration) {
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100249 /*
250 * When both current and HEAD are there, only
251 * show HEAD->current where HEAD would have
252 * appeared, skipping the entry for current.
253 */
254 if (decoration != current_and_HEAD) {
255 strbuf_addstr(sb, color_commit);
256 strbuf_addstr(sb, prefix);
257 strbuf_addstr(sb, color_reset);
258 strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
259 if (decoration->type == DECORATION_REF_TAG)
260 strbuf_addstr(sb, "tag: ");
261
Junio C Hamano429ad202015-05-13 12:40:35 -0700262 show_name(sb, decoration);
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100263
264 if (current_and_HEAD &&
265 decoration->type == DECORATION_REF_HEAD) {
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100266 strbuf_addstr(sb, " -> ");
267 strbuf_addstr(sb, color_reset);
268 strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
Junio C Hamano429ad202015-05-13 12:40:35 -0700269 show_name(sb, current_and_HEAD);
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100270 }
271 strbuf_addstr(sb, color_reset);
272
273 prefix = separator;
274 }
Linus Torvaldsca135e72007-04-16 16:05:10 -0700275 decoration = decoration->next;
276 }
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000277 strbuf_addstr(sb, color_commit);
Harry Jeffery92710952014-09-18 21:53:53 +0100278 strbuf_addstr(sb, suffix);
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000279 strbuf_addstr(sb, color_reset);
280}
281
282void show_decorations(struct rev_info *opt, struct commit *commit)
283{
284 struct strbuf sb = STRBUF_INIT;
285
286 if (opt->show_source && commit->util)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200287 fprintf(opt->diffopt.file, "\t%s", (char *) commit->util);
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000288 if (!opt->show_decorations)
289 return;
290 format_decorations(&sb, commit, opt->diffopt.use_color);
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200291 fputs(sb.buf, opt->diffopt.file);
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000292 strbuf_release(&sb);
Linus Torvaldsca135e72007-04-16 16:05:10 -0700293}
294
Johannes Schindeline00de242007-02-09 01:43:54 +0100295static unsigned int digits_in_number(unsigned int number)
296{
297 unsigned int i = 10, result = 1;
298 while (i <= number) {
299 i *= 10;
300 result++;
301 }
302 return result;
303}
304
Junio C Hamanod28b5d42012-12-21 22:06:01 -0800305void fmt_output_subject(struct strbuf *filename,
306 const char *subject,
Junio C Hamano021f2f42012-12-21 23:26:16 -0800307 struct rev_info *info)
Stephen Boyd6fa8e622009-03-22 19:14:04 -0700308{
Junio C Hamano021f2f42012-12-21 23:26:16 -0800309 const char *suffix = info->patch_suffix;
310 int nr = info->nr;
Junio C Hamanod28b5d42012-12-21 22:06:01 -0800311 int start_len = filename->len;
312 int max_len = start_len + FORMAT_PATCH_NAME_MAX - (strlen(suffix) + 1);
Stephen Boyd6fa8e622009-03-22 19:14:04 -0700313
Junio C Hamano5fe10fe2012-12-22 00:21:23 -0800314 if (0 < info->reroll_count)
315 strbuf_addf(filename, "v%d-", info->reroll_count);
Junio C Hamanod28b5d42012-12-21 22:06:01 -0800316 strbuf_addf(filename, "%04d-%s", nr, subject);
Christian Couderb09b8682009-03-27 01:13:01 +0100317
Junio C Hamanod28b5d42012-12-21 22:06:01 -0800318 if (max_len < filename->len)
319 strbuf_setlen(filename, max_len);
320 strbuf_addstr(filename, suffix);
321}
Jeff Kinga21c2f92012-05-21 19:10:32 -0400322
Junio C Hamanod28b5d42012-12-21 22:06:01 -0800323void fmt_output_commit(struct strbuf *filename,
324 struct commit *commit,
325 struct rev_info *info)
326{
327 struct pretty_print_context ctx = {0};
328 struct strbuf subject = STRBUF_INIT;
329
330 format_commit_message(commit, "%f", &subject, &ctx);
331 fmt_output_subject(filename, subject.buf, info);
332 strbuf_release(&subject);
Stephen Boyd6fa8e622009-03-22 19:14:04 -0700333}
334
Stephen Boyd108dab22009-03-22 19:14:05 -0700335void log_write_email_headers(struct rev_info *opt, struct commit *commit,
Junio C Hamano267123b2008-03-15 00:09:20 -0700336 const char **subject_p,
337 const char **extra_headers_p,
338 int *need_8bit_cte_p)
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500339{
340 const char *subject = NULL;
341 const char *extra_headers = opt->extra_headers;
brian m. carlson3a30aa12015-12-15 01:52:04 +0000342 const char *name = oid_to_hex(opt->zero_commit ?
343 &null_oid : &commit->object.oid);
Junio C Hamano267123b2008-03-15 00:09:20 -0700344
345 *need_8bit_cte_p = 0; /* unknown */
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500346 if (opt->total > 0) {
347 static char buffer[64];
348 snprintf(buffer, sizeof(buffer),
Jeff Kinge7af8e42011-05-30 10:19:05 -0400349 "Subject: [%s%s%0*d/%d] ",
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500350 opt->subject_prefix,
Jeff Kinge7af8e42011-05-30 10:19:05 -0400351 *opt->subject_prefix ? " " : "",
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500352 digits_in_number(opt->total),
353 opt->nr, opt->total);
354 subject = buffer;
355 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
356 static char buffer[256];
357 snprintf(buffer, sizeof(buffer),
358 "Subject: [%s] ",
359 opt->subject_prefix);
360 subject = buffer;
361 } else {
362 subject = "Subject: ";
363 }
364
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200365 fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700366 graph_show_oneline(opt->graph);
367 if (opt->message_id) {
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200368 fprintf(opt->diffopt.file, "Message-Id: <%s>\n", opt->message_id);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700369 graph_show_oneline(opt->graph);
370 }
Thomas Rastb079c502009-02-19 22:26:31 +0100371 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
372 int i, n;
373 n = opt->ref_message_ids->nr;
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200374 fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
Thomas Rastb079c502009-02-19 22:26:31 +0100375 for (i = 0; i < n; i++)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200376 fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
Thomas Rastb079c502009-02-19 22:26:31 +0100377 opt->ref_message_ids->items[i].string);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700378 graph_show_oneline(opt->graph);
379 }
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500380 if (opt->mime_boundary) {
381 static char subject_buffer[1024];
382 static char buffer[1024];
Stephen Boyd108dab22009-03-22 19:14:05 -0700383 struct strbuf filename = STRBUF_INIT;
Junio C Hamano267123b2008-03-15 00:09:20 -0700384 *need_8bit_cte_p = -1; /* NEVER */
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500385 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
386 "%s"
387 "MIME-Version: 1.0\n"
388 "Content-Type: multipart/mixed;"
389 " boundary=\"%s%s\"\n"
390 "\n"
391 "This is a multi-part message in MIME "
392 "format.\n"
393 "--%s%s\n"
394 "Content-Type: text/plain; "
395 "charset=UTF-8; format=fixed\n"
396 "Content-Transfer-Encoding: 8bit\n\n",
397 extra_headers ? extra_headers : "",
398 mime_boundary_leader, opt->mime_boundary,
399 mime_boundary_leader, opt->mime_boundary);
400 extra_headers = subject_buffer;
401
Junio C Hamano38ec23a2012-12-21 21:39:37 -0800402 if (opt->numbered_files)
403 strbuf_addf(&filename, "%d", opt->nr);
404 else
Junio C Hamanod28b5d42012-12-21 22:06:01 -0800405 fmt_output_commit(&filename, commit, opt);
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500406 snprintf(buffer, sizeof(buffer) - 1,
Kevin Ballard6b2fbaa2008-07-29 22:49:33 -0700407 "\n--%s%s\n"
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500408 "Content-Type: text/x-patch;"
Stephen Boyd108dab22009-03-22 19:14:05 -0700409 " name=\"%s\"\n"
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500410 "Content-Transfer-Encoding: 8bit\n"
411 "Content-Disposition: %s;"
Stephen Boyd108dab22009-03-22 19:14:05 -0700412 " filename=\"%s\"\n\n",
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500413 mime_boundary_leader, opt->mime_boundary,
Stephen Boyd108dab22009-03-22 19:14:05 -0700414 filename.buf,
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500415 opt->no_inline ? "attachment" : "inline",
Stephen Boyd108dab22009-03-22 19:14:05 -0700416 filename.buf);
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500417 opt->diffopt.stat_sep = buffer;
Stephen Boyd108dab22009-03-22 19:14:05 -0700418 strbuf_release(&filename);
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500419 }
420 *subject_p = subject;
421 *extra_headers_p = extra_headers;
422}
423
Junio C Hamanoc6b3ec42012-01-04 13:48:45 -0800424static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
425{
426 const char *color, *reset, *eol;
427
428 color = diff_get_color_opt(&opt->diffopt,
429 status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
430 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
431 while (*bol) {
432 eol = strchrnul(bol, '\n');
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200433 fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
Junio C Hamanoc6b3ec42012-01-04 13:48:45 -0800434 *eol ? "\n" : "");
Zoltan Klingercf3983d2014-07-09 12:10:21 +1000435 graph_show_oneline(opt->graph);
Junio C Hamanoc6b3ec42012-01-04 13:48:45 -0800436 bol = (*eol) ? (eol + 1) : eol;
437 }
438}
439
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700440static void show_signature(struct rev_info *opt, struct commit *commit)
441{
442 struct strbuf payload = STRBUF_INIT;
443 struct strbuf signature = STRBUF_INIT;
444 struct strbuf gpg_output = STRBUF_INIT;
445 int status;
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700446
Jeff King218aa3a2014-06-13 02:32:11 -0400447 if (parse_signed_commit(commit, &payload, &signature) <= 0)
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700448 goto out;
449
450 status = verify_signed_buffer(payload.buf, payload.len,
451 signature.buf, signature.len,
Michael J Gruber9cc4ac82013-02-14 17:04:44 +0100452 &gpg_output, NULL);
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700453 if (status && !gpg_output.len)
454 strbuf_addstr(&gpg_output, "No signature\n");
455
Junio C Hamanoc6b3ec42012-01-04 13:48:45 -0800456 show_sig_lines(opt, status, gpg_output.buf);
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700457
458 out:
459 strbuf_release(&gpg_output);
460 strbuf_release(&payload);
461 strbuf_release(&signature);
462}
463
Junio C Hamano824958e2012-01-04 13:51:28 -0800464static int which_parent(const unsigned char *sha1, const struct commit *commit)
465{
466 int nth;
467 const struct commit_list *parent;
468
469 for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
brian m. carlsoned1c9972015-11-10 02:22:29 +0000470 if (!hashcmp(parent->item->object.oid.hash, sha1))
Junio C Hamano824958e2012-01-04 13:51:28 -0800471 return nth;
472 nth++;
473 }
474 return -1;
475}
476
Junio C Hamanod041ffa2012-01-04 16:23:12 -0800477static int is_common_merge(const struct commit *commit)
478{
479 return (commit->parents
480 && commit->parents->next
481 && !commit->parents->next->next);
482}
483
Christian Couder063da622014-07-07 08:35:37 +0200484static void show_one_mergetag(struct commit *commit,
Junio C Hamano824958e2012-01-04 13:51:28 -0800485 struct commit_extra_header *extra,
Christian Couder063da622014-07-07 08:35:37 +0200486 void *data)
Junio C Hamano824958e2012-01-04 13:51:28 -0800487{
Christian Couder063da622014-07-07 08:35:37 +0200488 struct rev_info *opt = (struct rev_info *)data;
Junio C Hamano824958e2012-01-04 13:51:28 -0800489 unsigned char sha1[20];
490 struct tag *tag;
491 struct strbuf verify_message;
492 int status, nth;
493 size_t payload_size, gpg_message_offset;
494
495 hash_sha1_file(extra->value, extra->len, typename(OBJ_TAG), sha1);
496 tag = lookup_tag(sha1);
497 if (!tag)
498 return; /* error message already given */
499
500 strbuf_init(&verify_message, 256);
501 if (parse_tag_buffer(tag, extra->value, extra->len))
502 strbuf_addstr(&verify_message, "malformed mergetag\n");
Junio C Hamanod041ffa2012-01-04 16:23:12 -0800503 else if (is_common_merge(commit) &&
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000504 !oidcmp(&tag->tagged->oid,
505 &commit->parents->next->item->object.oid))
Junio C Hamanod041ffa2012-01-04 16:23:12 -0800506 strbuf_addf(&verify_message,
507 "merged tag '%s'\n", tag->tag);
brian m. carlsoned1c9972015-11-10 02:22:29 +0000508 else if ((nth = which_parent(tag->tagged->oid.hash, commit)) < 0)
Junio C Hamano824958e2012-01-04 13:51:28 -0800509 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
brian m. carlsoned1c9972015-11-10 02:22:29 +0000510 tag->tag, tag->tagged->oid.hash);
Junio C Hamano824958e2012-01-04 13:51:28 -0800511 else
512 strbuf_addf(&verify_message,
513 "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
514 gpg_message_offset = verify_message.len;
515
516 payload_size = parse_signature(extra->value, extra->len);
Michael J Gruber13150932013-02-14 17:04:43 +0100517 status = -1;
Michael J Gruber42c55ce2014-06-27 15:18:36 +0200518 if (extra->len > payload_size) {
519 /* could have a good signature */
520 if (!verify_signed_buffer(extra->value, payload_size,
521 extra->value + payload_size,
522 extra->len - payload_size,
523 &verify_message, NULL))
524 status = 0; /* good */
525 else if (verify_message.len <= gpg_message_offset)
526 strbuf_addstr(&verify_message, "No signature\n");
527 /* otherwise we couldn't verify, which is shown as bad */
528 }
Junio C Hamano824958e2012-01-04 13:51:28 -0800529
530 show_sig_lines(opt, status, verify_message.buf);
531 strbuf_release(&verify_message);
532}
533
534static void show_mergetag(struct rev_info *opt, struct commit *commit)
535{
Christian Couder063da622014-07-07 08:35:37 +0200536 for_each_mergetag(show_one_mergetag, commit, opt);
Junio C Hamano824958e2012-01-04 13:51:28 -0800537}
538
Adam Simpkins02865652008-04-29 01:32:59 -0700539void show_log(struct rev_info *opt)
Linus Torvalds91539832006-04-17 11:59:32 -0700540{
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500541 struct strbuf msgbuf = STRBUF_INIT;
Timo Hirvonen39bc9a62006-06-25 13:54:14 +0300542 struct log_info *log = opt->loginfo;
Linus Torvalds91539832006-04-17 11:59:32 -0700543 struct commit *commit = log->commit, *parent = log->parent;
Linus Torvalds91539832006-04-17 11:59:32 -0700544 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
Thomas Rastdd2e7942009-10-19 17:48:08 +0200545 const char *extra_headers = opt->extra_headers;
546 struct pretty_print_context ctx = {0};
Linus Torvalds91539832006-04-17 11:59:32 -0700547
548 opt->loginfo = NULL;
549 if (!opt->verbose_header) {
Adam Simpkins7fefda52008-05-04 03:36:54 -0700550 graph_show_commit(opt->graph);
551
Michael J Gruber1df2d652011-03-07 13:31:39 +0100552 if (!opt->graph)
Michael J Gruberb1b47552011-03-10 15:45:03 +0100553 put_revision_mark(opt, commit);
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200554 fputs(find_unique_abbrev(commit->object.oid.hash, abbrev_commit), opt->diffopt.file);
Adam Simpkins885cf802008-05-04 03:36:52 -0700555 if (opt->print_parents)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200556 show_parents(commit, abbrev_commit, opt->diffopt.file);
Jay Soffian91b849b2011-10-04 10:02:03 -0400557 if (opt->children.name)
558 show_children(opt, commit, abbrev_commit);
Linus Torvalds0f3a2902008-10-27 12:51:59 -0700559 show_decorations(opt, commit);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700560 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200561 putc('\n', opt->diffopt.file);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700562 graph_show_remainder(opt->graph);
563 }
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200564 putc(opt->diffopt.line_termination, opt->diffopt.file);
Linus Torvalds91539832006-04-17 11:59:32 -0700565 return;
566 }
567
568 /*
Jeff King87152272009-07-01 03:26:28 -0400569 * If use_terminator is set, we already handled any record termination
570 * at the end of the last record.
Adam Simpkins02865652008-04-29 01:32:59 -0700571 * Otherwise, add a diffopt.line_termination character before all
572 * entries but the first. (IOW, as a separator between entries)
Linus Torvalds91539832006-04-17 11:59:32 -0700573 */
Adam Simpkins7fefda52008-05-04 03:36:54 -0700574 if (opt->shown_one && !opt->use_terminator) {
575 /*
576 * If entries are separated by a newline, the output
577 * should look human-readable. If the last entry ended
578 * with a newline, print the graph output before this
579 * newline. Otherwise it will end up as a completely blank
580 * line and will look like a gap in the graph.
581 *
582 * If the entry separator is not a newline, the output is
583 * primarily intended for programmatic consumption, and we
584 * never want the extra graph output before the entry
585 * separator.
586 */
587 if (opt->diffopt.line_termination == '\n' &&
588 !opt->missing_newline)
589 graph_show_padding(opt->graph);
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200590 putc(opt->diffopt.line_termination, opt->diffopt.file);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700591 }
Linus Torvalds91539832006-04-17 11:59:32 -0700592 opt->shown_one = 1;
593
594 /*
Adam Simpkins7fefda52008-05-04 03:36:54 -0700595 * If the history graph was requested,
596 * print the graph, up to this commit's line
597 */
598 graph_show_commit(opt->graph);
599
600 /*
Linus Torvalds91539832006-04-17 11:59:32 -0700601 * Print header line of header..
602 */
Junio C Hamano3eefc182006-04-18 16:45:27 -0700603
Eric Wong9f23e042016-06-05 04:46:39 +0000604 if (cmit_fmt_is_mail(opt->commit_format)) {
Thomas Rastdd2e7942009-10-19 17:48:08 +0200605 log_write_email_headers(opt, commit, &ctx.subject, &extra_headers,
606 &ctx.need_8bit_cte);
Johannes Schindeline52a5de2007-02-23 01:35:03 +0100607 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200608 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
Junio C Hamano74bd9022006-12-16 15:31:25 -0800609 if (opt->commit_format != CMIT_FMT_ONELINE)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200610 fputs("commit ", opt->diffopt.file);
Adam Simpkins7528f272008-05-25 00:07:21 -0700611
Michael J Gruber1df2d652011-03-07 13:31:39 +0100612 if (!opt->graph)
Michael J Gruberb1b47552011-03-10 15:45:03 +0100613 put_revision_mark(opt, commit);
brian m. carlsoned1c9972015-11-10 02:22:29 +0000614 fputs(find_unique_abbrev(commit->object.oid.hash, abbrev_commit),
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200615 opt->diffopt.file);
Adam Simpkins885cf802008-05-04 03:36:52 -0700616 if (opt->print_parents)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200617 show_parents(commit, abbrev_commit, opt->diffopt.file);
Jay Soffian91b849b2011-10-04 10:02:03 -0400618 if (opt->children.name)
619 show_children(opt, commit, abbrev_commit);
Junio C Hamano73f0a152006-05-24 12:19:47 -0700620 if (parent)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200621 fprintf(opt->diffopt.file, " (from %s)",
brian m. carlsoned1c9972015-11-10 02:22:29 +0000622 find_unique_abbrev(parent->object.oid.hash,
Junio C Hamano3eefc182006-04-18 16:45:27 -0700623 abbrev_commit));
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200624 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
Linus Torvalds0f3a2902008-10-27 12:51:59 -0700625 show_decorations(opt, commit);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700626 if (opt->commit_format == CMIT_FMT_ONELINE) {
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200627 putc(' ', opt->diffopt.file);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700628 } else {
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200629 putc('\n', opt->diffopt.file);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700630 graph_show_oneline(opt->graph);
631 }
Nicolas Pitre903b45f2007-01-27 22:40:36 -0500632 if (opt->reflog_info) {
Adam Simpkins7fefda52008-05-04 03:36:54 -0700633 /*
634 * setup_revisions() ensures that opt->reflog_info
635 * and opt->graph cannot both be set,
636 * so we don't need to worry about printing the
637 * graph info here.
638 */
Junio C Hamano4d12a472007-01-20 00:51:41 -0800639 show_reflog_message(opt->reflog_info,
Junio C Hamano55ccf852012-05-07 14:11:32 -0700640 opt->commit_format == CMIT_FMT_ONELINE,
Jeff Kinga5481a62015-06-25 12:55:02 -0400641 &opt->date_mode,
Junio C Hamano55ccf852012-05-07 14:11:32 -0700642 opt->date_mode_explicit);
Adam Simpkins02865652008-04-29 01:32:59 -0700643 if (opt->commit_format == CMIT_FMT_ONELINE)
Nicolas Pitre903b45f2007-01-27 22:40:36 -0500644 return;
Nicolas Pitre903b45f2007-01-27 22:40:36 -0500645 }
Junio C Hamano3eefc182006-04-18 16:45:27 -0700646 }
Linus Torvalds91539832006-04-17 11:59:32 -0700647
Junio C Hamano824958e2012-01-04 13:51:28 -0800648 if (opt->show_signature) {
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700649 show_signature(opt, commit);
Junio C Hamano824958e2012-01-04 13:51:28 -0800650 show_mergetag(opt, commit);
651 }
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700652
Jeff King8597ea32014-06-10 17:44:13 -0400653 if (!get_cached_commit_buffer(commit, NULL))
Linus Torvalds3131b712008-02-09 14:02:07 -0800654 return;
655
Junio C Hamanoddf333f2012-10-17 18:51:47 -0700656 if (opt->show_notes) {
657 int raw;
658 struct strbuf notebuf = STRBUF_INIT;
659
660 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
brian m. carlsoned1c9972015-11-10 02:22:29 +0000661 format_display_notes(commit->object.oid.hash, &notebuf,
Junio C Hamanoddf333f2012-10-17 18:51:47 -0700662 get_log_output_encoding(), raw);
663 ctx.notes_message = notebuf.len
664 ? strbuf_detach(&notebuf, NULL)
665 : xcalloc(1, 1);
666 }
667
Linus Torvalds91539832006-04-17 11:59:32 -0700668 /*
669 * And then the pretty-printed message itself
670 */
Nguyễn Thái Ngọc Duy5289c562013-02-12 02:17:38 -0800671 if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
672 ctx.need_8bit_cte =
673 has_non_ascii(fmt_name(getenv("GIT_COMMITTER_NAME"),
674 getenv("GIT_COMMITTER_EMAIL")));
Thomas Rastdd2e7942009-10-19 17:48:08 +0200675 ctx.date_mode = opt->date_mode;
Jeff Kingf026c752012-05-04 01:25:18 -0400676 ctx.date_mode_explicit = opt->date_mode_explicit;
Thomas Rastdd2e7942009-10-19 17:48:08 +0200677 ctx.abbrev = opt->diffopt.abbrev;
678 ctx.after_subject = extra_headers;
Jeff King9553d2b2011-05-26 18:28:17 -0400679 ctx.preserve_subject = opt->preserve_subject;
Thomas Rast8f8f5472009-10-19 17:48:10 +0200680 ctx.reflog_info = opt->reflog_info;
Jeff King6bf13942011-05-26 18:27:49 -0400681 ctx.fmt = opt->commit_format;
Antoine Pelisse0e2913b2013-01-05 22:26:41 +0100682 ctx.mailmap = opt->mailmap;
Junio C Hamano30825172012-12-17 17:56:49 -0500683 ctx.color = opt->diffopt.use_color;
Linus Torvalds7cc13c72016-03-16 09:15:53 -0700684 ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
Alexey Shumkinecaee802013-06-26 14:19:50 +0400685 ctx.output_encoding = get_log_output_encoding();
Jeff Kinga9080472013-07-03 03:08:22 -0400686 if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
687 ctx.from_ident = &opt->from_ident;
Josef Kufner3ad87c82016-06-16 20:18:37 +0700688 if (opt->graph)
689 ctx.graph_width = graph_width(opt->graph);
Jeff King6bf13942011-05-26 18:27:49 -0400690 pretty_print_commit(&ctx, commit, &msgbuf);
Junio C Hamano212620f2012-10-17 20:48:25 -0700691
692 if (opt->add_signoff)
Nguyễn Thái Ngọc Duy5289c562013-02-12 02:17:38 -0800693 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
Junio C Hamano212620f2012-10-17 20:48:25 -0700694
Junio C Hamano5a664cf2012-10-17 19:02:46 -0700695 if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
Junio C Hamanobd1470b2012-10-17 21:27:22 -0700696 ctx.notes_message && *ctx.notes_message) {
Eric Wong9f23e042016-06-05 04:46:39 +0000697 if (cmit_fmt_is_mail(ctx.fmt)) {
Junio C Hamanobd1470b2012-10-17 21:27:22 -0700698 strbuf_addstr(&msgbuf, "---\n");
699 opt->shown_dashes = 1;
700 }
Junio C Hamano5a664cf2012-10-17 19:02:46 -0700701 strbuf_addstr(&msgbuf, ctx.notes_message);
Junio C Hamanobd1470b2012-10-17 21:27:22 -0700702 }
Junio C Hamanocf2251b2006-05-31 15:11:49 -0700703
Adam Simpkins7fefda52008-05-04 03:36:54 -0700704 if (opt->show_log_size) {
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200705 fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700706 graph_show_oneline(opt->graph);
707 }
Marco Costalba9fa34652007-07-20 20:15:13 +0200708
Adam Simpkins7fefda52008-05-04 03:36:54 -0700709 /*
710 * Set opt->missing_newline if msgbuf doesn't
711 * end in a newline (including if it is empty)
712 */
713 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
714 opt->missing_newline = 1;
715 else
716 opt->missing_newline = 0;
717
Jacob Keller660e1132016-08-31 16:27:20 -0700718 graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf);
Jeff Kingb9c7d6e2014-07-29 13:56:48 -0400719 if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
Adam Simpkins7fefda52008-05-04 03:36:54 -0700720 if (!opt->missing_newline)
721 graph_show_padding(opt->graph);
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200722 putc(opt->diffopt.line_termination, opt->diffopt.file);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700723 }
724
Pierre Habouzit674d1722007-09-10 12:35:06 +0200725 strbuf_release(&msgbuf);
Junio C Hamanoddf333f2012-10-17 18:51:47 -0700726 free(ctx.notes_message);
Linus Torvalds91539832006-04-17 11:59:32 -0700727}
728
Linus Torvaldscd2bdc52006-04-14 16:52:13 -0700729int log_tree_diff_flush(struct rev_info *opt)
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700730{
Junio C Hamanobd1470b2012-10-17 21:27:22 -0700731 opt->shown_dashes = 0;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700732 diffcore_std(&opt->diffopt);
Linus Torvalds91539832006-04-17 11:59:32 -0700733
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700734 if (diff_queue_is_empty()) {
735 int saved_fmt = opt->diffopt.output_format;
736 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
737 diff_flush(&opt->diffopt);
738 opt->diffopt.output_format = saved_fmt;
739 return 0;
740 }
Linus Torvalds91539832006-04-17 11:59:32 -0700741
Junio C Hamano3969cf72006-06-27 15:08:19 -0700742 if (opt->loginfo && !opt->no_commit_id) {
Adam Simpkins02865652008-04-29 01:32:59 -0700743 show_log(opt);
Linus Torvalds304b5af2007-10-09 09:35:22 -0700744 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
745 opt->verbose_header &&
Jeff Kingb9c7d6e2014-07-29 13:56:48 -0400746 opt->commit_format != CMIT_FMT_ONELINE &&
747 !commit_format_is_empty(opt->commit_format)) {
Junio C Hamano1d34c502012-11-13 10:09:07 -0800748 /*
749 * When showing a verbose header (i.e. log message),
750 * and not in --pretty=oneline format, we would want
751 * an extra newline between the end of log and the
752 * diff/diffstat output for readability.
753 */
Junio C Hamano3969cf72006-06-27 15:08:19 -0700754 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
Bo Yang81bd1b22010-05-26 15:08:03 +0800755 if (opt->diffopt.output_prefix) {
756 struct strbuf *msg = NULL;
757 msg = opt->diffopt.output_prefix(&opt->diffopt,
758 opt->diffopt.output_prefix_data);
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200759 fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
Bo Yang81bd1b22010-05-26 15:08:03 +0800760 }
Junio C Hamano1d34c502012-11-13 10:09:07 -0800761
762 /*
763 * We may have shown three-dashes line early
764 * between notes and the log message, in which
765 * case we only want a blank line after the
766 * notes without (an extra) three-dashes line.
767 * Otherwise, we show the three-dashes line if
768 * we are showing the patch with diffstat, but
769 * in that case, there is no extra blank line
770 * after the three-dashes line.
771 */
772 if (!opt->shown_dashes &&
773 (pch & opt->diffopt.output_format) == pch)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200774 fprintf(opt->diffopt.file, "---");
775 putc('\n', opt->diffopt.file);
Junio C Hamano3969cf72006-06-27 15:08:19 -0700776 }
777 }
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700778 diff_flush(&opt->diffopt);
779 return 1;
780}
781
Linus Torvaldscd2bdc52006-04-14 16:52:13 -0700782static int do_diff_combined(struct rev_info *opt, struct commit *commit)
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700783{
René Scharfe82889292011-12-17 11:20:07 +0100784 diff_tree_combined_merge(commit, opt->dense_combined_merges, opt);
Linus Torvalds91539832006-04-17 11:59:32 -0700785 return !opt->loginfo;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700786}
787
Linus Torvalds91539832006-04-17 11:59:32 -0700788/*
789 * Show the diff of a commit.
790 *
791 * Return true if we printed any log info messages
792 */
793static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700794{
Linus Torvalds91539832006-04-17 11:59:32 -0700795 int showed_log;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700796 struct commit_list *parents;
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000797 struct object_id *oid;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700798
Peter Valdemar Mørch84102a32008-08-11 08:46:25 +0200799 if (!opt->diff && !DIFF_OPT_TST(&opt->diffopt, EXIT_WITH_STATUS))
Linus Torvalds91539832006-04-17 11:59:32 -0700800 return 0;
801
Jeff King7059dcc2013-10-24 04:52:36 -0400802 parse_commit_or_die(commit);
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000803 oid = &commit->tree->object.oid;
Thomas Rastd1b9b762013-03-28 09:19:34 +0100804
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700805 /* Root commit? */
Thomas Rast53d00b32013-07-31 22:13:20 +0200806 parents = get_saved_parents(opt, commit);
Linus Torvalds91539832006-04-17 11:59:32 -0700807 if (!parents) {
Rene Scharfe2b603562006-10-26 18:52:39 +0200808 if (opt->show_root_diff) {
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000809 diff_root_tree_sha1(oid->hash, "", &opt->diffopt);
Rene Scharfe2b603562006-10-26 18:52:39 +0200810 log_tree_diff_flush(opt);
811 }
Linus Torvalds91539832006-04-17 11:59:32 -0700812 return !opt->loginfo;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700813 }
814
815 /* More than one parent? */
Linus Torvalds91539832006-04-17 11:59:32 -0700816 if (parents && parents->next) {
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700817 if (opt->ignore_merges)
818 return 0;
819 else if (opt->combine_merges)
820 return do_diff_combined(opt, commit);
Petr Baudis88d9d452010-02-10 02:11:49 +0100821 else if (opt->first_parent_only) {
822 /*
823 * Generate merge log entry only for the first
824 * parent, showing summary diff of the others
825 * we merged _in_.
826 */
Jeff King7059dcc2013-10-24 04:52:36 -0400827 parse_commit_or_die(parents->item);
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000828 diff_tree_sha1(parents->item->tree->object.oid.hash,
829 oid->hash, "", &opt->diffopt);
Petr Baudis88d9d452010-02-10 02:11:49 +0100830 log_tree_diff_flush(opt);
831 return !opt->loginfo;
832 }
Linus Torvalds91539832006-04-17 11:59:32 -0700833
834 /* If we show individual diffs, show the parent info */
835 log->parent = parents->item;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700836 }
837
Linus Torvalds91539832006-04-17 11:59:32 -0700838 showed_log = 0;
839 for (;;) {
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700840 struct commit *parent = parents->item;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700841
Jeff King7059dcc2013-10-24 04:52:36 -0400842 parse_commit_or_die(parent);
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000843 diff_tree_sha1(parent->tree->object.oid.hash,
844 oid->hash, "", &opt->diffopt);
Linus Torvalds91539832006-04-17 11:59:32 -0700845 log_tree_diff_flush(opt);
846
847 showed_log |= !opt->loginfo;
848
849 /* Set up the log info for the next parent, if any.. */
850 parents = parents->next;
851 if (!parents)
852 break;
853 log->parent = parents->item;
854 opt->loginfo = log;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700855 }
Linus Torvalds91539832006-04-17 11:59:32 -0700856 return showed_log;
857}
858
859int log_tree_commit(struct rev_info *opt, struct commit *commit)
860{
861 struct log_info log;
Johannes Schindelin6ea57702016-06-22 17:01:28 +0200862 int shown, close_file = opt->diffopt.close_file;
Linus Torvalds91539832006-04-17 11:59:32 -0700863
864 log.commit = commit;
865 log.parent = NULL;
866 opt->loginfo = &log;
Johannes Schindelin6ea57702016-06-22 17:01:28 +0200867 opt->diffopt.close_file = 0;
Linus Torvalds91539832006-04-17 11:59:32 -0700868
Thomas Rast12da1d12013-03-28 17:47:32 +0100869 if (opt->line_level_traverse)
870 return line_log_print(opt, commit);
871
Nguyễn Thái Ngọc Duy1b32dec2014-03-25 20:23:27 +0700872 if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200873 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
Junio C Hamano3eefc182006-04-18 16:45:27 -0700874 shown = log_tree_diff(opt, commit, &log);
875 if (!shown && opt->loginfo && opt->always_show_header) {
Linus Torvalds91539832006-04-17 11:59:32 -0700876 log.parent = NULL;
Adam Simpkins02865652008-04-29 01:32:59 -0700877 show_log(opt);
Junio C Hamano3eefc182006-04-18 16:45:27 -0700878 shown = 1;
Linus Torvalds91539832006-04-17 11:59:32 -0700879 }
Nguyễn Thái Ngọc Duy1b32dec2014-03-25 20:23:27 +0700880 if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200881 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
Linus Torvalds91539832006-04-17 11:59:32 -0700882 opt->loginfo = NULL;
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200883 maybe_flush_or_die(opt->diffopt.file, "stdout");
Johannes Schindelin6ea57702016-06-22 17:01:28 +0200884 if (close_file)
885 fclose(opt->diffopt.file);
Junio C Hamano3eefc182006-04-18 16:45:27 -0700886 return shown;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700887}