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