🌐 AI搜索 & 代理 主页
blob: 4618dd04ca1b6bd2bf53481f39851fb4e5d02336 [file] [log] [blame]
Junio C Hamano5f1c3f02006-04-09 08:11:111#include "cache.h"
2#include "diff.h"
3#include "commit.h"
René Scharfecab4feb2008-09-04 21:39:214#include "tag.h"
Adam Simpkins7fefda52008-05-04 10:36:545#include "graph.h"
Junio C Hamano5f1c3f02006-04-09 08:11:116#include "log-tree.h"
Johannes Schindelin8860fd42007-01-11 10:47:487#include "reflog-walk.h"
René Scharfecab4feb2008-09-04 21:39:218#include "refs.h"
Thomas Rastb079c502009-02-19 21:26:319#include "string-list.h"
Nazri Ramliy67a4b582010-06-19 01:37:3510#include "color.h"
Junio C Hamano0c37f1f2011-10-18 22:53:2311#include "gpg-interface.h"
Brandon Casey959a2622013-02-12 10:17:3912#include "sequencer.h"
Thomas Rast12da1d12013-03-28 16:47:3213#include "line-log.h"
Junio C Hamano5f1c3f02006-04-09 08:11:1114
Jeff King2608c242014-08-26 10:23:5415static struct decoration name_decoration = { "object names" };
Junio C Hamano76c61fb2015-05-13 17:25:1816static int decoration_loaded;
17static int decoration_flags;
Nazri Ramliya7524122010-06-19 01:37:3418
Nazri Ramliy67a4b582010-06-19 01:37:3519static char decoration_colors[][COLOR_MAXLEN] = {
20 GIT_COLOR_RESET,
21 GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */
22 GIT_COLOR_BOLD_RED, /* REF_REMOTE */
23 GIT_COLOR_BOLD_YELLOW, /* REF_TAG */
24 GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */
25 GIT_COLOR_BOLD_CYAN, /* REF_HEAD */
Nguyễn Thái Ngọc Duy76f5df32011-08-18 12:29:3726 GIT_COLOR_BOLD_BLUE, /* GRAFTED */
Nazri Ramliy67a4b582010-06-19 01:37:3527};
28
29static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
30{
Jeff Kingdaa0c3d2011-08-18 05:04:2331 if (want_color(decorate_use_color))
Nazri Ramliy67a4b582010-06-19 01:37:3532 return decoration_colors[ix];
33 return "";
34}
35
Nazri Ramliy5e11bee2010-06-24 00:21:1636static int parse_decorate_color_slot(const char *slot)
37{
38 /*
39 * We're comparing with 'ignore-case' on
40 * (because config.c sets them all tolower),
41 * but let's match the letters in the literal
42 * string values here with how they are
43 * documented in Documentation/config.txt, for
44 * consistency.
45 *
46 * We love being consistent, don't we?
47 */
48 if (!strcasecmp(slot, "branch"))
49 return DECORATION_REF_LOCAL;
50 if (!strcasecmp(slot, "remoteBranch"))
51 return DECORATION_REF_REMOTE;
52 if (!strcasecmp(slot, "tag"))
53 return DECORATION_REF_TAG;
54 if (!strcasecmp(slot, "stash"))
55 return DECORATION_REF_STASH;
56 if (!strcasecmp(slot, "HEAD"))
57 return DECORATION_REF_HEAD;
58 return -1;
59}
60
Jonathan Nieder88521172014-10-07 19:16:5761int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
Nazri Ramliy5e11bee2010-06-24 00:21:1662{
Jonathan Nieder88521172014-10-07 19:16:5763 int slot = parse_decorate_color_slot(slot_name);
Nazri Ramliy5e11bee2010-06-24 00:21:1664 if (slot < 0)
65 return 0;
66 if (!value)
67 return config_error_nonbool(var);
Jeff Kingf6c5a292014-10-07 19:33:0968 return color_parse(value, decoration_colors[slot]);
Nazri Ramliy5e11bee2010-06-24 00:21:1669}
70
Nazri Ramliy67a4b582010-06-19 01:37:3571/*
72 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
73 * for showing the commit sha1, use the same check for --decorate
74 */
75#define decorate_get_color_opt(o, ix) \
Jeff Kingf1c96262011-08-18 05:03:1276 decorate_get_color((o)->use_color, ix)
Nazri Ramliy67a4b582010-06-19 01:37:3577
Jeff King662174d2014-08-26 10:23:3678void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
René Scharfecab4feb2008-09-04 21:39:2179{
Jeff King96ffc062016-02-22 22:44:3280 struct name_decoration *res;
81 FLEX_ALLOC_STR(res, name, name);
Nazri Ramliya7524122010-06-19 01:37:3482 res->type = type;
René Scharfecab4feb2008-09-04 21:39:2183 res->next = add_decoration(&name_decoration, obj, res);
84}
85
Jeff King2608c242014-08-26 10:23:5486const struct name_decoration *get_name_decoration(const struct object *obj)
87{
88 return lookup_decoration(&name_decoration, obj);
89}
90
Michael Haggertyf124b732015-05-25 18:38:5791static int add_ref_decoration(const char *refname, const struct object_id *oid,
92 int flags, void *cb_data)
René Scharfecab4feb2008-09-04 21:39:2193{
Nguyễn Thái Ngọc Duy5267d292011-08-19 12:43:5094 struct object *obj;
Nazri Ramliya7524122010-06-19 01:37:3495 enum decoration_type type = DECORATION_NONE;
Nguyễn Thái Ngọc Duy5267d292011-08-19 12:43:5096
Junio C Hamano429ad202015-05-13 19:40:3597 assert(cb_data == NULL);
98
Mike Hommey58d121b2015-06-11 21:34:5999 if (starts_with(refname, git_replace_ref_base)) {
Michael Haggerty3d79f652015-05-25 18:38:58100 struct object_id original_oid;
Michael Haggertyafc711b2014-02-18 11:24:55101 if (!check_replace_refs)
Michael J Gruberb9ad5002011-08-25 15:09:30102 return 0;
Junio C Hamano31a0ad52015-08-03 18:01:10103 if (get_oid_hex(refname + strlen(git_replace_ref_base),
104 &original_oid)) {
Nguyễn Thái Ngọc Duy5267d292011-08-19 12:43:50105 warning("invalid replace ref %s", refname);
106 return 0;
107 }
Michael Haggerty3d79f652015-05-25 18:38:58108 obj = parse_object(original_oid.hash);
Nguyễn Thái Ngọc Duy5267d292011-08-19 12:43:50109 if (obj)
110 add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
111 return 0;
112 }
113
Michael Haggertyf124b732015-05-25 18:38:57114 obj = parse_object(oid->hash);
René Scharfecab4feb2008-09-04 21:39:21115 if (!obj)
116 return 0;
Nazri Ramliya7524122010-06-19 01:37:34117
Christian Couder59556542013-11-30 20:55:40118 if (starts_with(refname, "refs/heads/"))
Nazri Ramliya7524122010-06-19 01:37:34119 type = DECORATION_REF_LOCAL;
Christian Couder59556542013-11-30 20:55:40120 else if (starts_with(refname, "refs/remotes/"))
Nazri Ramliya7524122010-06-19 01:37:34121 type = DECORATION_REF_REMOTE;
Christian Couder59556542013-11-30 20:55:40122 else if (starts_with(refname, "refs/tags/"))
Nazri Ramliya7524122010-06-19 01:37:34123 type = DECORATION_REF_TAG;
Nguyễn Thái Ngọc Duy97ba6422012-01-05 12:39:40124 else if (!strcmp(refname, "refs/stash"))
Nazri Ramliya7524122010-06-19 01:37:34125 type = DECORATION_REF_STASH;
Nguyễn Thái Ngọc Duy97ba6422012-01-05 12:39:40126 else if (!strcmp(refname, "HEAD"))
Nazri Ramliya7524122010-06-19 01:37:34127 type = DECORATION_REF_HEAD;
128
Nazri Ramliya7524122010-06-19 01:37:34129 add_name_decoration(type, refname, obj);
René Scharfecab4feb2008-09-04 21:39:21130 while (obj->type == OBJ_TAG) {
131 obj = ((struct tag *)obj)->tagged;
132 if (!obj)
133 break;
brian m. carlson5e1361c2013-12-17 04:28:21134 if (!obj->parsed)
brian m. carlsoned1c9972015-11-10 02:22:29135 parse_object(obj->oid.hash);
Nazri Ramliya7524122010-06-19 01:37:34136 add_name_decoration(DECORATION_REF_TAG, refname, obj);
René Scharfecab4feb2008-09-04 21:39:21137 }
138 return 0;
139}
140
Nguyễn Thái Ngọc Duy76f5df32011-08-18 12:29:37141static int add_graft_decoration(const struct commit_graft *graft, void *cb_data)
142{
brian m. carlson7683e2e2015-03-13 23:39:34143 struct commit *commit = lookup_commit(graft->oid.hash);
Nguyễn Thái Ngọc Duy76f5df32011-08-18 12:29:37144 if (!commit)
145 return 0;
146 add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
147 return 0;
148}
149
Lars Hjemli33e70182009-08-15 14:23:12150void load_ref_decorations(int flags)
René Scharfecab4feb2008-09-04 21:39:21151{
Junio C Hamano76c61fb2015-05-13 17:25:18152 if (!decoration_loaded) {
Michael Haggerty2b2a5be2015-05-25 18:38:28153
Junio C Hamano76c61fb2015-05-13 17:25:18154 decoration_loaded = 1;
155 decoration_flags = flags;
Michael Haggertyf124b732015-05-25 18:38:57156 for_each_ref(add_ref_decoration, NULL);
157 head_ref(add_ref_decoration, NULL);
Nguyễn Thái Ngọc Duy76f5df32011-08-18 12:29:37158 for_each_commit_graft(add_graft_decoration, NULL);
René Scharfecab4feb2008-09-04 21:39:21159 }
160}
161
Johannes Schindelin4d7b0ef2016-06-22 15:01:32162static void show_parents(struct commit *commit, int abbrev, FILE *file)
Linus Torvaldsc8c893c2006-05-03 14:59:00163{
164 struct commit_list *p;
165 for (p = commit->parents; p ; p = p->next) {
166 struct commit *parent = p->item;
Johannes Schindelin4d7b0ef2016-06-22 15:01:32167 fprintf(file, " %s", find_unique_abbrev(parent->object.oid.hash, abbrev));
Linus Torvaldsc8c893c2006-05-03 14:59:00168 }
169}
170
Jay Soffian91b849b2011-10-04 14:02:03171static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
172{
173 struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
174 for ( ; p; p = p->next) {
Johannes Schindelin4d7b0ef2016-06-22 15:01:32175 fprintf(opt->diffopt.file, " %s", find_unique_abbrev(p->item->object.oid.hash, abbrev));
Jay Soffian91b849b2011-10-04 14:02:03176 }
177}
178
Nguyễn Thái Ngọc Duy9d3f0022013-04-18 23:08:43179/*
Junio C Hamano51ff0f22015-03-10 13:53:21180 * Do we have HEAD in the output, and also the branch it points at?
181 * If so, find that decoration entry for that current branch.
182 */
183static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration)
184{
185 const struct name_decoration *list, *head = NULL;
186 const char *branch_name = NULL;
187 unsigned char unused[20];
188 int rru_flags;
189
190 /* First find HEAD */
191 for (list = decoration; list; list = list->next)
192 if (list->type == DECORATION_REF_HEAD) {
193 head = list;
194 break;
195 }
196 if (!head)
197 return NULL;
198
199 /* Now resolve and find the matching current branch */
200 branch_name = resolve_ref_unsafe("HEAD", 0, unused, &rru_flags);
201 if (!(rru_flags & REF_ISSYMREF))
202 return NULL;
Junio C Hamano76c61fb2015-05-13 17:25:18203
Junio C Hamano429ad202015-05-13 19:40:35204 if (!starts_with(branch_name, "refs/"))
Junio C Hamano51ff0f22015-03-10 13:53:21205 return NULL;
206
207 /* OK, do we have that ref in the list? */
208 for (list = decoration; list; list = list->next)
209 if ((list->type == DECORATION_REF_LOCAL) &&
210 !strcmp(branch_name, list->name)) {
211 return list;
212 }
213
214 return NULL;
215}
216
Junio C Hamano429ad202015-05-13 19:40:35217static void show_name(struct strbuf *sb, const struct name_decoration *decoration)
218{
219 if (decoration_flags == DECORATE_SHORT_REFS)
220 strbuf_addstr(sb, prettify_refname(decoration->name));
221 else
222 strbuf_addstr(sb, decoration->name);
223}
224
Junio C Hamano51ff0f22015-03-10 13:53:21225/*
Harry Jeffery92710952014-09-18 20:53:53226 * The caller makes sure there is no funny color before calling.
227 * format_decorations_extended makes sure the same after return.
Nguyễn Thái Ngọc Duy9d3f0022013-04-18 23:08:43228 */
Harry Jeffery92710952014-09-18 20:53:53229void format_decorations_extended(struct strbuf *sb,
Nguyễn Thái Ngọc Duy9d3f0022013-04-18 23:08:43230 const struct commit *commit,
Harry Jeffery92710952014-09-18 20:53:53231 int use_color,
232 const char *prefix,
233 const char *separator,
234 const char *suffix)
Linus Torvaldsca135e72007-04-16 23:05:10235{
Jeff King2608c242014-08-26 10:23:54236 const struct name_decoration *decoration;
Junio C Hamano51ff0f22015-03-10 13:53:21237 const struct name_decoration *current_and_HEAD;
Nazri Ramliy67a4b582010-06-19 01:37:35238 const char *color_commit =
Nguyễn Thái Ngọc Duy9d3f0022013-04-18 23:08:43239 diff_get_color(use_color, DIFF_COMMIT);
Nazri Ramliy67a4b582010-06-19 01:37:35240 const char *color_reset =
Nguyễn Thái Ngọc Duy9d3f0022013-04-18 23:08:43241 decorate_get_color(use_color, DECORATION_NONE);
Linus Torvaldsca135e72007-04-16 23:05:10242
Jeff King2608c242014-08-26 10:23:54243 decoration = get_name_decoration(&commit->object);
Linus Torvaldsca135e72007-04-16 23:05:10244 if (!decoration)
245 return;
Junio C Hamano51ff0f22015-03-10 13:53:21246
247 current_and_HEAD = current_pointed_by_HEAD(decoration);
Linus Torvaldsca135e72007-04-16 23:05:10248 while (decoration) {
Junio C Hamano51ff0f22015-03-10 13:53:21249 /*
250 * When both current and HEAD are there, only
251 * show HEAD->current where HEAD would have
252 * appeared, skipping the entry for current.
253 */
254 if (decoration != current_and_HEAD) {
255 strbuf_addstr(sb, color_commit);
256 strbuf_addstr(sb, prefix);
257 strbuf_addstr(sb, color_reset);
258 strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
259 if (decoration->type == DECORATION_REF_TAG)
260 strbuf_addstr(sb, "tag: ");
261
Junio C Hamano429ad202015-05-13 19:40:35262 show_name(sb, decoration);
Junio C Hamano51ff0f22015-03-10 13:53:21263
264 if (current_and_HEAD &&
265 decoration->type == DECORATION_REF_HEAD) {
Junio C Hamano51ff0f22015-03-10 13:53:21266 strbuf_addstr(sb, " -> ");
267 strbuf_addstr(sb, color_reset);
268 strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
Junio C Hamano429ad202015-05-13 19:40:35269 show_name(sb, current_and_HEAD);
Junio C Hamano51ff0f22015-03-10 13:53:21270 }
271 strbuf_addstr(sb, color_reset);
272
273 prefix = separator;
274 }
Linus Torvaldsca135e72007-04-16 23:05:10275 decoration = decoration->next;
276 }
Nguyễn Thái Ngọc Duy9d3f0022013-04-18 23:08:43277 strbuf_addstr(sb, color_commit);
Harry Jeffery92710952014-09-18 20:53:53278 strbuf_addstr(sb, suffix);
Nguyễn Thái Ngọc Duy9d3f0022013-04-18 23:08:43279 strbuf_addstr(sb, color_reset);
280}
281
282void show_decorations(struct rev_info *opt, struct commit *commit)
283{
284 struct strbuf sb = STRBUF_INIT;
285
286 if (opt->show_source && commit->util)
Johannes Schindelin4d7b0ef2016-06-22 15:01:32287 fprintf(opt->diffopt.file, "\t%s", (char *) commit->util);
Nguyễn Thái Ngọc Duy9d3f0022013-04-18 23:08:43288 if (!opt->show_decorations)
289 return;
290 format_decorations(&sb, commit, opt->diffopt.use_color);
Johannes Schindelin4d7b0ef2016-06-22 15:01:32291 fputs(sb.buf, opt->diffopt.file);
Nguyễn Thái Ngọc Duy9d3f0022013-04-18 23:08:43292 strbuf_release(&sb);
Linus Torvaldsca135e72007-04-16 23:05:10293}
294
Johannes Schindeline00de242007-02-09 00:43:54295static unsigned int digits_in_number(unsigned int number)
296{
297 unsigned int i = 10, result = 1;
298 while (i <= number) {
299 i *= 10;
300 result++;
301 }
302 return result;
303}
304
Junio C Hamanod28b5d42012-12-22 06:06:01305void fmt_output_subject(struct strbuf *filename,
306 const char *subject,
Junio C Hamano021f2f42012-12-22 07:26:16307 struct rev_info *info)
Stephen Boyd6fa8e622009-03-23 02:14:04308{
Junio C Hamano021f2f42012-12-22 07:26:16309 const char *suffix = info->patch_suffix;
310 int nr = info->nr;
Junio C Hamanod28b5d42012-12-22 06:06:01311 int start_len = filename->len;
312 int max_len = start_len + FORMAT_PATCH_NAME_MAX - (strlen(suffix) + 1);
Stephen Boyd6fa8e622009-03-23 02:14:04313
Junio C Hamano5fe10fe2012-12-22 08:21:23314 if (0 < info->reroll_count)
315 strbuf_addf(filename, "v%d-", info->reroll_count);
Junio C Hamanod28b5d42012-12-22 06:06:01316 strbuf_addf(filename, "%04d-%s", nr, subject);
Christian Couderb09b8682009-03-27 00:13:01317
Junio C Hamanod28b5d42012-12-22 06:06:01318 if (max_len < filename->len)
319 strbuf_setlen(filename, max_len);
320 strbuf_addstr(filename, suffix);
321}
Jeff Kinga21c2f92012-05-21 23:10:32322
Junio C Hamanod28b5d42012-12-22 06:06:01323void fmt_output_commit(struct strbuf *filename,
324 struct commit *commit,
325 struct rev_info *info)
326{
327 struct pretty_print_context ctx = {0};
328 struct strbuf subject = STRBUF_INIT;
329
330 format_commit_message(commit, "%f", &subject, &ctx);
331 fmt_output_subject(filename, subject.buf, info);
332 strbuf_release(&subject);
Stephen Boyd6fa8e622009-03-23 02:14:04333}
334
René Scharfe8ffc8dc2017-03-01 11:36:38335void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
336{
337 if (opt->total > 0) {
338 strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ",
339 opt->subject_prefix,
340 *opt->subject_prefix ? " " : "",
341 digits_in_number(opt->total),
342 opt->nr, opt->total);
343 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
344 strbuf_addf(sb, "Subject: [%s] ",
345 opt->subject_prefix);
346 } else {
347 strbuf_addstr(sb, "Subject: ");
348 }
349}
350
Stephen Boyd108dab22009-03-23 02:14:05351void log_write_email_headers(struct rev_info *opt, struct commit *commit,
Junio C Hamano267123b2008-03-15 07:09:20352 const char **extra_headers_p,
353 int *need_8bit_cte_p)
Daniel Barkalowb02bd652008-02-19 03:56:08354{
Daniel Barkalowb02bd652008-02-19 03:56:08355 const char *extra_headers = opt->extra_headers;
brian m. carlson3a30aa12015-12-15 01:52:04356 const char *name = oid_to_hex(opt->zero_commit ?
357 &null_oid : &commit->object.oid);
Junio C Hamano267123b2008-03-15 07:09:20358
359 *need_8bit_cte_p = 0; /* unknown */
Daniel Barkalowb02bd652008-02-19 03:56:08360
Johannes Schindelin4d7b0ef2016-06-22 15:01:32361 fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
Adam Simpkins7fefda52008-05-04 10:36:54362 graph_show_oneline(opt->graph);
363 if (opt->message_id) {
Johannes Schindelin4d7b0ef2016-06-22 15:01:32364 fprintf(opt->diffopt.file, "Message-Id: <%s>\n", opt->message_id);
Adam Simpkins7fefda52008-05-04 10:36:54365 graph_show_oneline(opt->graph);
366 }
Thomas Rastb079c502009-02-19 21:26:31367 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
368 int i, n;
369 n = opt->ref_message_ids->nr;
Johannes Schindelin4d7b0ef2016-06-22 15:01:32370 fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
Thomas Rastb079c502009-02-19 21:26:31371 for (i = 0; i < n; i++)
Johannes Schindelin4d7b0ef2016-06-22 15:01:32372 fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
Thomas Rastb079c502009-02-19 21:26:31373 opt->ref_message_ids->items[i].string);
Adam Simpkins7fefda52008-05-04 10:36:54374 graph_show_oneline(opt->graph);
375 }
Daniel Barkalowb02bd652008-02-19 03:56:08376 if (opt->mime_boundary) {
377 static char subject_buffer[1024];
378 static char buffer[1024];
Stephen Boyd108dab22009-03-23 02:14:05379 struct strbuf filename = STRBUF_INIT;
Junio C Hamano267123b2008-03-15 07:09:20380 *need_8bit_cte_p = -1; /* NEVER */
Daniel Barkalowb02bd652008-02-19 03:56:08381 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
382 "%s"
383 "MIME-Version: 1.0\n"
384 "Content-Type: multipart/mixed;"
385 " boundary=\"%s%s\"\n"
386 "\n"
387 "This is a multi-part message in MIME "
388 "format.\n"
389 "--%s%s\n"
390 "Content-Type: text/plain; "
391 "charset=UTF-8; format=fixed\n"
392 "Content-Transfer-Encoding: 8bit\n\n",
393 extra_headers ? extra_headers : "",
394 mime_boundary_leader, opt->mime_boundary,
395 mime_boundary_leader, opt->mime_boundary);
396 extra_headers = subject_buffer;
397
Junio C Hamano38ec23a2012-12-22 05:39:37398 if (opt->numbered_files)
399 strbuf_addf(&filename, "%d", opt->nr);
400 else
Junio C Hamanod28b5d42012-12-22 06:06:01401 fmt_output_commit(&filename, commit, opt);
Daniel Barkalowb02bd652008-02-19 03:56:08402 snprintf(buffer, sizeof(buffer) - 1,
Kevin Ballard6b2fbaa2008-07-30 05:49:33403 "\n--%s%s\n"
Daniel Barkalowb02bd652008-02-19 03:56:08404 "Content-Type: text/x-patch;"
Stephen Boyd108dab22009-03-23 02:14:05405 " name=\"%s\"\n"
Daniel Barkalowb02bd652008-02-19 03:56:08406 "Content-Transfer-Encoding: 8bit\n"
407 "Content-Disposition: %s;"
Stephen Boyd108dab22009-03-23 02:14:05408 " filename=\"%s\"\n\n",
Daniel Barkalowb02bd652008-02-19 03:56:08409 mime_boundary_leader, opt->mime_boundary,
Stephen Boyd108dab22009-03-23 02:14:05410 filename.buf,
Daniel Barkalowb02bd652008-02-19 03:56:08411 opt->no_inline ? "attachment" : "inline",
Stephen Boyd108dab22009-03-23 02:14:05412 filename.buf);
Daniel Barkalowb02bd652008-02-19 03:56:08413 opt->diffopt.stat_sep = buffer;
Stephen Boyd108dab22009-03-23 02:14:05414 strbuf_release(&filename);
Daniel Barkalowb02bd652008-02-19 03:56:08415 }
Daniel Barkalowb02bd652008-02-19 03:56:08416 *extra_headers_p = extra_headers;
417}
418
Junio C Hamanoc6b3ec42012-01-04 21:48:45419static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
420{
421 const char *color, *reset, *eol;
422
423 color = diff_get_color_opt(&opt->diffopt,
424 status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
425 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
426 while (*bol) {
427 eol = strchrnul(bol, '\n');
Johannes Schindelin4d7b0ef2016-06-22 15:01:32428 fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
Junio C Hamanoc6b3ec42012-01-04 21:48:45429 *eol ? "\n" : "");
Zoltan Klingercf3983d2014-07-09 02:10:21430 graph_show_oneline(opt->graph);
Junio C Hamanoc6b3ec42012-01-04 21:48:45431 bol = (*eol) ? (eol + 1) : eol;
432 }
433}
434
Junio C Hamano0c37f1f2011-10-18 22:53:23435static void show_signature(struct rev_info *opt, struct commit *commit)
436{
437 struct strbuf payload = STRBUF_INIT;
438 struct strbuf signature = STRBUF_INIT;
439 struct strbuf gpg_output = STRBUF_INIT;
440 int status;
Junio C Hamano0c37f1f2011-10-18 22:53:23441
Jeff King218aa3a2014-06-13 06:32:11442 if (parse_signed_commit(commit, &payload, &signature) <= 0)
Junio C Hamano0c37f1f2011-10-18 22:53:23443 goto out;
444
445 status = verify_signed_buffer(payload.buf, payload.len,
446 signature.buf, signature.len,
Michael J Gruber9cc4ac82013-02-14 16:04:44447 &gpg_output, NULL);
Junio C Hamano0c37f1f2011-10-18 22:53:23448 if (status && !gpg_output.len)
449 strbuf_addstr(&gpg_output, "No signature\n");
450
Junio C Hamanoc6b3ec42012-01-04 21:48:45451 show_sig_lines(opt, status, gpg_output.buf);
Junio C Hamano0c37f1f2011-10-18 22:53:23452
453 out:
454 strbuf_release(&gpg_output);
455 strbuf_release(&payload);
456 strbuf_release(&signature);
457}
458
Junio C Hamano824958e2012-01-04 21:51:28459static int which_parent(const unsigned char *sha1, const struct commit *commit)
460{
461 int nth;
462 const struct commit_list *parent;
463
464 for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
brian m. carlsoned1c9972015-11-10 02:22:29465 if (!hashcmp(parent->item->object.oid.hash, sha1))
Junio C Hamano824958e2012-01-04 21:51:28466 return nth;
467 nth++;
468 }
469 return -1;
470}
471
Junio C Hamanod041ffa2012-01-05 00:23:12472static int is_common_merge(const struct commit *commit)
473{
474 return (commit->parents
475 && commit->parents->next
476 && !commit->parents->next->next);
477}
478
Christian Couder063da622014-07-07 06:35:37479static void show_one_mergetag(struct commit *commit,
Junio C Hamano824958e2012-01-04 21:51:28480 struct commit_extra_header *extra,
Christian Couder063da622014-07-07 06:35:37481 void *data)
Junio C Hamano824958e2012-01-04 21:51:28482{
Christian Couder063da622014-07-07 06:35:37483 struct rev_info *opt = (struct rev_info *)data;
Junio C Hamano824958e2012-01-04 21:51:28484 unsigned char sha1[20];
485 struct tag *tag;
486 struct strbuf verify_message;
487 int status, nth;
488 size_t payload_size, gpg_message_offset;
489
490 hash_sha1_file(extra->value, extra->len, typename(OBJ_TAG), sha1);
491 tag = lookup_tag(sha1);
492 if (!tag)
493 return; /* error message already given */
494
495 strbuf_init(&verify_message, 256);
496 if (parse_tag_buffer(tag, extra->value, extra->len))
497 strbuf_addstr(&verify_message, "malformed mergetag\n");
Junio C Hamanod041ffa2012-01-05 00:23:12498 else if (is_common_merge(commit) &&
brian m. carlsonf2fd0762015-11-10 02:22:28499 !oidcmp(&tag->tagged->oid,
500 &commit->parents->next->item->object.oid))
Junio C Hamanod041ffa2012-01-05 00:23:12501 strbuf_addf(&verify_message,
502 "merged tag '%s'\n", tag->tag);
brian m. carlsoned1c9972015-11-10 02:22:29503 else if ((nth = which_parent(tag->tagged->oid.hash, commit)) < 0)
Junio C Hamano824958e2012-01-04 21:51:28504 strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
brian m. carlsoned1c9972015-11-10 02:22:29505 tag->tag, tag->tagged->oid.hash);
Junio C Hamano824958e2012-01-04 21:51:28506 else
507 strbuf_addf(&verify_message,
508 "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
509 gpg_message_offset = verify_message.len;
510
511 payload_size = parse_signature(extra->value, extra->len);
Michael J Gruber13150932013-02-14 16:04:43512 status = -1;
Michael J Gruber42c55ce2014-06-27 13:18:36513 if (extra->len > payload_size) {
514 /* could have a good signature */
515 if (!verify_signed_buffer(extra->value, payload_size,
516 extra->value + payload_size,
517 extra->len - payload_size,
518 &verify_message, NULL))
519 status = 0; /* good */
520 else if (verify_message.len <= gpg_message_offset)
521 strbuf_addstr(&verify_message, "No signature\n");
522 /* otherwise we couldn't verify, which is shown as bad */
523 }
Junio C Hamano824958e2012-01-04 21:51:28524
525 show_sig_lines(opt, status, verify_message.buf);
526 strbuf_release(&verify_message);
527}
528
529static void show_mergetag(struct rev_info *opt, struct commit *commit)
530{
Christian Couder063da622014-07-07 06:35:37531 for_each_mergetag(show_one_mergetag, commit, opt);
Junio C Hamano824958e2012-01-04 21:51:28532}
533
Adam Simpkins02865652008-04-29 08:32:59534void show_log(struct rev_info *opt)
Linus Torvalds91539832006-04-17 18:59:32535{
Brandon Caseyf285a2d2008-10-09 19:12:12536 struct strbuf msgbuf = STRBUF_INIT;
Timo Hirvonen39bc9a62006-06-25 10:54:14537 struct log_info *log = opt->loginfo;
Linus Torvalds91539832006-04-17 18:59:32538 struct commit *commit = log->commit, *parent = log->parent;
Linus Torvalds91539832006-04-17 18:59:32539 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
Thomas Rastdd2e7942009-10-19 15:48:08540 const char *extra_headers = opt->extra_headers;
541 struct pretty_print_context ctx = {0};
Linus Torvalds91539832006-04-17 18:59:32542
543 opt->loginfo = NULL;
Linus Torvalds91539832006-04-17 18:59:32544 if (!opt->verbose_header) {
Adam Simpkins7fefda52008-05-04 10:36:54545 graph_show_commit(opt->graph);
546
Michael J Gruber1df2d652011-03-07 12:31:39547 if (!opt->graph)
Michael J Gruberb1b47552011-03-10 14:45:03548 put_revision_mark(opt, commit);
Johannes Schindelin4d7b0ef2016-06-22 15:01:32549 fputs(find_unique_abbrev(commit->object.oid.hash, abbrev_commit), opt->diffopt.file);
Adam Simpkins885cf802008-05-04 10:36:52550 if (opt->print_parents)
Johannes Schindelin4d7b0ef2016-06-22 15:01:32551 show_parents(commit, abbrev_commit, opt->diffopt.file);
Jay Soffian91b849b2011-10-04 14:02:03552 if (opt->children.name)
553 show_children(opt, commit, abbrev_commit);
Linus Torvalds0f3a2902008-10-27 19:51:59554 show_decorations(opt, commit);
Adam Simpkins7fefda52008-05-04 10:36:54555 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
Johannes Schindelin4d7b0ef2016-06-22 15:01:32556 putc('\n', opt->diffopt.file);
Adam Simpkins7fefda52008-05-04 10:36:54557 graph_show_remainder(opt->graph);
558 }
Johannes Schindelin4d7b0ef2016-06-22 15:01:32559 putc(opt->diffopt.line_termination, opt->diffopt.file);
Linus Torvalds91539832006-04-17 18:59:32560 return;
561 }
562
563 /*
Jeff King87152272009-07-01 07:26:28564 * If use_terminator is set, we already handled any record termination
565 * at the end of the last record.
Adam Simpkins02865652008-04-29 08:32:59566 * Otherwise, add a diffopt.line_termination character before all
567 * entries but the first. (IOW, as a separator between entries)
Linus Torvalds91539832006-04-17 18:59:32568 */
Adam Simpkins7fefda52008-05-04 10:36:54569 if (opt->shown_one && !opt->use_terminator) {
570 /*
571 * If entries are separated by a newline, the output
572 * should look human-readable. If the last entry ended
573 * with a newline, print the graph output before this
574 * newline. Otherwise it will end up as a completely blank
575 * line and will look like a gap in the graph.
576 *
577 * If the entry separator is not a newline, the output is
578 * primarily intended for programmatic consumption, and we
579 * never want the extra graph output before the entry
580 * separator.
581 */
582 if (opt->diffopt.line_termination == '\n' &&
583 !opt->missing_newline)
584 graph_show_padding(opt->graph);
Johannes Schindelin4d7b0ef2016-06-22 15:01:32585 putc(opt->diffopt.line_termination, opt->diffopt.file);
Adam Simpkins7fefda52008-05-04 10:36:54586 }
Linus Torvalds91539832006-04-17 18:59:32587 opt->shown_one = 1;
588
589 /*
Adam Simpkins7fefda52008-05-04 10:36:54590 * If the history graph was requested,
591 * print the graph, up to this commit's line
592 */
593 graph_show_commit(opt->graph);
594
595 /*
Linus Torvalds91539832006-04-17 18:59:32596 * Print header line of header..
597 */
Junio C Hamano3eefc182006-04-18 23:45:27598
Eric Wong9f23e042016-06-05 04:46:39599 if (cmit_fmt_is_mail(opt->commit_format)) {
René Scharfe6d167fd2017-03-01 11:37:07600 log_write_email_headers(opt, commit, &extra_headers,
Thomas Rastdd2e7942009-10-19 15:48:08601 &ctx.need_8bit_cte);
René Scharfe6d167fd2017-03-01 11:37:07602 ctx.rev = opt;
603 ctx.print_email_subject = 1;
Johannes Schindeline52a5de2007-02-23 00:35:03604 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
Johannes Schindelin4d7b0ef2016-06-22 15:01:32605 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
Junio C Hamano74bd9022006-12-16 23:31:25606 if (opt->commit_format != CMIT_FMT_ONELINE)
Johannes Schindelin4d7b0ef2016-06-22 15:01:32607 fputs("commit ", opt->diffopt.file);
Adam Simpkins7528f272008-05-25 07:07:21608
Michael J Gruber1df2d652011-03-07 12:31:39609 if (!opt->graph)
Michael J Gruberb1b47552011-03-10 14:45:03610 put_revision_mark(opt, commit);
brian m. carlsoned1c9972015-11-10 02:22:29611 fputs(find_unique_abbrev(commit->object.oid.hash, abbrev_commit),
Johannes Schindelin4d7b0ef2016-06-22 15:01:32612 opt->diffopt.file);
Adam Simpkins885cf802008-05-04 10:36:52613 if (opt->print_parents)
Johannes Schindelin4d7b0ef2016-06-22 15:01:32614 show_parents(commit, abbrev_commit, opt->diffopt.file);
Jay Soffian91b849b2011-10-04 14:02:03615 if (opt->children.name)
616 show_children(opt, commit, abbrev_commit);
Junio C Hamano73f0a152006-05-24 19:19:47617 if (parent)
Johannes Schindelin4d7b0ef2016-06-22 15:01:32618 fprintf(opt->diffopt.file, " (from %s)",
brian m. carlsoned1c9972015-11-10 02:22:29619 find_unique_abbrev(parent->object.oid.hash,
Junio C Hamano3eefc182006-04-18 23:45:27620 abbrev_commit));
Johannes Schindelin4d7b0ef2016-06-22 15:01:32621 fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
Linus Torvalds0f3a2902008-10-27 19:51:59622 show_decorations(opt, commit);
Adam Simpkins7fefda52008-05-04 10:36:54623 if (opt->commit_format == CMIT_FMT_ONELINE) {
Johannes Schindelin4d7b0ef2016-06-22 15:01:32624 putc(' ', opt->diffopt.file);
Adam Simpkins7fefda52008-05-04 10:36:54625 } else {
Johannes Schindelin4d7b0ef2016-06-22 15:01:32626 putc('\n', opt->diffopt.file);
Adam Simpkins7fefda52008-05-04 10:36:54627 graph_show_oneline(opt->graph);
628 }
Nicolas Pitre903b45f2007-01-28 03:40:36629 if (opt->reflog_info) {
Adam Simpkins7fefda52008-05-04 10:36:54630 /*
631 * setup_revisions() ensures that opt->reflog_info
632 * and opt->graph cannot both be set,
633 * so we don't need to worry about printing the
634 * graph info here.
635 */
Junio C Hamano4d12a472007-01-20 08:51:41636 show_reflog_message(opt->reflog_info,
Junio C Hamano55ccf852012-05-07 21:11:32637 opt->commit_format == CMIT_FMT_ONELINE,
Jeff Kinga5481a62015-06-25 16:55:02638 &opt->date_mode,
Junio C Hamano55ccf852012-05-07 21:11:32639 opt->date_mode_explicit);
Adam Simpkins02865652008-04-29 08:32:59640 if (opt->commit_format == CMIT_FMT_ONELINE)
Nicolas Pitre903b45f2007-01-28 03:40:36641 return;
Nicolas Pitre903b45f2007-01-28 03:40:36642 }
Junio C Hamano3eefc182006-04-18 23:45:27643 }
Linus Torvalds91539832006-04-17 18:59:32644
Junio C Hamano824958e2012-01-04 21:51:28645 if (opt->show_signature) {
Junio C Hamano0c37f1f2011-10-18 22:53:23646 show_signature(opt, commit);
Junio C Hamano824958e2012-01-04 21:51:28647 show_mergetag(opt, commit);
648 }
Junio C Hamano0c37f1f2011-10-18 22:53:23649
Jeff King8597ea32014-06-10 21:44:13650 if (!get_cached_commit_buffer(commit, NULL))
Linus Torvalds3131b712008-02-09 22:02:07651 return;
652
Junio C Hamanoddf333f2012-10-18 01:51:47653 if (opt->show_notes) {
654 int raw;
655 struct strbuf notebuf = STRBUF_INIT;
656
657 raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
brian m. carlsoned1c9972015-11-10 02:22:29658 format_display_notes(commit->object.oid.hash, &notebuf,
Junio C Hamanoddf333f2012-10-18 01:51:47659 get_log_output_encoding(), raw);
660 ctx.notes_message = notebuf.len
661 ? strbuf_detach(&notebuf, NULL)
662 : xcalloc(1, 1);
663 }
664
Linus Torvalds91539832006-04-17 18:59:32665 /*
666 * And then the pretty-printed message itself
667 */
Nguyễn Thái Ngọc Duy5289c562013-02-12 10:17:38668 if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
669 ctx.need_8bit_cte =
670 has_non_ascii(fmt_name(getenv("GIT_COMMITTER_NAME"),
671 getenv("GIT_COMMITTER_EMAIL")));
Thomas Rastdd2e7942009-10-19 15:48:08672 ctx.date_mode = opt->date_mode;
Jeff Kingf026c752012-05-04 05:25:18673 ctx.date_mode_explicit = opt->date_mode_explicit;
Thomas Rastdd2e7942009-10-19 15:48:08674 ctx.abbrev = opt->diffopt.abbrev;
675 ctx.after_subject = extra_headers;
Jeff King9553d2b2011-05-26 22:28:17676 ctx.preserve_subject = opt->preserve_subject;
Thomas Rast8f8f5472009-10-19 15:48:10677 ctx.reflog_info = opt->reflog_info;
Jeff King6bf13942011-05-26 22:27:49678 ctx.fmt = opt->commit_format;
Antoine Pelisse0e2913b2013-01-05 21:26:41679 ctx.mailmap = opt->mailmap;
Junio C Hamano30825172012-12-17 22:56:49680 ctx.color = opt->diffopt.use_color;
Linus Torvalds7cc13c72016-03-16 16:15:53681 ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
Alexey Shumkinecaee802013-06-26 10:19:50682 ctx.output_encoding = get_log_output_encoding();
Jeff Kinga9080472013-07-03 07:08:22683 if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
684 ctx.from_ident = &opt->from_ident;
Josef Kufner3ad87c82016-06-16 13:18:37685 if (opt->graph)
686 ctx.graph_width = graph_width(opt->graph);
Jeff King6bf13942011-05-26 22:27:49687 pretty_print_commit(&ctx, commit, &msgbuf);
Junio C Hamano212620f2012-10-18 03:48:25688
689 if (opt->add_signoff)
Nguyễn Thái Ngọc Duy5289c562013-02-12 10:17:38690 append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
Junio C Hamano212620f2012-10-18 03:48:25691
Junio C Hamano5a664cf2012-10-18 02:02:46692 if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
Junio C Hamanobd1470b2012-10-18 04:27:22693 ctx.notes_message && *ctx.notes_message) {
Eric Wong9f23e042016-06-05 04:46:39694 if (cmit_fmt_is_mail(ctx.fmt)) {
Junio C Hamanobd1470b2012-10-18 04:27:22695 strbuf_addstr(&msgbuf, "---\n");
696 opt->shown_dashes = 1;
697 }
Junio C Hamano5a664cf2012-10-18 02:02:46698 strbuf_addstr(&msgbuf, ctx.notes_message);
Junio C Hamanobd1470b2012-10-18 04:27:22699 }
Junio C Hamanocf2251b2006-05-31 22:11:49700
Adam Simpkins7fefda52008-05-04 10:36:54701 if (opt->show_log_size) {
Johannes Schindelin4d7b0ef2016-06-22 15:01:32702 fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
Adam Simpkins7fefda52008-05-04 10:36:54703 graph_show_oneline(opt->graph);
704 }
Marco Costalba9fa34652007-07-20 18:15:13705
Adam Simpkins7fefda52008-05-04 10:36:54706 /*
707 * Set opt->missing_newline if msgbuf doesn't
708 * end in a newline (including if it is empty)
709 */
710 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
711 opt->missing_newline = 1;
712 else
713 opt->missing_newline = 0;
714
Jacob Keller660e1132016-08-31 23:27:20715 graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf);
Jeff Kingb9c7d6e2014-07-29 17:56:48716 if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
Adam Simpkins7fefda52008-05-04 10:36:54717 if (!opt->missing_newline)
718 graph_show_padding(opt->graph);
Johannes Schindelin4d7b0ef2016-06-22 15:01:32719 putc(opt->diffopt.line_termination, opt->diffopt.file);
Adam Simpkins7fefda52008-05-04 10:36:54720 }
721
Pierre Habouzit674d1722007-09-10 10:35:06722 strbuf_release(&msgbuf);
Junio C Hamanoddf333f2012-10-18 01:51:47723 free(ctx.notes_message);
Linus Torvalds91539832006-04-17 18:59:32724}
725
Linus Torvaldscd2bdc52006-04-14 23:52:13726int log_tree_diff_flush(struct rev_info *opt)
Junio C Hamano5f1c3f02006-04-09 08:11:11727{
Junio C Hamanobd1470b2012-10-18 04:27:22728 opt->shown_dashes = 0;
Junio C Hamano5f1c3f02006-04-09 08:11:11729 diffcore_std(&opt->diffopt);
Linus Torvalds91539832006-04-17 18:59:32730
Junio C Hamano5f1c3f02006-04-09 08:11:11731 if (diff_queue_is_empty()) {
732 int saved_fmt = opt->diffopt.output_format;
733 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
734 diff_flush(&opt->diffopt);
735 opt->diffopt.output_format = saved_fmt;
736 return 0;
737 }
Linus Torvalds91539832006-04-17 18:59:32738
Junio C Hamano3969cf72006-06-27 22:08:19739 if (opt->loginfo && !opt->no_commit_id) {
Adam Simpkins02865652008-04-29 08:32:59740 show_log(opt);
Linus Torvalds304b5af2007-10-09 16:35:22741 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
742 opt->verbose_header &&
Jeff Kingb9c7d6e2014-07-29 17:56:48743 opt->commit_format != CMIT_FMT_ONELINE &&
744 !commit_format_is_empty(opt->commit_format)) {
Junio C Hamano1d34c502012-11-13 18:09:07745 /*
746 * When showing a verbose header (i.e. log message),
747 * and not in --pretty=oneline format, we would want
748 * an extra newline between the end of log and the
749 * diff/diffstat output for readability.
750 */
Junio C Hamano3969cf72006-06-27 22:08:19751 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
Bo Yang81bd1b22010-05-26 07:08:03752 if (opt->diffopt.output_prefix) {
753 struct strbuf *msg = NULL;
754 msg = opt->diffopt.output_prefix(&opt->diffopt,
755 opt->diffopt.output_prefix_data);
Johannes Schindelin4d7b0ef2016-06-22 15:01:32756 fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
Bo Yang81bd1b22010-05-26 07:08:03757 }
Junio C Hamano1d34c502012-11-13 18:09:07758
759 /*
760 * We may have shown three-dashes line early
761 * between notes and the log message, in which
762 * case we only want a blank line after the
763 * notes without (an extra) three-dashes line.
764 * Otherwise, we show the three-dashes line if
765 * we are showing the patch with diffstat, but
766 * in that case, there is no extra blank line
767 * after the three-dashes line.
768 */
769 if (!opt->shown_dashes &&
770 (pch & opt->diffopt.output_format) == pch)
Johannes Schindelin4d7b0ef2016-06-22 15:01:32771 fprintf(opt->diffopt.file, "---");
772 putc('\n', opt->diffopt.file);
Junio C Hamano3969cf72006-06-27 22:08:19773 }
774 }
Junio C Hamano5f1c3f02006-04-09 08:11:11775 diff_flush(&opt->diffopt);
776 return 1;
777}
778
Linus Torvaldscd2bdc52006-04-14 23:52:13779static int do_diff_combined(struct rev_info *opt, struct commit *commit)
Junio C Hamano5f1c3f02006-04-09 08:11:11780{
René Scharfe82889292011-12-17 10:20:07781 diff_tree_combined_merge(commit, opt->dense_combined_merges, opt);
Linus Torvalds91539832006-04-17 18:59:32782 return !opt->loginfo;
Junio C Hamano5f1c3f02006-04-09 08:11:11783}
784
Linus Torvalds91539832006-04-17 18:59:32785/*
786 * Show the diff of a commit.
787 *
788 * Return true if we printed any log info messages
789 */
790static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
Junio C Hamano5f1c3f02006-04-09 08:11:11791{
Linus Torvalds91539832006-04-17 18:59:32792 int showed_log;
Junio C Hamano5f1c3f02006-04-09 08:11:11793 struct commit_list *parents;
brian m. carlsonf2fd0762015-11-10 02:22:28794 struct object_id *oid;
Junio C Hamano5f1c3f02006-04-09 08:11:11795
Peter Valdemar Mørch84102a32008-08-11 06:46:25796 if (!opt->diff && !DIFF_OPT_TST(&opt->diffopt, EXIT_WITH_STATUS))
Linus Torvalds91539832006-04-17 18:59:32797 return 0;
798
Jeff King7059dcc2013-10-24 08:52:36799 parse_commit_or_die(commit);
brian m. carlsonf2fd0762015-11-10 02:22:28800 oid = &commit->tree->object.oid;
Thomas Rastd1b9b762013-03-28 08:19:34801
Junio C Hamano5f1c3f02006-04-09 08:11:11802 /* Root commit? */
Thomas Rast53d00b32013-07-31 20:13:20803 parents = get_saved_parents(opt, commit);
Linus Torvalds91539832006-04-17 18:59:32804 if (!parents) {
Rene Scharfe2b603562006-10-26 16:52:39805 if (opt->show_root_diff) {
brian m. carlsonf2fd0762015-11-10 02:22:28806 diff_root_tree_sha1(oid->hash, "", &opt->diffopt);
Rene Scharfe2b603562006-10-26 16:52:39807 log_tree_diff_flush(opt);
808 }
Linus Torvalds91539832006-04-17 18:59:32809 return !opt->loginfo;
Junio C Hamano5f1c3f02006-04-09 08:11:11810 }
811
812 /* More than one parent? */
Linus Torvalds91539832006-04-17 18:59:32813 if (parents && parents->next) {
Junio C Hamano5f1c3f02006-04-09 08:11:11814 if (opt->ignore_merges)
815 return 0;
816 else if (opt->combine_merges)
817 return do_diff_combined(opt, commit);
Petr Baudis88d9d452010-02-10 01:11:49818 else if (opt->first_parent_only) {
819 /*
820 * Generate merge log entry only for the first
821 * parent, showing summary diff of the others
822 * we merged _in_.
823 */
Jeff King7059dcc2013-10-24 08:52:36824 parse_commit_or_die(parents->item);
brian m. carlsonf2fd0762015-11-10 02:22:28825 diff_tree_sha1(parents->item->tree->object.oid.hash,
826 oid->hash, "", &opt->diffopt);
Petr Baudis88d9d452010-02-10 01:11:49827 log_tree_diff_flush(opt);
828 return !opt->loginfo;
829 }
Linus Torvalds91539832006-04-17 18:59:32830
831 /* If we show individual diffs, show the parent info */
832 log->parent = parents->item;
Junio C Hamano5f1c3f02006-04-09 08:11:11833 }
834
Linus Torvalds91539832006-04-17 18:59:32835 showed_log = 0;
836 for (;;) {
Junio C Hamano5f1c3f02006-04-09 08:11:11837 struct commit *parent = parents->item;
Junio C Hamano5f1c3f02006-04-09 08:11:11838
Jeff King7059dcc2013-10-24 08:52:36839 parse_commit_or_die(parent);
brian m. carlsonf2fd0762015-11-10 02:22:28840 diff_tree_sha1(parent->tree->object.oid.hash,
841 oid->hash, "", &opt->diffopt);
Linus Torvalds91539832006-04-17 18:59:32842 log_tree_diff_flush(opt);
843
844 showed_log |= !opt->loginfo;
845
846 /* Set up the log info for the next parent, if any.. */
847 parents = parents->next;
848 if (!parents)
849 break;
850 log->parent = parents->item;
851 opt->loginfo = log;
Junio C Hamano5f1c3f02006-04-09 08:11:11852 }
Linus Torvalds91539832006-04-17 18:59:32853 return showed_log;
854}
855
856int log_tree_commit(struct rev_info *opt, struct commit *commit)
857{
858 struct log_info log;
Johannes Schindelin6ea57702016-06-22 15:01:28859 int shown, close_file = opt->diffopt.close_file;
Linus Torvalds91539832006-04-17 18:59:32860
861 log.commit = commit;
862 log.parent = NULL;
863 opt->loginfo = &log;
Johannes Schindelin6ea57702016-06-22 15:01:28864 opt->diffopt.close_file = 0;
Linus Torvalds91539832006-04-17 18:59:32865
Thomas Rast12da1d12013-03-28 16:47:32866 if (opt->line_level_traverse)
867 return line_log_print(opt, commit);
868
Nguyễn Thái Ngọc Duy1b32dec2014-03-25 13:23:27869 if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
Johannes Schindelin4d7b0ef2016-06-22 15:01:32870 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
Junio C Hamano3eefc182006-04-18 23:45:27871 shown = log_tree_diff(opt, commit, &log);
872 if (!shown && opt->loginfo && opt->always_show_header) {
Linus Torvalds91539832006-04-17 18:59:32873 log.parent = NULL;
Adam Simpkins02865652008-04-29 08:32:59874 show_log(opt);
Junio C Hamano3eefc182006-04-18 23:45:27875 shown = 1;
Linus Torvalds91539832006-04-17 18:59:32876 }
Nguyễn Thái Ngọc Duy1b32dec2014-03-25 13:23:27877 if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
Johannes Schindelin4d7b0ef2016-06-22 15:01:32878 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
Linus Torvalds91539832006-04-17 18:59:32879 opt->loginfo = NULL;
Johannes Schindelin4d7b0ef2016-06-22 15:01:32880 maybe_flush_or_die(opt->diffopt.file, "stdout");
Johannes Schindelin6ea57702016-06-22 15:01:28881 if (close_file)
882 fclose(opt->diffopt.file);
Junio C Hamano3eefc182006-04-18 23:45:27883 return shown;
Junio C Hamano5f1c3f02006-04-09 08:11:11884}