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