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