]> git.ipfire.org Git - thirdparty/git.git/blame - log-tree.c
Merge branch 'maint'
[thirdparty/git.git] / log-tree.c
CommitLineData
5f1c3f07
JH
1#include "cache.h"
2#include "diff.h"
3#include "commit.h"
cab4feb6 4#include "tag.h"
7fefda5c 5#include "graph.h"
5f1c3f07 6#include "log-tree.h"
8860fd42 7#include "reflog-walk.h"
cab4feb6 8#include "refs.h"
b079c50e 9#include "string-list.h"
5f1c3f07 10
ca135e7a
LT
11struct decoration name_decoration = { "object names" };
12
cab4feb6
RS
13static void add_name_decoration(const char *prefix, const char *name, struct object *obj)
14{
15 int plen = strlen(prefix);
16 int nlen = strlen(name);
17 struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + plen + nlen);
18 memcpy(res->name, prefix, plen);
19 memcpy(res->name + plen, name, nlen + 1);
20 res->next = add_decoration(&name_decoration, obj, res);
21}
22
23static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
24{
25 struct object *obj = parse_object(sha1);
26 if (!obj)
27 return 0;
33e7018c
LH
28 if (!cb_data || *(int *)cb_data == DECORATE_SHORT_REFS)
29 refname = prettify_refname(refname);
cab4feb6
RS
30 add_name_decoration("", refname, obj);
31 while (obj->type == OBJ_TAG) {
32 obj = ((struct tag *)obj)->tagged;
33 if (!obj)
34 break;
35 add_name_decoration("tag: ", refname, obj);
36 }
37 return 0;
38}
39
33e7018c 40void load_ref_decorations(int flags)
cab4feb6
RS
41{
42 static int loaded;
43 if (!loaded) {
44 loaded = 1;
33e7018c 45 for_each_ref(add_ref_decoration, &flags);
77abcbdc 46 head_ref(add_ref_decoration, &flags);
cab4feb6
RS
47 }
48}
49
c8c893c6
LT
50static void show_parents(struct commit *commit, int abbrev)
51{
52 struct commit_list *p;
53 for (p = commit->parents; p ; p = p->next) {
54 struct commit *parent = p->item;
7fcda920 55 printf(" %s", find_unique_abbrev(parent->object.sha1, abbrev));
c8c893c6
LT
56 }
57}
58
0f3a290b 59void show_decorations(struct rev_info *opt, struct commit *commit)
ca135e7a
LT
60{
61 const char *prefix;
62 struct name_decoration *decoration;
63
0f3a290b 64 if (opt->show_source && commit->util)
8c4021ab 65 printf("\t%s", (char *) commit->util);
d467a525
LT
66 if (!opt->show_decorations)
67 return;
ca135e7a
LT
68 decoration = lookup_decoration(&name_decoration, &commit->object);
69 if (!decoration)
70 return;
71 prefix = " (";
72 while (decoration) {
73 printf("%s%s", prefix, decoration->name);
74 prefix = ", ";
75 decoration = decoration->next;
76 }
77 putchar(')');
78}
79
fc1c75ec
FBH
80/*
81 * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
82 * Signed-off-by: and Acked-by: lines.
83 */
84static int detect_any_signoff(char *letter, int size)
85{
fd13b21f 86 char *cp;
fc1c75ec
FBH
87 int seen_colon = 0;
88 int seen_at = 0;
89 int seen_name = 0;
90 int seen_head = 0;
91
92 cp = letter + size;
fd13b21f 93 while (letter <= --cp && *cp == '\n')
fc1c75ec
FBH
94 continue;
95
96 while (letter <= cp) {
fd13b21f 97 char ch = *cp--;
fc1c75ec
FBH
98 if (ch == '\n')
99 break;
100
101 if (!seen_at) {
102 if (ch == '@')
103 seen_at = 1;
104 continue;
105 }
106 if (!seen_colon) {
107 if (ch == '@')
108 return 0;
109 else if (ch == ':')
110 seen_colon = 1;
111 else
112 seen_name = 1;
113 continue;
114 }
115 if (('A' <= ch && ch <= 'Z') ||
116 ('a' <= ch && ch <= 'z') ||
117 ch == '-') {
118 seen_head = 1;
119 continue;
120 }
121 /* no empty last line doesn't match */
122 return 0;
123 }
124 return seen_head && seen_name;
125}
126
674d1727 127static void append_signoff(struct strbuf *sb, const char *signoff)
cf2251b6 128{
cf2251b6 129 static const char signed_off_by[] = "Signed-off-by: ";
80583c0e 130 size_t signoff_len = strlen(signoff);
fc1c75ec 131 int has_signoff = 0;
80583c0e 132 char *cp;
cf2251b6 133
674d1727 134 cp = sb->buf;
cf2251b6
JH
135
136 /* First see if we already have the sign-off by the signer */
fc1c75ec
FBH
137 while ((cp = strstr(cp, signed_off_by))) {
138
139 has_signoff = 1;
140
cf2251b6 141 cp += strlen(signed_off_by);
674d1727 142 if (cp + signoff_len >= sb->buf + sb->len)
fc1c75ec
FBH
143 break;
144 if (strncmp(cp, signoff, signoff_len))
145 continue;
146 if (!isspace(cp[signoff_len]))
147 continue;
148 /* we already have him */
674d1727 149 return;
cf2251b6
JH
150 }
151
fc1c75ec 152 if (!has_signoff)
674d1727 153 has_signoff = detect_any_signoff(sb->buf, sb->len);
fc1c75ec
FBH
154
155 if (!has_signoff)
674d1727 156 strbuf_addch(sb, '\n');
c35f4c37 157
674d1727
PH
158 strbuf_addstr(sb, signed_off_by);
159 strbuf_add(sb, signoff, signoff_len);
160 strbuf_addch(sb, '\n');
cf2251b6
JH
161}
162
e00de24b
JS
163static unsigned int digits_in_number(unsigned int number)
164{
165 unsigned int i = 10, result = 1;
166 while (i <= number) {
167 i *= 10;
168 result++;
169 }
170 return result;
171}
172
6fa8e627
SB
173void get_patch_filename(struct commit *commit, int nr, const char *suffix,
174 struct strbuf *buf)
175{
176 int suffix_len = strlen(suffix) + 1;
177 int start_len = buf->len;
178
179 strbuf_addf(buf, commit ? "%04d-" : "%d", nr);
180 if (commit) {
b09b868f
CC
181 int max_len = start_len + FORMAT_PATCH_NAME_MAX - suffix_len;
182
6fa8e627 183 format_commit_message(commit, "%f", buf, DATE_NORMAL);
b09b868f
CC
184 if (max_len < buf->len)
185 strbuf_setlen(buf, max_len);
186 strbuf_addstr(buf, suffix);
6fa8e627
SB
187 }
188}
189
108dab28 190void log_write_email_headers(struct rev_info *opt, struct commit *commit,
267123b4
JH
191 const char **subject_p,
192 const char **extra_headers_p,
193 int *need_8bit_cte_p)
b02bd65f
DB
194{
195 const char *subject = NULL;
196 const char *extra_headers = opt->extra_headers;
108dab28 197 const char *name = sha1_to_hex(commit->object.sha1);
267123b4
JH
198
199 *need_8bit_cte_p = 0; /* unknown */
b02bd65f
DB
200 if (opt->total > 0) {
201 static char buffer[64];
202 snprintf(buffer, sizeof(buffer),
203 "Subject: [%s %0*d/%d] ",
204 opt->subject_prefix,
205 digits_in_number(opt->total),
206 opt->nr, opt->total);
207 subject = buffer;
208 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
209 static char buffer[256];
210 snprintf(buffer, sizeof(buffer),
211 "Subject: [%s] ",
212 opt->subject_prefix);
213 subject = buffer;
214 } else {
215 subject = "Subject: ";
216 }
217
218 printf("From %s Mon Sep 17 00:00:00 2001\n", name);
7fefda5c
AS
219 graph_show_oneline(opt->graph);
220 if (opt->message_id) {
b02bd65f 221 printf("Message-Id: <%s>\n", opt->message_id);
7fefda5c
AS
222 graph_show_oneline(opt->graph);
223 }
b079c50e
TR
224 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
225 int i, n;
226 n = opt->ref_message_ids->nr;
227 printf("In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
228 for (i = 0; i < n; i++)
229 printf("%s<%s>\n", (i > 0 ? "\t" : "References: "),
230 opt->ref_message_ids->items[i].string);
7fefda5c
AS
231 graph_show_oneline(opt->graph);
232 }
b02bd65f
DB
233 if (opt->mime_boundary) {
234 static char subject_buffer[1024];
235 static char buffer[1024];
108dab28 236 struct strbuf filename = STRBUF_INIT;
267123b4 237 *need_8bit_cte_p = -1; /* NEVER */
b02bd65f
DB
238 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
239 "%s"
240 "MIME-Version: 1.0\n"
241 "Content-Type: multipart/mixed;"
242 " boundary=\"%s%s\"\n"
243 "\n"
244 "This is a multi-part message in MIME "
245 "format.\n"
246 "--%s%s\n"
247 "Content-Type: text/plain; "
248 "charset=UTF-8; format=fixed\n"
249 "Content-Transfer-Encoding: 8bit\n\n",
250 extra_headers ? extra_headers : "",
251 mime_boundary_leader, opt->mime_boundary,
252 mime_boundary_leader, opt->mime_boundary);
253 extra_headers = subject_buffer;
254
108dab28
SB
255 get_patch_filename(opt->numbered_files ? NULL : commit, opt->nr,
256 opt->patch_suffix, &filename);
b02bd65f 257 snprintf(buffer, sizeof(buffer) - 1,
6b2fbaaf 258 "\n--%s%s\n"
b02bd65f 259 "Content-Type: text/x-patch;"
108dab28 260 " name=\"%s\"\n"
b02bd65f
DB
261 "Content-Transfer-Encoding: 8bit\n"
262 "Content-Disposition: %s;"
108dab28 263 " filename=\"%s\"\n\n",
b02bd65f 264 mime_boundary_leader, opt->mime_boundary,
108dab28 265 filename.buf,
b02bd65f 266 opt->no_inline ? "attachment" : "inline",
108dab28 267 filename.buf);
b02bd65f 268 opt->diffopt.stat_sep = buffer;
108dab28 269 strbuf_release(&filename);
b02bd65f
DB
270 }
271 *subject_p = subject;
272 *extra_headers_p = extra_headers;
273}
274
02865655 275void show_log(struct rev_info *opt)
91539833 276{
f285a2d7 277 struct strbuf msgbuf = STRBUF_INIT;
39bc9a6c 278 struct log_info *log = opt->loginfo;
91539833
LT
279 struct commit *commit = log->commit, *parent = log->parent;
280 int abbrev = opt->diffopt.abbrev;
281 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
20ff0680 282 const char *subject = NULL, *extra_headers = opt->extra_headers;
6bf4f1b4 283 int need_8bit_cte = 0;
91539833
LT
284
285 opt->loginfo = NULL;
286 if (!opt->verbose_header) {
7fefda5c
AS
287 graph_show_commit(opt->graph);
288
7528f27d
AS
289 if (!opt->graph) {
290 if (commit->object.flags & BOUNDARY)
291 putchar('-');
292 else if (commit->object.flags & UNINTERESTING)
293 putchar('^');
294 else if (opt->left_right) {
295 if (commit->object.flags & SYMMETRIC_LEFT)
296 putchar('<');
297 else
298 putchar('>');
299 }
74bd9029 300 }
7fcda920 301 fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
885cf808 302 if (opt->print_parents)
c8c893c6 303 show_parents(commit, abbrev_commit);
0f3a290b 304 show_decorations(opt, commit);
7fefda5c
AS
305 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
306 putchar('\n');
307 graph_show_remainder(opt->graph);
308 }
1dcb6922 309 putchar(opt->diffopt.line_termination);
91539833
LT
310 return;
311 }
312
313 /*
8715227d
JK
314 * If use_terminator is set, we already handled any record termination
315 * at the end of the last record.
02865655
AS
316 * Otherwise, add a diffopt.line_termination character before all
317 * entries but the first. (IOW, as a separator between entries)
91539833 318 */
7fefda5c
AS
319 if (opt->shown_one && !opt->use_terminator) {
320 /*
321 * If entries are separated by a newline, the output
322 * should look human-readable. If the last entry ended
323 * with a newline, print the graph output before this
324 * newline. Otherwise it will end up as a completely blank
325 * line and will look like a gap in the graph.
326 *
327 * If the entry separator is not a newline, the output is
328 * primarily intended for programmatic consumption, and we
329 * never want the extra graph output before the entry
330 * separator.
331 */
332 if (opt->diffopt.line_termination == '\n' &&
333 !opt->missing_newline)
334 graph_show_padding(opt->graph);
abd4e222 335 putchar(opt->diffopt.line_termination);
7fefda5c 336 }
91539833
LT
337 opt->shown_one = 1;
338
7fefda5c
AS
339 /*
340 * If the history graph was requested,
341 * print the graph, up to this commit's line
342 */
343 graph_show_commit(opt->graph);
344
91539833
LT
345 /*
346 * Print header line of header..
347 */
3eefc189 348
596524b3 349 if (opt->commit_format == CMIT_FMT_EMAIL) {
108dab28 350 log_write_email_headers(opt, commit, &subject, &extra_headers,
267123b4 351 &need_8bit_cte);
e52a5de4 352 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
8f67f8ae 353 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
74bd9029
JH
354 if (opt->commit_format != CMIT_FMT_ONELINE)
355 fputs("commit ", stdout);
7528f27d
AS
356
357 if (!opt->graph) {
358 if (commit->object.flags & BOUNDARY)
359 putchar('-');
360 else if (commit->object.flags & UNINTERESTING)
361 putchar('^');
362 else if (opt->left_right) {
363 if (commit->object.flags & SYMMETRIC_LEFT)
364 putchar('<');
365 else
366 putchar('>');
367 }
74bd9029 368 }
7fcda920 369 fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit),
74bd9029 370 stdout);
885cf808 371 if (opt->print_parents)
c66b6c06 372 show_parents(commit, abbrev_commit);
73f0a157 373 if (parent)
3eefc189 374 printf(" (from %s)",
7fcda920 375 find_unique_abbrev(parent->object.sha1,
3eefc189 376 abbrev_commit));
0f3a290b 377 show_decorations(opt, commit);
8f67f8ae 378 printf("%s", diff_get_color_opt(&opt->diffopt, DIFF_RESET));
7fefda5c
AS
379 if (opt->commit_format == CMIT_FMT_ONELINE) {
380 putchar(' ');
381 } else {
382 putchar('\n');
383 graph_show_oneline(opt->graph);
384 }
903b45fe 385 if (opt->reflog_info) {
7fefda5c
AS
386 /*
387 * setup_revisions() ensures that opt->reflog_info
388 * and opt->graph cannot both be set,
389 * so we don't need to worry about printing the
390 * graph info here.
391 */
4d12a471 392 show_reflog_message(opt->reflog_info,
4e244cbc 393 opt->commit_format == CMIT_FMT_ONELINE,
f4ea32f0
JK
394 opt->date_mode_explicit ?
395 opt->date_mode :
396 DATE_NORMAL);
02865655 397 if (opt->commit_format == CMIT_FMT_ONELINE)
903b45fe 398 return;
903b45fe 399 }
3eefc189 400 }
91539833 401
3131b713
LT
402 if (!commit->buffer)
403 return;
404
91539833
LT
405 /*
406 * And then the pretty-printed message itself
407 */
6bf4f1b4
JH
408 if (need_8bit_cte >= 0)
409 need_8bit_cte = has_non_ascii(opt->add_signoff);
674d1727 410 pretty_print_commit(opt->commit_format, commit, &msgbuf,
4593fb84 411 abbrev, subject, extra_headers, opt->date_mode,
6bf4f1b4 412 need_8bit_cte);
cf2251b6
JH
413
414 if (opt->add_signoff)
674d1727 415 append_signoff(&msgbuf, opt->add_signoff);
7fefda5c 416 if (opt->show_log_size) {
674d1727 417 printf("log size %i\n", (int)msgbuf.len);
7fefda5c
AS
418 graph_show_oneline(opt->graph);
419 }
9fa3465d 420
7fefda5c
AS
421 /*
422 * Set opt->missing_newline if msgbuf doesn't
423 * end in a newline (including if it is empty)
424 */
425 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
426 opt->missing_newline = 1;
427 else
428 opt->missing_newline = 0;
429
430 if (opt->graph)
431 graph_show_commit_msg(opt->graph, &msgbuf);
432 else
42c8c74c 433 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
7fefda5c
AS
434 if (opt->use_terminator) {
435 if (!opt->missing_newline)
436 graph_show_padding(opt->graph);
9b58bfe8 437 putchar('\n');
7fefda5c
AS
438 }
439
674d1727 440 strbuf_release(&msgbuf);
91539833
LT
441}
442
cd2bdc53 443int log_tree_diff_flush(struct rev_info *opt)
5f1c3f07
JH
444{
445 diffcore_std(&opt->diffopt);
91539833 446
5f1c3f07
JH
447 if (diff_queue_is_empty()) {
448 int saved_fmt = opt->diffopt.output_format;
449 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
450 diff_flush(&opt->diffopt);
451 opt->diffopt.output_format = saved_fmt;
452 return 0;
453 }
91539833 454
3969cf7d
JH
455 if (opt->loginfo && !opt->no_commit_id) {
456 /* When showing a verbose header (i.e. log message),
457 * and not in --pretty=oneline format, we would want
458 * an extra newline between the end of log and the
459 * output for readability.
460 */
02865655 461 show_log(opt);
304b5af6
LT
462 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
463 opt->verbose_header &&
3969cf7d
JH
464 opt->commit_format != CMIT_FMT_ONELINE) {
465 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
466 if ((pch & opt->diffopt.output_format) == pch)
abd4e222
LT
467 printf("---");
468 putchar('\n');
3969cf7d
JH
469 }
470 }
5f1c3f07
JH
471 diff_flush(&opt->diffopt);
472 return 1;
473}
474
cd2bdc53 475static int do_diff_combined(struct rev_info *opt, struct commit *commit)
5f1c3f07
JH
476{
477 unsigned const char *sha1 = commit->object.sha1;
478
91539833
LT
479 diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
480 return !opt->loginfo;
5f1c3f07
JH
481}
482
91539833
LT
483/*
484 * Show the diff of a commit.
485 *
486 * Return true if we printed any log info messages
487 */
488static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
5f1c3f07 489{
91539833 490 int showed_log;
5f1c3f07
JH
491 struct commit_list *parents;
492 unsigned const char *sha1 = commit->object.sha1;
493
84102a33 494 if (!opt->diff && !DIFF_OPT_TST(&opt->diffopt, EXIT_WITH_STATUS))
91539833
LT
495 return 0;
496
5f1c3f07 497 /* Root commit? */
91539833
LT
498 parents = commit->parents;
499 if (!parents) {
2b60356d
RS
500 if (opt->show_root_diff) {
501 diff_root_tree_sha1(sha1, "", &opt->diffopt);
502 log_tree_diff_flush(opt);
503 }
91539833 504 return !opt->loginfo;
5f1c3f07
JH
505 }
506
507 /* More than one parent? */
91539833 508 if (parents && parents->next) {
5f1c3f07
JH
509 if (opt->ignore_merges)
510 return 0;
511 else if (opt->combine_merges)
512 return do_diff_combined(opt, commit);
91539833
LT
513
514 /* If we show individual diffs, show the parent info */
515 log->parent = parents->item;
5f1c3f07
JH
516 }
517
91539833
LT
518 showed_log = 0;
519 for (;;) {
5f1c3f07 520 struct commit *parent = parents->item;
5f1c3f07 521
91539833
LT
522 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
523 log_tree_diff_flush(opt);
524
525 showed_log |= !opt->loginfo;
526
527 /* Set up the log info for the next parent, if any.. */
528 parents = parents->next;
529 if (!parents)
530 break;
531 log->parent = parents->item;
532 opt->loginfo = log;
533 }
534 return showed_log;
535}
536
537int log_tree_commit(struct rev_info *opt, struct commit *commit)
538{
539 struct log_info log;
3eefc189 540 int shown;
91539833
LT
541
542 log.commit = commit;
543 log.parent = NULL;
544 opt->loginfo = &log;
545
3eefc189
JH
546 shown = log_tree_diff(opt, commit, &log);
547 if (!shown && opt->loginfo && opt->always_show_header) {
91539833 548 log.parent = NULL;
02865655 549 show_log(opt);
3eefc189 550 shown = 1;
5f1c3f07 551 }
91539833 552 opt->loginfo = NULL;
06f59e9f 553 maybe_flush_or_die(stdout, "stdout");
3eefc189 554 return shown;
5f1c3f07 555}