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