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