Merge branch 'maint' of git://repo.or.cz/git-gui into maint
[git] / log-tree.c
1 #include "cache.h"
2 #include "diff.h"
3 #include "commit.h"
4 #include "log-tree.h"
5 #include "reflog-walk.h"
6
7 struct decoration name_decoration = { "object names" };
8
9 static void show_parents(struct commit *commit, int abbrev)
10 {
11         struct commit_list *p;
12         for (p = commit->parents; p ; p = p->next) {
13                 struct commit *parent = p->item;
14                 printf(" %s", diff_unique_abbrev(parent->object.sha1, abbrev));
15         }
16 }
17
18 void show_decorations(struct commit *commit)
19 {
20         const char *prefix;
21         struct name_decoration *decoration;
22
23         decoration = lookup_decoration(&name_decoration, &commit->object);
24         if (!decoration)
25                 return;
26         prefix = " (";
27         while (decoration) {
28                 printf("%s%s", prefix, decoration->name);
29                 prefix = ", ";
30                 decoration = decoration->next;
31         }
32         putchar(')');
33 }
34
35 /*
36  * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
37  * Signed-off-by: and Acked-by: lines.
38  */
39 static int detect_any_signoff(char *letter, int size)
40 {
41         char ch, *cp;
42         int seen_colon = 0;
43         int seen_at = 0;
44         int seen_name = 0;
45         int seen_head = 0;
46
47         cp = letter + size;
48         while (letter <= --cp && (ch = *cp) == '\n')
49                 continue;
50
51         while (letter <= cp) {
52                 ch = *cp--;
53                 if (ch == '\n')
54                         break;
55
56                 if (!seen_at) {
57                         if (ch == '@')
58                                 seen_at = 1;
59                         continue;
60                 }
61                 if (!seen_colon) {
62                         if (ch == '@')
63                                 return 0;
64                         else if (ch == ':')
65                                 seen_colon = 1;
66                         else
67                                 seen_name = 1;
68                         continue;
69                 }
70                 if (('A' <= ch && ch <= 'Z') ||
71                     ('a' <= ch && ch <= 'z') ||
72                     ch == '-') {
73                         seen_head = 1;
74                         continue;
75                 }
76                 /* no empty last line doesn't match */
77                 return 0;
78         }
79         return seen_head && seen_name;
80 }
81
82 static void append_signoff(struct strbuf *sb, const char *signoff)
83 {
84         static const char signed_off_by[] = "Signed-off-by: ";
85         size_t signoff_len = strlen(signoff);
86         int has_signoff = 0;
87         char *cp;
88
89         cp = sb->buf;
90
91         /* First see if we already have the sign-off by the signer */
92         while ((cp = strstr(cp, signed_off_by))) {
93
94                 has_signoff = 1;
95
96                 cp += strlen(signed_off_by);
97                 if (cp + signoff_len >= sb->buf + sb->len)
98                         break;
99                 if (strncmp(cp, signoff, signoff_len))
100                         continue;
101                 if (!isspace(cp[signoff_len]))
102                         continue;
103                 /* we already have him */
104                 return;
105         }
106
107         if (!has_signoff)
108                 has_signoff = detect_any_signoff(sb->buf, sb->len);
109
110         if (!has_signoff)
111                 strbuf_addch(sb, '\n');
112
113         strbuf_addstr(sb, signed_off_by);
114         strbuf_add(sb, signoff, signoff_len);
115         strbuf_addch(sb, '\n');
116 }
117
118 static unsigned int digits_in_number(unsigned int number)
119 {
120         unsigned int i = 10, result = 1;
121         while (i <= number) {
122                 i *= 10;
123                 result++;
124         }
125         return result;
126 }
127
128 static int has_non_ascii(const char *s)
129 {
130         int ch;
131         if (!s)
132                 return 0;
133         while ((ch = *s++) != '\0') {
134                 if (non_ascii(ch))
135                         return 1;
136         }
137         return 0;
138 }
139
140 void show_log(struct rev_info *opt, const char *sep)
141 {
142         struct strbuf msgbuf;
143         struct log_info *log = opt->loginfo;
144         struct commit *commit = log->commit, *parent = log->parent;
145         int abbrev = opt->diffopt.abbrev;
146         int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
147         const char *extra;
148         const char *subject = NULL, *extra_headers = opt->extra_headers;
149
150         opt->loginfo = NULL;
151         if (!opt->verbose_header) {
152                 if (opt->left_right) {
153                         if (commit->object.flags & BOUNDARY)
154                                 putchar('-');
155                         else if (commit->object.flags & SYMMETRIC_LEFT)
156                                 putchar('<');
157                         else
158                                 putchar('>');
159                 }
160                 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
161                 if (opt->parents)
162                         show_parents(commit, abbrev_commit);
163                 show_decorations(commit);
164                 putchar(opt->diffopt.line_termination);
165                 return;
166         }
167
168         /*
169          * The "oneline" format has several special cases:
170          *  - The pretty-printed commit lacks a newline at the end
171          *    of the buffer, but we do want to make sure that we
172          *    have a newline there. If the separator isn't already
173          *    a newline, add an extra one.
174          *  - unlike other log messages, the one-line format does
175          *    not have an empty line between entries.
176          */
177         extra = "";
178         if (*sep != '\n' && opt->commit_format == CMIT_FMT_ONELINE)
179                 extra = "\n";
180         if (opt->shown_one && opt->commit_format != CMIT_FMT_ONELINE)
181                 putchar(opt->diffopt.line_termination);
182         opt->shown_one = 1;
183
184         /*
185          * Print header line of header..
186          */
187
188         if (opt->commit_format == CMIT_FMT_EMAIL) {
189                 char *sha1 = sha1_to_hex(commit->object.sha1);
190                 if (opt->total > 0) {
191                         static char buffer[64];
192                         snprintf(buffer, sizeof(buffer),
193                                         "Subject: [%s %0*d/%d] ",
194                                         opt->subject_prefix,
195                                         digits_in_number(opt->total),
196                                         opt->nr, opt->total);
197                         subject = buffer;
198                 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
199                         static char buffer[256];
200                         snprintf(buffer, sizeof(buffer),
201                                         "Subject: [%s] ",
202                                         opt->subject_prefix);
203                         subject = buffer;
204                 } else {
205                         subject = "Subject: ";
206                 }
207
208                 printf("From %s Mon Sep 17 00:00:00 2001\n", sha1);
209                 if (opt->message_id)
210                         printf("Message-Id: <%s>\n", opt->message_id);
211                 if (opt->ref_message_id)
212                         printf("In-Reply-To: <%s>\nReferences: <%s>\n",
213                                opt->ref_message_id, opt->ref_message_id);
214                 if (opt->mime_boundary) {
215                         static char subject_buffer[1024];
216                         static char buffer[1024];
217                         snprintf(subject_buffer, sizeof(subject_buffer) - 1,
218                                  "%s"
219                                  "MIME-Version: 1.0\n"
220                                  "Content-Type: multipart/mixed;"
221                                  " boundary=\"%s%s\"\n"
222                                  "\n"
223                                  "This is a multi-part message in MIME "
224                                  "format.\n"
225                                  "--%s%s\n"
226                                  "Content-Type: text/plain; "
227                                  "charset=UTF-8; format=fixed\n"
228                                  "Content-Transfer-Encoding: 8bit\n\n",
229                                  extra_headers ? extra_headers : "",
230                                  mime_boundary_leader, opt->mime_boundary,
231                                  mime_boundary_leader, opt->mime_boundary);
232                         extra_headers = subject_buffer;
233
234                         snprintf(buffer, sizeof(buffer) - 1,
235                                  "--%s%s\n"
236                                  "Content-Type: text/x-patch;"
237                                  " name=\"%s.diff\"\n"
238                                  "Content-Transfer-Encoding: 8bit\n"
239                                  "Content-Disposition: %s;"
240                                  " filename=\"%s.diff\"\n\n",
241                                  mime_boundary_leader, opt->mime_boundary,
242                                  sha1,
243                                  opt->no_inline ? "attachment" : "inline",
244                                  sha1);
245                         opt->diffopt.stat_sep = buffer;
246                 }
247         } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
248                 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
249                 if (opt->commit_format != CMIT_FMT_ONELINE)
250                         fputs("commit ", stdout);
251                 if (commit->object.flags & BOUNDARY)
252                         putchar('-');
253                 else if (opt->left_right) {
254                         if (commit->object.flags & SYMMETRIC_LEFT)
255                                 putchar('<');
256                         else
257                                 putchar('>');
258                 }
259                 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit),
260                       stdout);
261                 if (opt->parents)
262                         show_parents(commit, abbrev_commit);
263                 if (parent)
264                         printf(" (from %s)",
265                                diff_unique_abbrev(parent->object.sha1,
266                                                   abbrev_commit));
267                 show_decorations(commit);
268                 printf("%s", diff_get_color_opt(&opt->diffopt, DIFF_RESET));
269                 putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n');
270                 if (opt->reflog_info) {
271                         show_reflog_message(opt->reflog_info,
272                                     opt->commit_format == CMIT_FMT_ONELINE,
273                                     opt->date_mode);
274                         if (opt->commit_format == CMIT_FMT_ONELINE) {
275                                 printf("%s", sep);
276                                 return;
277                         }
278                 }
279         }
280
281         /*
282          * And then the pretty-printed message itself
283          */
284         strbuf_init(&msgbuf, 0);
285         pretty_print_commit(opt->commit_format, commit, &msgbuf,
286                             abbrev, subject, extra_headers, opt->date_mode,
287                             has_non_ascii(opt->add_signoff));
288
289         if (opt->add_signoff)
290                 append_signoff(&msgbuf, opt->add_signoff);
291         if (opt->show_log_size)
292                 printf("log size %i\n", (int)msgbuf.len);
293
294         if (msgbuf.len)
295                 printf("%s%s%s", msgbuf.buf, extra, sep);
296         strbuf_release(&msgbuf);
297 }
298
299 int log_tree_diff_flush(struct rev_info *opt)
300 {
301         diffcore_std(&opt->diffopt);
302
303         if (diff_queue_is_empty()) {
304                 int saved_fmt = opt->diffopt.output_format;
305                 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
306                 diff_flush(&opt->diffopt);
307                 opt->diffopt.output_format = saved_fmt;
308                 return 0;
309         }
310
311         if (opt->loginfo && !opt->no_commit_id) {
312                 /* When showing a verbose header (i.e. log message),
313                  * and not in --pretty=oneline format, we would want
314                  * an extra newline between the end of log and the
315                  * output for readability.
316                  */
317                 show_log(opt, opt->diffopt.msg_sep);
318                 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
319                     opt->verbose_header &&
320                     opt->commit_format != CMIT_FMT_ONELINE) {
321                         int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
322                         if ((pch & opt->diffopt.output_format) == pch)
323                                 printf("---");
324                         putchar('\n');
325                 }
326         }
327         diff_flush(&opt->diffopt);
328         return 1;
329 }
330
331 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
332 {
333         unsigned const char *sha1 = commit->object.sha1;
334
335         diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
336         return !opt->loginfo;
337 }
338
339 /*
340  * Show the diff of a commit.
341  *
342  * Return true if we printed any log info messages
343  */
344 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
345 {
346         int showed_log;
347         struct commit_list *parents;
348         unsigned const char *sha1 = commit->object.sha1;
349
350         if (!opt->diff)
351                 return 0;
352
353         /* Root commit? */
354         parents = commit->parents;
355         if (!parents) {
356                 if (opt->show_root_diff) {
357                         diff_root_tree_sha1(sha1, "", &opt->diffopt);
358                         log_tree_diff_flush(opt);
359                 }
360                 return !opt->loginfo;
361         }
362
363         /* More than one parent? */
364         if (parents && parents->next) {
365                 if (opt->ignore_merges)
366                         return 0;
367                 else if (opt->combine_merges)
368                         return do_diff_combined(opt, commit);
369
370                 /* If we show individual diffs, show the parent info */
371                 log->parent = parents->item;
372         }
373
374         showed_log = 0;
375         for (;;) {
376                 struct commit *parent = parents->item;
377
378                 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
379                 log_tree_diff_flush(opt);
380
381                 showed_log |= !opt->loginfo;
382
383                 /* Set up the log info for the next parent, if any.. */
384                 parents = parents->next;
385                 if (!parents)
386                         break;
387                 log->parent = parents->item;
388                 opt->loginfo = log;
389         }
390         return showed_log;
391 }
392
393 int log_tree_commit(struct rev_info *opt, struct commit *commit)
394 {
395         struct log_info log;
396         int shown;
397
398         log.commit = commit;
399         log.parent = NULL;
400         opt->loginfo = &log;
401
402         shown = log_tree_diff(opt, commit, &log);
403         if (!shown && opt->loginfo && opt->always_show_header) {
404                 log.parent = NULL;
405                 show_log(opt, "");
406                 shown = 1;
407         }
408         opt->loginfo = NULL;
409         maybe_flush_or_die(stdout, "stdout");
410         return shown;
411 }