blob: f3178a66a95bb15915eaca9713dc442dd7397984 [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 Sunshine40ce4162018-07-22 05:57:17 -040018#include "range-diff.h"
Junio C Hamano5f1c3f02006-04-09 01:11:11 -070019
Jeff King2608c242014-08-26 06:23:54 -040020static struct decoration name_decoration = { "object names" };
Junio C Hamano76c61fb2015-05-13 10:25:18 -070021static int decoration_loaded;
22static int decoration_flags;
Nazri Ramliya7524122010-06-19 09:37:34 +080023
Nazri Ramliy67a4b582010-06-19 09:37:35 +080024static char decoration_colors[][COLOR_MAXLEN] = {
25 GIT_COLOR_RESET,
26 GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */
27 GIT_COLOR_BOLD_RED, /* REF_REMOTE */
28 GIT_COLOR_BOLD_YELLOW, /* REF_TAG */
29 GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */
30 GIT_COLOR_BOLD_CYAN, /* REF_HEAD */
Nguyễn Thái Ngọc Duy76f5df32011-08-18 19:29:37 +070031 GIT_COLOR_BOLD_BLUE, /* GRAFTED */
Nazri Ramliy67a4b582010-06-19 09:37:35 +080032};
33
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +020034static const char *color_decorate_slots[] = {
35 [DECORATION_REF_LOCAL] = "branch",
36 [DECORATION_REF_REMOTE] = "remoteBranch",
37 [DECORATION_REF_TAG] = "tag",
38 [DECORATION_REF_STASH] = "stash",
39 [DECORATION_REF_HEAD] = "HEAD",
Nguyễn Thái Ngọc Duy09c4ba42018-05-26 15:55:31 +020040 [DECORATION_GRAFTED] = "grafted",
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +020041};
42
Nazri Ramliy67a4b582010-06-19 09:37:35 +080043static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
44{
Jeff Kingdaa0c3d2011-08-17 22:04:23 -070045 if (want_color(decorate_use_color))
Nazri Ramliy67a4b582010-06-19 09:37:35 +080046 return decoration_colors[ix];
47 return "";
48}
49
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +020050define_list_config_array(color_decorate_slots);
Nazri Ramliy5e11bee2010-06-24 08:21:16 +080051
Jonathan Nieder88521172014-10-07 15:16:57 -040052int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
Nazri Ramliy5e11bee2010-06-24 08:21:16 +080053{
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +020054 int slot = LOOKUP_CONFIG(color_decorate_slots, slot_name);
Nazri Ramliy5e11bee2010-06-24 08:21:16 +080055 if (slot < 0)
56 return 0;
57 if (!value)
58 return config_error_nonbool(var);
Jeff Kingf6c5a292014-10-07 15:33:09 -040059 return color_parse(value, decoration_colors[slot]);
Nazri Ramliy5e11bee2010-06-24 08:21:16 +080060}
61
Nazri Ramliy67a4b582010-06-19 09:37:35 +080062/*
63 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
64 * for showing the commit sha1, use the same check for --decorate
65 */
66#define decorate_get_color_opt(o, ix) \
Jeff Kingf1c96262011-08-17 22:03:12 -070067 decorate_get_color((o)->use_color, ix)
Nazri Ramliy67a4b582010-06-19 09:37:35 +080068
Jeff King662174d2014-08-26 06:23:36 -040069void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
René Scharfecab4feb2008-09-04 23:39:21 +020070{
Jeff King96ffc062016-02-22 17:44:32 -050071 struct name_decoration *res;
72 FLEX_ALLOC_STR(res, name, name);
Nazri Ramliya7524122010-06-19 09:37:34 +080073 res->type = type;
René Scharfecab4feb2008-09-04 23:39:21 +020074 res->next = add_decoration(&name_decoration, obj, res);
75}
76
Jeff King2608c242014-08-26 06:23:54 -040077const struct name_decoration *get_name_decoration(const struct object *obj)
78{
René Scharfe0cc73802019-09-08 19:58:51 +020079 load_ref_decorations(NULL, DECORATE_SHORT_REFS);
Jeff King2608c242014-08-26 06:23:54 -040080 return lookup_decoration(&name_decoration, obj);
81}
82
Derrick Stoleec9f7a792020-04-16 14:15:48 +000083static int match_ref_pattern(const char *refname,
84 const struct string_list_item *item)
85{
86 int matched = 0;
87 if (item->util == NULL) {
88 if (!wildmatch(item->string, refname, 0))
89 matched = 1;
90 } else {
91 const char *rest;
92 if (skip_prefix(refname, item->string, &rest) &&
93 (!*rest || *rest == '/'))
94 matched = 1;
95 }
96 return matched;
97}
98
99static int ref_filter_match(const char *refname,
100 const struct decoration_filter *filter)
101{
102 struct string_list_item *item;
103 const struct string_list *exclude_patterns = filter->exclude_ref_pattern;
104 const struct string_list *include_patterns = filter->include_ref_pattern;
Derrick Stoleea6be5e62020-04-16 14:15:49 +0000105 const struct string_list *exclude_patterns_config =
106 filter->exclude_ref_config_pattern;
Derrick Stoleec9f7a792020-04-16 14:15:48 +0000107
108 if (exclude_patterns && exclude_patterns->nr) {
109 for_each_string_list_item(item, exclude_patterns) {
110 if (match_ref_pattern(refname, item))
111 return 0;
112 }
113 }
114
115 if (include_patterns && include_patterns->nr) {
Derrick Stoleec9f7a792020-04-16 14:15:48 +0000116 for_each_string_list_item(item, include_patterns) {
Derrick Stoleea6be5e62020-04-16 14:15:49 +0000117 if (match_ref_pattern(refname, item))
118 return 1;
Derrick Stoleec9f7a792020-04-16 14:15:48 +0000119 }
Derrick Stoleea6be5e62020-04-16 14:15:49 +0000120 return 0;
Derrick Stoleec9f7a792020-04-16 14:15:48 +0000121 }
Derrick Stoleea6be5e62020-04-16 14:15:49 +0000122
123 if (exclude_patterns_config && exclude_patterns_config->nr) {
124 for_each_string_list_item(item, exclude_patterns_config) {
125 if (match_ref_pattern(refname, item))
126 return 0;
127 }
128 }
129
Derrick Stoleec9f7a792020-04-16 14:15:48 +0000130 return 1;
131}
132
Michael Haggertyf124b732015-05-25 18:38:57 +0000133static int add_ref_decoration(const char *refname, const struct object_id *oid,
134 int flags, void *cb_data)
René Scharfecab4feb2008-09-04 23:39:21 +0200135{
Nguyễn Thái Ngọc Duy5267d292011-08-19 19:43:50 +0700136 struct object *obj;
Nazri Ramliya7524122010-06-19 09:37:34 +0800137 enum decoration_type type = DECORATION_NONE;
Rafael Ascensão65516f52017-11-21 21:33:41 +0000138 struct decoration_filter *filter = (struct decoration_filter *)cb_data;
Nguyễn Thái Ngọc Duy5267d292011-08-19 19:43:50 +0700139
Derrick Stoleec9f7a792020-04-16 14:15:48 +0000140 if (filter && !ref_filter_match(refname, filter))
Rafael Ascensão65516f52017-11-21 21:33:41 +0000141 return 0;
Junio C Hamano429ad202015-05-13 12:40:35 -0700142
Mike Hommey58d121b2015-06-12 06:34:59 +0900143 if (starts_with(refname, git_replace_ref_base)) {
Michael Haggerty3d79f652015-05-25 18:38:58 +0000144 struct object_id original_oid;
Jeff King6ebd1ca2018-07-18 16:45:20 -0400145 if (!read_replace_refs)
Michael J Gruberb9ad5002011-08-25 17:09:30 +0200146 return 0;
Junio C Hamano31a0ad52015-08-03 11:01:10 -0700147 if (get_oid_hex(refname + strlen(git_replace_ref_base),
148 &original_oid)) {
Nguyễn Thái Ngọc Duy5267d292011-08-19 19:43:50 +0700149 warning("invalid replace ref %s", refname);
150 return 0;
151 }
Stefan Beller109cd762018-06-28 18:21:51 -0700152 obj = parse_object(the_repository, &original_oid);
Nguyễn Thái Ngọc Duy5267d292011-08-19 19:43:50 +0700153 if (obj)
154 add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
155 return 0;
156 }
157
Stefan Beller109cd762018-06-28 18:21:51 -0700158 obj = parse_object(the_repository, oid);
René Scharfecab4feb2008-09-04 23:39:21 +0200159 if (!obj)
160 return 0;
Nazri Ramliya7524122010-06-19 09:37:34 +0800161
Christian Couder59556542013-11-30 21:55:40 +0100162 if (starts_with(refname, "refs/heads/"))
Nazri Ramliya7524122010-06-19 09:37:34 +0800163 type = DECORATION_REF_LOCAL;
Christian Couder59556542013-11-30 21:55:40 +0100164 else if (starts_with(refname, "refs/remotes/"))
Nazri Ramliya7524122010-06-19 09:37:34 +0800165 type = DECORATION_REF_REMOTE;
Christian Couder59556542013-11-30 21:55:40 +0100166 else if (starts_with(refname, "refs/tags/"))
Nazri Ramliya7524122010-06-19 09:37:34 +0800167 type = DECORATION_REF_TAG;
Nguyễn Thái Ngọc Duy97ba6422012-01-05 19:39:40 +0700168 else if (!strcmp(refname, "refs/stash"))
Nazri Ramliya7524122010-06-19 09:37:34 +0800169 type = DECORATION_REF_STASH;
Nguyễn Thái Ngọc Duy97ba6422012-01-05 19:39:40 +0700170 else if (!strcmp(refname, "HEAD"))
Nazri Ramliya7524122010-06-19 09:37:34 +0800171 type = DECORATION_REF_HEAD;
172
Nazri Ramliya7524122010-06-19 09:37:34 +0800173 add_name_decoration(type, refname, obj);
René Scharfecab4feb2008-09-04 23:39:21 +0200174 while (obj->type == OBJ_TAG) {
175 obj = ((struct tag *)obj)->tagged;
176 if (!obj)
177 break;
brian m. carlson5e1361c2013-12-17 04:28:21 +0000178 if (!obj->parsed)
Stefan Beller109cd762018-06-28 18:21:51 -0700179 parse_object(the_repository, &obj->oid);
Nazri Ramliya7524122010-06-19 09:37:34 +0800180 add_name_decoration(DECORATION_REF_TAG, refname, obj);
René Scharfecab4feb2008-09-04 23:39:21 +0200181 }
182 return 0;
183}
184
Nguyễn Thái Ngọc Duy76f5df32011-08-18 19:29:37 +0700185static int add_graft_decoration(const struct commit_graft *graft, void *cb_data)
186{
Stefan Bellerc1f5eb42018-06-28 18:21:59 -0700187 struct commit *commit = lookup_commit(the_repository, &graft->oid);
Nguyễn Thái Ngọc Duy76f5df32011-08-18 19:29:37 +0700188 if (!commit)
189 return 0;
190 add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
191 return 0;
192}
193
Rafael Ascensão65516f52017-11-21 21:33:41 +0000194void load_ref_decorations(struct decoration_filter *filter, int flags)
René Scharfecab4feb2008-09-04 23:39:21 +0200195{
Junio C Hamano76c61fb2015-05-13 10:25:18 -0700196 if (!decoration_loaded) {
Rafael Ascensão65516f52017-11-21 21:33:41 +0000197 if (filter) {
198 struct string_list_item *item;
199 for_each_string_list_item(item, filter->exclude_ref_pattern) {
200 normalize_glob_ref(item, NULL, item->string);
201 }
202 for_each_string_list_item(item, filter->include_ref_pattern) {
203 normalize_glob_ref(item, NULL, item->string);
204 }
Derrick Stoleea6be5e62020-04-16 14:15:49 +0000205 for_each_string_list_item(item, filter->exclude_ref_config_pattern) {
206 normalize_glob_ref(item, NULL, item->string);
207 }
Rafael Ascensão65516f52017-11-21 21:33:41 +0000208 }
Junio C Hamano76c61fb2015-05-13 10:25:18 -0700209 decoration_loaded = 1;
210 decoration_flags = flags;
Rafael Ascensão65516f52017-11-21 21:33:41 +0000211 for_each_ref(add_ref_decoration, filter);
212 head_ref(add_ref_decoration, filter);
213 for_each_commit_graft(add_graft_decoration, filter);
René Scharfecab4feb2008-09-04 23:39:21 +0200214 }
215}
216
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200217static void show_parents(struct commit *commit, int abbrev, FILE *file)
Linus Torvaldsc8c893c2006-05-03 07:59:00 -0700218{
219 struct commit_list *p;
220 for (p = commit->parents; p ; p = p->next) {
221 struct commit *parent = p->item;
brian m. carlsonaab95832018-03-12 02:27:30 +0000222 fprintf(file, " %s", find_unique_abbrev(&parent->object.oid, abbrev));
Linus Torvaldsc8c893c2006-05-03 07:59:00 -0700223 }
224}
225
Jay Soffian91b849b2011-10-04 10:02:03 -0400226static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
227{
228 struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
229 for ( ; p; p = p->next) {
brian m. carlsonaab95832018-03-12 02:27:30 +0000230 fprintf(opt->diffopt.file, " %s", find_unique_abbrev(&p->item->object.oid, abbrev));
Jay Soffian91b849b2011-10-04 10:02:03 -0400231 }
232}
233
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000234/*
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100235 * Do we have HEAD in the output, and also the branch it points at?
236 * If so, find that decoration entry for that current branch.
237 */
238static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration)
239{
240 const struct name_decoration *list, *head = NULL;
241 const char *branch_name = NULL;
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100242 int rru_flags;
243
244 /* First find HEAD */
245 for (list = decoration; list; list = list->next)
246 if (list->type == DECORATION_REF_HEAD) {
247 head = list;
248 break;
249 }
250 if (!head)
251 return NULL;
252
253 /* Now resolve and find the matching current branch */
René Scharfe744c0402017-09-23 11:45:04 +0200254 branch_name = resolve_ref_unsafe("HEAD", 0, NULL, &rru_flags);
Jeff Kingd79be492017-10-19 13:49:01 -0400255 if (!branch_name || !(rru_flags & REF_ISSYMREF))
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100256 return NULL;
Junio C Hamano76c61fb2015-05-13 10:25:18 -0700257
Junio C Hamano429ad202015-05-13 12:40:35 -0700258 if (!starts_with(branch_name, "refs/"))
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100259 return NULL;
260
261 /* OK, do we have that ref in the list? */
262 for (list = decoration; list; list = list->next)
263 if ((list->type == DECORATION_REF_LOCAL) &&
264 !strcmp(branch_name, list->name)) {
265 return list;
266 }
267
268 return NULL;
269}
270
Junio C Hamano429ad202015-05-13 12:40:35 -0700271static void show_name(struct strbuf *sb, const struct name_decoration *decoration)
272{
273 if (decoration_flags == DECORATE_SHORT_REFS)
274 strbuf_addstr(sb, prettify_refname(decoration->name));
275 else
276 strbuf_addstr(sb, decoration->name);
277}
278
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100279/*
Harry Jeffery92710952014-09-18 21:53:53 +0100280 * The caller makes sure there is no funny color before calling.
281 * format_decorations_extended makes sure the same after return.
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000282 */
Harry Jeffery92710952014-09-18 21:53:53 +0100283void format_decorations_extended(struct strbuf *sb,
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000284 const struct commit *commit,
Harry Jeffery92710952014-09-18 21:53:53 +0100285 int use_color,
286 const char *prefix,
287 const char *separator,
288 const char *suffix)
Linus Torvaldsca135e72007-04-16 16:05:10 -0700289{
Jeff King2608c242014-08-26 06:23:54 -0400290 const struct name_decoration *decoration;
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100291 const struct name_decoration *current_and_HEAD;
Nazri Ramliy67a4b582010-06-19 09:37:35 +0800292 const char *color_commit =
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000293 diff_get_color(use_color, DIFF_COMMIT);
Nazri Ramliy67a4b582010-06-19 09:37:35 +0800294 const char *color_reset =
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000295 decorate_get_color(use_color, DECORATION_NONE);
Linus Torvaldsca135e72007-04-16 16:05:10 -0700296
Jeff King2608c242014-08-26 06:23:54 -0400297 decoration = get_name_decoration(&commit->object);
Linus Torvaldsca135e72007-04-16 16:05:10 -0700298 if (!decoration)
299 return;
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100300
301 current_and_HEAD = current_pointed_by_HEAD(decoration);
Linus Torvaldsca135e72007-04-16 16:05:10 -0700302 while (decoration) {
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100303 /*
304 * When both current and HEAD are there, only
305 * show HEAD->current where HEAD would have
306 * appeared, skipping the entry for current.
307 */
308 if (decoration != current_and_HEAD) {
309 strbuf_addstr(sb, color_commit);
310 strbuf_addstr(sb, prefix);
311 strbuf_addstr(sb, color_reset);
312 strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
313 if (decoration->type == DECORATION_REF_TAG)
314 strbuf_addstr(sb, "tag: ");
315
Junio C Hamano429ad202015-05-13 12:40:35 -0700316 show_name(sb, decoration);
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100317
318 if (current_and_HEAD &&
319 decoration->type == DECORATION_REF_HEAD) {
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100320 strbuf_addstr(sb, " -> ");
321 strbuf_addstr(sb, color_reset);
322 strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
Junio C Hamano429ad202015-05-13 12:40:35 -0700323 show_name(sb, current_and_HEAD);
Junio C Hamano51ff0f22015-03-10 14:53:21 +0100324 }
325 strbuf_addstr(sb, color_reset);
326
327 prefix = separator;
328 }
Linus Torvaldsca135e72007-04-16 16:05:10 -0700329 decoration = decoration->next;
330 }
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000331 strbuf_addstr(sb, color_commit);
Harry Jeffery92710952014-09-18 21:53:53 +0100332 strbuf_addstr(sb, suffix);
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000333 strbuf_addstr(sb, color_reset);
334}
335
336void show_decorations(struct rev_info *opt, struct commit *commit)
337{
338 struct strbuf sb = STRBUF_INIT;
339
Nguyễn Thái Ngọc Duy87be2522018-05-19 07:28:24 +0200340 if (opt->sources) {
341 char **slot = revision_sources_peek(opt->sources, commit);
342
343 if (slot && *slot)
344 fprintf(opt->diffopt.file, "\t%s", *slot);
345 }
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000346 if (!opt->show_decorations)
347 return;
348 format_decorations(&sb, commit, opt->diffopt.use_color);
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200349 fputs(sb.buf, opt->diffopt.file);
Nguyễn Thái Ngọc Duy9d3f0022013-04-19 09:08:43 +1000350 strbuf_release(&sb);
Linus Torvaldsca135e72007-04-16 16:05:10 -0700351}
352
Johannes Schindeline00de242007-02-09 01:43:54 +0100353static unsigned int digits_in_number(unsigned int number)
354{
355 unsigned int i = 10, result = 1;
356 while (i <= number) {
357 i *= 10;
358 result++;
359 }
360 return result;
361}
362
Junio C Hamanod28b5d42012-12-21 22:06:01 -0800363void fmt_output_subject(struct strbuf *filename,
364 const char *subject,
Junio C Hamano021f2f42012-12-21 23:26:16 -0800365 struct rev_info *info)
Stephen Boyd6fa8e622009-03-22 19:14:04 -0700366{
Junio C Hamano021f2f42012-12-21 23:26:16 -0800367 const char *suffix = info->patch_suffix;
368 int nr = info->nr;
Junio C Hamanod28b5d42012-12-21 22:06:01 -0800369 int start_len = filename->len;
Junio C Hamano3baf58b2020-11-06 13:56:24 -0800370 int max_len = start_len + info->patch_name_max - (strlen(suffix) + 1);
Stephen Boyd6fa8e622009-03-22 19:14:04 -0700371
ZheNing Hudb919882021-03-23 11:12:25 +0000372 if (info->reroll_count) {
373 struct strbuf temp = STRBUF_INIT;
374
375 strbuf_addf(&temp, "v%s", info->reroll_count);
376 format_sanitized_subject(filename, temp.buf, temp.len);
377 strbuf_addstr(filename, "-");
378 strbuf_release(&temp);
379 }
Junio C Hamanod28b5d42012-12-21 22:06:01 -0800380 strbuf_addf(filename, "%04d-%s", nr, subject);
Christian Couderb09b8682009-03-27 01:13:01 +0100381
Junio C Hamanod28b5d42012-12-21 22:06:01 -0800382 if (max_len < filename->len)
383 strbuf_setlen(filename, max_len);
384 strbuf_addstr(filename, suffix);
385}
Jeff Kinga21c2f92012-05-21 19:10:32 -0400386
Junio C Hamanod28b5d42012-12-21 22:06:01 -0800387void fmt_output_commit(struct strbuf *filename,
388 struct commit *commit,
389 struct rev_info *info)
390{
391 struct pretty_print_context ctx = {0};
392 struct strbuf subject = STRBUF_INIT;
393
394 format_commit_message(commit, "%f", &subject, &ctx);
395 fmt_output_subject(filename, subject.buf, info);
396 strbuf_release(&subject);
Stephen Boyd6fa8e622009-03-22 19:14:04 -0700397}
398
René Scharfe8ffc8dc2017-03-01 12:36:38 +0100399void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
400{
401 if (opt->total > 0) {
402 strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ",
403 opt->subject_prefix,
404 *opt->subject_prefix ? " " : "",
405 digits_in_number(opt->total),
406 opt->nr, opt->total);
407 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
408 strbuf_addf(sb, "Subject: [%s] ",
409 opt->subject_prefix);
410 } else {
411 strbuf_addstr(sb, "Subject: ");
412 }
413}
414
Stephen Boyd108dab22009-03-22 19:14:05 -0700415void log_write_email_headers(struct rev_info *opt, struct commit *commit,
Junio C Hamano267123b2008-03-15 00:09:20 -0700416 const char **extra_headers_p,
brian m. carlson50cd54e2018-05-02 02:20:52 +0000417 int *need_8bit_cte_p,
418 int maybe_multipart)
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500419{
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500420 const char *extra_headers = opt->extra_headers;
brian m. carlson3a30aa12015-12-15 01:52:04 +0000421 const char *name = oid_to_hex(opt->zero_commit ?
422 &null_oid : &commit->object.oid);
Junio C Hamano267123b2008-03-15 00:09:20 -0700423
424 *need_8bit_cte_p = 0; /* unknown */
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500425
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200426 fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700427 graph_show_oneline(opt->graph);
428 if (opt->message_id) {
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200429 fprintf(opt->diffopt.file, "Message-Id: <%s>\n", opt->message_id);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700430 graph_show_oneline(opt->graph);
431 }
Thomas Rastb079c502009-02-19 22:26:31 +0100432 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
433 int i, n;
434 n = opt->ref_message_ids->nr;
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200435 fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
Thomas Rastb079c502009-02-19 22:26:31 +0100436 for (i = 0; i < n; i++)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200437 fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
Thomas Rastb079c502009-02-19 22:26:31 +0100438 opt->ref_message_ids->items[i].string);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700439 graph_show_oneline(opt->graph);
440 }
brian m. carlson50cd54e2018-05-02 02:20:52 +0000441 if (opt->mime_boundary && maybe_multipart) {
Jeff Kingd50b69b2018-05-18 18:57:44 -0700442 static struct strbuf subject_buffer = STRBUF_INIT;
443 static struct strbuf buffer = STRBUF_INIT;
Stephen Boyd108dab22009-03-22 19:14:05 -0700444 struct strbuf filename = STRBUF_INIT;
Junio C Hamano267123b2008-03-15 00:09:20 -0700445 *need_8bit_cte_p = -1; /* NEVER */
Jeff Kingd50b69b2018-05-18 18:57:44 -0700446
447 strbuf_reset(&subject_buffer);
448 strbuf_reset(&buffer);
449
450 strbuf_addf(&subject_buffer,
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500451 "%s"
452 "MIME-Version: 1.0\n"
453 "Content-Type: multipart/mixed;"
454 " boundary=\"%s%s\"\n"
455 "\n"
456 "This is a multi-part message in MIME "
457 "format.\n"
458 "--%s%s\n"
459 "Content-Type: text/plain; "
460 "charset=UTF-8; format=fixed\n"
461 "Content-Transfer-Encoding: 8bit\n\n",
462 extra_headers ? extra_headers : "",
463 mime_boundary_leader, opt->mime_boundary,
464 mime_boundary_leader, opt->mime_boundary);
Jeff Kingd50b69b2018-05-18 18:57:44 -0700465 extra_headers = subject_buffer.buf;
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500466
Junio C Hamano38ec23a2012-12-21 21:39:37 -0800467 if (opt->numbered_files)
468 strbuf_addf(&filename, "%d", opt->nr);
469 else
Junio C Hamanod28b5d42012-12-21 22:06:01 -0800470 fmt_output_commit(&filename, commit, opt);
Jeff Kingd50b69b2018-05-18 18:57:44 -0700471 strbuf_addf(&buffer,
Kevin Ballard6b2fbaa2008-07-29 22:49:33 -0700472 "\n--%s%s\n"
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500473 "Content-Type: text/x-patch;"
Stephen Boyd108dab22009-03-22 19:14:05 -0700474 " name=\"%s\"\n"
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500475 "Content-Transfer-Encoding: 8bit\n"
476 "Content-Disposition: %s;"
Stephen Boyd108dab22009-03-22 19:14:05 -0700477 " filename=\"%s\"\n\n",
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500478 mime_boundary_leader, opt->mime_boundary,
Stephen Boyd108dab22009-03-22 19:14:05 -0700479 filename.buf,
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500480 opt->no_inline ? "attachment" : "inline",
Stephen Boyd108dab22009-03-22 19:14:05 -0700481 filename.buf);
Jeff Kingd50b69b2018-05-18 18:57:44 -0700482 opt->diffopt.stat_sep = buffer.buf;
Stephen Boyd108dab22009-03-22 19:14:05 -0700483 strbuf_release(&filename);
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500484 }
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500485 *extra_headers_p = extra_headers;
486}
487
Junio C Hamanoc6b3ec42012-01-04 13:48:45 -0800488static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
489{
490 const char *color, *reset, *eol;
491
492 color = diff_get_color_opt(&opt->diffopt,
493 status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
494 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
495 while (*bol) {
496 eol = strchrnul(bol, '\n');
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200497 fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
Junio C Hamanoc6b3ec42012-01-04 13:48:45 -0800498 *eol ? "\n" : "");
Zoltan Klingercf3983d2014-07-09 12:10:21 +1000499 graph_show_oneline(opt->graph);
Junio C Hamanoc6b3ec42012-01-04 13:48:45 -0800500 bol = (*eol) ? (eol + 1) : eol;
501 }
502}
503
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700504static void show_signature(struct rev_info *opt, struct commit *commit)
505{
506 struct strbuf payload = STRBUF_INIT;
507 struct strbuf signature = STRBUF_INIT;
Hans Jerry Illikainen67948982020-03-04 11:48:04 +0000508 struct signature_check sigc = { 0 };
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700509 int status;
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700510
brian m. carlson1fb5cf02021-01-18 23:49:11 +0000511 if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0)
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700512 goto out;
513
Hans Jerry Illikainen67948982020-03-04 11:48:04 +0000514 status = check_signature(payload.buf, payload.len, signature.buf,
515 signature.len, &sigc);
516 if (status && !sigc.gpg_output)
517 show_sig_lines(opt, status, "No signature\n");
518 else
519 show_sig_lines(opt, status, sigc.gpg_output);
520 signature_check_clear(&sigc);
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700521
522 out:
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700523 strbuf_release(&payload);
524 strbuf_release(&signature);
525}
526
brian m. carlsona92ea682017-05-06 22:10:18 +0000527static int which_parent(const struct object_id *oid, const struct commit *commit)
Junio C Hamano824958e2012-01-04 13:51:28 -0800528{
529 int nth;
530 const struct commit_list *parent;
531
532 for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
Jeff King4a7e27e2018-08-28 17:22:40 -0400533 if (oideq(&parent->item->object.oid, oid))
Junio C Hamano824958e2012-01-04 13:51:28 -0800534 return nth;
535 nth++;
536 }
537 return -1;
538}
539
Junio C Hamanod041ffa2012-01-04 16:23:12 -0800540static int is_common_merge(const struct commit *commit)
541{
542 return (commit->parents
543 && commit->parents->next
544 && !commit->parents->next->next);
545}
546
Johannes Schindelinfef461e2018-04-25 11:54:04 +0200547static int show_one_mergetag(struct commit *commit,
548 struct commit_extra_header *extra,
549 void *data)
Junio C Hamano824958e2012-01-04 13:51:28 -0800550{
Christian Couder063da622014-07-07 08:35:37 +0200551 struct rev_info *opt = (struct rev_info *)data;
brian m. carlsona92ea682017-05-06 22:10:18 +0000552 struct object_id oid;
Junio C Hamano824958e2012-01-04 13:51:28 -0800553 struct tag *tag;
554 struct strbuf verify_message;
Hans Jerry Illikainen67948982020-03-04 11:48:04 +0000555 struct signature_check sigc = { 0 };
Junio C Hamano824958e2012-01-04 13:51:28 -0800556 int status, nth;
brian m. carlson482c1192021-02-11 02:08:03 +0000557 struct strbuf payload = STRBUF_INIT;
558 struct strbuf signature = STRBUF_INIT;
Junio C Hamano824958e2012-01-04 13:51:28 -0800559
Matheus Tavares2dcde202020-01-30 17:32:22 -0300560 hash_object_file(the_hash_algo, extra->value, extra->len,
561 type_name(OBJ_TAG), &oid);
Stefan Bellerce71efb2018-06-28 18:22:03 -0700562 tag = lookup_tag(the_repository, &oid);
Junio C Hamano824958e2012-01-04 13:51:28 -0800563 if (!tag)
Johannes Schindelinfef461e2018-04-25 11:54:04 +0200564 return -1; /* error message already given */
Junio C Hamano824958e2012-01-04 13:51:28 -0800565
566 strbuf_init(&verify_message, 256);
Stefan Beller0e740fe2018-06-28 18:22:04 -0700567 if (parse_tag_buffer(the_repository, tag, extra->value, extra->len))
Junio C Hamano824958e2012-01-04 13:51:28 -0800568 strbuf_addstr(&verify_message, "malformed mergetag\n");
Junio C Hamanod041ffa2012-01-04 16:23:12 -0800569 else if (is_common_merge(commit) &&
Jeff King4a7e27e2018-08-28 17:22:40 -0400570 oideq(&tag->tagged->oid,
571 &commit->parents->next->item->object.oid))
Junio C Hamanod041ffa2012-01-04 16:23:12 -0800572 strbuf_addf(&verify_message,
573 "merged tag '%s'\n", tag->tag);
brian m. carlsona92ea682017-05-06 22:10:18 +0000574 else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0)
Junio C Hamano824958e2012-01-04 13:51:28 -0800575 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
Harald van Dijk237a2812020-02-29 13:07:57 +0000576 tag->tag, oid_to_hex(&tag->tagged->oid));
Junio C Hamano824958e2012-01-04 13:51:28 -0800577 else
578 strbuf_addf(&verify_message,
579 "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
Junio C Hamano824958e2012-01-04 13:51:28 -0800580
Michael J Gruber13150932013-02-14 17:04:43 +0100581 status = -1;
brian m. carlson482c1192021-02-11 02:08:03 +0000582 if (parse_signature(extra->value, extra->len, &payload, &signature)) {
Michael J Gruber42c55ce2014-06-27 15:18:36 +0200583 /* could have a good signature */
brian m. carlson482c1192021-02-11 02:08:03 +0000584 status = check_signature(payload.buf, payload.len,
585 signature.buf, signature.len, &sigc);
Hans Jerry Illikainen67948982020-03-04 11:48:04 +0000586 if (sigc.gpg_output)
587 strbuf_addstr(&verify_message, sigc.gpg_output);
588 else
Michael J Gruber42c55ce2014-06-27 15:18:36 +0200589 strbuf_addstr(&verify_message, "No signature\n");
Hans Jerry Illikainen67948982020-03-04 11:48:04 +0000590 signature_check_clear(&sigc);
Michael J Gruber42c55ce2014-06-27 15:18:36 +0200591 /* otherwise we couldn't verify, which is shown as bad */
592 }
Junio C Hamano824958e2012-01-04 13:51:28 -0800593
594 show_sig_lines(opt, status, verify_message.buf);
595 strbuf_release(&verify_message);
brian m. carlson482c1192021-02-11 02:08:03 +0000596 strbuf_release(&payload);
597 strbuf_release(&signature);
Johannes Schindelinfef461e2018-04-25 11:54:04 +0200598 return 0;
Junio C Hamano824958e2012-01-04 13:51:28 -0800599}
600
Johannes Schindelinfef461e2018-04-25 11:54:04 +0200601static int show_mergetag(struct rev_info *opt, struct commit *commit)
Junio C Hamano824958e2012-01-04 13:51:28 -0800602{
Johannes Schindelinfef461e2018-04-25 11:54:04 +0200603 return for_each_mergetag(show_one_mergetag, commit, opt);
Junio C Hamano824958e2012-01-04 13:51:28 -0800604}
605
Eric Sunshine3fcc7a22018-07-22 05:57:08 -0400606static void next_commentary_block(struct rev_info *opt, struct strbuf *sb)
607{
608 const char *x = opt->shown_dashes ? "\n" : "---\n";
609 if (sb)
610 strbuf_addstr(sb, x);
611 else
612 fputs(x, opt->diffopt.file);
613 opt->shown_dashes = 1;
614}
615
Adam Simpkins02865652008-04-29 01:32:59 -0700616void show_log(struct rev_info *opt)
Linus Torvalds91539832006-04-17 11:59:32 -0700617{
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500618 struct strbuf msgbuf = STRBUF_INIT;
Timo Hirvonen39bc9a62006-06-25 13:54:14 +0300619 struct log_info *log = opt->loginfo;
Linus Torvalds91539832006-04-17 11:59:32 -0700620 struct commit *commit = log->commit, *parent = log->parent;
brian m. carlson2ed19602018-07-16 01:28:06 +0000621 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : the_hash_algo->hexsz;
Thomas Rastdd2e7942009-10-19 17:48:08 +0200622 const char *extra_headers = opt->extra_headers;
623 struct pretty_print_context ctx = {0};
Linus Torvalds91539832006-04-17 11:59:32 -0700624
625 opt->loginfo = NULL;
626 if (!opt->verbose_header) {
Adam Simpkins7fefda52008-05-04 03:36:54 -0700627 graph_show_commit(opt->graph);
628
Michael J Gruber1df2d652011-03-07 13:31:39 +0100629 if (!opt->graph)
Michael J Gruberb1b47552011-03-10 15:45:03 +0100630 put_revision_mark(opt, commit);
brian m. carlsonaab95832018-03-12 02:27:30 +0000631 fputs(find_unique_abbrev(&commit->object.oid, abbrev_commit), opt->diffopt.file);
Adam Simpkins885cf802008-05-04 03:36:52 -0700632 if (opt->print_parents)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200633 show_parents(commit, abbrev_commit, opt->diffopt.file);
Jay Soffian91b849b2011-10-04 10:02:03 -0400634 if (opt->children.name)
635 show_children(opt, commit, abbrev_commit);
Linus Torvalds0f3a2902008-10-27 12:51:59 -0700636 show_decorations(opt, commit);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700637 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200638 putc('\n', opt->diffopt.file);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700639 graph_show_remainder(opt->graph);
640 }
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200641 putc(opt->diffopt.line_termination, opt->diffopt.file);
Linus Torvalds91539832006-04-17 11:59:32 -0700642 return;
643 }
644
645 /*
Jeff King87152272009-07-01 03:26:28 -0400646 * If use_terminator is set, we already handled any record termination
647 * at the end of the last record.
Adam Simpkins02865652008-04-29 01:32:59 -0700648 * Otherwise, add a diffopt.line_termination character before all
649 * entries but the first. (IOW, as a separator between entries)
Linus Torvalds91539832006-04-17 11:59:32 -0700650 */
Adam Simpkins7fefda52008-05-04 03:36:54 -0700651 if (opt->shown_one && !opt->use_terminator) {
652 /*
653 * If entries are separated by a newline, the output
654 * should look human-readable. If the last entry ended
655 * with a newline, print the graph output before this
656 * newline. Otherwise it will end up as a completely blank
657 * line and will look like a gap in the graph.
658 *
659 * If the entry separator is not a newline, the output is
660 * primarily intended for programmatic consumption, and we
661 * never want the extra graph output before the entry
662 * separator.
663 */
664 if (opt->diffopt.line_termination == '\n' &&
665 !opt->missing_newline)
666 graph_show_padding(opt->graph);
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200667 putc(opt->diffopt.line_termination, opt->diffopt.file);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700668 }
Linus Torvalds91539832006-04-17 11:59:32 -0700669 opt->shown_one = 1;
670
671 /*
Adam Simpkins7fefda52008-05-04 03:36:54 -0700672 * If the history graph was requested,
673 * print the graph, up to this commit's line
674 */
675 graph_show_commit(opt->graph);
676
677 /*
Linus Torvalds91539832006-04-17 11:59:32 -0700678 * Print header line of header..
679 */
Junio C Hamano3eefc182006-04-18 16:45:27 -0700680
Eric Wong9f23e042016-06-05 04:46:39 +0000681 if (cmit_fmt_is_mail(opt->commit_format)) {
René Scharfe6d167fd2017-03-01 12:37:07 +0100682 log_write_email_headers(opt, commit, &extra_headers,
brian m. carlson50cd54e2018-05-02 02:20:52 +0000683 &ctx.need_8bit_cte, 1);
René Scharfe6d167fd2017-03-01 12:37:07 +0100684 ctx.rev = opt;
685 ctx.print_email_subject = 1;
Johannes Schindeline52a5de2007-02-23 01:35:03 +0100686 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200687 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
Junio C Hamano74bd9022006-12-16 15:31:25 -0800688 if (opt->commit_format != CMIT_FMT_ONELINE)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200689 fputs("commit ", opt->diffopt.file);
Adam Simpkins7528f272008-05-25 00:07:21 -0700690
Michael J Gruber1df2d652011-03-07 13:31:39 +0100691 if (!opt->graph)
Michael J Gruberb1b47552011-03-10 15:45:03 +0100692 put_revision_mark(opt, commit);
brian m. carlsonaab95832018-03-12 02:27:30 +0000693 fputs(find_unique_abbrev(&commit->object.oid,
694 abbrev_commit),
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200695 opt->diffopt.file);
Adam Simpkins885cf802008-05-04 03:36:52 -0700696 if (opt->print_parents)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200697 show_parents(commit, abbrev_commit, opt->diffopt.file);
Jay Soffian91b849b2011-10-04 10:02:03 -0400698 if (opt->children.name)
699 show_children(opt, commit, abbrev_commit);
Junio C Hamano73f0a152006-05-24 12:19:47 -0700700 if (parent)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200701 fprintf(opt->diffopt.file, " (from %s)",
brian m. carlsonaab95832018-03-12 02:27:30 +0000702 find_unique_abbrev(&parent->object.oid, abbrev_commit));
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200703 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
Linus Torvalds0f3a2902008-10-27 12:51:59 -0700704 show_decorations(opt, commit);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700705 if (opt->commit_format == CMIT_FMT_ONELINE) {
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200706 putc(' ', opt->diffopt.file);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700707 } else {
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200708 putc('\n', opt->diffopt.file);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700709 graph_show_oneline(opt->graph);
710 }
Nicolas Pitre903b45f2007-01-27 22:40:36 -0500711 if (opt->reflog_info) {
Adam Simpkins7fefda52008-05-04 03:36:54 -0700712 /*
713 * setup_revisions() ensures that opt->reflog_info
714 * and opt->graph cannot both be set,
715 * so we don't need to worry about printing the
716 * graph info here.
717 */
Junio C Hamano4d12a472007-01-20 00:51:41 -0800718 show_reflog_message(opt->reflog_info,
Junio C Hamano55ccf852012-05-07 14:11:32 -0700719 opt->commit_format == CMIT_FMT_ONELINE,
Jeff Kinga5481a62015-06-25 12:55:02 -0400720 &opt->date_mode,
Junio C Hamano55ccf852012-05-07 14:11:32 -0700721 opt->date_mode_explicit);
Adam Simpkins02865652008-04-29 01:32:59 -0700722 if (opt->commit_format == CMIT_FMT_ONELINE)
Nicolas Pitre903b45f2007-01-27 22:40:36 -0500723 return;
Nicolas Pitre903b45f2007-01-27 22:40:36 -0500724 }
Junio C Hamano3eefc182006-04-18 16:45:27 -0700725 }
Linus Torvalds91539832006-04-17 11:59:32 -0700726
Junio C Hamano824958e2012-01-04 13:51:28 -0800727 if (opt->show_signature) {
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700728 show_signature(opt, commit);
Junio C Hamano824958e2012-01-04 13:51:28 -0800729 show_mergetag(opt, commit);
730 }
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700731
Junio C Hamanoddf333f2012-10-17 18:51:47 -0700732 if (opt->show_notes) {
733 int raw;
734 struct strbuf notebuf = STRBUF_INIT;
735
736 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
brian m. carlsonfb61e4d2017-05-30 10:30:41 -0700737 format_display_notes(&commit->object.oid, &notebuf,
Junio C Hamanoddf333f2012-10-17 18:51:47 -0700738 get_log_output_encoding(), raw);
René Scharfe82f51af2019-08-25 14:53:26 +0200739 ctx.notes_message = strbuf_detach(&notebuf, NULL);
Junio C Hamanoddf333f2012-10-17 18:51:47 -0700740 }
741
Linus Torvalds91539832006-04-17 11:59:32 -0700742 /*
743 * And then the pretty-printed message itself
744 */
Nguyễn Thái Ngọc Duy5289c562013-02-12 02:17:38 -0800745 if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
746 ctx.need_8bit_cte =
William Hubbs39ab4d02019-02-04 12:48:50 -0600747 has_non_ascii(fmt_name(WANT_COMMITTER_IDENT));
Thomas Rastdd2e7942009-10-19 17:48:08 +0200748 ctx.date_mode = opt->date_mode;
Jeff Kingf026c752012-05-04 01:25:18 -0400749 ctx.date_mode_explicit = opt->date_mode_explicit;
Thomas Rastdd2e7942009-10-19 17:48:08 +0200750 ctx.abbrev = opt->diffopt.abbrev;
751 ctx.after_subject = extra_headers;
Jeff King9553d2b2011-05-26 18:28:17 -0400752 ctx.preserve_subject = opt->preserve_subject;
Emma Brooks19d097e2020-04-08 04:31:38 +0000753 ctx.encode_email_headers = opt->encode_email_headers;
Thomas Rast8f8f5472009-10-19 17:48:10 +0200754 ctx.reflog_info = opt->reflog_info;
Jeff King6bf13942011-05-26 18:27:49 -0400755 ctx.fmt = opt->commit_format;
Antoine Pelisse0e2913b2013-01-05 22:26:41 +0100756 ctx.mailmap = opt->mailmap;
Junio C Hamano30825172012-12-17 17:56:49 -0500757 ctx.color = opt->diffopt.use_color;
Linus Torvalds7cc13c72016-03-16 09:15:53 -0700758 ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
Alexey Shumkinecaee802013-06-26 14:19:50 +0400759 ctx.output_encoding = get_log_output_encoding();
Issac Trottsad6f0282019-01-10 22:30:46 -0800760 ctx.rev = opt;
Jeff Kinga9080472013-07-03 03:08:22 -0400761 if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
762 ctx.from_ident = &opt->from_ident;
Josef Kufner3ad87c82016-06-16 20:18:37 +0700763 if (opt->graph)
764 ctx.graph_width = graph_width(opt->graph);
Jeff King6bf13942011-05-26 18:27:49 -0400765 pretty_print_commit(&ctx, commit, &msgbuf);
Junio C Hamano212620f2012-10-17 20:48:25 -0700766
767 if (opt->add_signoff)
Nguyễn Thái Ngọc Duy5289c562013-02-12 02:17:38 -0800768 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
Junio C Hamano212620f2012-10-17 20:48:25 -0700769
Junio C Hamano5a664cf2012-10-17 19:02:46 -0700770 if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
Junio C Hamanobd1470b2012-10-17 21:27:22 -0700771 ctx.notes_message && *ctx.notes_message) {
Eric Sunshine3fcc7a22018-07-22 05:57:08 -0400772 if (cmit_fmt_is_mail(ctx.fmt))
773 next_commentary_block(opt, &msgbuf);
Junio C Hamano5a664cf2012-10-17 19:02:46 -0700774 strbuf_addstr(&msgbuf, ctx.notes_message);
Junio C Hamanobd1470b2012-10-17 21:27:22 -0700775 }
Junio C Hamanocf2251b2006-05-31 15:11:49 -0700776
Adam Simpkins7fefda52008-05-04 03:36:54 -0700777 if (opt->show_log_size) {
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200778 fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700779 graph_show_oneline(opt->graph);
780 }
Marco Costalba9fa34652007-07-20 20:15:13 +0200781
Adam Simpkins7fefda52008-05-04 03:36:54 -0700782 /*
783 * Set opt->missing_newline if msgbuf doesn't
784 * end in a newline (including if it is empty)
785 */
786 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
787 opt->missing_newline = 1;
788 else
789 opt->missing_newline = 0;
790
Jacob Keller660e1132016-08-31 16:27:20 -0700791 graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf);
Jeff Kingb9c7d6e2014-07-29 13:56:48 -0400792 if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
Adam Simpkins7fefda52008-05-04 03:36:54 -0700793 if (!opt->missing_newline)
794 graph_show_padding(opt->graph);
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200795 putc(opt->diffopt.line_termination, opt->diffopt.file);
Adam Simpkins7fefda52008-05-04 03:36:54 -0700796 }
797
Pierre Habouzit674d1722007-09-10 12:35:06 +0200798 strbuf_release(&msgbuf);
Junio C Hamanoddf333f2012-10-17 18:51:47 -0700799 free(ctx.notes_message);
Eric Sunshineee6cbf72018-07-22 05:57:09 -0400800
801 if (cmit_fmt_is_mail(ctx.fmt) && opt->idiff_oid1) {
802 struct diff_queue_struct dq;
803
804 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
805 DIFF_QUEUE_CLEAR(&diff_queued_diff);
806
807 next_commentary_block(opt, NULL);
808 fprintf_ln(opt->diffopt.file, "%s", opt->idiff_title);
Eric Sunshine72a72392020-09-08 03:16:09 -0400809 show_interdiff(opt->idiff_oid1, opt->idiff_oid2, 2,
810 &opt->diffopt);
Eric Sunshineee6cbf72018-07-22 05:57:09 -0400811
812 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
813 }
Eric Sunshine40ce4162018-07-22 05:57:17 -0400814
815 if (cmit_fmt_is_mail(ctx.fmt) && opt->rdiff1) {
816 struct diff_queue_struct dq;
Martin Ågrenac0edf12018-12-03 16:21:31 -0500817 struct diff_options opts;
Johannes Schindelinf1ce6c12021-02-05 14:46:11 +0000818 struct range_diff_options range_diff_opts = {
819 .creation_factor = opt->creation_factor,
820 .dual_color = 1,
821 .diffopt = &opts
822 };
Eric Sunshine40ce4162018-07-22 05:57:17 -0400823
824 memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
825 DIFF_QUEUE_CLEAR(&diff_queued_diff);
826
827 next_commentary_block(opt, NULL);
828 fprintf_ln(opt->diffopt.file, "%s", opt->rdiff_title);
Martin Ågrenac0edf12018-12-03 16:21:31 -0500829 /*
830 * Pass minimum required diff-options to range-diff; others
831 * can be added later if deemed desirable.
832 */
833 diff_setup(&opts);
834 opts.file = opt->diffopt.file;
835 opts.use_color = opt->diffopt.use_color;
836 diff_setup_done(&opts);
Johannes Schindelinf1ce6c12021-02-05 14:46:11 +0000837 show_range_diff(opt->rdiff1, opt->rdiff2, &range_diff_opts);
Eric Sunshine40ce4162018-07-22 05:57:17 -0400838
839 memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
840 }
Linus Torvalds91539832006-04-17 11:59:32 -0700841}
842
Linus Torvaldscd2bdc52006-04-14 16:52:13 -0700843int log_tree_diff_flush(struct rev_info *opt)
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700844{
Junio C Hamanobd1470b2012-10-17 21:27:22 -0700845 opt->shown_dashes = 0;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700846 diffcore_std(&opt->diffopt);
Linus Torvalds91539832006-04-17 11:59:32 -0700847
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700848 if (diff_queue_is_empty()) {
849 int saved_fmt = opt->diffopt.output_format;
850 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
851 diff_flush(&opt->diffopt);
852 opt->diffopt.output_format = saved_fmt;
853 return 0;
854 }
Linus Torvalds91539832006-04-17 11:59:32 -0700855
Junio C Hamano3969cf72006-06-27 15:08:19 -0700856 if (opt->loginfo && !opt->no_commit_id) {
Adam Simpkins02865652008-04-29 01:32:59 -0700857 show_log(opt);
Linus Torvalds304b5af2007-10-09 09:35:22 -0700858 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
859 opt->verbose_header &&
Jeff Kingb9c7d6e2014-07-29 13:56:48 -0400860 opt->commit_format != CMIT_FMT_ONELINE &&
861 !commit_format_is_empty(opt->commit_format)) {
Junio C Hamano1d34c502012-11-13 10:09:07 -0800862 /*
863 * When showing a verbose header (i.e. log message),
864 * and not in --pretty=oneline format, we would want
865 * an extra newline between the end of log and the
866 * diff/diffstat output for readability.
867 */
Junio C Hamano3969cf72006-06-27 15:08:19 -0700868 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
Bo Yang81bd1b22010-05-26 15:08:03 +0800869 if (opt->diffopt.output_prefix) {
870 struct strbuf *msg = NULL;
871 msg = opt->diffopt.output_prefix(&opt->diffopt,
872 opt->diffopt.output_prefix_data);
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200873 fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
Bo Yang81bd1b22010-05-26 15:08:03 +0800874 }
Junio C Hamano1d34c502012-11-13 10:09:07 -0800875
876 /*
877 * We may have shown three-dashes line early
Eric Sunshine3fcc7a22018-07-22 05:57:08 -0400878 * between generated commentary (notes, etc.)
879 * and the log message, in which case we only
880 * want a blank line after the commentary
881 * without (an extra) three-dashes line.
Junio C Hamano1d34c502012-11-13 10:09:07 -0800882 * Otherwise, we show the three-dashes line if
883 * we are showing the patch with diffstat, but
884 * in that case, there is no extra blank line
885 * after the three-dashes line.
886 */
887 if (!opt->shown_dashes &&
888 (pch & opt->diffopt.output_format) == pch)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200889 fprintf(opt->diffopt.file, "---");
890 putc('\n', opt->diffopt.file);
Junio C Hamano3969cf72006-06-27 15:08:19 -0700891 }
892 }
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700893 diff_flush(&opt->diffopt);
894 return 1;
895}
896
Linus Torvaldscd2bdc52006-04-14 16:52:13 -0700897static int do_diff_combined(struct rev_info *opt, struct commit *commit)
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700898{
Sergey Organovd01141d2020-09-29 14:31:22 +0300899 diff_tree_combined_merge(commit, opt);
Linus Torvalds91539832006-04-17 11:59:32 -0700900 return !opt->loginfo;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700901}
902
Linus Torvalds91539832006-04-17 11:59:32 -0700903/*
904 * Show the diff of a commit.
905 *
906 * Return true if we printed any log info messages
907 */
908static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700909{
Linus Torvalds91539832006-04-17 11:59:32 -0700910 int showed_log;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700911 struct commit_list *parents;
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000912 struct object_id *oid;
Sergey Organova6d19ec2020-12-21 18:19:53 +0300913 int is_merge;
914 int all_need_diff = opt->diff || opt->diffopt.flags.exit_with_status;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700915
Sergey Organova6d19ec2020-12-21 18:19:53 +0300916 if (!all_need_diff && !opt->merges_need_diff)
Linus Torvalds91539832006-04-17 11:59:32 -0700917 return 0;
918
Jeff King7059dcc2013-10-24 04:52:36 -0400919 parse_commit_or_die(commit);
Derrick Stolee2e27bd72018-04-06 19:09:38 +0000920 oid = get_commit_tree_oid(commit);
Thomas Rastd1b9b762013-03-28 09:19:34 +0100921
Thomas Rast53d00b32013-07-31 22:13:20 +0200922 parents = get_saved_parents(opt, commit);
Sergey Organova6d19ec2020-12-21 18:19:53 +0300923 is_merge = parents && parents->next;
924 if (!is_merge && !all_need_diff)
925 return 0;
926
927 /* Root commit? */
Linus Torvalds91539832006-04-17 11:59:32 -0700928 if (!parents) {
Rene Scharfe2b603562006-10-26 18:52:39 +0200929 if (opt->show_root_diff) {
Brandon Williams7b8dea02017-05-30 10:30:57 -0700930 diff_root_tree_oid(oid, "", &opt->diffopt);
Rene Scharfe2b603562006-10-26 18:52:39 +0200931 log_tree_diff_flush(opt);
932 }
Linus Torvalds91539832006-04-17 11:59:32 -0700933 return !opt->loginfo;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700934 }
935
Sergey Organova6d19ec2020-12-21 18:19:53 +0300936 if (is_merge) {
Sergey Organov1a2c4d82020-12-21 18:19:46 +0300937 if (opt->combine_merges)
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700938 return do_diff_combined(opt, commit);
Sergey Organov1a2c4d82020-12-21 18:19:46 +0300939 if (opt->separate_merges) {
940 if (!opt->first_parent_merges) {
941 /* Show parent info for multiple diffs */
942 log->parent = parents->item;
943 }
944 } else
945 return 0;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700946 }
947
Linus Torvalds91539832006-04-17 11:59:32 -0700948 showed_log = 0;
949 for (;;) {
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700950 struct commit *parent = parents->item;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700951
Jeff King7059dcc2013-10-24 04:52:36 -0400952 parse_commit_or_die(parent);
Derrick Stolee2e27bd72018-04-06 19:09:38 +0000953 diff_tree_oid(get_commit_tree_oid(parent),
Brandon Williams66f414f2017-05-30 10:31:03 -0700954 oid, "", &opt->diffopt);
Linus Torvalds91539832006-04-17 11:59:32 -0700955 log_tree_diff_flush(opt);
956
957 showed_log |= !opt->loginfo;
958
959 /* Set up the log info for the next parent, if any.. */
960 parents = parents->next;
Sergey Organov3291eea2020-12-21 18:19:40 +0300961 if (!parents || opt->first_parent_merges)
Linus Torvalds91539832006-04-17 11:59:32 -0700962 break;
963 log->parent = parents->item;
964 opt->loginfo = log;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700965 }
Linus Torvalds91539832006-04-17 11:59:32 -0700966 return showed_log;
967}
968
969int log_tree_commit(struct rev_info *opt, struct commit *commit)
970{
971 struct log_info log;
Ævar Arnfjörð Bjarmasone900d492021-02-11 11:45:34 +0100972 int shown;
973 /* maybe called by e.g. cmd_log_walk(), maybe stand-alone */
974 int no_free = opt->diffopt.no_free;
Linus Torvalds91539832006-04-17 11:59:32 -0700975
976 log.commit = commit;
977 log.parent = NULL;
978 opt->loginfo = &log;
Ævar Arnfjörð Bjarmasone900d492021-02-11 11:45:34 +0100979 opt->diffopt.no_free = 1;
Linus Torvalds91539832006-04-17 11:59:32 -0700980
Thomas Rast12da1d12013-03-28 17:47:32 +0100981 if (opt->line_level_traverse)
982 return line_log_print(opt, commit);
983
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);
Junio C Hamano3eefc182006-04-18 16:45:27 -0700986 shown = log_tree_diff(opt, commit, &log);
987 if (!shown && opt->loginfo && opt->always_show_header) {
Linus Torvalds91539832006-04-17 11:59:32 -0700988 log.parent = NULL;
Adam Simpkins02865652008-04-29 01:32:59 -0700989 show_log(opt);
Junio C Hamano3eefc182006-04-18 16:45:27 -0700990 shown = 1;
Linus Torvalds91539832006-04-17 11:59:32 -0700991 }
Nguyễn Thái Ngọc Duy1b32dec2014-03-25 20:23:27 +0700992 if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200993 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
Linus Torvalds91539832006-04-17 11:59:32 -0700994 opt->loginfo = NULL;
Johannes Schindelin4d7b0ef2016-06-22 17:01:32 +0200995 maybe_flush_or_die(opt->diffopt.file, "stdout");
Ævar Arnfjörð Bjarmasone900d492021-02-11 11:45:34 +0100996 opt->diffopt.no_free = no_free;
997 diff_free(&opt->diffopt);
Junio C Hamano3eefc182006-04-18 16:45:27 -0700998 return shown;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700999}