Remove dead code: show_log() sep argument and diff_options.msg_sep
[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 log_write_email_headers(struct rev_info *opt, const char *name,
141                              const char **subject_p,
142                              const char **extra_headers_p,
143                              int *need_8bit_cte_p)
144 {
145         const char *subject = NULL;
146         const char *extra_headers = opt->extra_headers;
147
148         *need_8bit_cte_p = 0; /* unknown */
149         if (opt->total > 0) {
150                 static char buffer[64];
151                 snprintf(buffer, sizeof(buffer),
152                          "Subject: [%s %0*d/%d] ",
153                          opt->subject_prefix,
154                          digits_in_number(opt->total),
155                          opt->nr, opt->total);
156                 subject = buffer;
157         } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
158                 static char buffer[256];
159                 snprintf(buffer, sizeof(buffer),
160                          "Subject: [%s] ",
161                          opt->subject_prefix);
162                 subject = buffer;
163         } else {
164                 subject = "Subject: ";
165         }
166
167         printf("From %s Mon Sep 17 00:00:00 2001\n", name);
168         if (opt->message_id)
169                 printf("Message-Id: <%s>\n", opt->message_id);
170         if (opt->ref_message_id)
171                 printf("In-Reply-To: <%s>\nReferences: <%s>\n",
172                        opt->ref_message_id, opt->ref_message_id);
173         if (opt->mime_boundary) {
174                 static char subject_buffer[1024];
175                 static char buffer[1024];
176                 *need_8bit_cte_p = -1; /* NEVER */
177                 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
178                          "%s"
179                          "MIME-Version: 1.0\n"
180                          "Content-Type: multipart/mixed;"
181                          " boundary=\"%s%s\"\n"
182                          "\n"
183                          "This is a multi-part message in MIME "
184                          "format.\n"
185                          "--%s%s\n"
186                          "Content-Type: text/plain; "
187                          "charset=UTF-8; format=fixed\n"
188                          "Content-Transfer-Encoding: 8bit\n\n",
189                          extra_headers ? extra_headers : "",
190                          mime_boundary_leader, opt->mime_boundary,
191                          mime_boundary_leader, opt->mime_boundary);
192                 extra_headers = subject_buffer;
193
194                 snprintf(buffer, sizeof(buffer) - 1,
195                          "--%s%s\n"
196                          "Content-Type: text/x-patch;"
197                          " name=\"%s.diff\"\n"
198                          "Content-Transfer-Encoding: 8bit\n"
199                          "Content-Disposition: %s;"
200                          " filename=\"%s.diff\"\n\n",
201                          mime_boundary_leader, opt->mime_boundary,
202                          name,
203                          opt->no_inline ? "attachment" : "inline",
204                          name);
205                 opt->diffopt.stat_sep = buffer;
206         }
207         *subject_p = subject;
208         *extra_headers_p = extra_headers;
209 }
210
211 void show_log(struct rev_info *opt)
212 {
213         struct strbuf msgbuf;
214         struct log_info *log = opt->loginfo;
215         struct commit *commit = log->commit, *parent = log->parent;
216         int abbrev = opt->diffopt.abbrev;
217         int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
218         const char *subject = NULL, *extra_headers = opt->extra_headers;
219         int need_8bit_cte = 0;
220
221         opt->loginfo = NULL;
222         if (!opt->verbose_header) {
223                 if (commit->object.flags & BOUNDARY)
224                         putchar('-');
225                 else if (commit->object.flags & UNINTERESTING)
226                         putchar('^');
227                 else if (opt->left_right) {
228                         if (commit->object.flags & SYMMETRIC_LEFT)
229                                 putchar('<');
230                         else
231                                 putchar('>');
232                 }
233                 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
234                 if (opt->parents)
235                         show_parents(commit, abbrev_commit);
236                 show_decorations(commit);
237                 putchar(opt->diffopt.line_termination);
238                 return;
239         }
240
241         /*
242          * If use_terminator is set, add a newline at the end of the entry.
243          * Otherwise, add a diffopt.line_termination character before all
244          * entries but the first.  (IOW, as a separator between entries)
245          */
246         if (opt->shown_one && !opt->use_terminator)
247                 putchar(opt->diffopt.line_termination);
248         opt->shown_one = 1;
249
250         /*
251          * Print header line of header..
252          */
253
254         if (opt->commit_format == CMIT_FMT_EMAIL) {
255                 log_write_email_headers(opt, sha1_to_hex(commit->object.sha1),
256                                         &subject, &extra_headers,
257                                         &need_8bit_cte);
258         } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
259                 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
260                 if (opt->commit_format != CMIT_FMT_ONELINE)
261                         fputs("commit ", stdout);
262                 if (commit->object.flags & BOUNDARY)
263                         putchar('-');
264                 else if (commit->object.flags & UNINTERESTING)
265                         putchar('^');
266                 else if (opt->left_right) {
267                         if (commit->object.flags & SYMMETRIC_LEFT)
268                                 putchar('<');
269                         else
270                                 putchar('>');
271                 }
272                 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit),
273                       stdout);
274                 if (opt->parents)
275                         show_parents(commit, abbrev_commit);
276                 if (parent)
277                         printf(" (from %s)",
278                                diff_unique_abbrev(parent->object.sha1,
279                                                   abbrev_commit));
280                 show_decorations(commit);
281                 printf("%s", diff_get_color_opt(&opt->diffopt, DIFF_RESET));
282                 putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n');
283                 if (opt->reflog_info) {
284                         show_reflog_message(opt->reflog_info,
285                                     opt->commit_format == CMIT_FMT_ONELINE,
286                                     opt->date_mode);
287                         if (opt->commit_format == CMIT_FMT_ONELINE)
288                                 return;
289                 }
290         }
291
292         if (!commit->buffer)
293                 return;
294
295         /*
296          * And then the pretty-printed message itself
297          */
298         strbuf_init(&msgbuf, 0);
299         if (need_8bit_cte >= 0)
300                 need_8bit_cte = has_non_ascii(opt->add_signoff);
301         pretty_print_commit(opt->commit_format, commit, &msgbuf,
302                             abbrev, subject, extra_headers, opt->date_mode,
303                             need_8bit_cte);
304
305         if (opt->add_signoff)
306                 append_signoff(&msgbuf, opt->add_signoff);
307         if (opt->show_log_size)
308                 printf("log size %i\n", (int)msgbuf.len);
309
310         if (msgbuf.len) {
311                 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
312                 if (opt->use_terminator)
313                         putchar('\n');
314         }
315         strbuf_release(&msgbuf);
316 }
317
318 int log_tree_diff_flush(struct rev_info *opt)
319 {
320         diffcore_std(&opt->diffopt);
321
322         if (diff_queue_is_empty()) {
323                 int saved_fmt = opt->diffopt.output_format;
324                 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
325                 diff_flush(&opt->diffopt);
326                 opt->diffopt.output_format = saved_fmt;
327                 return 0;
328         }
329
330         if (opt->loginfo && !opt->no_commit_id) {
331                 /* When showing a verbose header (i.e. log message),
332                  * and not in --pretty=oneline format, we would want
333                  * an extra newline between the end of log and the
334                  * output for readability.
335                  */
336                 show_log(opt);
337                 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
338                     opt->verbose_header &&
339                     opt->commit_format != CMIT_FMT_ONELINE) {
340                         int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
341                         if ((pch & opt->diffopt.output_format) == pch)
342                                 printf("---");
343                         putchar('\n');
344                 }
345         }
346         diff_flush(&opt->diffopt);
347         return 1;
348 }
349
350 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
351 {
352         unsigned const char *sha1 = commit->object.sha1;
353
354         diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
355         return !opt->loginfo;
356 }
357
358 /*
359  * Show the diff of a commit.
360  *
361  * Return true if we printed any log info messages
362  */
363 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
364 {
365         int showed_log;
366         struct commit_list *parents;
367         unsigned const char *sha1 = commit->object.sha1;
368
369         if (!opt->diff)
370                 return 0;
371
372         /* Root commit? */
373         parents = commit->parents;
374         if (!parents) {
375                 if (opt->show_root_diff) {
376                         diff_root_tree_sha1(sha1, "", &opt->diffopt);
377                         log_tree_diff_flush(opt);
378                 }
379                 return !opt->loginfo;
380         }
381
382         /* More than one parent? */
383         if (parents && parents->next) {
384                 if (opt->ignore_merges)
385                         return 0;
386                 else if (opt->combine_merges)
387                         return do_diff_combined(opt, commit);
388
389                 /* If we show individual diffs, show the parent info */
390                 log->parent = parents->item;
391         }
392
393         showed_log = 0;
394         for (;;) {
395                 struct commit *parent = parents->item;
396
397                 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
398                 log_tree_diff_flush(opt);
399
400                 showed_log |= !opt->loginfo;
401
402                 /* Set up the log info for the next parent, if any.. */
403                 parents = parents->next;
404                 if (!parents)
405                         break;
406                 log->parent = parents->item;
407                 opt->loginfo = log;
408         }
409         return showed_log;
410 }
411
412 int log_tree_commit(struct rev_info *opt, struct commit *commit)
413 {
414         struct log_info log;
415         int shown;
416
417         log.commit = commit;
418         log.parent = NULL;
419         opt->loginfo = &log;
420
421         shown = log_tree_diff(opt, commit, &log);
422         if (!shown && opt->loginfo && opt->always_show_header) {
423                 log.parent = NULL;
424                 show_log(opt);
425                 shown = 1;
426         }
427         opt->loginfo = NULL;
428         maybe_flush_or_die(stdout, "stdout");
429         return shown;
430 }