submodule: Demonstrate known breakage during recursive merge
[git/jnareb-git.git] / log-tree.c
blob24c295ea1dc91180b9685299f48812593162bdfe
1 #include "cache.h"
2 #include "diff.h"
3 #include "commit.h"
4 #include "tag.h"
5 #include "graph.h"
6 #include "log-tree.h"
7 #include "reflog-walk.h"
8 #include "refs.h"
9 #include "string-list.h"
10 #include "color.h"
12 struct decoration name_decoration = { "object names" };
14 enum decoration_type {
15 DECORATION_NONE = 0,
16 DECORATION_REF_LOCAL,
17 DECORATION_REF_REMOTE,
18 DECORATION_REF_TAG,
19 DECORATION_REF_STASH,
20 DECORATION_REF_HEAD,
21 DECORATION_GRAFTED,
24 static 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 */
31 GIT_COLOR_BOLD_BLUE, /* GRAFTED */
34 static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
36 if (want_color(decorate_use_color))
37 return decoration_colors[ix];
38 return "";
41 static int parse_decorate_color_slot(const char *slot)
44 * We're comparing with 'ignore-case' on
45 * (because config.c sets them all tolower),
46 * but let's match the letters in the literal
47 * string values here with how they are
48 * documented in Documentation/config.txt, for
49 * consistency.
51 * We love being consistent, don't we?
53 if (!strcasecmp(slot, "branch"))
54 return DECORATION_REF_LOCAL;
55 if (!strcasecmp(slot, "remoteBranch"))
56 return DECORATION_REF_REMOTE;
57 if (!strcasecmp(slot, "tag"))
58 return DECORATION_REF_TAG;
59 if (!strcasecmp(slot, "stash"))
60 return DECORATION_REF_STASH;
61 if (!strcasecmp(slot, "HEAD"))
62 return DECORATION_REF_HEAD;
63 return -1;
66 int parse_decorate_color_config(const char *var, const int ofs, const char *value)
68 int slot = parse_decorate_color_slot(var + ofs);
69 if (slot < 0)
70 return 0;
71 if (!value)
72 return config_error_nonbool(var);
73 color_parse(value, var, decoration_colors[slot]);
74 return 0;
78 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
79 * for showing the commit sha1, use the same check for --decorate
81 #define decorate_get_color_opt(o, ix) \
82 decorate_get_color((o)->use_color, ix)
84 static void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
86 int nlen = strlen(name);
87 struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + nlen);
88 memcpy(res->name, name, nlen + 1);
89 res->type = type;
90 res->next = add_decoration(&name_decoration, obj, res);
93 static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
95 struct object *obj;
96 enum decoration_type type = DECORATION_NONE;
98 if (!prefixcmp(refname, "refs/replace/")) {
99 unsigned char original_sha1[20];
100 if (!read_replace_refs)
101 return 0;
102 if (get_sha1_hex(refname + 13, original_sha1)) {
103 warning("invalid replace ref %s", refname);
104 return 0;
106 obj = parse_object(original_sha1);
107 if (obj)
108 add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
109 return 0;
112 obj = parse_object(sha1);
113 if (!obj)
114 return 0;
116 if (!prefixcmp(refname, "refs/heads/"))
117 type = DECORATION_REF_LOCAL;
118 else if (!prefixcmp(refname, "refs/remotes/"))
119 type = DECORATION_REF_REMOTE;
120 else if (!prefixcmp(refname, "refs/tags/"))
121 type = DECORATION_REF_TAG;
122 else if (!prefixcmp(refname, "refs/stash"))
123 type = DECORATION_REF_STASH;
124 else if (!prefixcmp(refname, "HEAD"))
125 type = DECORATION_REF_HEAD;
127 if (!cb_data || *(int *)cb_data == DECORATE_SHORT_REFS)
128 refname = prettify_refname(refname);
129 add_name_decoration(type, refname, obj);
130 while (obj->type == OBJ_TAG) {
131 obj = ((struct tag *)obj)->tagged;
132 if (!obj)
133 break;
134 add_name_decoration(DECORATION_REF_TAG, refname, obj);
136 return 0;
139 static int add_graft_decoration(const struct commit_graft *graft, void *cb_data)
141 struct commit *commit = lookup_commit(graft->sha1);
142 if (!commit)
143 return 0;
144 add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
145 return 0;
148 void load_ref_decorations(int flags)
150 static int loaded;
151 if (!loaded) {
152 loaded = 1;
153 for_each_ref(add_ref_decoration, &flags);
154 head_ref(add_ref_decoration, &flags);
155 for_each_commit_graft(add_graft_decoration, NULL);
159 static void show_parents(struct commit *commit, int abbrev)
161 struct commit_list *p;
162 for (p = commit->parents; p ; p = p->next) {
163 struct commit *parent = p->item;
164 printf(" %s", find_unique_abbrev(parent->object.sha1, abbrev));
168 void show_decorations(struct rev_info *opt, struct commit *commit)
170 const char *prefix;
171 struct name_decoration *decoration;
172 const char *color_commit =
173 diff_get_color_opt(&opt->diffopt, DIFF_COMMIT);
174 const char *color_reset =
175 decorate_get_color_opt(&opt->diffopt, DECORATION_NONE);
177 if (opt->show_source && commit->util)
178 printf("\t%s", (char *) commit->util);
179 if (!opt->show_decorations)
180 return;
181 decoration = lookup_decoration(&name_decoration, &commit->object);
182 if (!decoration)
183 return;
184 prefix = " (";
185 while (decoration) {
186 printf("%s", prefix);
187 fputs(decorate_get_color_opt(&opt->diffopt, decoration->type),
188 stdout);
189 if (decoration->type == DECORATION_REF_TAG)
190 fputs("tag: ", stdout);
191 printf("%s", decoration->name);
192 fputs(color_reset, stdout);
193 fputs(color_commit, stdout);
194 prefix = ", ";
195 decoration = decoration->next;
197 putchar(')');
201 * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
202 * Signed-off-by: and Acked-by: lines.
204 static int detect_any_signoff(char *letter, int size)
206 char *cp;
207 int seen_colon = 0;
208 int seen_at = 0;
209 int seen_name = 0;
210 int seen_head = 0;
212 cp = letter + size;
213 while (letter <= --cp && *cp == '\n')
214 continue;
216 while (letter <= cp) {
217 char ch = *cp--;
218 if (ch == '\n')
219 break;
221 if (!seen_at) {
222 if (ch == '@')
223 seen_at = 1;
224 continue;
226 if (!seen_colon) {
227 if (ch == '@')
228 return 0;
229 else if (ch == ':')
230 seen_colon = 1;
231 else
232 seen_name = 1;
233 continue;
235 if (('A' <= ch && ch <= 'Z') ||
236 ('a' <= ch && ch <= 'z') ||
237 ch == '-') {
238 seen_head = 1;
239 continue;
241 /* no empty last line doesn't match */
242 return 0;
244 return seen_head && seen_name;
247 static void append_signoff(struct strbuf *sb, const char *signoff)
249 static const char signed_off_by[] = "Signed-off-by: ";
250 size_t signoff_len = strlen(signoff);
251 int has_signoff = 0;
252 char *cp;
254 cp = sb->buf;
256 /* First see if we already have the sign-off by the signer */
257 while ((cp = strstr(cp, signed_off_by))) {
259 has_signoff = 1;
261 cp += strlen(signed_off_by);
262 if (cp + signoff_len >= sb->buf + sb->len)
263 break;
264 if (strncmp(cp, signoff, signoff_len))
265 continue;
266 if (!isspace(cp[signoff_len]))
267 continue;
268 /* we already have him */
269 return;
272 if (!has_signoff)
273 has_signoff = detect_any_signoff(sb->buf, sb->len);
275 if (!has_signoff)
276 strbuf_addch(sb, '\n');
278 strbuf_addstr(sb, signed_off_by);
279 strbuf_add(sb, signoff, signoff_len);
280 strbuf_addch(sb, '\n');
283 static unsigned int digits_in_number(unsigned int number)
285 unsigned int i = 10, result = 1;
286 while (i <= number) {
287 i *= 10;
288 result++;
290 return result;
293 void get_patch_filename(struct commit *commit, int nr, const char *suffix,
294 struct strbuf *buf)
296 int suffix_len = strlen(suffix) + 1;
297 int start_len = buf->len;
299 strbuf_addf(buf, commit ? "%04d-" : "%d", nr);
300 if (commit) {
301 int max_len = start_len + FORMAT_PATCH_NAME_MAX - suffix_len;
302 struct pretty_print_context ctx = {0};
303 ctx.date_mode = DATE_NORMAL;
305 format_commit_message(commit, "%f", buf, &ctx);
306 if (max_len < buf->len)
307 strbuf_setlen(buf, max_len);
308 strbuf_addstr(buf, suffix);
312 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
313 const char **subject_p,
314 const char **extra_headers_p,
315 int *need_8bit_cte_p)
317 const char *subject = NULL;
318 const char *extra_headers = opt->extra_headers;
319 const char *name = sha1_to_hex(commit->object.sha1);
321 *need_8bit_cte_p = 0; /* unknown */
322 if (opt->total > 0) {
323 static char buffer[64];
324 snprintf(buffer, sizeof(buffer),
325 "Subject: [%s%s%0*d/%d] ",
326 opt->subject_prefix,
327 *opt->subject_prefix ? " " : "",
328 digits_in_number(opt->total),
329 opt->nr, opt->total);
330 subject = buffer;
331 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
332 static char buffer[256];
333 snprintf(buffer, sizeof(buffer),
334 "Subject: [%s] ",
335 opt->subject_prefix);
336 subject = buffer;
337 } else {
338 subject = "Subject: ";
341 printf("From %s Mon Sep 17 00:00:00 2001\n", name);
342 graph_show_oneline(opt->graph);
343 if (opt->message_id) {
344 printf("Message-Id: <%s>\n", opt->message_id);
345 graph_show_oneline(opt->graph);
347 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
348 int i, n;
349 n = opt->ref_message_ids->nr;
350 printf("In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
351 for (i = 0; i < n; i++)
352 printf("%s<%s>\n", (i > 0 ? "\t" : "References: "),
353 opt->ref_message_ids->items[i].string);
354 graph_show_oneline(opt->graph);
356 if (opt->mime_boundary) {
357 static char subject_buffer[1024];
358 static char buffer[1024];
359 struct strbuf filename = STRBUF_INIT;
360 *need_8bit_cte_p = -1; /* NEVER */
361 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
362 "%s"
363 "MIME-Version: 1.0\n"
364 "Content-Type: multipart/mixed;"
365 " boundary=\"%s%s\"\n"
366 "\n"
367 "This is a multi-part message in MIME "
368 "format.\n"
369 "--%s%s\n"
370 "Content-Type: text/plain; "
371 "charset=UTF-8; format=fixed\n"
372 "Content-Transfer-Encoding: 8bit\n\n",
373 extra_headers ? extra_headers : "",
374 mime_boundary_leader, opt->mime_boundary,
375 mime_boundary_leader, opt->mime_boundary);
376 extra_headers = subject_buffer;
378 get_patch_filename(opt->numbered_files ? NULL : commit, opt->nr,
379 opt->patch_suffix, &filename);
380 snprintf(buffer, sizeof(buffer) - 1,
381 "\n--%s%s\n"
382 "Content-Type: text/x-patch;"
383 " name=\"%s\"\n"
384 "Content-Transfer-Encoding: 8bit\n"
385 "Content-Disposition: %s;"
386 " filename=\"%s\"\n\n",
387 mime_boundary_leader, opt->mime_boundary,
388 filename.buf,
389 opt->no_inline ? "attachment" : "inline",
390 filename.buf);
391 opt->diffopt.stat_sep = buffer;
392 strbuf_release(&filename);
394 *subject_p = subject;
395 *extra_headers_p = extra_headers;
398 void show_log(struct rev_info *opt)
400 struct strbuf msgbuf = STRBUF_INIT;
401 struct log_info *log = opt->loginfo;
402 struct commit *commit = log->commit, *parent = log->parent;
403 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
404 const char *extra_headers = opt->extra_headers;
405 struct pretty_print_context ctx = {0};
407 opt->loginfo = NULL;
408 ctx.show_notes = opt->show_notes;
409 if (!opt->verbose_header) {
410 graph_show_commit(opt->graph);
412 if (!opt->graph)
413 put_revision_mark(opt, commit);
414 fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
415 if (opt->print_parents)
416 show_parents(commit, abbrev_commit);
417 show_decorations(opt, commit);
418 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
419 putchar('\n');
420 graph_show_remainder(opt->graph);
422 putchar(opt->diffopt.line_termination);
423 return;
427 * If use_terminator is set, we already handled any record termination
428 * at the end of the last record.
429 * Otherwise, add a diffopt.line_termination character before all
430 * entries but the first. (IOW, as a separator between entries)
432 if (opt->shown_one && !opt->use_terminator) {
434 * If entries are separated by a newline, the output
435 * should look human-readable. If the last entry ended
436 * with a newline, print the graph output before this
437 * newline. Otherwise it will end up as a completely blank
438 * line and will look like a gap in the graph.
440 * If the entry separator is not a newline, the output is
441 * primarily intended for programmatic consumption, and we
442 * never want the extra graph output before the entry
443 * separator.
445 if (opt->diffopt.line_termination == '\n' &&
446 !opt->missing_newline)
447 graph_show_padding(opt->graph);
448 putchar(opt->diffopt.line_termination);
450 opt->shown_one = 1;
453 * If the history graph was requested,
454 * print the graph, up to this commit's line
456 graph_show_commit(opt->graph);
459 * Print header line of header..
462 if (opt->commit_format == CMIT_FMT_EMAIL) {
463 log_write_email_headers(opt, commit, &ctx.subject, &extra_headers,
464 &ctx.need_8bit_cte);
465 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
466 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
467 if (opt->commit_format != CMIT_FMT_ONELINE)
468 fputs("commit ", stdout);
470 if (!opt->graph)
471 put_revision_mark(opt, commit);
472 fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit),
473 stdout);
474 if (opt->print_parents)
475 show_parents(commit, abbrev_commit);
476 if (parent)
477 printf(" (from %s)",
478 find_unique_abbrev(parent->object.sha1,
479 abbrev_commit));
480 show_decorations(opt, commit);
481 printf("%s", diff_get_color_opt(&opt->diffopt, DIFF_RESET));
482 if (opt->commit_format == CMIT_FMT_ONELINE) {
483 putchar(' ');
484 } else {
485 putchar('\n');
486 graph_show_oneline(opt->graph);
488 if (opt->reflog_info) {
490 * setup_revisions() ensures that opt->reflog_info
491 * and opt->graph cannot both be set,
492 * so we don't need to worry about printing the
493 * graph info here.
495 show_reflog_message(opt->reflog_info,
496 opt->commit_format == CMIT_FMT_ONELINE,
497 opt->date_mode_explicit ?
498 opt->date_mode :
499 DATE_NORMAL);
500 if (opt->commit_format == CMIT_FMT_ONELINE)
501 return;
505 if (!commit->buffer)
506 return;
509 * And then the pretty-printed message itself
511 if (ctx.need_8bit_cte >= 0)
512 ctx.need_8bit_cte = has_non_ascii(opt->add_signoff);
513 ctx.date_mode = opt->date_mode;
514 ctx.abbrev = opt->diffopt.abbrev;
515 ctx.after_subject = extra_headers;
516 ctx.preserve_subject = opt->preserve_subject;
517 ctx.reflog_info = opt->reflog_info;
518 ctx.fmt = opt->commit_format;
519 pretty_print_commit(&ctx, commit, &msgbuf);
521 if (opt->add_signoff)
522 append_signoff(&msgbuf, opt->add_signoff);
523 if (opt->show_log_size) {
524 printf("log size %i\n", (int)msgbuf.len);
525 graph_show_oneline(opt->graph);
529 * Set opt->missing_newline if msgbuf doesn't
530 * end in a newline (including if it is empty)
532 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
533 opt->missing_newline = 1;
534 else
535 opt->missing_newline = 0;
537 if (opt->graph)
538 graph_show_commit_msg(opt->graph, &msgbuf);
539 else
540 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
541 if (opt->use_terminator) {
542 if (!opt->missing_newline)
543 graph_show_padding(opt->graph);
544 putchar('\n');
547 strbuf_release(&msgbuf);
550 int log_tree_diff_flush(struct rev_info *opt)
552 diffcore_std(&opt->diffopt);
554 if (diff_queue_is_empty()) {
555 int saved_fmt = opt->diffopt.output_format;
556 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
557 diff_flush(&opt->diffopt);
558 opt->diffopt.output_format = saved_fmt;
559 return 0;
562 if (opt->loginfo && !opt->no_commit_id) {
563 /* When showing a verbose header (i.e. log message),
564 * and not in --pretty=oneline format, we would want
565 * an extra newline between the end of log and the
566 * output for readability.
568 show_log(opt);
569 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
570 opt->verbose_header &&
571 opt->commit_format != CMIT_FMT_ONELINE) {
572 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
573 if ((pch & opt->diffopt.output_format) == pch)
574 printf("---");
575 if (opt->diffopt.output_prefix) {
576 struct strbuf *msg = NULL;
577 msg = opt->diffopt.output_prefix(&opt->diffopt,
578 opt->diffopt.output_prefix_data);
579 fwrite(msg->buf, msg->len, 1, stdout);
581 putchar('\n');
584 diff_flush(&opt->diffopt);
585 return 1;
588 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
590 unsigned const char *sha1 = commit->object.sha1;
592 diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
593 return !opt->loginfo;
597 * Show the diff of a commit.
599 * Return true if we printed any log info messages
601 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
603 int showed_log;
604 struct commit_list *parents;
605 unsigned const char *sha1 = commit->object.sha1;
607 if (!opt->diff && !DIFF_OPT_TST(&opt->diffopt, EXIT_WITH_STATUS))
608 return 0;
610 /* Root commit? */
611 parents = commit->parents;
612 if (!parents) {
613 if (opt->show_root_diff) {
614 diff_root_tree_sha1(sha1, "", &opt->diffopt);
615 log_tree_diff_flush(opt);
617 return !opt->loginfo;
620 /* More than one parent? */
621 if (parents && parents->next) {
622 if (opt->ignore_merges)
623 return 0;
624 else if (opt->combine_merges)
625 return do_diff_combined(opt, commit);
626 else if (opt->first_parent_only) {
628 * Generate merge log entry only for the first
629 * parent, showing summary diff of the others
630 * we merged _in_.
632 diff_tree_sha1(parents->item->object.sha1, sha1, "", &opt->diffopt);
633 log_tree_diff_flush(opt);
634 return !opt->loginfo;
637 /* If we show individual diffs, show the parent info */
638 log->parent = parents->item;
641 showed_log = 0;
642 for (;;) {
643 struct commit *parent = parents->item;
645 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
646 log_tree_diff_flush(opt);
648 showed_log |= !opt->loginfo;
650 /* Set up the log info for the next parent, if any.. */
651 parents = parents->next;
652 if (!parents)
653 break;
654 log->parent = parents->item;
655 opt->loginfo = log;
657 return showed_log;
660 int log_tree_commit(struct rev_info *opt, struct commit *commit)
662 struct log_info log;
663 int shown;
665 log.commit = commit;
666 log.parent = NULL;
667 opt->loginfo = &log;
669 shown = log_tree_diff(opt, commit, &log);
670 if (!shown && opt->loginfo && opt->always_show_header) {
671 log.parent = NULL;
672 show_log(opt);
673 shown = 1;
675 opt->loginfo = NULL;
676 maybe_flush_or_die(stdout, "stdout");
677 return shown;