]> asedeno.scripts.mit.edu Git - git.git/blob - log-tree.c
bec609676e1cee18d25b3158ac3049e886e19860
[git.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 enum 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
22 static void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
23 {
24         int nlen = strlen(name);
25         struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + nlen);
26         memcpy(res->name, name, nlen + 1);
27         res->type = type;
28         res->next = add_decoration(&name_decoration, obj, res);
29 }
30
31 static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
32 {
33         struct object *obj = parse_object(sha1);
34         enum decoration_type type = DECORATION_NONE;
35         if (!obj)
36                 return 0;
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
49         if (!cb_data || *(int *)cb_data == DECORATE_SHORT_REFS)
50                 refname = prettify_refname(refname);
51         add_name_decoration(type, refname, obj);
52         while (obj->type == OBJ_TAG) {
53                 obj = ((struct tag *)obj)->tagged;
54                 if (!obj)
55                         break;
56                 add_name_decoration(DECORATION_REF_TAG, refname, obj);
57         }
58         return 0;
59 }
60
61 void load_ref_decorations(int flags)
62 {
63         static int loaded;
64         if (!loaded) {
65                 loaded = 1;
66                 for_each_ref(add_ref_decoration, &flags);
67                 head_ref(add_ref_decoration, &flags);
68         }
69 }
70
71 static 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;
76                 printf(" %s", find_unique_abbrev(parent->object.sha1, abbrev));
77         }
78 }
79
80 void show_decorations(struct rev_info *opt, struct commit *commit)
81 {
82         const char *prefix;
83         struct name_decoration *decoration;
84
85         if (opt->show_source && commit->util)
86                 printf("\t%s", (char *) commit->util);
87         if (!opt->show_decorations)
88                 return;
89         decoration = lookup_decoration(&name_decoration, &commit->object);
90         if (!decoration)
91                 return;
92         prefix = " (";
93         while (decoration) {
94                 printf("%s", prefix);
95                 if (decoration->type == DECORATION_REF_TAG)
96                         printf("tag: ");
97                 printf("%s", decoration->name);
98                 prefix = ", ";
99                 decoration = decoration->next;
100         }
101         putchar(')');
102 }
103
104 /*
105  * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
106  * Signed-off-by: and Acked-by: lines.
107  */
108 static int detect_any_signoff(char *letter, int size)
109 {
110         char *cp;
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;
117         while (letter <= --cp && *cp == '\n')
118                 continue;
119
120         while (letter <= cp) {
121                 char ch = *cp--;
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
151 static void append_signoff(struct strbuf *sb, const char *signoff)
152 {
153         static const char signed_off_by[] = "Signed-off-by: ";
154         size_t signoff_len = strlen(signoff);
155         int has_signoff = 0;
156         char *cp;
157
158         cp = sb->buf;
159
160         /* First see if we already have the sign-off by the signer */
161         while ((cp = strstr(cp, signed_off_by))) {
162
163                 has_signoff = 1;
164
165                 cp += strlen(signed_off_by);
166                 if (cp + signoff_len >= sb->buf + sb->len)
167                         break;
168                 if (strncmp(cp, signoff, signoff_len))
169                         continue;
170                 if (!isspace(cp[signoff_len]))
171                         continue;
172                 /* we already have him */
173                 return;
174         }
175
176         if (!has_signoff)
177                 has_signoff = detect_any_signoff(sb->buf, sb->len);
178
179         if (!has_signoff)
180                 strbuf_addch(sb, '\n');
181
182         strbuf_addstr(sb, signed_off_by);
183         strbuf_add(sb, signoff, signoff_len);
184         strbuf_addch(sb, '\n');
185 }
186
187 static 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
197 void 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) {
205                 int max_len = start_len + FORMAT_PATCH_NAME_MAX - suffix_len;
206                 struct pretty_print_context ctx = {0};
207                 ctx.date_mode = DATE_NORMAL;
208
209                 format_commit_message(commit, "%f", buf, &ctx);
210                 if (max_len < buf->len)
211                         strbuf_setlen(buf, max_len);
212                 strbuf_addstr(buf, suffix);
213         }
214 }
215
216 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
217                              const char **subject_p,
218                              const char **extra_headers_p,
219                              int *need_8bit_cte_p)
220 {
221         const char *subject = NULL;
222         const char *extra_headers = opt->extra_headers;
223         const char *name = sha1_to_hex(commit->object.sha1);
224
225         *need_8bit_cte_p = 0; /* unknown */
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);
245         graph_show_oneline(opt->graph);
246         if (opt->message_id) {
247                 printf("Message-Id: <%s>\n", opt->message_id);
248                 graph_show_oneline(opt->graph);
249         }
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);
257                 graph_show_oneline(opt->graph);
258         }
259         if (opt->mime_boundary) {
260                 static char subject_buffer[1024];
261                 static char buffer[1024];
262                 struct strbuf filename =  STRBUF_INIT;
263                 *need_8bit_cte_p = -1; /* NEVER */
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
281                 get_patch_filename(opt->numbered_files ? NULL : commit, opt->nr,
282                                     opt->patch_suffix, &filename);
283                 snprintf(buffer, sizeof(buffer) - 1,
284                          "\n--%s%s\n"
285                          "Content-Type: text/x-patch;"
286                          " name=\"%s\"\n"
287                          "Content-Transfer-Encoding: 8bit\n"
288                          "Content-Disposition: %s;"
289                          " filename=\"%s\"\n\n",
290                          mime_boundary_leader, opt->mime_boundary,
291                          filename.buf,
292                          opt->no_inline ? "attachment" : "inline",
293                          filename.buf);
294                 opt->diffopt.stat_sep = buffer;
295                 strbuf_release(&filename);
296         }
297         *subject_p = subject;
298         *extra_headers_p = extra_headers;
299 }
300
301 void show_log(struct rev_info *opt)
302 {
303         struct strbuf msgbuf = STRBUF_INIT;
304         struct log_info *log = opt->loginfo;
305         struct commit *commit = log->commit, *parent = log->parent;
306         int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
307         const char *extra_headers = opt->extra_headers;
308         struct pretty_print_context ctx = {0};
309
310         opt->loginfo = NULL;
311         ctx.show_notes = opt->show_notes;
312         if (!opt->verbose_header) {
313                 graph_show_commit(opt->graph);
314
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                         }
326                 }
327                 fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
328                 if (opt->print_parents)
329                         show_parents(commit, abbrev_commit);
330                 show_decorations(opt, commit);
331                 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
332                         putchar('\n');
333                         graph_show_remainder(opt->graph);
334                 }
335                 putchar(opt->diffopt.line_termination);
336                 return;
337         }
338
339         /*
340          * If use_terminator is set, we already handled any record termination
341          * at the end of the last record.
342          * Otherwise, add a diffopt.line_termination character before all
343          * entries but the first.  (IOW, as a separator between entries)
344          */
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);
361                 putchar(opt->diffopt.line_termination);
362         }
363         opt->shown_one = 1;
364
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
371         /*
372          * Print header line of header..
373          */
374
375         if (opt->commit_format == CMIT_FMT_EMAIL) {
376                 log_write_email_headers(opt, commit, &ctx.subject, &extra_headers,
377                                         &ctx.need_8bit_cte);
378         } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
379                 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
380                 if (opt->commit_format != CMIT_FMT_ONELINE)
381                         fputs("commit ", stdout);
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                         }
394                 }
395                 fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit),
396                       stdout);
397                 if (opt->print_parents)
398                         show_parents(commit, abbrev_commit);
399                 if (parent)
400                         printf(" (from %s)",
401                                find_unique_abbrev(parent->object.sha1,
402                                                   abbrev_commit));
403                 show_decorations(opt, commit);
404                 printf("%s", diff_get_color_opt(&opt->diffopt, DIFF_RESET));
405                 if (opt->commit_format == CMIT_FMT_ONELINE) {
406                         putchar(' ');
407                 } else {
408                         putchar('\n');
409                         graph_show_oneline(opt->graph);
410                 }
411                 if (opt->reflog_info) {
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                          */
418                         show_reflog_message(opt->reflog_info,
419                                     opt->commit_format == CMIT_FMT_ONELINE,
420                                     opt->date_mode_explicit ?
421                                         opt->date_mode :
422                                         DATE_NORMAL);
423                         if (opt->commit_format == CMIT_FMT_ONELINE)
424                                 return;
425                 }
426         }
427
428         if (!commit->buffer)
429                 return;
430
431         /*
432          * And then the pretty-printed message itself
433          */
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;
439         ctx.reflog_info = opt->reflog_info;
440         pretty_print_commit(opt->commit_format, commit, &msgbuf, &ctx);
441
442         if (opt->add_signoff)
443                 append_signoff(&msgbuf, opt->add_signoff);
444         if (opt->show_log_size) {
445                 printf("log size %i\n", (int)msgbuf.len);
446                 graph_show_oneline(opt->graph);
447         }
448
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
461                 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
462         if (opt->use_terminator) {
463                 if (!opt->missing_newline)
464                         graph_show_padding(opt->graph);
465                 putchar('\n');
466         }
467
468         strbuf_release(&msgbuf);
469 }
470
471 int log_tree_diff_flush(struct rev_info *opt)
472 {
473         diffcore_std(&opt->diffopt);
474
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         }
482
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                  */
489                 show_log(opt);
490                 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
491                     opt->verbose_header &&
492                     opt->commit_format != CMIT_FMT_ONELINE) {
493                         int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
494                         if ((pch & opt->diffopt.output_format) == pch)
495                                 printf("---");
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                         }
502                         putchar('\n');
503                 }
504         }
505         diff_flush(&opt->diffopt);
506         return 1;
507 }
508
509 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
510 {
511         unsigned const char *sha1 = commit->object.sha1;
512
513         diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
514         return !opt->loginfo;
515 }
516
517 /*
518  * Show the diff of a commit.
519  *
520  * Return true if we printed any log info messages
521  */
522 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
523 {
524         int showed_log;
525         struct commit_list *parents;
526         unsigned const char *sha1 = commit->object.sha1;
527
528         if (!opt->diff && !DIFF_OPT_TST(&opt->diffopt, EXIT_WITH_STATUS))
529                 return 0;
530
531         /* Root commit? */
532         parents = commit->parents;
533         if (!parents) {
534                 if (opt->show_root_diff) {
535                         diff_root_tree_sha1(sha1, "", &opt->diffopt);
536                         log_tree_diff_flush(opt);
537                 }
538                 return !opt->loginfo;
539         }
540
541         /* More than one parent? */
542         if (parents && parents->next) {
543                 if (opt->ignore_merges)
544                         return 0;
545                 else if (opt->combine_merges)
546                         return do_diff_combined(opt, commit);
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                 }
557
558                 /* If we show individual diffs, show the parent info */
559                 log->parent = parents->item;
560         }
561
562         showed_log = 0;
563         for (;;) {
564                 struct commit *parent = parents->item;
565
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
581 int log_tree_commit(struct rev_info *opt, struct commit *commit)
582 {
583         struct log_info log;
584         int shown;
585
586         log.commit = commit;
587         log.parent = NULL;
588         opt->loginfo = &log;
589
590         shown = log_tree_diff(opt, commit, &log);
591         if (!shown && opt->loginfo && opt->always_show_header) {
592                 log.parent = NULL;
593                 show_log(opt);
594                 shown = 1;
595         }
596         opt->loginfo = NULL;
597         maybe_flush_or_die(stdout, "stdout");
598         return shown;
599 }