blob: 8797aa14c43e693c4cb64bc93dac4fe78716558d [file] [log] [blame]
Junio C Hamano5f1c3f02006-04-09 01:11:11 -07001#include "cache.h"
2#include "diff.h"
3#include "commit.h"
4#include "log-tree.h"
Johannes Schindelin8860fd42007-01-11 11:47:48 +01005#include "reflog-walk.h"
Junio C Hamano5f1c3f02006-04-09 01:11:11 -07006
Linus Torvaldsc8c893c2006-05-03 07:59:00 -07007static 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
Franck Bui-Huufc1c75e2006-08-29 13:37:06 +020016/*
17 * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
18 * Signed-off-by: and Acked-by: lines.
19 */
20static 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
Junio C Hamanocf2251b2006-05-31 15:11:49 -070063static int append_signoff(char *buf, int buf_sz, int at, const char *signoff)
64{
Junio C Hamanocf2251b2006-05-31 15:11:49 -070065 static const char signed_off_by[] = "Signed-off-by: ";
Franck Bui-Huufc1c75e2006-08-29 13:37:06 +020066 int signoff_len = strlen(signoff);
67 int has_signoff = 0;
Junio C Hamanocf2251b2006-05-31 15:11:49 -070068 char *cp = buf;
69
70 /* Do we have enough space to add it? */
Franck Bui-Huuc35f4c32006-08-13 11:30:27 -070071 if (buf_sz - at <= strlen(signed_off_by) + signoff_len + 3)
Junio C Hamanocf2251b2006-05-31 15:11:49 -070072 return at;
73
74 /* First see if we already have the sign-off by the signer */
Franck Bui-Huufc1c75e2006-08-29 13:37:06 +020075 while ((cp = strstr(cp, signed_off_by))) {
76
77 has_signoff = 1;
78
Junio C Hamanocf2251b2006-05-31 15:11:49 -070079 cp += strlen(signed_off_by);
Franck Bui-Huufc1c75e2006-08-29 13:37:06 +020080 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;
Junio C Hamanocf2251b2006-05-31 15:11:49 -070088 }
89
Franck Bui-Huufc1c75e2006-08-29 13:37:06 +020090 if (!has_signoff)
91 has_signoff = detect_any_signoff(buf, at);
92
93 if (!has_signoff)
94 buf[at++] = '\n';
Franck Bui-Huuc35f4c32006-08-13 11:30:27 -070095
Junio C Hamanocf2251b2006-05-31 15:11:49 -070096 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
Johannes Schindeline00de242007-02-09 01:43:54 +0100105static 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
Timo Hirvonen39bc9a62006-06-25 13:54:14 +0300115void show_log(struct rev_info *opt, const char *sep)
Linus Torvalds91539832006-04-17 11:59:32 -0700116{
117 static char this_header[16384];
Timo Hirvonen39bc9a62006-06-25 13:54:14 +0300118 struct log_info *log = opt->loginfo;
Linus Torvalds91539832006-04-17 11:59:32 -0700119 struct commit *commit = log->commit, *parent = log->parent;
120 int abbrev = opt->diffopt.abbrev;
121 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
Linus Torvaldsa4d34e22006-04-17 17:43:40 -0700122 const char *extra;
Linus Torvalds91539832006-04-17 11:59:32 -0700123 int len;
Johannes Schindelin20ff0682006-06-02 15:21:17 +0200124 const char *subject = NULL, *extra_headers = opt->extra_headers;
Linus Torvalds91539832006-04-17 11:59:32 -0700125
126 opt->loginfo = NULL;
127 if (!opt->verbose_header) {
Junio C Hamano74bd9022006-12-16 15:31:25 -0800128 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 }
Linus Torvaldsc8c893c2006-05-03 07:59:00 -0700136 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
137 if (opt->parents)
138 show_parents(commit, abbrev_commit);
Ryan Anderson1dcb6922006-08-07 05:11:23 -0700139 putchar(opt->diffopt.line_termination);
Linus Torvalds91539832006-04-17 11:59:32 -0700140 return;
141 }
142
143 /*
Linus Torvaldsa4d34e22006-04-17 17:43:40 -0700144 * 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.
Linus Torvalds91539832006-04-17 11:59:32 -0700151 */
Linus Torvaldsa4d34e22006-04-17 17:43:40 -0700152 extra = "";
153 if (*sep != '\n' && opt->commit_format == CMIT_FMT_ONELINE)
154 extra = "\n";
Linus Torvalds91539832006-04-17 11:59:32 -0700155 if (opt->shown_one && opt->commit_format != CMIT_FMT_ONELINE)
Linus Torvaldsabd4e222007-02-07 11:49:56 -0800156 putchar(opt->diffopt.line_termination);
Linus Torvalds91539832006-04-17 11:59:32 -0700157 opt->shown_one = 1;
158
159 /*
160 * Print header line of header..
161 */
Junio C Hamano3eefc182006-04-18 16:45:27 -0700162
Johannes Schindelin596524b2006-05-05 04:30:52 +0200163 if (opt->commit_format == CMIT_FMT_EMAIL) {
Johannes Schindelin698ce6f2006-05-20 15:40:29 +0200164 char *sha1 = sha1_to_hex(commit->object.sha1);
Johannes Schindelin596524b2006-05-05 04:30:52 +0200165 if (opt->total > 0) {
166 static char buffer[64];
167 snprintf(buffer, sizeof(buffer),
Johannes Schindeline00de242007-02-09 01:43:54 +0100168 "Subject: [PATCH %0*d/%d] ",
169 digits_in_number(opt->total),
Johannes Schindelin596524b2006-05-05 04:30:52 +0200170 opt->nr, opt->total);
171 subject = buffer;
Johannes Schindelin8ac80a52006-05-05 04:31:29 +0200172 } else if (opt->total == 0)
Johannes Schindelin596524b2006-05-05 04:30:52 +0200173 subject = "Subject: [PATCH] ";
Johannes Schindelin8ac80a52006-05-05 04:31:29 +0200174 else
175 subject = "Subject: ";
176
Johannes Schindelin698ce6f2006-05-20 15:40:29 +0200177 printf("From %s Mon Sep 17 00:00:00 2001\n", sha1);
Josh Triplettd1566f72006-07-14 17:48:51 -0700178 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);
Johannes Schindelin698ce6f2006-05-20 15:40:29 +0200183 if (opt->mime_boundary) {
184 static char subject_buffer[1024];
185 static char buffer[1024];
186 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
Johannes Schindelin20ff0682006-06-02 15:21:17 +0200187 "%s"
Johannes Schindelin698ce6f2006-05-20 15:40:29 +0200188 "MIME-Version: 1.0\n"
Junio C Hamano33ee4cf2007-03-04 16:08:04 -0800189 "Content-Type: multipart/mixed;"
Johannes Schindelin698ce6f2006-05-20 15:40:29 +0200190 " 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",
Johannes Schindelin20ff0682006-06-02 15:21:17 +0200198 extra_headers ? extra_headers : "",
Johannes Schindelin698ce6f2006-05-20 15:40:29 +0200199 mime_boundary_leader, opt->mime_boundary,
200 mime_boundary_leader, opt->mime_boundary);
Johannes Schindelin20ff0682006-06-02 15:21:17 +0200201 extra_headers = subject_buffer;
Johannes Schindelin698ce6f2006-05-20 15:40:29 +0200202
203 snprintf(buffer, sizeof(buffer) - 1,
204 "--%s%s\n"
Junio C Hamano33ee4cf2007-03-04 16:08:04 -0800205 "Content-Type: text/x-patch;"
Johannes Schindelin698ce6f2006-05-20 15:40:29 +0200206 " name=\"%s.diff\"\n"
207 "Content-Transfer-Encoding: 8bit\n"
Junio C Hamano33ee4cf2007-03-04 16:08:04 -0800208 "Content-Disposition: %s;"
Johannes Schindelin698ce6f2006-05-20 15:40:29 +0200209 " filename=\"%s.diff\"\n\n",
210 mime_boundary_leader, opt->mime_boundary,
Johannes Schindelinc112f682007-03-04 00:12:06 +0100211 sha1,
212 opt->no_inline ? "attachment" : "inline",
213 sha1);
Johannes Schindelin698ce6f2006-05-20 15:40:29 +0200214 opt->diffopt.stat_sep = buffer;
215 }
Johannes Schindeline52a5de2007-02-23 01:35:03 +0100216 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
Junio C Hamano74bd9022006-12-16 15:31:25 -0800217 fputs(diff_get_color(opt->diffopt.color_diff, DIFF_COMMIT),
218 stdout);
219 if (opt->commit_format != CMIT_FMT_ONELINE)
220 fputs("commit ", stdout);
221 if (opt->left_right) {
222 if (commit->object.flags & BOUNDARY)
223 putchar('-');
224 else if (commit->object.flags & SYMMETRIC_LEFT)
225 putchar('<');
226 else
227 putchar('>');
228 }
229 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit),
230 stdout);
Junio C Hamanoc66b6c02006-05-06 14:42:08 -0700231 if (opt->parents)
232 show_parents(commit, abbrev_commit);
Junio C Hamano73f0a152006-05-24 12:19:47 -0700233 if (parent)
Junio C Hamano3eefc182006-04-18 16:45:27 -0700234 printf(" (from %s)",
235 diff_unique_abbrev(parent->object.sha1,
236 abbrev_commit));
Jeff Kingce436972006-07-23 05:24:18 -0400237 printf("%s",
238 diff_get_color(opt->diffopt.color_diff, DIFF_RESET));
Johannes Schindeline5576672006-07-24 14:41:41 +0200239 putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n');
Nicolas Pitre903b45f2007-01-27 22:40:36 -0500240 if (opt->reflog_info) {
Junio C Hamano4d12a472007-01-20 00:51:41 -0800241 show_reflog_message(opt->reflog_info,
Johannes Schindelin4e244cb2007-02-08 21:58:33 +0100242 opt->commit_format == CMIT_FMT_ONELINE,
243 opt->relative_date);
Nicolas Pitre903b45f2007-01-27 22:40:36 -0500244 if (opt->commit_format == CMIT_FMT_ONELINE) {
245 printf("%s", sep);
246 return;
247 }
248 }
Junio C Hamano3eefc182006-04-18 16:45:27 -0700249 }
Linus Torvalds91539832006-04-17 11:59:32 -0700250
251 /*
252 * And then the pretty-printed message itself
253 */
Jonas Fonseca3dfb9272006-08-28 15:52:13 +0200254 len = pretty_print_commit(opt->commit_format, commit, ~0u, this_header,
255 sizeof(this_header), abbrev, subject,
256 extra_headers, opt->relative_date);
Junio C Hamanocf2251b2006-05-31 15:11:49 -0700257
258 if (opt->add_signoff)
259 len = append_signoff(this_header, sizeof(this_header), len,
260 opt->add_signoff);
Linus Torvaldsa4d34e22006-04-17 17:43:40 -0700261 printf("%s%s%s", this_header, extra, sep);
Linus Torvalds91539832006-04-17 11:59:32 -0700262}
263
Linus Torvaldscd2bdc52006-04-14 16:52:13 -0700264int log_tree_diff_flush(struct rev_info *opt)
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700265{
266 diffcore_std(&opt->diffopt);
Linus Torvalds91539832006-04-17 11:59:32 -0700267
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700268 if (diff_queue_is_empty()) {
269 int saved_fmt = opt->diffopt.output_format;
270 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
271 diff_flush(&opt->diffopt);
272 opt->diffopt.output_format = saved_fmt;
273 return 0;
274 }
Linus Torvalds91539832006-04-17 11:59:32 -0700275
Junio C Hamano3969cf72006-06-27 15:08:19 -0700276 if (opt->loginfo && !opt->no_commit_id) {
277 /* When showing a verbose header (i.e. log message),
278 * and not in --pretty=oneline format, we would want
279 * an extra newline between the end of log and the
280 * output for readability.
281 */
Timo Hirvonen39bc9a62006-06-25 13:54:14 +0300282 show_log(opt, opt->diffopt.msg_sep);
Junio C Hamano3969cf72006-06-27 15:08:19 -0700283 if (opt->verbose_header &&
284 opt->commit_format != CMIT_FMT_ONELINE) {
285 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
286 if ((pch & opt->diffopt.output_format) == pch)
Linus Torvaldsabd4e222007-02-07 11:49:56 -0800287 printf("---");
288 putchar('\n');
Junio C Hamano3969cf72006-06-27 15:08:19 -0700289 }
290 }
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700291 diff_flush(&opt->diffopt);
292 return 1;
293}
294
Linus Torvaldscd2bdc52006-04-14 16:52:13 -0700295static int do_diff_combined(struct rev_info *opt, struct commit *commit)
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700296{
297 unsigned const char *sha1 = commit->object.sha1;
298
Linus Torvalds91539832006-04-17 11:59:32 -0700299 diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
300 return !opt->loginfo;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700301}
302
Linus Torvalds91539832006-04-17 11:59:32 -0700303/*
304 * Show the diff of a commit.
305 *
306 * Return true if we printed any log info messages
307 */
308static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700309{
Linus Torvalds91539832006-04-17 11:59:32 -0700310 int showed_log;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700311 struct commit_list *parents;
312 unsigned const char *sha1 = commit->object.sha1;
313
Linus Torvalds91539832006-04-17 11:59:32 -0700314 if (!opt->diff)
315 return 0;
316
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700317 /* Root commit? */
Linus Torvalds91539832006-04-17 11:59:32 -0700318 parents = commit->parents;
319 if (!parents) {
Rene Scharfe2b603562006-10-26 18:52:39 +0200320 if (opt->show_root_diff) {
321 diff_root_tree_sha1(sha1, "", &opt->diffopt);
322 log_tree_diff_flush(opt);
323 }
Linus Torvalds91539832006-04-17 11:59:32 -0700324 return !opt->loginfo;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700325 }
326
327 /* More than one parent? */
Linus Torvalds91539832006-04-17 11:59:32 -0700328 if (parents && parents->next) {
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700329 if (opt->ignore_merges)
330 return 0;
331 else if (opt->combine_merges)
332 return do_diff_combined(opt, commit);
Linus Torvalds91539832006-04-17 11:59:32 -0700333
334 /* If we show individual diffs, show the parent info */
335 log->parent = parents->item;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700336 }
337
Linus Torvalds91539832006-04-17 11:59:32 -0700338 showed_log = 0;
339 for (;;) {
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700340 struct commit *parent = parents->item;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700341
Linus Torvalds91539832006-04-17 11:59:32 -0700342 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
343 log_tree_diff_flush(opt);
344
345 showed_log |= !opt->loginfo;
346
347 /* Set up the log info for the next parent, if any.. */
348 parents = parents->next;
349 if (!parents)
350 break;
351 log->parent = parents->item;
352 opt->loginfo = log;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700353 }
Linus Torvalds91539832006-04-17 11:59:32 -0700354 return showed_log;
355}
356
357int log_tree_commit(struct rev_info *opt, struct commit *commit)
358{
359 struct log_info log;
Junio C Hamano3eefc182006-04-18 16:45:27 -0700360 int shown;
Linus Torvalds91539832006-04-17 11:59:32 -0700361
362 log.commit = commit;
363 log.parent = NULL;
364 opt->loginfo = &log;
365
Junio C Hamano3eefc182006-04-18 16:45:27 -0700366 shown = log_tree_diff(opt, commit, &log);
367 if (!shown && opt->loginfo && opt->always_show_header) {
Linus Torvalds91539832006-04-17 11:59:32 -0700368 log.parent = NULL;
Timo Hirvonen39bc9a62006-06-25 13:54:14 +0300369 show_log(opt, "");
Junio C Hamano3eefc182006-04-18 16:45:27 -0700370 shown = 1;
Linus Torvalds91539832006-04-17 11:59:32 -0700371 }
372 opt->loginfo = NULL;
Junio C Hamano3eefc182006-04-18 16:45:27 -0700373 return shown;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700374}