blob: 55a68d0c6101a7a287c9544e1963427fd0b77793 [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"
Stefan Bellercbd53a22018-05-15 16:42:15 -07004#include "object-store.h"
Stefan Beller109cd762018-06-28 18:21:51 -07005#include "repository.h"
Junio C Hamano5f1c3f02006-04-09 01:11:11 -07006#include "commit.h"
René Scharfecab4feb2008-09-04 23:39:21 +02007#include "tag.h"
Adam Simpkins7fefda52008-05-04 03:36:54 -07008#include "graph.h"
Junio C Hamano5f1c3f02006-04-09 01:11:11 -07009#include "log-tree.h"
Johannes Schindelin8860fd42007-01-11 11:47:48 +010010#include "reflog-walk.h"
René Scharfecab4feb2008-09-04 23:39:21 +020011#include "refs.h"
Thomas Rastb079c502009-02-19 22:26:31 +010012#include "string-list.h"
Nazri Ramliy67a4b582010-06-19 09:37:35 +080013#include "color.h"
Junio C Hamano0c37f1f2011-10-18 15:53:23 -070014#include "gpg-interface.h"
Brandon Casey959a2622013-02-12 02:17:39 -080015#include "sequencer.h"
Thomas Rast12da1d12013-03-28 17:47:32 +010016#include "line-log.h"
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +020017#include "help.h"
Eric Sunshineee6cbf72018-07-22 05:57:09 -040018#include "interdiff.h"
Eric Sunshine40ce4162018-07-22 05:57:17 -040019#include "range-diff.h"
Junio C Hamano5f1c3f02006-04-09 01:11:11 -070020
Jeff King2608c242014-08-26 06:23:54 -040021static struct decoration name_decoration = { "object names" };
Junio C Hamano76c61fb2015-05-13 10:25:18 -070022static int decoration_loaded;
23static int decoration_flags;
Nazri Ramliya7524122010-06-19 09:37:34 +080024
Nazri Ramliy67a4b582010-06-19 09:37:35 +080025static char decoration_colors[][COLOR_MAXLEN] = {
26 GIT_COLOR_RESET,
27 GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */
28 GIT_COLOR_BOLD_RED, /* REF_REMOTE */
29 GIT_COLOR_BOLD_YELLOW, /* REF_TAG */
30 GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */
31 GIT_COLOR_BOLD_CYAN, /* REF_HEAD */
Nguyễn Thái Ngọc Duy76f5df32011-08-18 19:29:37 +070032 GIT_COLOR_BOLD_BLUE, /* GRAFTED */
Nazri Ramliy67a4b582010-06-19 09:37:35 +080033};
34
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +020035static const char *color_decorate_slots[] = {
36 [DECORATION_REF_LOCAL] = "branch",
37 [DECORATION_REF_REMOTE] = "remoteBranch",
38 [DECORATION_REF_TAG] = "tag",
39 [DECORATION_REF_STASH] = "stash",
40 [DECORATION_REF_HEAD] = "HEAD",
Nguyễn Thái Ngọc Duy09c4ba42018-05-26 15:55:31 +020041 [DECORATION_GRAFTED] = "grafted",
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +020042};
43
Nazri Ramliy67a4b582010-06-19 09:37:35 +080044static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
45{
Jeff Kingdaa0c3d2011-08-17 22:04:23 -070046 if (want_color(decorate_use_color))
Nazri Ramliy67a4b582010-06-19 09:37:35 +080047 return decoration_colors[ix];
48 return "";
49}
50
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +020051define_list_config_array(color_decorate_slots);
Nazri Ramliy5e11bee2010-06-24 08:21:16 +080052
Jonathan Nieder88521172014-10-07 15:16:57 -040053int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
Nazri Ramliy5e11bee2010-06-24 08:21:16 +080054{
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +020055 int slot = LOOKUP_CONFIG(color_decorate_slots, slot_name);
Nazri Ramliy5e11bee2010-06-24 08:21:16 +080056 if (slot < 0)
57 return 0;
58 if (!value)
59 return config_error_nonbool(var);
Jeff Kingf6c5a292014-10-07 15:33:09 -040060 return color_parse(value, decoration_colors[slot]);
Nazri Ramliy5e11bee2010-06-24 08:21:16 +080061}
62
Nazri Ramliy67a4b582010-06-19 09:37:35 +080063/*
64 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
65 * for showing the commit sha1, use the same check for --decorate
66 */
67#define decorate_get_color_opt(o, ix) \
Jeff Kingf1c96262011-08-17 22:03:12 -070068 decorate_get_color((o)->use_color, ix)
Nazri Ramliy67a4b582010-06-19 09:37:35 +080069
Jeff King662174d2014-08-26 06:23:36 -040070void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
René Scharfecab4feb2008-09-04 23:39:21 +020071{
Jeff King96ffc062016-02-22 17:44:32 -050072 struct name_decoration *res;
73 FLEX_ALLOC_STR(res, name, name);
Nazri Ramliya7524122010-06-19 09:37:34 +080074 res->type = type;
René Scharfecab4feb2008-09-04 23:39:21 +020075 res->next = add_decoration(&name_decoration, obj, res);
76}
77
Jeff King2608c242014-08-26 06:23:54 -040078const struct name_decoration *get_name_decoration(const struct object *obj)
79{
René Scharfe0cc73802019-09-08 19:58:51 +020080 load_ref_decorations(NULL, DECORATE_SHORT_REFS);
Jeff King2608c242014-08-26 06:23:54 -040081 return lookup_decoration(&name_decoration, obj);
82}
83
Derrick Stoleec9f7a792020-04-16 14:15:48 +000084static int match_ref_pattern(const char *refname,
85 const struct string_list_item *item)
86{
87 int matched = 0;
88 if (item->util == NULL) {
89 if (!wildmatch(item->string, refname, 0))
90 matched = 1;
91 } else {
92 const char *rest;
93 if (skip_prefix(refname, item->string, &rest) &&
94 (!*rest || *rest == '/'))
95 matched = 1;
96 }
97 return matched;
98}
99
100static int ref_filter_match(const char *refname,
101 const struct decoration_filter *filter)
102{
103 struct string_list_item *item;
104 const struct string_list *exclude_patterns = filter->exclude_ref_pattern;
105 const struct string_list *include_patterns = filter->include_ref_pattern;
Derrick Stoleea6be5e62020-04-16 14:15:49 +0000106 const struct string_list *exclude_patterns_config =
107 filter->exclude_ref_config_pattern;
Derrick Stoleec9f7a792020-04-16 14:15:48 +0000108
109 if (exclude_patterns && exclude_patterns->nr) {
110 for_each_string_list_item(item, exclude_patterns) {
111 if (match_ref_pattern(refname, item))
112 return 0;
113 }
114 }
115
116 if (include_patterns && include_patterns->nr) {
Derrick Stoleec9f7a792020-04-16 14:15:48 +0000117 for_each_string_list_item(item, include_patterns) {
Derrick Stoleea6be5e62020-04-16 14:15:49 +0000118 if (match_ref_pattern(refname, item))
119 return 1;
Derrick Stoleec9f7a792020-04-16 14:15:48 +0000120 }
Derrick Stoleea6be5e62020-04-16 14:15:49 +0000121 return 0;
Derrick Stoleec9f7a792020-04-16 14:15:48 +0000122 }
Derrick Stoleea6be5e62020-04-16 14:15:49 +0000123
124 if (exclude_patterns_config && exclude_patterns_config->nr) {
125 for_each_string_list_item(item, exclude_patterns_config) {
126 if (match_ref_pattern(refname, item))
127 return 0;
128 }
129 }
130
Derrick Stoleec9f7a792020-04-16 14:15:48 +0000131 return 1;
132}
133
Michael Haggertyf124b732015-05-25 18:38:57 +0000134static int add_ref_decoration(const char *refname, const struct object_id *oid,
135 int flags, void *cb_data)
René Scharfecab4feb2008-09-04 23:39:21 +0200136{
Nguyễn Thái Ngọc Duy5267d292011-08-19 19:43:50 +0700137 struct object *obj;
Nazri Ramliya7524122010-06-19 09:37:34 +0800138 enum decoration_type type = DECORATION_NONE;
Rafael Ascensão65516f52017-11-21 21:33:41 +0000139 struct decoration_filter *filter = (struct decoration_filter *)cb_data;
Nguyễn Thái Ngọc Duy5267d292011-08-19 19:43:50 +0700140
Derrick Stoleec9f7a792020-04-16 14:15:48 +0000141 if (filter && !ref_filter_match(refname, filter))
Rafael Ascensão65516f52017-11-21 21:33:41 +0000142 return 0;
Junio C Hamano429ad202015-05-13 12:40:35 -0700143
Mike Hommey58d121b2015-06-12 06:34:59 +0900144 if (starts_with(refname, git_replace_ref_base)) {
Michael Haggerty3d79f652015-05-25 18:38:58 +0000145 struct object_id original_oid;
Jeff King6ebd1ca2018-07-18 16:45:20 -0400146 if (!read_replace_refs)
Michael J Gruberb9ad5002011-08-25 17:09:30 +0200147 return 0;
Junio C Hamano31a0ad52015-08-03 11:01:10 -0700148 if (get_oid_hex(refname + strlen(git_replace_ref_base),
149 &original_oid)) {
Nguyễn Thái Ngọc Duy5267d292011-08-19 19:43:50 +0700150 warning("invalid replace ref %s", refname);
151 return 0;
152 }
Stefan Beller109cd762018-06-28 18:21:51 -0700153 obj = parse_object(the_repository, &original_oid);
Nguyễn Thái Ngọc Duy5267d292011-08-19 19:43:50 +0700154 if (obj)
155 add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
156 return 0;
157 }
158
Stefan Beller109cd762018-06-28 18:21:51 -0700159 obj = parse_object(the_repository, oid);
René Scharfecab4feb2008-09-04 23:39:21 +0200160 if (!obj)
161 return 0;
Nazri Ramliya7524122010-06-19 09:37:34 +0800162
Christian Couder59556542013-11-30 21:55:40 +0100163 if (starts_with(refname, "refs/heads/"))
Nazri Ramliya7524122010-06-19 09:37:34 +0800164 type = DECORATION_REF_LOCAL;
Christian Couder59556542013-11-30 21:55:40 +0100165 else if (starts_with(refname, "refs/remotes/"))
Nazri Ramliya7524122010-06-19 09:37:34 +0800166 type = DECORATION_REF_REMOTE;
Christian Couder59556542013-11-30 21:55:40 +0100167 else if (starts_with(refname, "refs/tags/"))
Nazri Ramliya7524122010-06-19 09:37:34 +0800168 type = DECORATION_REF_TAG;
Nguyễn Thái Ngọc Duy97ba6422012-01-05 19:39:40 +0700169 else if (!strcmp(refname, "refs/stash"))
Nazri Ramliya7524122010-06-19 09:37:34 +0800170 type = DECORATION_REF_STASH;
Nguyễn Thái Ngọc Duy97ba6422012-01-05 19:39:40 +0700171 else if (!strcmp(refname, "HEAD"))
Nazri Ramliya7524122010-06-19 09:37:34 +0800172 type = DECORATION_REF_HEAD;
173
Nazri Ramliya7524122010-06-19 09:37:34 +0800174 add_name_decoration(type, refname, obj);
René Scharfecab4feb2008-09-04 23:39:21 +0200175 while (obj->type == OBJ_TAG) {
176 obj = ((struct tag *)obj)->tagged;
177 if (!obj)
178 break;
brian m. carlson5e1361c2013-12-17 04:28:21 +0000179 if (!obj->parsed)
Stefan Beller109cd762018-06-28 18:21:51 -0700180 parse_object(the_repository, &obj->oid);
Nazri Ramliya7524122010-06-19 09:37:34 +0800181 add_name_decoration(DECORATION_REF_TAG, refname, obj);
René Scharfecab4feb2008-09-04 23:39:21 +0200182 }
183 return 0;
184}
185
Nguyễn Thái Ngọc Duy76f5df32011-08-18 19:29:37 +0700186static int add_graft_decoration(const struct commit_graft *graft, void *cb_data)
187{
Stefan Bellerc1f5eb42018-06-28 18:21:59 -0700188 struct commit *commit = lookup_commit(the_repository, &graft->oid);
Nguyễn Thái Ngọc Duy76f5df32011-08-18 19:29:37 +0700189 if (!commit)
190 return 0;
191 add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
192 return 0;
193}
194
Rafael Ascensão65516f52017-11-21 21:33:41 +0000195void load_ref_decorations(struct decoration_filter *filter, int flags)
René Scharfecab4feb2008-09-04 23:39:21 +0200196{
Junio C Hamano76c61fb2015-05-13 10:25:18 -0700197 if (!decoration_loaded) {
Rafael Ascensão65516f52017-11-21 21:33:41 +0000198 if (filter) {
199 struct string_list_item *item;
200 for_each_string_list_item(item, filter->exclude_ref_pattern) {
201 normalize_glob_ref(item, NULL, item->string);
202 }
203 for_each_string_list_item(item, filter->include_ref_pattern) {
204 normalize_glob_ref(item, NULL, item->string);
205 }
Derrick Stoleea6be5e62020-04-16 14:15:49 +0000206 for_each_string_list_item(item, filter->exclude_ref_config_pattern) {
207 normalize_glob_ref(item, NULL, item->string);
208 }
Rafael Ascensão65516f52017-11-21 21:33:41 +0000209 }
Junio C Hamano76c61fb2015-05-13 10:25:18 -0700210 decoration_loaded = 1;
211 decoration_flags = flags;
Rafael Ascensão65516f52017-11-21 21:33:41 +0000212 for_each_ref(add_ref_decoration, filter);
213 head_ref(add_ref_decoration, filter);
214 for_each_commit_graft(add_graft_decoration, filter);
René Scharfecab4feb2008-09-04 23:39:21 +0200215 }
216}
217
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200218static void show_parents(struct commit *commit, int abbrev, FILE *file)
Linus Torvaldsc8c893c2006-05-03 07:59:00 -0700219{
220 struct commit_list *p;
221 for (p = commit->parents; p ; p = p->next) {
222 struct commit *parent = p->item;
brian m. carlsonaab95832018-03-12 02:27:30 +0000223 fprintf(file, " %s", find_unique_abbrev(&parent->object.oid, abbrev));
Linus Torvaldsc8c893c2006-05-03 07:59:00 -0700224 }
225}
226
Jay Soffian91b849b2011-10-04 10:02:03 -0400227static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
228{
229 struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
230 for ( ; p; p = p->next) {
brian m. carlsonaab95832018-03-12 02:27:30 +0000231 fprintf(opt->diffopt.file, " %s", find_unique_abbrev(&p->item->object.oid, abbrev));
Jay Soffian91b849b2011-10-04 10:02:03 -0400232 }
233}
234
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000235/*
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100236 * Do we have HEAD in the output, and also the branch it points at?
237 * If so, find that decoration entry for that current branch.
238 */
239static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration)
240{
241 const struct name_decoration *list, *head = NULL;
242 const char *branch_name = NULL;
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100243 int rru_flags;
244
245 /* First find HEAD */
246 for (list = decoration; list; list = list->next)
247 if (list->type == DECORATION_REF_HEAD) {
248 head = list;
249 break;
250 }
251 if (!head)
252 return NULL;
253
254 /* Now resolve and find the matching current branch */
René Scharfe744c0402017-09-23 11:45:04 +0200255 branch_name = resolve_ref_unsafe("HEAD", 0, NULL, &rru_flags);
Jeff Kingd79be492017-10-19 13:49:01 -0400256 if (!branch_name || !(rru_flags & REF_ISSYMREF))
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100257 return NULL;
Junio C Hamano76c61fb2015-05-13 10:25:18 -0700258
Junio C Hamano429ad202015-05-13 12:40:35 -0700259 if (!starts_with(branch_name, "refs/"))
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100260 return NULL;
261
262 /* OK, do we have that ref in the list? */
263 for (list = decoration; list; list = list->next)
264 if ((list->type == DECORATION_REF_LOCAL) &&
265 !strcmp(branch_name, list->name)) {
266 return list;
267 }
268
269 return NULL;
270}
271
Junio C Hamano429ad202015-05-13 12:40:35 -0700272static void show_name(struct strbuf *sb, const struct name_decoration *decoration)
273{
274 if (decoration_flags == DECORATE_SHORT_REFS)
275 strbuf_addstr(sb, prettify_refname(decoration->name));
276 else
277 strbuf_addstr(sb, decoration->name);
278}
279
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100280/*
Harry Jeffery92710952014-09-18 21:53:53 +0100281 * The caller makes sure there is no funny color before calling.
282 * format_decorations_extended makes sure the same after return.
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000283 */
Harry Jeffery92710952014-09-18 21:53:53 +0100284void format_decorations_extended(struct strbuf *sb,
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000285 const struct commit *commit,
Harry Jeffery92710952014-09-18 21:53:53 +0100286 int use_color,
287 const char *prefix,
288 const char *separator,
289 const char *suffix)
Linus Torvaldsca135e72007-04-16 16:05:10 -0700290{
Jeff King2608c242014-08-26 06:23:54 -0400291 const struct name_decoration *decoration;
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100292 const struct name_decoration *current_and_HEAD;
Nazri Ramliy67a4b582010-06-19 09:37:35 +0800293 const char *color_commit =
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000294 diff_get_color(use_color, DIFF_COMMIT);
Nazri Ramliy67a4b582010-06-19 09:37:35 +0800295 const char *color_reset =
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000296 decorate_get_color(use_color, DECORATION_NONE);
Linus Torvaldsca135e72007-04-16 16:05:10 -0700297
Jeff King2608c242014-08-26 06:23:54 -0400298 decoration = get_name_decoration(&commit->object);
Linus Torvaldsca135e72007-04-16 16:05:10 -0700299 if (!decoration)
300 return;
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100301
302 current_and_HEAD = current_pointed_by_HEAD(decoration);
Linus Torvaldsca135e72007-04-16 16:05:10 -0700303 while (decoration) {
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100304 /*
305 * When both current and HEAD are there, only
306 * show HEAD->current where HEAD would have
307 * appeared, skipping the entry for current.
308 */
309 if (decoration != current_and_HEAD) {
310 strbuf_addstr(sb, color_commit);
311 strbuf_addstr(sb, prefix);
312 strbuf_addstr(sb, color_reset);
313 strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
314 if (decoration->type == DECORATION_REF_TAG)
315 strbuf_addstr(sb, "tag: ");
316
Junio C Hamano429ad202015-05-13 12:40:35 -0700317 show_name(sb, decoration);
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100318
319 if (current_and_HEAD &&
320 decoration->type == DECORATION_REF_HEAD) {
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100321 strbuf_addstr(sb, " -> ");
322 strbuf_addstr(sb, color_reset);
323 strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
Junio C Hamano429ad202015-05-13 12:40:35 -0700324 show_name(sb, current_and_HEAD);
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100325 }
326 strbuf_addstr(sb, color_reset);
327
328 prefix = separator;
329 }
Linus Torvaldsca135e72007-04-16 16:05:10 -0700330 decoration = decoration->next;
331 }
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000332 strbuf_addstr(sb, color_commit);
Harry Jeffery92710952014-09-18 21:53:53 +0100333 strbuf_addstr(sb, suffix);
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000334 strbuf_addstr(sb, color_reset);
335}
336
337void show_decorations(struct rev_info *opt, struct commit *commit)
338{
339 struct strbuf sb = STRBUF_INIT;
340
Nguyễn Thái Ngọc Duy87be2522018-05-19 07:28:24 +0200341 if (opt->sources) {
342 char **slot = revision_sources_peek(opt->sources, commit);
343
344 if (slot && *slot)
345 fprintf(opt->diffopt.file, "\t%s", *slot);
346 }
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000347 if (!opt->show_decorations)
348 return;
349 format_decorations(&sb, commit, opt->diffopt.use_color);
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200350 fputs(sb.buf, opt->diffopt.file);
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000351 strbuf_release(&sb);
Linus Torvaldsca135e72007-04-16 16:05:10 -0700352}
353
Johannes Schindeline00de242007-02-09 01:43:54 +0100354static unsigned int digits_in_number(unsigned int number)
355{
356 unsigned int i = 10, result = 1;
357 while (i <= number) {
358 i *= 10;
359 result++;
360 }
361 return result;
362}
363
Junio C Hamanod28b5d42012-12-21 22:06:01 -0800364void fmt_output_subject(struct strbuf *filename,
365 const char *subject,
Junio C Hamano021f2f42012-12-21 23:26:16 -0800366 struct rev_info *info)
Stephen Boyd6fa8e622009-03-22 19:14:04 -0700367{
Junio C Hamano021f2f42012-12-21 23:26:16 -0800368 const char *suffix = info->patch_suffix;
369 int nr = info->nr;
Junio C Hamanod28b5d42012-12-21 22:06:01 -0800370 int start_len = filename->len;
371 int max_len = start_len + FORMAT_PATCH_NAME_MAX - (strlen(suffix) + 1);
Stephen Boyd6fa8e622009-03-22 19:14:04 -0700372
Junio C Hamano5fe10fe2012-12-22 00:21:23 -0800373 if (0 < info->reroll_count)
374 strbuf_addf(filename, "v%d-", info->reroll_count);
Junio C Hamanod28b5d42012-12-21 22:06:01 -0800375 strbuf_addf(filename, "%04d-%s", nr, subject);
Christian Couderb09b8682009-03-27 01:13:01 +0100376
Junio C Hamanod28b5d42012-12-21 22:06:01 -0800377 if (max_len < filename->len)
378 strbuf_setlen(filename, max_len);
379 strbuf_addstr(filename, suffix);
380}
Jeff Kinga21c2f92012-05-21 19:10:32 -0400381
Junio C Hamanod28b5d42012-12-21 22:06:01 -0800382void fmt_output_commit(struct strbuf *filename,
383 struct commit *commit,
384 struct rev_info *info)
385{
386 struct pretty_print_context ctx = {0};
387 struct strbuf subject = STRBUF_INIT;
388
389 format_commit_message(commit, "%f", &subject, &ctx);
390 fmt_output_subject(filename, subject.buf, info);
391 strbuf_release(&subject);
Stephen Boyd6fa8e622009-03-22 19:14:04 -0700392}
393
René Scharfe8ffc8dc2017-03-01 12:36:38 +0100394void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
395{
396 if (opt->total > 0) {
397 strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ",
398 opt->subject_prefix,
399 *opt->subject_prefix ? " " : "",
400 digits_in_number(opt->total),
401 opt->nr, opt->total);
402 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
403 strbuf_addf(sb, "Subject: [%s] ",
404 opt->subject_prefix);
405 } else {
406 strbuf_addstr(sb, "Subject: ");
407 }
408}
409
Stephen Boyd108dab22009-03-22 19:14:05 -0700410void log_write_email_headers(struct rev_info *opt, struct commit *commit,
Junio C Hamano267123b2008-03-15 00:09:20 -0700411 const char **extra_headers_p,
brian m. carlson50cd54e2018-05-02 02:20:52 +0000412 int *need_8bit_cte_p,
413 int maybe_multipart)
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500414{
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500415 const char *extra_headers = opt->extra_headers;
brian m. carlson3a30aa12015-12-15 01:52:04 +0000416 const char *name = oid_to_hex(opt->zero_commit ?
417 &null_oid : &commit->object.oid);
Junio C Hamano267123b2008-03-15 00:09:20 -0700418
419 *need_8bit_cte_p = 0; /* unknown */
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500420
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200421 fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700422 graph_show_oneline(opt->graph);
423 if (opt->message_id) {
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200424 fprintf(opt->diffopt.file, "Message-Id: <%s>\n", opt->message_id);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700425 graph_show_oneline(opt->graph);
426 }
Thomas Rastb079c502009-02-19 22:26:31 +0100427 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
428 int i, n;
429 n = opt->ref_message_ids->nr;
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200430 fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
Thomas Rastb079c502009-02-19 22:26:31 +0100431 for (i = 0; i < n; i++)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200432 fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
Thomas Rastb079c502009-02-19 22:26:31 +0100433 opt->ref_message_ids->items[i].string);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700434 graph_show_oneline(opt->graph);
435 }
brian m. carlson50cd54e2018-05-02 02:20:52 +0000436 if (opt->mime_boundary && maybe_multipart) {
Jeff Kingd50b69b2018-05-18 18:57:44 -0700437 static struct strbuf subject_buffer = STRBUF_INIT;
438 static struct strbuf buffer = STRBUF_INIT;
Stephen Boyd108dab22009-03-22 19:14:05 -0700439 struct strbuf filename = STRBUF_INIT;
Junio C Hamano267123b2008-03-15 00:09:20 -0700440 *need_8bit_cte_p = -1; /* NEVER */
Jeff Kingd50b69b2018-05-18 18:57:44 -0700441
442 strbuf_reset(&subject_buffer);
443 strbuf_reset(&buffer);
444
445 strbuf_addf(&subject_buffer,
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500446 "%s"
447 "MIME-Version: 1.0\n"
448 "Content-Type: multipart/mixed;"
449 " boundary=\"%s%s\"\n"
450 "\n"
451 "This is a multi-part message in MIME "
452 "format.\n"
453 "--%s%s\n"
454 "Content-Type: text/plain; "
455 "charset=UTF-8; format=fixed\n"
456 "Content-Transfer-Encoding: 8bit\n\n",
457 extra_headers ? extra_headers : "",
458 mime_boundary_leader, opt->mime_boundary,
459 mime_boundary_leader, opt->mime_boundary);
Jeff Kingd50b69b2018-05-18 18:57:44 -0700460 extra_headers = subject_buffer.buf;
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500461
Junio C Hamano38ec23a2012-12-21 21:39:37 -0800462 if (opt->numbered_files)
463 strbuf_addf(&filename, "%d", opt->nr);
464 else
Junio C Hamanod28b5d42012-12-21 22:06:01 -0800465 fmt_output_commit(&filename, commit, opt);
Jeff Kingd50b69b2018-05-18 18:57:44 -0700466 strbuf_addf(&buffer,
Kevin Ballard6b2fbaa2008-07-29 22:49:33 -0700467 "\n--%s%s\n"
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500468 "Content-Type: text/x-patch;"
Stephen Boyd108dab22009-03-22 19:14:05 -0700469 " name=\"%s\"\n"
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500470 "Content-Transfer-Encoding: 8bit\n"
471 "Content-Disposition: %s;"
Stephen Boyd108dab22009-03-22 19:14:05 -0700472 " filename=\"%s\"\n\n",
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500473 mime_boundary_leader, opt->mime_boundary,
Stephen Boyd108dab22009-03-22 19:14:05 -0700474 filename.buf,
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500475 opt->no_inline ? "attachment" : "inline",
Stephen Boyd108dab22009-03-22 19:14:05 -0700476 filename.buf);
Jeff Kingd50b69b2018-05-18 18:57:44 -0700477 opt->diffopt.stat_sep = buffer.buf;
Stephen Boyd108dab22009-03-22 19:14:05 -0700478 strbuf_release(&filename);
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500479 }
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500480 *extra_headers_p = extra_headers;
481}
482
Junio C Hamanoc6b3ec42012-01-04 13:48:45 -0800483static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
484{
485 const char *color, *reset, *eol;
486
487 color = diff_get_color_opt(&opt->diffopt,
488 status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
489 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
490 while (*bol) {
491 eol = strchrnul(bol, '\n');
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200492 fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
Junio C Hamanoc6b3ec42012-01-04 13:48:45 -0800493 *eol ? "\n" : "");
Zoltan Klingercf3983d2014-07-09 12:10:21 +1000494 graph_show_oneline(opt->graph);
Junio C Hamanoc6b3ec42012-01-04 13:48:45 -0800495 bol = (*eol) ? (eol + 1) : eol;
496 }
497}
498
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700499static void show_signature(struct rev_info *opt, struct commit *commit)
500{
501 struct strbuf payload = STRBUF_INIT;
502 struct strbuf signature = STRBUF_INIT;
Hans Jerry Illikainen67948982020-03-04 11:48:04 +0000503 struct signature_check sigc = { 0 };
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700504 int status;
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700505
Jeff King218aa3a2014-06-13 02:32:11 -0400506 if (parse_signed_commit(commit, &payload, &signature) <= 0)
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700507 goto out;
508
Hans Jerry Illikainen67948982020-03-04 11:48:04 +0000509 status = check_signature(payload.buf, payload.len, signature.buf,
510 signature.len, &sigc);
511 if (status && !sigc.gpg_output)
512 show_sig_lines(opt, status, "No signature\n");
513 else
514 show_sig_lines(opt, status, sigc.gpg_output);
515 signature_check_clear(&sigc);
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700516
517 out:
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700518 strbuf_release(&payload);
519 strbuf_release(&signature);
520}
521
brian m. carlsona92ea682017-05-06 22:10:18 +0000522static int which_parent(const struct object_id *oid, const struct commit *commit)
Junio C Hamano824958e2012-01-04 13:51:28 -0800523{
524 int nth;
525 const struct commit_list *parent;
526
527 for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
Jeff King4a7e27e2018-08-28 17:22:40 -0400528 if (oideq(&parent->item->object.oid, oid))
Junio C Hamano824958e2012-01-04 13:51:28 -0800529 return nth;
530 nth++;
531 }
532 return -1;
533}
534
Junio C Hamanod041ffa2012-01-04 16:23:12 -0800535static int is_common_merge(const struct commit *commit)
536{
537 return (commit->parents
538 && commit->parents->next
539 && !commit->parents->next->next);
540}
541
Johannes Schindelinfef461e2018-04-25 11:54:04 +0200542static int show_one_mergetag(struct commit *commit,
543 struct commit_extra_header *extra,
544 void *data)
Junio C Hamano824958e2012-01-04 13:51:28 -0800545{
Christian Couder063da622014-07-07 08:35:37 +0200546 struct rev_info *opt = (struct rev_info *)data;
brian m. carlsona92ea682017-05-06 22:10:18 +0000547 struct object_id oid;
Junio C Hamano824958e2012-01-04 13:51:28 -0800548 struct tag *tag;
549 struct strbuf verify_message;
Hans Jerry Illikainen67948982020-03-04 11:48:04 +0000550 struct signature_check sigc = { 0 };
Junio C Hamano824958e2012-01-04 13:51:28 -0800551 int status, nth;
Hans Jerry Illikainen67948982020-03-04 11:48:04 +0000552 size_t payload_size;
Junio C Hamano824958e2012-01-04 13:51:28 -0800553
Matheus Tavares2dcde202020-01-30 17:32:22 -0300554 hash_object_file(the_hash_algo, extra->value, extra->len,
555 type_name(OBJ_TAG), &oid);
Stefan Bellerce71efb2018-06-28 18:22:03 -0700556 tag = lookup_tag(the_repository, &oid);
Junio C Hamano824958e2012-01-04 13:51:28 -0800557 if (!tag)
Johannes Schindelinfef461e2018-04-25 11:54:04 +0200558 return -1; /* error message already given */
Junio C Hamano824958e2012-01-04 13:51:28 -0800559
560 strbuf_init(&verify_message, 256);
Stefan Beller0e740fe2018-06-28 18:22:04 -0700561 if (parse_tag_buffer(the_repository, tag, extra->value, extra->len))
Junio C Hamano824958e2012-01-04 13:51:28 -0800562 strbuf_addstr(&verify_message, "malformed mergetag\n");
Junio C Hamanod041ffa2012-01-04 16:23:12 -0800563 else if (is_common_merge(commit) &&
Jeff King4a7e27e2018-08-28 17:22:40 -0400564 oideq(&tag->tagged->oid,
565 &commit->parents->next->item->object.oid))
Junio C Hamanod041ffa2012-01-04 16:23:12 -0800566 strbuf_addf(&verify_message,
567 "merged tag '%s'\n", tag->tag);
brian m. carlsona92ea682017-05-06 22:10:18 +0000568 else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0)
Junio C Hamano824958e2012-01-04 13:51:28 -0800569 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
Harald van Dijk237a2812020-02-29 13:07:57 +0000570 tag->tag, oid_to_hex(&tag->tagged->oid));
Junio C Hamano824958e2012-01-04 13:51:28 -0800571 else
572 strbuf_addf(&verify_message,
573 "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
Junio C Hamano824958e2012-01-04 13:51:28 -0800574
575 payload_size = parse_signature(extra->value, extra->len);
Michael J Gruber13150932013-02-14 17:04:43 +0100576 status = -1;
Michael J Gruber42c55ce2014-06-27 15:18:36 +0200577 if (extra->len > payload_size) {
578 /* could have a good signature */
Hans Jerry Illikainen67948982020-03-04 11:48:04 +0000579 status = check_signature(extra->value, payload_size,
580 extra->value + payload_size,
581 extra->len - payload_size, &sigc);
582 if (sigc.gpg_output)
583 strbuf_addstr(&verify_message, sigc.gpg_output);
584 else
Michael J Gruber42c55ce2014-06-27 15:18:36 +0200585 strbuf_addstr(&verify_message, "No signature\n");
Hans Jerry Illikainen67948982020-03-04 11:48:04 +0000586 signature_check_clear(&sigc);
Michael J Gruber42c55ce2014-06-27 15:18:36 +0200587 /* otherwise we couldn't verify, which is shown as bad */
588 }
Junio C Hamano824958e2012-01-04 13:51:28 -0800589
590 show_sig_lines(opt, status, verify_message.buf);
591 strbuf_release(&verify_message);
Johannes Schindelinfef461e2018-04-25 11:54:04 +0200592 return 0;
Junio C Hamano824958e2012-01-04 13:51:28 -0800593}
594
Johannes Schindelinfef461e2018-04-25 11:54:04 +0200595static int show_mergetag(struct rev_info *opt, struct commit *commit)
Junio C Hamano824958e2012-01-04 13:51:28 -0800596{
Johannes Schindelinfef461e2018-04-25 11:54:04 +0200597 return for_each_mergetag(show_one_mergetag, commit, opt);
Junio C Hamano824958e2012-01-04 13:51:28 -0800598}
599
Eric Sunshine3fcc7a22018-07-22 05:57:08 -0400600static void next_commentary_block(struct rev_info *opt, struct strbuf *sb)
601{
602 const char *x = opt->shown_dashes ? "\n" : "---\n";
603 if (sb)
604 strbuf_addstr(sb, x);
605 else
606 fputs(x, opt->diffopt.file);
607 opt->shown_dashes = 1;
608}
609
Adam Simpkins02865652008-04-29 01:32:59 -0700610void show_log(struct rev_info *opt)
Linus Torvalds91539832006-04-17 11:59:32 -0700611{
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500612 struct strbuf msgbuf = STRBUF_INIT;
Timo Hirvonen39bc9a62006-06-25 13:54:14 +0300613 struct log_info *log = opt->loginfo;
Linus Torvalds91539832006-04-17 11:59:32 -0700614 struct commit *commit = log->commit, *parent = log->parent;
brian m. carlson2ed19602018-07-16 01:28:06 +0000615 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : the_hash_algo->hexsz;
Thomas Rastdd2e7942009-10-19 17:48:08 +0200616 const char *extra_headers = opt->extra_headers;
617 struct pretty_print_context ctx = {0};
Linus Torvalds91539832006-04-17 11:59:32 -0700618
619 opt->loginfo = NULL;
620 if (!opt->verbose_header) {
Adam Simpkins7fefda52008-05-04 03:36:54 -0700621 graph_show_commit(opt->graph);
622
Michael J Gruber1df2d652011-03-07 13:31:39 +0100623 if (!opt->graph)
Michael J Gruberb1b47552011-03-10 15:45:03 +0100624 put_revision_mark(opt, commit);
brian m. carlsonaab95832018-03-12 02:27:30 +0000625 fputs(find_unique_abbrev(&commit->object.oid, abbrev_commit), opt->diffopt.file);
Adam Simpkins885cf802008-05-04 03:36:52 -0700626 if (opt->print_parents)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200627 show_parents(commit, abbrev_commit, opt->diffopt.file);
Jay Soffian91b849b2011-10-04 10:02:03 -0400628 if (opt->children.name)
629 show_children(opt, commit, abbrev_commit);
Linus Torvalds0f3a2902008-10-27 12:51:59 -0700630 show_decorations(opt, commit);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700631 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200632 putc('\n', opt->diffopt.file);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700633 graph_show_remainder(opt->graph);
634 }
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200635 putc(opt->diffopt.line_termination, opt->diffopt.file);
Linus Torvalds91539832006-04-17 11:59:32 -0700636 return;
637 }
638
639 /*
Jeff King87152272009-07-01 03:26:28 -0400640 * If use_terminator is set, we already handled any record termination
641 * at the end of the last record.
Adam Simpkins02865652008-04-29 01:32:59 -0700642 * Otherwise, add a diffopt.line_termination character before all
643 * entries but the first. (IOW, as a separator between entries)
Linus Torvalds91539832006-04-17 11:59:32 -0700644 */
Adam Simpkins7fefda52008-05-04 03:36:54 -0700645 if (opt->shown_one && !opt->use_terminator) {
646 /*
647 * If entries are separated by a newline, the output
648 * should look human-readable. If the last entry ended
649 * with a newline, print the graph output before this
650 * newline. Otherwise it will end up as a completely blank
651 * line and will look like a gap in the graph.
652 *
653 * If the entry separator is not a newline, the output is
654 * primarily intended for programmatic consumption, and we
655 * never want the extra graph output before the entry
656 * separator.
657 */
658 if (opt->diffopt.line_termination == '\n' &&
659 !opt->missing_newline)
660 graph_show_padding(opt->graph);
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200661 putc(opt->diffopt.line_termination, opt->diffopt.file);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700662 }
Linus Torvalds91539832006-04-17 11:59:32 -0700663 opt->shown_one = 1;
664
665 /*
Adam Simpkins7fefda52008-05-04 03:36:54 -0700666 * If the history graph was requested,
667 * print the graph, up to this commit's line
668 */
669 graph_show_commit(opt->graph);
670
671 /*
Linus Torvalds91539832006-04-17 11:59:32 -0700672 * Print header line of header..
673 */
Junio C Hamano3eefc182006-04-18 16:45:27 -0700674
Eric Wong9f23e042016-06-05 04:46:39 +0000675 if (cmit_fmt_is_mail(opt->commit_format)) {
René Scharfe6d167fd2017-03-01 12:37:07 +0100676 log_write_email_headers(opt, commit, &extra_headers,
brian m. carlson50cd54e2018-05-02 02:20:52 +0000677 &ctx.need_8bit_cte, 1);
René Scharfe6d167fd2017-03-01 12:37:07 +0100678 ctx.rev = opt;
679 ctx.print_email_subject = 1;
Johannes Schindeline52a5de2007-02-23 01:35:03 +0100680 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200681 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
Junio C Hamano74bd9022006-12-16 15:31:25 -0800682 if (opt->commit_format != CMIT_FMT_ONELINE)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200683 fputs("commit ", opt->diffopt.file);
Adam Simpkins7528f272008-05-25 00:07:21 -0700684
Michael J Gruber1df2d652011-03-07 13:31:39 +0100685 if (!opt->graph)
Michael J Gruberb1b47552011-03-10 15:45:03 +0100686 put_revision_mark(opt, commit);
brian m. carlsonaab95832018-03-12 02:27:30 +0000687 fputs(find_unique_abbrev(&commit->object.oid,
688 abbrev_commit),
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200689 opt->diffopt.file);
Adam Simpkins885cf802008-05-04 03:36:52 -0700690 if (opt->print_parents)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200691 show_parents(commit, abbrev_commit, opt->diffopt.file);
Jay Soffian91b849b2011-10-04 10:02:03 -0400692 if (opt->children.name)
693 show_children(opt, commit, abbrev_commit);
Junio C Hamano73f0a152006-05-24 12:19:47 -0700694 if (parent)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200695 fprintf(opt->diffopt.file, " (from %s)",
brian m. carlsonaab95832018-03-12 02:27:30 +0000696 find_unique_abbrev(&parent->object.oid, abbrev_commit));
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200697 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
Linus Torvalds0f3a2902008-10-27 12:51:59 -0700698 show_decorations(opt, commit);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700699 if (opt->commit_format == CMIT_FMT_ONELINE) {
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200700 putc(' ', opt->diffopt.file);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700701 } else {
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200702 putc('\n', opt->diffopt.file);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700703 graph_show_oneline(opt->graph);
704 }
Nicolas Pitre903b45f2007-01-27 22:40:36 -0500705 if (opt->reflog_info) {
Adam Simpkins7fefda52008-05-04 03:36:54 -0700706 /*
707 * setup_revisions() ensures that opt->reflog_info
708 * and opt->graph cannot both be set,
709 * so we don't need to worry about printing the
710 * graph info here.
711 */
Junio C Hamano4d12a472007-01-20 00:51:41 -0800712 show_reflog_message(opt->reflog_info,
Junio C Hamano55ccf852012-05-07 14:11:32 -0700713 opt->commit_format == CMIT_FMT_ONELINE,
Jeff Kinga5481a62015-06-25 12:55:02 -0400714 &opt->date_mode,
Junio C Hamano55ccf852012-05-07 14:11:32 -0700715 opt->date_mode_explicit);
Adam Simpkins02865652008-04-29 01:32:59 -0700716 if (opt->commit_format == CMIT_FMT_ONELINE)
Nicolas Pitre903b45f2007-01-27 22:40:36 -0500717 return;
Nicolas Pitre903b45f2007-01-27 22:40:36 -0500718 }
Junio C Hamano3eefc182006-04-18 16:45:27 -0700719 }
Linus Torvalds91539832006-04-17 11:59:32 -0700720
Junio C Hamano824958e2012-01-04 13:51:28 -0800721 if (opt->show_signature) {
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700722 show_signature(opt, commit);
Junio C Hamano824958e2012-01-04 13:51:28 -0800723 show_mergetag(opt, commit);
724 }
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700725
Junio C Hamanoddf333f2012-10-17 18:51:47 -0700726 if (opt->show_notes) {
727 int raw;
728 struct strbuf notebuf = STRBUF_INIT;
729
730 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
brian m. carlsonfb61e4d2017-05-30 10:30:41 -0700731 format_display_notes(&commit->object.oid, &notebuf,
Junio C Hamanoddf333f2012-10-17 18:51:47 -0700732 get_log_output_encoding(), raw);
René Scharfe82f51af2019-08-25 14:53:26 +0200733 ctx.notes_message = strbuf_detach(&notebuf, NULL);
Junio C Hamanoddf333f2012-10-17 18:51:47 -0700734 }
735
Linus Torvalds91539832006-04-17 11:59:32 -0700736 /*
737 * And then the pretty-printed message itself
738 */
Nguyễn Thái Ngọc Duy5289c562013-02-12 02:17:38 -0800739 if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
740 ctx.need_8bit_cte =
William Hubbs39ab4d02019-02-04 12:48:50 -0600741 has_non_ascii(fmt_name(WANT_COMMITTER_IDENT));
Thomas Rastdd2e7942009-10-19 17:48:08 +0200742 ctx.date_mode = opt->date_mode;
Jeff Kingf026c752012-05-04 01:25:18 -0400743 ctx.date_mode_explicit = opt->date_mode_explicit;
Thomas Rastdd2e7942009-10-19 17:48:08 +0200744 ctx.abbrev = opt->diffopt.abbrev;
745 ctx.after_subject = extra_headers;
Jeff King9553d2b2011-05-26 18:28:17 -0400746 ctx.preserve_subject = opt->preserve_subject;
Emma Brooks19d097e2020-04-08 04:31:38 +0000747 ctx.encode_email_headers = opt->encode_email_headers;
Thomas Rast8f8f5472009-10-19 17:48:10 +0200748 ctx.reflog_info = opt->reflog_info;
Jeff King6bf13942011-05-26 18:27:49 -0400749 ctx.fmt = opt->commit_format;
Antoine Pelisse0e2913b2013-01-05 22:26:41 +0100750 ctx.mailmap = opt->mailmap;
Junio C Hamano30825172012-12-17 17:56:49 -0500751 ctx.color = opt->diffopt.use_color;
Linus Torvalds7cc13c72016-03-16 09:15:53 -0700752 ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
Alexey Shumkinecaee802013-06-26 14:19:50 +0400753 ctx.output_encoding = get_log_output_encoding();
Issac Trottsad6f0282019-01-10 22:30:46 -0800754 ctx.rev = opt;
Jeff Kinga9080472013-07-03 03:08:22 -0400755 if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
756 ctx.from_ident = &opt->from_ident;
Josef Kufner3ad87c82016-06-16 20:18:37 +0700757 if (opt->graph)
758 ctx.graph_width = graph_width(opt->graph);
Jeff King6bf13942011-05-26 18:27:49 -0400759 pretty_print_commit(&ctx, commit, &msgbuf);
Junio C Hamano212620f2012-10-17 20:48:25 -0700760
761 if (opt->add_signoff)
Nguyễn Thái Ngọc Duy5289c562013-02-12 02:17:38 -0800762 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
Junio C Hamano212620f2012-10-17 20:48:25 -0700763
Junio C Hamano5a664cf2012-10-17 19:02:46 -0700764 if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
Junio C Hamanobd1470b2012-10-17 21:27:22 -0700765 ctx.notes_message && *ctx.notes_message) {
Eric Sunshine3fcc7a22018-07-22 05:57:08 -0400766 if (cmit_fmt_is_mail(ctx.fmt))
767 next_commentary_block(opt, &msgbuf);
Junio C Hamano5a664cf2012-10-17 19:02:46 -0700768 strbuf_addstr(&msgbuf, ctx.notes_message);
Junio C Hamanobd1470b2012-10-17 21:27:22 -0700769 }
Junio C Hamanocf2251b2006-05-31 15:11:49 -0700770
Adam Simpkins7fefda52008-05-04 03:36:54 -0700771 if (opt->show_log_size) {
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200772 fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700773 graph_show_oneline(opt->graph);
774 }
Marco Costalba9fa34652007-07-20 20:15:13 +0200775
Adam Simpkins7fefda52008-05-04 03:36:54 -0700776 /*
777 * Set opt->missing_newline if msgbuf doesn't
778 * end in a newline (including if it is empty)
779 */
780 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
781 opt->missing_newline = 1;
782 else
783 opt->missing_newline = 0;
784
Jacob Keller660e1132016-08-31 16:27:20 -0700785 graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf);
Jeff Kingb9c7d6e2014-07-29 13:56:48 -0400786 if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
Adam Simpkins7fefda52008-05-04 03:36:54 -0700787 if (!opt->missing_newline)
788 graph_show_padding(opt->graph);
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200789 putc(opt->diffopt.line_termination, opt->diffopt.file);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700790 }
791
Pierre Habouzit674d1722007-09-10 12:35:06 +0200792 strbuf_release(&msgbuf);
Junio C Hamanoddf333f2012-10-17 18:51:47 -0700793 free(ctx.notes_message);
Eric Sunshineee6cbf72018-07-22 05:57:09 -0400794
795 if (cmit_fmt_is_mail(ctx.fmt) && opt->idiff_oid1) {
796 struct diff_queue_struct dq;
797
798 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
799 DIFF_QUEUE_CLEAR(&diff_queued_diff);
800
801 next_commentary_block(opt, NULL);
802 fprintf_ln(opt->diffopt.file, "%s", opt->idiff_title);
803 show_interdiff(opt, 2);
804
805 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
806 }
Eric Sunshine40ce4162018-07-22 05:57:17 -0400807
808 if (cmit_fmt_is_mail(ctx.fmt) && opt->rdiff1) {
809 struct diff_queue_struct dq;
Martin Ågrenac0edf12018-12-03 16:21:31 -0500810 struct diff_options opts;
Eric Sunshine40ce4162018-07-22 05:57:17 -0400811
812 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
813 DIFF_QUEUE_CLEAR(&diff_queued_diff);
814
815 next_commentary_block(opt, NULL);
816 fprintf_ln(opt->diffopt.file, "%s", opt->rdiff_title);
Martin Ågrenac0edf12018-12-03 16:21:31 -0500817 /*
818 * Pass minimum required diff-options to range-diff; others
819 * can be added later if deemed desirable.
820 */
821 diff_setup(&opts);
822 opts.file = opt->diffopt.file;
823 opts.use_color = opt->diffopt.use_color;
824 diff_setup_done(&opts);
Eric Sunshine40ce4162018-07-22 05:57:17 -0400825 show_range_diff(opt->rdiff1, opt->rdiff2,
Denton Liubd361912019-11-20 13:18:45 -0800826 opt->creation_factor, 1, &opts, NULL);
Eric Sunshine40ce4162018-07-22 05:57:17 -0400827
828 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
829 }
Linus Torvalds91539832006-04-17 11:59:32 -0700830}
831
Linus Torvaldscd2bdc52006-04-14 16:52:13 -0700832int log_tree_diff_flush(struct rev_info *opt)
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700833{
Junio C Hamanobd1470b2012-10-17 21:27:22 -0700834 opt->shown_dashes = 0;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700835 diffcore_std(&opt->diffopt);
Linus Torvalds91539832006-04-17 11:59:32 -0700836
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700837 if (diff_queue_is_empty()) {
838 int saved_fmt = opt->diffopt.output_format;
839 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
840 diff_flush(&opt->diffopt);
841 opt->diffopt.output_format = saved_fmt;
842 return 0;
843 }
Linus Torvalds91539832006-04-17 11:59:32 -0700844
Junio C Hamano3969cf72006-06-27 15:08:19 -0700845 if (opt->loginfo && !opt->no_commit_id) {
Adam Simpkins02865652008-04-29 01:32:59 -0700846 show_log(opt);
Linus Torvalds304b5af2007-10-09 09:35:22 -0700847 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
848 opt->verbose_header &&
Jeff Kingb9c7d6e2014-07-29 13:56:48 -0400849 opt->commit_format != CMIT_FMT_ONELINE &&
850 !commit_format_is_empty(opt->commit_format)) {
Junio C Hamano1d34c502012-11-13 10:09:07 -0800851 /*
852 * When showing a verbose header (i.e. log message),
853 * and not in --pretty=oneline format, we would want
854 * an extra newline between the end of log and the
855 * diff/diffstat output for readability.
856 */
Junio C Hamano3969cf72006-06-27 15:08:19 -0700857 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
Bo Yang81bd1b22010-05-26 15:08:03 +0800858 if (opt->diffopt.output_prefix) {
859 struct strbuf *msg = NULL;
860 msg = opt->diffopt.output_prefix(&opt->diffopt,
861 opt->diffopt.output_prefix_data);
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200862 fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
Bo Yang81bd1b22010-05-26 15:08:03 +0800863 }
Junio C Hamano1d34c502012-11-13 10:09:07 -0800864
865 /*
866 * We may have shown three-dashes line early
Eric Sunshine3fcc7a22018-07-22 05:57:08 -0400867 * between generated commentary (notes, etc.)
868 * and the log message, in which case we only
869 * want a blank line after the commentary
870 * without (an extra) three-dashes line.
Junio C Hamano1d34c502012-11-13 10:09:07 -0800871 * Otherwise, we show the three-dashes line if
872 * we are showing the patch with diffstat, but
873 * in that case, there is no extra blank line
874 * after the three-dashes line.
875 */
876 if (!opt->shown_dashes &&
877 (pch & opt->diffopt.output_format) == pch)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200878 fprintf(opt->diffopt.file, "---");
879 putc('\n', opt->diffopt.file);
Junio C Hamano3969cf72006-06-27 15:08:19 -0700880 }
881 }
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700882 diff_flush(&opt->diffopt);
883 return 1;
884}
885
Linus Torvaldscd2bdc52006-04-14 16:52:13 -0700886static int do_diff_combined(struct rev_info *opt, struct commit *commit)
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700887{
René Scharfe82889292011-12-17 11:20:07 +0100888 diff_tree_combined_merge(commit, opt->dense_combined_merges, opt);
Linus Torvalds91539832006-04-17 11:59:32 -0700889 return !opt->loginfo;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700890}
891
Linus Torvalds91539832006-04-17 11:59:32 -0700892/*
893 * Show the diff of a commit.
894 *
895 * Return true if we printed any log info messages
896 */
897static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700898{
Linus Torvalds91539832006-04-17 11:59:32 -0700899 int showed_log;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700900 struct commit_list *parents;
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000901 struct object_id *oid;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700902
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700903 if (!opt->diff && !opt->diffopt.flags.exit_with_status)
Linus Torvalds91539832006-04-17 11:59:32 -0700904 return 0;
905
Jeff King7059dcc2013-10-24 04:52:36 -0400906 parse_commit_or_die(commit);
Derrick Stolee2e27bd72018-04-06 19:09:38 +0000907 oid = get_commit_tree_oid(commit);
Thomas Rastd1b9b762013-03-28 09:19:34 +0100908
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700909 /* Root commit? */
Thomas Rast53d00b32013-07-31 22:13:20 +0200910 parents = get_saved_parents(opt, commit);
Linus Torvalds91539832006-04-17 11:59:32 -0700911 if (!parents) {
Rene Scharfe2b603562006-10-26 18:52:39 +0200912 if (opt->show_root_diff) {
Brandon Williams7b8dea02017-05-30 10:30:57 -0700913 diff_root_tree_oid(oid, "", &opt->diffopt);
Rene Scharfe2b603562006-10-26 18:52:39 +0200914 log_tree_diff_flush(opt);
915 }
Linus Torvalds91539832006-04-17 11:59:32 -0700916 return !opt->loginfo;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700917 }
918
919 /* More than one parent? */
Linus Torvalds91539832006-04-17 11:59:32 -0700920 if (parents && parents->next) {
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700921 if (opt->ignore_merges)
922 return 0;
923 else if (opt->combine_merges)
924 return do_diff_combined(opt, commit);
Petr Baudis88d9d452010-02-10 02:11:49 +0100925 else if (opt->first_parent_only) {
926 /*
927 * Generate merge log entry only for the first
928 * parent, showing summary diff of the others
929 * we merged _in_.
930 */
Jeff King7059dcc2013-10-24 04:52:36 -0400931 parse_commit_or_die(parents->item);
Derrick Stolee2e27bd72018-04-06 19:09:38 +0000932 diff_tree_oid(get_commit_tree_oid(parents->item),
Brandon Williams66f414f2017-05-30 10:31:03 -0700933 oid, "", &opt->diffopt);
Petr Baudis88d9d452010-02-10 02:11:49 +0100934 log_tree_diff_flush(opt);
935 return !opt->loginfo;
936 }
Linus Torvalds91539832006-04-17 11:59:32 -0700937
938 /* If we show individual diffs, show the parent info */
939 log->parent = parents->item;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700940 }
941
Linus Torvalds91539832006-04-17 11:59:32 -0700942 showed_log = 0;
943 for (;;) {
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700944 struct commit *parent = parents->item;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700945
Jeff King7059dcc2013-10-24 04:52:36 -0400946 parse_commit_or_die(parent);
Derrick Stolee2e27bd72018-04-06 19:09:38 +0000947 diff_tree_oid(get_commit_tree_oid(parent),
Brandon Williams66f414f2017-05-30 10:31:03 -0700948 oid, "", &opt->diffopt);
Linus Torvalds91539832006-04-17 11:59:32 -0700949 log_tree_diff_flush(opt);
950
951 showed_log |= !opt->loginfo;
952
953 /* Set up the log info for the next parent, if any.. */
954 parents = parents->next;
955 if (!parents)
956 break;
957 log->parent = parents->item;
958 opt->loginfo = log;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700959 }
Linus Torvalds91539832006-04-17 11:59:32 -0700960 return showed_log;
961}
962
963int log_tree_commit(struct rev_info *opt, struct commit *commit)
964{
965 struct log_info log;
Johannes Schindelin6ea57702016-06-22 17:01:28 +0200966 int shown, close_file = opt->diffopt.close_file;
Linus Torvalds91539832006-04-17 11:59:32 -0700967
968 log.commit = commit;
969 log.parent = NULL;
970 opt->loginfo = &log;
Johannes Schindelin6ea57702016-06-22 17:01:28 +0200971 opt->diffopt.close_file = 0;
Linus Torvalds91539832006-04-17 11:59:32 -0700972
Thomas Rast12da1d12013-03-28 17:47:32 +0100973 if (opt->line_level_traverse)
974 return line_log_print(opt, commit);
975
Nguyễn Thái Ngọc Duy1b32dec2014-03-25 20:23:27 +0700976 if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200977 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
Junio C Hamano3eefc182006-04-18 16:45:27 -0700978 shown = log_tree_diff(opt, commit, &log);
979 if (!shown && opt->loginfo && opt->always_show_header) {
Linus Torvalds91539832006-04-17 11:59:32 -0700980 log.parent = NULL;
Adam Simpkins02865652008-04-29 01:32:59 -0700981 show_log(opt);
Junio C Hamano3eefc182006-04-18 16:45:27 -0700982 shown = 1;
Linus Torvalds91539832006-04-17 11:59:32 -0700983 }
Nguyễn Thái Ngọc Duy1b32dec2014-03-25 20:23:27 +0700984 if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200985 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
Linus Torvalds91539832006-04-17 11:59:32 -0700986 opt->loginfo = NULL;
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200987 maybe_flush_or_die(opt->diffopt.file, "stdout");
Johannes Schindelin6ea57702016-06-22 17:01:28 +0200988 if (close_file)
989 fclose(opt->diffopt.file);
Junio C Hamano3eefc182006-04-18 16:45:27 -0700990 return shown;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700991}