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