format-patch: --numbered-files and --stdout aren't mutually exclusive
[git/git.git] / log-tree.c
CommitLineData
5f1c3f07
JH
1#include "cache.h"
2#include "diff.h"
3#include "commit.h"
cab4feb6 4#include "tag.h"
7fefda5c 5#include "graph.h"
5f1c3f07 6#include "log-tree.h"
8860fd42 7#include "reflog-walk.h"
cab4feb6 8#include "refs.h"
b079c50e 9#include "string-list.h"
5f1c3f07 10
ca135e7a
LT
11struct decoration name_decoration = { "object names" };
12
cab4feb6
RS
13static void add_name_decoration(const char *prefix, const char *name, struct object *obj)
14{
15 int plen = strlen(prefix);
16 int nlen = strlen(name);
17 struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + plen + nlen);
18 memcpy(res->name, prefix, plen);
19 memcpy(res->name + plen, name, nlen + 1);
20 res->next = add_decoration(&name_decoration, obj, res);
21}
22
23static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
24{
25 struct object *obj = parse_object(sha1);
26 if (!obj)
27 return 0;
28 add_name_decoration("", refname, obj);
29 while (obj->type == OBJ_TAG) {
30 obj = ((struct tag *)obj)->tagged;
31 if (!obj)
32 break;
33 add_name_decoration("tag: ", refname, obj);
34 }
35 return 0;
36}
37
38void load_ref_decorations(void)
39{
40 static int loaded;
41 if (!loaded) {
42 loaded = 1;
43 for_each_ref(add_ref_decoration, NULL);
44 }
45}
46
c8c893c6
LT
47static void show_parents(struct commit *commit, int abbrev)
48{
49 struct commit_list *p;
50 for (p = commit->parents; p ; p = p->next) {
51 struct commit *parent = p->item;
7fcda920 52 printf(" %s", find_unique_abbrev(parent->object.sha1, abbrev));
c8c893c6
LT
53 }
54}
55
0f3a290b 56void show_decorations(struct rev_info *opt, struct commit *commit)
ca135e7a
LT
57{
58 const char *prefix;
59 struct name_decoration *decoration;
60
0f3a290b 61 if (opt->show_source && commit->util)
8c4021ab 62 printf("\t%s", (char *) commit->util);
d467a525
LT
63 if (!opt->show_decorations)
64 return;
ca135e7a
LT
65 decoration = lookup_decoration(&name_decoration, &commit->object);
66 if (!decoration)
67 return;
68 prefix = " (";
69 while (decoration) {
70 printf("%s%s", prefix, decoration->name);
71 prefix = ", ";
72 decoration = decoration->next;
73 }
74 putchar(')');
75}
76
fc1c75ec
FBH
77/*
78 * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
79 * Signed-off-by: and Acked-by: lines.
80 */
81static int detect_any_signoff(char *letter, int size)
82{
fd13b21f 83 char *cp;
fc1c75ec
FBH
84 int seen_colon = 0;
85 int seen_at = 0;
86 int seen_name = 0;
87 int seen_head = 0;
88
89 cp = letter + size;
fd13b21f 90 while (letter <= --cp && *cp == '\n')
fc1c75ec
FBH
91 continue;
92
93 while (letter <= cp) {
fd13b21f 94 char ch = *cp--;
fc1c75ec
FBH
95 if (ch == '\n')
96 break;
97
98 if (!seen_at) {
99 if (ch == '@')
100 seen_at = 1;
101 continue;
102 }
103 if (!seen_colon) {
104 if (ch == '@')
105 return 0;
106 else if (ch == ':')
107 seen_colon = 1;
108 else
109 seen_name = 1;
110 continue;
111 }
112 if (('A' <= ch && ch <= 'Z') ||
113 ('a' <= ch && ch <= 'z') ||
114 ch == '-') {
115 seen_head = 1;
116 continue;
117 }
118 /* no empty last line doesn't match */
119 return 0;
120 }
121 return seen_head && seen_name;
122}
123
674d1727 124static void append_signoff(struct strbuf *sb, const char *signoff)
cf2251b6 125{
cf2251b6 126 static const char signed_off_by[] = "Signed-off-by: ";
80583c0e 127 size_t signoff_len = strlen(signoff);
fc1c75ec 128 int has_signoff = 0;
80583c0e 129 char *cp;
cf2251b6 130
674d1727 131 cp = sb->buf;
cf2251b6
JH
132
133 /* First see if we already have the sign-off by the signer */
fc1c75ec
FBH
134 while ((cp = strstr(cp, signed_off_by))) {
135
136 has_signoff = 1;
137
cf2251b6 138 cp += strlen(signed_off_by);
674d1727 139 if (cp + signoff_len >= sb->buf + sb->len)
fc1c75ec
FBH
140 break;
141 if (strncmp(cp, signoff, signoff_len))
142 continue;
143 if (!isspace(cp[signoff_len]))
144 continue;
145 /* we already have him */
674d1727 146 return;
cf2251b6
JH
147 }
148
fc1c75ec 149 if (!has_signoff)
674d1727 150 has_signoff = detect_any_signoff(sb->buf, sb->len);
fc1c75ec
FBH
151
152 if (!has_signoff)
674d1727 153 strbuf_addch(sb, '\n');
c35f4c37 154
674d1727
PH
155 strbuf_addstr(sb, signed_off_by);
156 strbuf_add(sb, signoff, signoff_len);
157 strbuf_addch(sb, '\n');
cf2251b6
JH
158}
159
e00de24b
JS
160static unsigned int digits_in_number(unsigned int number)
161{
162 unsigned int i = 10, result = 1;
163 while (i <= number) {
164 i *= 10;
165 result++;
166 }
167 return result;
168}
169
4593fb84
JH
170static int has_non_ascii(const char *s)
171{
172 int ch;
173 if (!s)
174 return 0;
175 while ((ch = *s++) != '\0') {
176 if (non_ascii(ch))
177 return 1;
178 }
179 return 0;
180}
181
6fa8e627
SB
182void get_patch_filename(struct commit *commit, int nr, const char *suffix,
183 struct strbuf *buf)
184{
185 int suffix_len = strlen(suffix) + 1;
186 int start_len = buf->len;
187
188 strbuf_addf(buf, commit ? "%04d-" : "%d", nr);
189 if (commit) {
190 format_commit_message(commit, "%f", buf, DATE_NORMAL);
191 /*
192 * Replace characters at the end with the suffix if the
193 * filename is too long
194 */
195 if (buf->len + suffix_len > FORMAT_PATCH_NAME_MAX + start_len)
196 strbuf_splice(buf,
197 start_len + FORMAT_PATCH_NAME_MAX - suffix_len,
198 suffix_len, suffix, suffix_len);
199 else
200 strbuf_addstr(buf, suffix);
201 }
202}
203
108dab28 204void log_write_email_headers(struct rev_info *opt, struct commit *commit,
267123b4
JH
205 const char **subject_p,
206 const char **extra_headers_p,
207 int *need_8bit_cte_p)
b02bd65f
DB
208{
209 const char *subject = NULL;
210 const char *extra_headers = opt->extra_headers;
108dab28 211 const char *name = sha1_to_hex(commit->object.sha1);
267123b4
JH
212
213 *need_8bit_cte_p = 0; /* unknown */
b02bd65f
DB
214 if (opt->total > 0) {
215 static char buffer[64];
216 snprintf(buffer, sizeof(buffer),
217 "Subject: [%s %0*d/%d] ",
218 opt->subject_prefix,
219 digits_in_number(opt->total),
220 opt->nr, opt->total);
221 subject = buffer;
222 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
223 static char buffer[256];
224 snprintf(buffer, sizeof(buffer),
225 "Subject: [%s] ",
226 opt->subject_prefix);
227 subject = buffer;
228 } else {
229 subject = "Subject: ";
230 }
231
232 printf("From %s Mon Sep 17 00:00:00 2001\n", name);
7fefda5c
AS
233 graph_show_oneline(opt->graph);
234 if (opt->message_id) {
b02bd65f 235 printf("Message-Id: <%s>\n", opt->message_id);
7fefda5c
AS
236 graph_show_oneline(opt->graph);
237 }
b079c50e
TR
238 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
239 int i, n;
240 n = opt->ref_message_ids->nr;
241 printf("In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
242 for (i = 0; i < n; i++)
243 printf("%s<%s>\n", (i > 0 ? "\t" : "References: "),
244 opt->ref_message_ids->items[i].string);
7fefda5c
AS
245 graph_show_oneline(opt->graph);
246 }
b02bd65f
DB
247 if (opt->mime_boundary) {
248 static char subject_buffer[1024];
249 static char buffer[1024];
108dab28 250 struct strbuf filename = STRBUF_INIT;
267123b4 251 *need_8bit_cte_p = -1; /* NEVER */
b02bd65f
DB
252 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
253 "%s"
254 "MIME-Version: 1.0\n"
255 "Content-Type: multipart/mixed;"
256 " boundary=\"%s%s\"\n"
257 "\n"
258 "This is a multi-part message in MIME "
259 "format.\n"
260 "--%s%s\n"
261 "Content-Type: text/plain; "
262 "charset=UTF-8; format=fixed\n"
263 "Content-Transfer-Encoding: 8bit\n\n",
264 extra_headers ? extra_headers : "",
265 mime_boundary_leader, opt->mime_boundary,
266 mime_boundary_leader, opt->mime_boundary);
267 extra_headers = subject_buffer;
268
108dab28
SB
269 get_patch_filename(opt->numbered_files ? NULL : commit, opt->nr,
270 opt->patch_suffix, &filename);
b02bd65f 271 snprintf(buffer, sizeof(buffer) - 1,
6b2fbaaf 272 "\n--%s%s\n"
b02bd65f 273 "Content-Type: text/x-patch;"
108dab28 274 " name=\"%s\"\n"
b02bd65f
DB
275 "Content-Transfer-Encoding: 8bit\n"
276 "Content-Disposition: %s;"
108dab28 277 " filename=\"%s\"\n\n",
b02bd65f 278 mime_boundary_leader, opt->mime_boundary,
108dab28 279 filename.buf,
b02bd65f 280 opt->no_inline ? "attachment" : "inline",
108dab28 281 filename.buf);
b02bd65f 282 opt->diffopt.stat_sep = buffer;
108dab28 283 strbuf_release(&filename);
b02bd65f
DB
284 }
285 *subject_p = subject;
286 *extra_headers_p = extra_headers;
287}
288
02865655 289void show_log(struct rev_info *opt)
91539833 290{
f285a2d7 291 struct strbuf msgbuf = STRBUF_INIT;
39bc9a6c 292 struct log_info *log = opt->loginfo;
91539833
LT
293 struct commit *commit = log->commit, *parent = log->parent;
294 int abbrev = opt->diffopt.abbrev;
295 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
20ff0680 296 const char *subject = NULL, *extra_headers = opt->extra_headers;
6bf4f1b4 297 int need_8bit_cte = 0;
91539833
LT
298
299 opt->loginfo = NULL;
300 if (!opt->verbose_header) {
7fefda5c
AS
301 graph_show_commit(opt->graph);
302
7528f27d
AS
303 if (!opt->graph) {
304 if (commit->object.flags & BOUNDARY)
305 putchar('-');
306 else if (commit->object.flags & UNINTERESTING)
307 putchar('^');
308 else if (opt->left_right) {
309 if (commit->object.flags & SYMMETRIC_LEFT)
310 putchar('<');
311 else
312 putchar('>');
313 }
74bd9029 314 }
7fcda920 315 fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
885cf808 316 if (opt->print_parents)
c8c893c6 317 show_parents(commit, abbrev_commit);
0f3a290b 318 show_decorations(opt, commit);
7fefda5c
AS
319 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
320 putchar('\n');
321 graph_show_remainder(opt->graph);
322 }
1dcb6922 323 putchar(opt->diffopt.line_termination);
91539833
LT
324 return;
325 }
326
327 /*
02865655
AS
328 * If use_terminator is set, add a newline at the end of the entry.
329 * Otherwise, add a diffopt.line_termination character before all
330 * entries but the first. (IOW, as a separator between entries)
91539833 331 */
7fefda5c
AS
332 if (opt->shown_one && !opt->use_terminator) {
333 /*
334 * If entries are separated by a newline, the output
335 * should look human-readable. If the last entry ended
336 * with a newline, print the graph output before this
337 * newline. Otherwise it will end up as a completely blank
338 * line and will look like a gap in the graph.
339 *
340 * If the entry separator is not a newline, the output is
341 * primarily intended for programmatic consumption, and we
342 * never want the extra graph output before the entry
343 * separator.
344 */
345 if (opt->diffopt.line_termination == '\n' &&
346 !opt->missing_newline)
347 graph_show_padding(opt->graph);
abd4e222 348 putchar(opt->diffopt.line_termination);
7fefda5c 349 }
91539833
LT
350 opt->shown_one = 1;
351
7fefda5c
AS
352 /*
353 * If the history graph was requested,
354 * print the graph, up to this commit's line
355 */
356 graph_show_commit(opt->graph);
357
91539833
LT
358 /*
359 * Print header line of header..
360 */
3eefc189 361
596524b3 362 if (opt->commit_format == CMIT_FMT_EMAIL) {
108dab28 363 log_write_email_headers(opt, commit, &subject, &extra_headers,
267123b4 364 &need_8bit_cte);
e52a5de4 365 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
8f67f8ae 366 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
74bd9029
JH
367 if (opt->commit_format != CMIT_FMT_ONELINE)
368 fputs("commit ", stdout);
7528f27d
AS
369
370 if (!opt->graph) {
371 if (commit->object.flags & BOUNDARY)
372 putchar('-');
373 else if (commit->object.flags & UNINTERESTING)
374 putchar('^');
375 else if (opt->left_right) {
376 if (commit->object.flags & SYMMETRIC_LEFT)
377 putchar('<');
378 else
379 putchar('>');
380 }
74bd9029 381 }
7fcda920 382 fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit),
74bd9029 383 stdout);
885cf808 384 if (opt->print_parents)
c66b6c06 385 show_parents(commit, abbrev_commit);
73f0a157 386 if (parent)
3eefc189 387 printf(" (from %s)",
7fcda920 388 find_unique_abbrev(parent->object.sha1,
3eefc189 389 abbrev_commit));
0f3a290b 390 show_decorations(opt, commit);
8f67f8ae 391 printf("%s", diff_get_color_opt(&opt->diffopt, DIFF_RESET));
7fefda5c
AS
392 if (opt->commit_format == CMIT_FMT_ONELINE) {
393 putchar(' ');
394 } else {
395 putchar('\n');
396 graph_show_oneline(opt->graph);
397 }
903b45fe 398 if (opt->reflog_info) {
7fefda5c
AS
399 /*
400 * setup_revisions() ensures that opt->reflog_info
401 * and opt->graph cannot both be set,
402 * so we don't need to worry about printing the
403 * graph info here.
404 */
4d12a471 405 show_reflog_message(opt->reflog_info,
4e244cbc 406 opt->commit_format == CMIT_FMT_ONELINE,
a7b02ccf 407 opt->date_mode);
02865655 408 if (opt->commit_format == CMIT_FMT_ONELINE)
903b45fe 409 return;
903b45fe 410 }
3eefc189 411 }
91539833 412
3131b713
LT
413 if (!commit->buffer)
414 return;
415
91539833
LT
416 /*
417 * And then the pretty-printed message itself
418 */
6bf4f1b4
JH
419 if (need_8bit_cte >= 0)
420 need_8bit_cte = has_non_ascii(opt->add_signoff);
674d1727 421 pretty_print_commit(opt->commit_format, commit, &msgbuf,
4593fb84 422 abbrev, subject, extra_headers, opt->date_mode,
6bf4f1b4 423 need_8bit_cte);
cf2251b6
JH
424
425 if (opt->add_signoff)
674d1727 426 append_signoff(&msgbuf, opt->add_signoff);
7fefda5c 427 if (opt->show_log_size) {
674d1727 428 printf("log size %i\n", (int)msgbuf.len);
7fefda5c
AS
429 graph_show_oneline(opt->graph);
430 }
9fa3465d 431
7fefda5c
AS
432 /*
433 * Set opt->missing_newline if msgbuf doesn't
434 * end in a newline (including if it is empty)
435 */
436 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
437 opt->missing_newline = 1;
438 else
439 opt->missing_newline = 0;
440
441 if (opt->graph)
442 graph_show_commit_msg(opt->graph, &msgbuf);
443 else
42c8c74c 444 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
7fefda5c
AS
445 if (opt->use_terminator) {
446 if (!opt->missing_newline)
447 graph_show_padding(opt->graph);
9b58bfe8 448 putchar('\n');
7fefda5c
AS
449 }
450
674d1727 451 strbuf_release(&msgbuf);
91539833
LT
452}
453
cd2bdc53 454int log_tree_diff_flush(struct rev_info *opt)
5f1c3f07
JH
455{
456 diffcore_std(&opt->diffopt);
91539833 457
5f1c3f07
JH
458 if (diff_queue_is_empty()) {
459 int saved_fmt = opt->diffopt.output_format;
460 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
461 diff_flush(&opt->diffopt);
462 opt->diffopt.output_format = saved_fmt;
463 return 0;
464 }
91539833 465
3969cf7d
JH
466 if (opt->loginfo && !opt->no_commit_id) {
467 /* When showing a verbose header (i.e. log message),
468 * and not in --pretty=oneline format, we would want
469 * an extra newline between the end of log and the
470 * output for readability.
471 */
02865655 472 show_log(opt);
304b5af6
LT
473 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
474 opt->verbose_header &&
3969cf7d
JH
475 opt->commit_format != CMIT_FMT_ONELINE) {
476 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
477 if ((pch & opt->diffopt.output_format) == pch)
abd4e222
LT
478 printf("---");
479 putchar('\n');
3969cf7d
JH
480 }
481 }
5f1c3f07
JH
482 diff_flush(&opt->diffopt);
483 return 1;
484}
485
cd2bdc53 486static int do_diff_combined(struct rev_info *opt, struct commit *commit)
5f1c3f07
JH
487{
488 unsigned const char *sha1 = commit->object.sha1;
489
91539833
LT
490 diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
491 return !opt->loginfo;
5f1c3f07
JH
492}
493
91539833
LT
494/*
495 * Show the diff of a commit.
496 *
497 * Return true if we printed any log info messages
498 */
499static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
5f1c3f07 500{
91539833 501 int showed_log;
5f1c3f07
JH
502 struct commit_list *parents;
503 unsigned const char *sha1 = commit->object.sha1;
504
84102a33 505 if (!opt->diff && !DIFF_OPT_TST(&opt->diffopt, EXIT_WITH_STATUS))
91539833
LT
506 return 0;
507
5f1c3f07 508 /* Root commit? */
91539833
LT
509 parents = commit->parents;
510 if (!parents) {
2b60356d
RS
511 if (opt->show_root_diff) {
512 diff_root_tree_sha1(sha1, "", &opt->diffopt);
513 log_tree_diff_flush(opt);
514 }
91539833 515 return !opt->loginfo;
5f1c3f07
JH
516 }
517
518 /* More than one parent? */
91539833 519 if (parents && parents->next) {
5f1c3f07
JH
520 if (opt->ignore_merges)
521 return 0;
522 else if (opt->combine_merges)
523 return do_diff_combined(opt, commit);
91539833
LT
524
525 /* If we show individual diffs, show the parent info */
526 log->parent = parents->item;
5f1c3f07
JH
527 }
528
91539833
LT
529 showed_log = 0;
530 for (;;) {
5f1c3f07 531 struct commit *parent = parents->item;
5f1c3f07 532
91539833
LT
533 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
534 log_tree_diff_flush(opt);
535
536 showed_log |= !opt->loginfo;
537
538 /* Set up the log info for the next parent, if any.. */
539 parents = parents->next;
540 if (!parents)
541 break;
542 log->parent = parents->item;
543 opt->loginfo = log;
544 }
545 return showed_log;
546}
547
548int log_tree_commit(struct rev_info *opt, struct commit *commit)
549{
550 struct log_info log;
3eefc189 551 int shown;
91539833
LT
552
553 log.commit = commit;
554 log.parent = NULL;
555 opt->loginfo = &log;
556
3eefc189
JH
557 shown = log_tree_diff(opt, commit, &log);
558 if (!shown && opt->loginfo && opt->always_show_header) {
91539833 559 log.parent = NULL;
02865655 560 show_log(opt);
3eefc189 561 shown = 1;
5f1c3f07 562 }
91539833 563 opt->loginfo = NULL;
06f59e9f 564 maybe_flush_or_die(stdout, "stdout");
3eefc189 565 return shown;
5f1c3f07 566}