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