builtin-commit: use reduce_heads() only when appropriate
[git/dscho.git] / log-tree.c
blob2c1f3e673ae7c9441cb171244d3fb91a8dd9fb1b
1 #include "cache.h"
2 #include "diff.h"
3 #include "commit.h"
4 #include "tag.h"
5 #include "graph.h"
6 #include "log-tree.h"
7 #include "reflog-walk.h"
8 #include "refs.h"
10 struct decoration name_decoration = { "object names" };
12 static void add_name_decoration(const char *prefix, const char *name, struct object *obj)
14 int plen = strlen(prefix);
15 int nlen = strlen(name);
16 struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + plen + nlen);
17 memcpy(res->name, prefix, plen);
18 memcpy(res->name + plen, name, nlen + 1);
19 res->next = add_decoration(&name_decoration, obj, res);
22 static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
24 struct object *obj = parse_object(sha1);
25 if (!obj)
26 return 0;
27 add_name_decoration("", refname, obj);
28 while (obj->type == OBJ_TAG) {
29 obj = ((struct tag *)obj)->tagged;
30 if (!obj)
31 break;
32 add_name_decoration("tag: ", refname, obj);
34 return 0;
37 void load_ref_decorations(void)
39 static int loaded;
40 if (!loaded) {
41 loaded = 1;
42 for_each_ref(add_ref_decoration, NULL);
46 static void show_parents(struct commit *commit, int abbrev)
48 struct commit_list *p;
49 for (p = commit->parents; p ; p = p->next) {
50 struct commit *parent = p->item;
51 printf(" %s", diff_unique_abbrev(parent->object.sha1, abbrev));
55 void show_decorations(struct commit *commit)
57 const char *prefix;
58 struct name_decoration *decoration;
60 decoration = lookup_decoration(&name_decoration, &commit->object);
61 if (!decoration)
62 return;
63 prefix = " (";
64 while (decoration) {
65 printf("%s%s", prefix, decoration->name);
66 prefix = ", ";
67 decoration = decoration->next;
69 putchar(')');
73 * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
74 * Signed-off-by: and Acked-by: lines.
76 static int detect_any_signoff(char *letter, int size)
78 char ch, *cp;
79 int seen_colon = 0;
80 int seen_at = 0;
81 int seen_name = 0;
82 int seen_head = 0;
84 cp = letter + size;
85 while (letter <= --cp && (ch = *cp) == '\n')
86 continue;
88 while (letter <= cp) {
89 ch = *cp--;
90 if (ch == '\n')
91 break;
93 if (!seen_at) {
94 if (ch == '@')
95 seen_at = 1;
96 continue;
98 if (!seen_colon) {
99 if (ch == '@')
100 return 0;
101 else if (ch == ':')
102 seen_colon = 1;
103 else
104 seen_name = 1;
105 continue;
107 if (('A' <= ch && ch <= 'Z') ||
108 ('a' <= ch && ch <= 'z') ||
109 ch == '-') {
110 seen_head = 1;
111 continue;
113 /* no empty last line doesn't match */
114 return 0;
116 return seen_head && seen_name;
119 static void append_signoff(struct strbuf *sb, const char *signoff)
121 static const char signed_off_by[] = "Signed-off-by: ";
122 size_t signoff_len = strlen(signoff);
123 int has_signoff = 0;
124 char *cp;
126 cp = sb->buf;
128 /* First see if we already have the sign-off by the signer */
129 while ((cp = strstr(cp, signed_off_by))) {
131 has_signoff = 1;
133 cp += strlen(signed_off_by);
134 if (cp + signoff_len >= sb->buf + sb->len)
135 break;
136 if (strncmp(cp, signoff, signoff_len))
137 continue;
138 if (!isspace(cp[signoff_len]))
139 continue;
140 /* we already have him */
141 return;
144 if (!has_signoff)
145 has_signoff = detect_any_signoff(sb->buf, sb->len);
147 if (!has_signoff)
148 strbuf_addch(sb, '\n');
150 strbuf_addstr(sb, signed_off_by);
151 strbuf_add(sb, signoff, signoff_len);
152 strbuf_addch(sb, '\n');
155 static unsigned int digits_in_number(unsigned int number)
157 unsigned int i = 10, result = 1;
158 while (i <= number) {
159 i *= 10;
160 result++;
162 return result;
165 static int has_non_ascii(const char *s)
167 int ch;
168 if (!s)
169 return 0;
170 while ((ch = *s++) != '\0') {
171 if (non_ascii(ch))
172 return 1;
174 return 0;
177 void log_write_email_headers(struct rev_info *opt, const char *name,
178 const char **subject_p,
179 const char **extra_headers_p,
180 int *need_8bit_cte_p)
182 const char *subject = NULL;
183 const char *extra_headers = opt->extra_headers;
185 *need_8bit_cte_p = 0; /* unknown */
186 if (opt->total > 0) {
187 static char buffer[64];
188 snprintf(buffer, sizeof(buffer),
189 "Subject: [%s %0*d/%d] ",
190 opt->subject_prefix,
191 digits_in_number(opt->total),
192 opt->nr, opt->total);
193 subject = buffer;
194 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
195 static char buffer[256];
196 snprintf(buffer, sizeof(buffer),
197 "Subject: [%s] ",
198 opt->subject_prefix);
199 subject = buffer;
200 } else {
201 subject = "Subject: ";
204 printf("From %s Mon Sep 17 00:00:00 2001\n", name);
205 graph_show_oneline(opt->graph);
206 if (opt->message_id) {
207 printf("Message-Id: <%s>\n", opt->message_id);
208 graph_show_oneline(opt->graph);
210 if (opt->ref_message_id) {
211 printf("In-Reply-To: <%s>\nReferences: <%s>\n",
212 opt->ref_message_id, opt->ref_message_id);
213 graph_show_oneline(opt->graph);
215 if (opt->mime_boundary) {
216 static char subject_buffer[1024];
217 static char buffer[1024];
218 *need_8bit_cte_p = -1; /* NEVER */
219 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
220 "%s"
221 "MIME-Version: 1.0\n"
222 "Content-Type: multipart/mixed;"
223 " boundary=\"%s%s\"\n"
224 "\n"
225 "This is a multi-part message in MIME "
226 "format.\n"
227 "--%s%s\n"
228 "Content-Type: text/plain; "
229 "charset=UTF-8; format=fixed\n"
230 "Content-Transfer-Encoding: 8bit\n\n",
231 extra_headers ? extra_headers : "",
232 mime_boundary_leader, opt->mime_boundary,
233 mime_boundary_leader, opt->mime_boundary);
234 extra_headers = subject_buffer;
236 snprintf(buffer, sizeof(buffer) - 1,
237 "\n--%s%s\n"
238 "Content-Type: text/x-patch;"
239 " name=\"%s.diff\"\n"
240 "Content-Transfer-Encoding: 8bit\n"
241 "Content-Disposition: %s;"
242 " filename=\"%s.diff\"\n\n",
243 mime_boundary_leader, opt->mime_boundary,
244 name,
245 opt->no_inline ? "attachment" : "inline",
246 name);
247 opt->diffopt.stat_sep = buffer;
249 *subject_p = subject;
250 *extra_headers_p = extra_headers;
253 void show_log(struct rev_info *opt)
255 struct strbuf msgbuf;
256 struct log_info *log = opt->loginfo;
257 struct commit *commit = log->commit, *parent = log->parent;
258 int abbrev = opt->diffopt.abbrev;
259 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
260 const char *subject = NULL, *extra_headers = opt->extra_headers;
261 int need_8bit_cte = 0;
263 opt->loginfo = NULL;
264 if (!opt->verbose_header) {
265 graph_show_commit(opt->graph);
267 if (!opt->graph) {
268 if (commit->object.flags & BOUNDARY)
269 putchar('-');
270 else if (commit->object.flags & UNINTERESTING)
271 putchar('^');
272 else if (opt->left_right) {
273 if (commit->object.flags & SYMMETRIC_LEFT)
274 putchar('<');
275 else
276 putchar('>');
279 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
280 if (opt->print_parents)
281 show_parents(commit, abbrev_commit);
282 show_decorations(commit);
283 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
284 putchar('\n');
285 graph_show_remainder(opt->graph);
287 putchar(opt->diffopt.line_termination);
288 return;
292 * If use_terminator is set, add a newline at the end of the entry.
293 * Otherwise, add a diffopt.line_termination character before all
294 * entries but the first. (IOW, as a separator between entries)
296 if (opt->shown_one && !opt->use_terminator) {
298 * If entries are separated by a newline, the output
299 * should look human-readable. If the last entry ended
300 * with a newline, print the graph output before this
301 * newline. Otherwise it will end up as a completely blank
302 * line and will look like a gap in the graph.
304 * If the entry separator is not a newline, the output is
305 * primarily intended for programmatic consumption, and we
306 * never want the extra graph output before the entry
307 * separator.
309 if (opt->diffopt.line_termination == '\n' &&
310 !opt->missing_newline)
311 graph_show_padding(opt->graph);
312 putchar(opt->diffopt.line_termination);
314 opt->shown_one = 1;
317 * If the history graph was requested,
318 * print the graph, up to this commit's line
320 graph_show_commit(opt->graph);
323 * Print header line of header..
326 if (opt->commit_format == CMIT_FMT_EMAIL) {
327 log_write_email_headers(opt, sha1_to_hex(commit->object.sha1),
328 &subject, &extra_headers,
329 &need_8bit_cte);
330 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
331 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
332 if (opt->commit_format != CMIT_FMT_ONELINE)
333 fputs("commit ", stdout);
335 if (!opt->graph) {
336 if (commit->object.flags & BOUNDARY)
337 putchar('-');
338 else if (commit->object.flags & UNINTERESTING)
339 putchar('^');
340 else if (opt->left_right) {
341 if (commit->object.flags & SYMMETRIC_LEFT)
342 putchar('<');
343 else
344 putchar('>');
347 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit),
348 stdout);
349 if (opt->print_parents)
350 show_parents(commit, abbrev_commit);
351 if (parent)
352 printf(" (from %s)",
353 diff_unique_abbrev(parent->object.sha1,
354 abbrev_commit));
355 show_decorations(commit);
356 printf("%s", diff_get_color_opt(&opt->diffopt, DIFF_RESET));
357 if (opt->commit_format == CMIT_FMT_ONELINE) {
358 putchar(' ');
359 } else {
360 putchar('\n');
361 graph_show_oneline(opt->graph);
363 if (opt->reflog_info) {
365 * setup_revisions() ensures that opt->reflog_info
366 * and opt->graph cannot both be set,
367 * so we don't need to worry about printing the
368 * graph info here.
370 show_reflog_message(opt->reflog_info,
371 opt->commit_format == CMIT_FMT_ONELINE,
372 opt->date_mode);
373 if (opt->commit_format == CMIT_FMT_ONELINE)
374 return;
378 if (!commit->buffer)
379 return;
382 * And then the pretty-printed message itself
384 strbuf_init(&msgbuf, 0);
385 if (need_8bit_cte >= 0)
386 need_8bit_cte = has_non_ascii(opt->add_signoff);
387 pretty_print_commit(opt->commit_format, commit, &msgbuf,
388 abbrev, subject, extra_headers, opt->date_mode,
389 need_8bit_cte);
391 if (opt->add_signoff)
392 append_signoff(&msgbuf, opt->add_signoff);
393 if (opt->show_log_size) {
394 printf("log size %i\n", (int)msgbuf.len);
395 graph_show_oneline(opt->graph);
399 * Set opt->missing_newline if msgbuf doesn't
400 * end in a newline (including if it is empty)
402 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
403 opt->missing_newline = 1;
404 else
405 opt->missing_newline = 0;
407 if (opt->graph)
408 graph_show_commit_msg(opt->graph, &msgbuf);
409 else
410 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
411 if (opt->use_terminator) {
412 if (!opt->missing_newline)
413 graph_show_padding(opt->graph);
414 putchar('\n');
417 strbuf_release(&msgbuf);
420 int log_tree_diff_flush(struct rev_info *opt)
422 diffcore_std(&opt->diffopt);
424 if (diff_queue_is_empty()) {
425 int saved_fmt = opt->diffopt.output_format;
426 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
427 diff_flush(&opt->diffopt);
428 opt->diffopt.output_format = saved_fmt;
429 return 0;
432 if (opt->loginfo && !opt->no_commit_id) {
433 /* When showing a verbose header (i.e. log message),
434 * and not in --pretty=oneline format, we would want
435 * an extra newline between the end of log and the
436 * output for readability.
438 show_log(opt);
439 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
440 opt->verbose_header &&
441 opt->commit_format != CMIT_FMT_ONELINE) {
442 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
443 if ((pch & opt->diffopt.output_format) == pch)
444 printf("---");
445 putchar('\n');
448 diff_flush(&opt->diffopt);
449 return 1;
452 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
454 unsigned const char *sha1 = commit->object.sha1;
456 diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
457 return !opt->loginfo;
461 * Show the diff of a commit.
463 * Return true if we printed any log info messages
465 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
467 int showed_log;
468 struct commit_list *parents;
469 unsigned const char *sha1 = commit->object.sha1;
471 if (!opt->diff && !DIFF_OPT_TST(&opt->diffopt, EXIT_WITH_STATUS))
472 return 0;
474 /* Root commit? */
475 parents = commit->parents;
476 if (!parents) {
477 if (opt->show_root_diff) {
478 diff_root_tree_sha1(sha1, "", &opt->diffopt);
479 log_tree_diff_flush(opt);
481 return !opt->loginfo;
484 /* More than one parent? */
485 if (parents && parents->next) {
486 if (opt->ignore_merges)
487 return 0;
488 else if (opt->combine_merges)
489 return do_diff_combined(opt, commit);
491 /* If we show individual diffs, show the parent info */
492 log->parent = parents->item;
495 showed_log = 0;
496 for (;;) {
497 struct commit *parent = parents->item;
499 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
500 log_tree_diff_flush(opt);
502 showed_log |= !opt->loginfo;
504 /* Set up the log info for the next parent, if any.. */
505 parents = parents->next;
506 if (!parents)
507 break;
508 log->parent = parents->item;
509 opt->loginfo = log;
511 return showed_log;
514 int log_tree_commit(struct rev_info *opt, struct commit *commit)
516 struct log_info log;
517 int shown;
519 log.commit = commit;
520 log.parent = NULL;
521 opt->loginfo = &log;
523 shown = log_tree_diff(opt, commit, &log);
524 if (!shown && opt->loginfo && opt->always_show_header) {
525 log.parent = NULL;
526 show_log(opt);
527 shown = 1;
529 opt->loginfo = NULL;
530 maybe_flush_or_die(stdout, "stdout");
531 return shown;