Merge branch 'maint'
[git/git.git] / log-tree.c
CommitLineData
5f1c3f07
JH
1#include "cache.h"
2#include "diff.h"
3#include "commit.h"
4#include "log-tree.h"
8860fd42 5#include "reflog-walk.h"
5f1c3f07 6
ca135e7a
LT
7struct decoration name_decoration = { "object names" };
8
c8c893c6
LT
9static 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
50e62a8e 18void show_decorations(struct commit *commit)
ca135e7a
LT
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
fc1c75ec
FBH
35/*
36 * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
37 * Signed-off-by: and Acked-by: lines.
38 */
39static 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
674d1727 82static void append_signoff(struct strbuf *sb, const char *signoff)
cf2251b6 83{
cf2251b6 84 static const char signed_off_by[] = "Signed-off-by: ";
80583c0e 85 size_t signoff_len = strlen(signoff);
fc1c75ec 86 int has_signoff = 0;
80583c0e 87 char *cp;
cf2251b6 88
674d1727 89 cp = sb->buf;
cf2251b6
JH
90
91 /* First see if we already have the sign-off by the signer */
fc1c75ec
FBH
92 while ((cp = strstr(cp, signed_off_by))) {
93
94 has_signoff = 1;
95
cf2251b6 96 cp += strlen(signed_off_by);
674d1727 97 if (cp + signoff_len >= sb->buf + sb->len)
fc1c75ec
FBH
98 break;
99 if (strncmp(cp, signoff, signoff_len))
100 continue;
101 if (!isspace(cp[signoff_len]))
102 continue;
103 /* we already have him */
674d1727 104 return;
cf2251b6
JH
105 }
106
fc1c75ec 107 if (!has_signoff)
674d1727 108 has_signoff = detect_any_signoff(sb->buf, sb->len);
fc1c75ec
FBH
109
110 if (!has_signoff)
674d1727 111 strbuf_addch(sb, '\n');
c35f4c37 112
674d1727
PH
113 strbuf_addstr(sb, signed_off_by);
114 strbuf_add(sb, signoff, signoff_len);
115 strbuf_addch(sb, '\n');
cf2251b6
JH
116}
117
e00de24b
JS
118static 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
39bc9a6c 128void show_log(struct rev_info *opt, const char *sep)
91539833 129{
674d1727 130 struct strbuf msgbuf;
39bc9a6c 131 struct log_info *log = opt->loginfo;
91539833
LT
132 struct commit *commit = log->commit, *parent = log->parent;
133 int abbrev = opt->diffopt.abbrev;
134 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
a4d34e2d 135 const char *extra;
20ff0680 136 const char *subject = NULL, *extra_headers = opt->extra_headers;
91539833
LT
137
138 opt->loginfo = NULL;
139 if (!opt->verbose_header) {
74bd9029
JH
140 if (opt->left_right) {
141 if (commit->object.flags & BOUNDARY)
142 putchar('-');
143 else if (commit->object.flags & SYMMETRIC_LEFT)
144 putchar('<');
145 else
146 putchar('>');
147 }
c8c893c6
LT
148 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
149 if (opt->parents)
150 show_parents(commit, abbrev_commit);
ca135e7a 151 show_decorations(commit);
1dcb6922 152 putchar(opt->diffopt.line_termination);
91539833
LT
153 return;
154 }
155
156 /*
a4d34e2d
LT
157 * The "oneline" format has several special cases:
158 * - The pretty-printed commit lacks a newline at the end
159 * of the buffer, but we do want to make sure that we
160 * have a newline there. If the separator isn't already
161 * a newline, add an extra one.
162 * - unlike other log messages, the one-line format does
163 * not have an empty line between entries.
91539833 164 */
a4d34e2d
LT
165 extra = "";
166 if (*sep != '\n' && opt->commit_format == CMIT_FMT_ONELINE)
167 extra = "\n";
91539833 168 if (opt->shown_one && opt->commit_format != CMIT_FMT_ONELINE)
abd4e222 169 putchar(opt->diffopt.line_termination);
91539833
LT
170 opt->shown_one = 1;
171
172 /*
173 * Print header line of header..
174 */
3eefc189 175
596524b3 176 if (opt->commit_format == CMIT_FMT_EMAIL) {
698ce6f8 177 char *sha1 = sha1_to_hex(commit->object.sha1);
596524b3
JS
178 if (opt->total > 0) {
179 static char buffer[64];
180 snprintf(buffer, sizeof(buffer),
2d9e4a47
RJ
181 "Subject: [%s %0*d/%d] ",
182 opt->subject_prefix,
e00de24b 183 digits_in_number(opt->total),
596524b3
JS
184 opt->nr, opt->total);
185 subject = buffer;
88c447e8 186 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
2d9e4a47
RJ
187 static char buffer[256];
188 snprintf(buffer, sizeof(buffer),
189 "Subject: [%s] ",
190 opt->subject_prefix);
191 subject = buffer;
192 } else {
8ac80a57 193 subject = "Subject: ";
2d9e4a47 194 }
8ac80a57 195
698ce6f8 196 printf("From %s Mon Sep 17 00:00:00 2001\n", sha1);
d1566f78
JT
197 if (opt->message_id)
198 printf("Message-Id: <%s>\n", opt->message_id);
199 if (opt->ref_message_id)
200 printf("In-Reply-To: <%s>\nReferences: <%s>\n",
201 opt->ref_message_id, opt->ref_message_id);
698ce6f8
JS
202 if (opt->mime_boundary) {
203 static char subject_buffer[1024];
204 static char buffer[1024];
205 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
20ff0680 206 "%s"
698ce6f8 207 "MIME-Version: 1.0\n"
33ee4cfb 208 "Content-Type: multipart/mixed;"
698ce6f8
JS
209 " boundary=\"%s%s\"\n"
210 "\n"
211 "This is a multi-part message in MIME "
212 "format.\n"
213 "--%s%s\n"
214 "Content-Type: text/plain; "
215 "charset=UTF-8; format=fixed\n"
216 "Content-Transfer-Encoding: 8bit\n\n",
20ff0680 217 extra_headers ? extra_headers : "",
698ce6f8
JS
218 mime_boundary_leader, opt->mime_boundary,
219 mime_boundary_leader, opt->mime_boundary);
20ff0680 220 extra_headers = subject_buffer;
698ce6f8
JS
221
222 snprintf(buffer, sizeof(buffer) - 1,
223 "--%s%s\n"
33ee4cfb 224 "Content-Type: text/x-patch;"
698ce6f8
JS
225 " name=\"%s.diff\"\n"
226 "Content-Transfer-Encoding: 8bit\n"
33ee4cfb 227 "Content-Disposition: %s;"
698ce6f8
JS
228 " filename=\"%s.diff\"\n\n",
229 mime_boundary_leader, opt->mime_boundary,
c112f689
JS
230 sha1,
231 opt->no_inline ? "attachment" : "inline",
232 sha1);
698ce6f8
JS
233 opt->diffopt.stat_sep = buffer;
234 }
e52a5de4 235 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
74bd9029
JH
236 fputs(diff_get_color(opt->diffopt.color_diff, DIFF_COMMIT),
237 stdout);
238 if (opt->commit_format != CMIT_FMT_ONELINE)
239 fputs("commit ", stdout);
e330a406
LT
240 if (commit->object.flags & BOUNDARY)
241 putchar('-');
242 else if (opt->left_right) {
243 if (commit->object.flags & SYMMETRIC_LEFT)
74bd9029
JH
244 putchar('<');
245 else
246 putchar('>');
247 }
248 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit),
249 stdout);
c66b6c06
JH
250 if (opt->parents)
251 show_parents(commit, abbrev_commit);
73f0a157 252 if (parent)
3eefc189
JH
253 printf(" (from %s)",
254 diff_unique_abbrev(parent->object.sha1,
255 abbrev_commit));
ca135e7a 256 show_decorations(commit);
ce436973
JK
257 printf("%s",
258 diff_get_color(opt->diffopt.color_diff, DIFF_RESET));
e557667e 259 putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n');
903b45fe 260 if (opt->reflog_info) {
4d12a471 261 show_reflog_message(opt->reflog_info,
4e244cbc 262 opt->commit_format == CMIT_FMT_ONELINE,
a7b02ccf 263 opt->date_mode);
903b45fe
NP
264 if (opt->commit_format == CMIT_FMT_ONELINE) {
265 printf("%s", sep);
266 return;
267 }
268 }
3eefc189 269 }
91539833
LT
270
271 /*
272 * And then the pretty-printed message itself
273 */
674d1727
PH
274 strbuf_init(&msgbuf, 0);
275 pretty_print_commit(opt->commit_format, commit, &msgbuf,
276 abbrev, subject, extra_headers, opt->date_mode);
cf2251b6
JH
277
278 if (opt->add_signoff)
674d1727 279 append_signoff(&msgbuf, opt->add_signoff);
9fa3465d 280 if (opt->show_log_size)
674d1727 281 printf("log size %i\n", (int)msgbuf.len);
9fa3465d 282
cc61ae82
JH
283 if (msgbuf.len)
284 printf("%s%s%s", msgbuf.buf, extra, sep);
674d1727 285 strbuf_release(&msgbuf);
91539833
LT
286}
287
cd2bdc53 288int log_tree_diff_flush(struct rev_info *opt)
5f1c3f07
JH
289{
290 diffcore_std(&opt->diffopt);
91539833 291
5f1c3f07
JH
292 if (diff_queue_is_empty()) {
293 int saved_fmt = opt->diffopt.output_format;
294 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
295 diff_flush(&opt->diffopt);
296 opt->diffopt.output_format = saved_fmt;
297 return 0;
298 }
91539833 299
3969cf7d
JH
300 if (opt->loginfo && !opt->no_commit_id) {
301 /* When showing a verbose header (i.e. log message),
302 * and not in --pretty=oneline format, we would want
303 * an extra newline between the end of log and the
304 * output for readability.
305 */
39bc9a6c 306 show_log(opt, opt->diffopt.msg_sep);
304b5af6
LT
307 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
308 opt->verbose_header &&
3969cf7d
JH
309 opt->commit_format != CMIT_FMT_ONELINE) {
310 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
311 if ((pch & opt->diffopt.output_format) == pch)
abd4e222
LT
312 printf("---");
313 putchar('\n');
3969cf7d
JH
314 }
315 }
5f1c3f07
JH
316 diff_flush(&opt->diffopt);
317 return 1;
318}
319
cd2bdc53 320static int do_diff_combined(struct rev_info *opt, struct commit *commit)
5f1c3f07
JH
321{
322 unsigned const char *sha1 = commit->object.sha1;
323
91539833
LT
324 diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
325 return !opt->loginfo;
5f1c3f07
JH
326}
327
91539833
LT
328/*
329 * Show the diff of a commit.
330 *
331 * Return true if we printed any log info messages
332 */
333static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
5f1c3f07 334{
91539833 335 int showed_log;
5f1c3f07
JH
336 struct commit_list *parents;
337 unsigned const char *sha1 = commit->object.sha1;
338
91539833
LT
339 if (!opt->diff)
340 return 0;
341
5f1c3f07 342 /* Root commit? */
91539833
LT
343 parents = commit->parents;
344 if (!parents) {
2b60356d
RS
345 if (opt->show_root_diff) {
346 diff_root_tree_sha1(sha1, "", &opt->diffopt);
347 log_tree_diff_flush(opt);
348 }
91539833 349 return !opt->loginfo;
5f1c3f07
JH
350 }
351
352 /* More than one parent? */
91539833 353 if (parents && parents->next) {
5f1c3f07
JH
354 if (opt->ignore_merges)
355 return 0;
356 else if (opt->combine_merges)
357 return do_diff_combined(opt, commit);
91539833
LT
358
359 /* If we show individual diffs, show the parent info */
360 log->parent = parents->item;
5f1c3f07
JH
361 }
362
91539833
LT
363 showed_log = 0;
364 for (;;) {
5f1c3f07 365 struct commit *parent = parents->item;
5f1c3f07 366
91539833
LT
367 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
368 log_tree_diff_flush(opt);
369
370 showed_log |= !opt->loginfo;
371
372 /* Set up the log info for the next parent, if any.. */
373 parents = parents->next;
374 if (!parents)
375 break;
376 log->parent = parents->item;
377 opt->loginfo = log;
378 }
379 return showed_log;
380}
381
382int log_tree_commit(struct rev_info *opt, struct commit *commit)
383{
384 struct log_info log;
3eefc189 385 int shown;
91539833
LT
386
387 log.commit = commit;
388 log.parent = NULL;
389 opt->loginfo = &log;
390
3eefc189
JH
391 shown = log_tree_diff(opt, commit, &log);
392 if (!shown && opt->loginfo && opt->always_show_header) {
91539833 393 log.parent = NULL;
39bc9a6c 394 show_log(opt, "");
3eefc189 395 shown = 1;
5f1c3f07 396 }
91539833 397 opt->loginfo = NULL;
06f59e9f 398 maybe_flush_or_die(stdout, "stdout");
3eefc189 399 return shown;
5f1c3f07 400}