Merge branch 'master' of git://git.kernel.org/pub/scm/git/git
[git/jnareb-git.git] / log-tree.c
blob59d63eb67e57ab55bdca6493fa8b28b0c870ff21
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"
9 #include "string-list.h"
11 struct decoration name_decoration = { "object names" };
13 static void add_name_decoration(const char *prefix, const char *name, struct object *obj)
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);
23 static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
25 struct object *obj = parse_object(sha1);
26 if (!obj)
27 return 0;
28 refname = prettify_refname(refname);
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);
36 return 0;
39 void load_ref_decorations(void)
41 static int loaded;
42 if (!loaded) {
43 loaded = 1;
44 for_each_ref(add_ref_decoration, NULL);
48 static void show_parents(struct commit *commit, int abbrev)
50 struct commit_list *p;
51 for (p = commit->parents; p ; p = p->next) {
52 struct commit *parent = p->item;
53 printf(" %s", find_unique_abbrev(parent->object.sha1, abbrev));
57 void show_decorations(struct rev_info *opt, struct commit *commit)
59 const char *prefix;
60 struct name_decoration *decoration;
62 if (opt->show_source && commit->util)
63 printf("\t%s", (char *) commit->util);
64 if (!opt->show_decorations)
65 return;
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;
75 putchar(')');
79 * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
80 * Signed-off-by: and Acked-by: lines.
82 static int detect_any_signoff(char *letter, int size)
84 char *cp;
85 int seen_colon = 0;
86 int seen_at = 0;
87 int seen_name = 0;
88 int seen_head = 0;
90 cp = letter + size;
91 while (letter <= --cp && *cp == '\n')
92 continue;
94 while (letter <= cp) {
95 char ch = *cp--;
96 if (ch == '\n')
97 break;
99 if (!seen_at) {
100 if (ch == '@')
101 seen_at = 1;
102 continue;
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;
113 if (('A' <= ch && ch <= 'Z') ||
114 ('a' <= ch && ch <= 'z') ||
115 ch == '-') {
116 seen_head = 1;
117 continue;
119 /* no empty last line doesn't match */
120 return 0;
122 return seen_head && seen_name;
125 static void append_signoff(struct strbuf *sb, const char *signoff)
127 static const char signed_off_by[] = "Signed-off-by: ";
128 size_t signoff_len = strlen(signoff);
129 int has_signoff = 0;
130 char *cp;
132 cp = sb->buf;
134 /* First see if we already have the sign-off by the signer */
135 while ((cp = strstr(cp, signed_off_by))) {
137 has_signoff = 1;
139 cp += strlen(signed_off_by);
140 if (cp + signoff_len >= sb->buf + sb->len)
141 break;
142 if (strncmp(cp, signoff, signoff_len))
143 continue;
144 if (!isspace(cp[signoff_len]))
145 continue;
146 /* we already have him */
147 return;
150 if (!has_signoff)
151 has_signoff = detect_any_signoff(sb->buf, sb->len);
153 if (!has_signoff)
154 strbuf_addch(sb, '\n');
156 strbuf_addstr(sb, signed_off_by);
157 strbuf_add(sb, signoff, signoff_len);
158 strbuf_addch(sb, '\n');
161 static unsigned int digits_in_number(unsigned int number)
163 unsigned int i = 10, result = 1;
164 while (i <= number) {
165 i *= 10;
166 result++;
168 return result;
171 static int has_non_ascii(const char *s)
173 int ch;
174 if (!s)
175 return 0;
176 while ((ch = *s++) != '\0') {
177 if (non_ascii(ch))
178 return 1;
180 return 0;
183 void get_patch_filename(struct commit *commit, int nr, const char *suffix,
184 struct strbuf *buf)
186 int suffix_len = strlen(suffix) + 1;
187 int start_len = buf->len;
189 strbuf_addf(buf, commit ? "%04d-" : "%d", nr);
190 if (commit) {
191 int max_len = start_len + FORMAT_PATCH_NAME_MAX - suffix_len;
193 format_commit_message(commit, "%f", buf, DATE_NORMAL);
194 if (max_len < buf->len)
195 strbuf_setlen(buf, max_len);
196 strbuf_addstr(buf, suffix);
200 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
201 const char **subject_p,
202 const char **extra_headers_p,
203 int *need_8bit_cte_p)
205 const char *subject = NULL;
206 const char *extra_headers = opt->extra_headers;
207 const char *name = sha1_to_hex(commit->object.sha1);
209 *need_8bit_cte_p = 0; /* unknown */
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: ";
228 printf("From %s Mon Sep 17 00:00:00 2001\n", name);
229 graph_show_oneline(opt->graph);
230 if (opt->message_id) {
231 printf("Message-Id: <%s>\n", opt->message_id);
232 graph_show_oneline(opt->graph);
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);
241 graph_show_oneline(opt->graph);
243 if (opt->mime_boundary) {
244 static char subject_buffer[1024];
245 static char buffer[1024];
246 struct strbuf filename = STRBUF_INIT;
247 *need_8bit_cte_p = -1; /* NEVER */
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;
265 get_patch_filename(opt->numbered_files ? NULL : commit, opt->nr,
266 opt->patch_suffix, &filename);
267 snprintf(buffer, sizeof(buffer) - 1,
268 "\n--%s%s\n"
269 "Content-Type: text/x-patch;"
270 " name=\"%s\"\n"
271 "Content-Transfer-Encoding: 8bit\n"
272 "Content-Disposition: %s;"
273 " filename=\"%s\"\n\n",
274 mime_boundary_leader, opt->mime_boundary,
275 filename.buf,
276 opt->no_inline ? "attachment" : "inline",
277 filename.buf);
278 opt->diffopt.stat_sep = buffer;
279 strbuf_release(&filename);
281 *subject_p = subject;
282 *extra_headers_p = extra_headers;
285 void show_log(struct rev_info *opt)
287 struct strbuf msgbuf = STRBUF_INIT;
288 struct log_info *log = opt->loginfo;
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;
292 const char *subject = NULL, *extra_headers = opt->extra_headers;
293 int need_8bit_cte = 0;
295 opt->loginfo = NULL;
296 if (!opt->verbose_header) {
297 graph_show_commit(opt->graph);
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('>');
311 fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
312 if (opt->print_parents)
313 show_parents(commit, abbrev_commit);
314 show_decorations(opt, commit);
315 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
316 putchar('\n');
317 graph_show_remainder(opt->graph);
319 putchar(opt->diffopt.line_termination);
320 return;
324 * If use_terminator is set, add a newline at the end of the entry.
325 * Otherwise, add a diffopt.line_termination character before all
326 * entries but the first. (IOW, as a separator between entries)
328 if (opt->shown_one && !opt->use_terminator) {
330 * If entries are separated by a newline, the output
331 * should look human-readable. If the last entry ended
332 * with a newline, print the graph output before this
333 * newline. Otherwise it will end up as a completely blank
334 * line and will look like a gap in the graph.
336 * If the entry separator is not a newline, the output is
337 * primarily intended for programmatic consumption, and we
338 * never want the extra graph output before the entry
339 * separator.
341 if (opt->diffopt.line_termination == '\n' &&
342 !opt->missing_newline)
343 graph_show_padding(opt->graph);
344 putchar(opt->diffopt.line_termination);
346 opt->shown_one = 1;
349 * If the history graph was requested,
350 * print the graph, up to this commit's line
352 graph_show_commit(opt->graph);
355 * Print header line of header..
358 if (opt->commit_format == CMIT_FMT_EMAIL) {
359 log_write_email_headers(opt, commit, &subject, &extra_headers,
360 &need_8bit_cte);
361 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
362 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
363 if (opt->commit_format != CMIT_FMT_ONELINE)
364 fputs("commit ", stdout);
366 if (!opt->graph) {
367 if (commit->object.flags & BOUNDARY)
368 putchar('-');
369 else if (commit->object.flags & UNINTERESTING)
370 putchar('^');
371 else if (opt->left_right) {
372 if (commit->object.flags & SYMMETRIC_LEFT)
373 putchar('<');
374 else
375 putchar('>');
378 fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit),
379 stdout);
380 if (opt->print_parents)
381 show_parents(commit, abbrev_commit);
382 if (parent)
383 printf(" (from %s)",
384 find_unique_abbrev(parent->object.sha1,
385 abbrev_commit));
386 show_decorations(opt, commit);
387 printf("%s", diff_get_color_opt(&opt->diffopt, DIFF_RESET));
388 if (opt->commit_format == CMIT_FMT_ONELINE) {
389 putchar(' ');
390 } else {
391 putchar('\n');
392 graph_show_oneline(opt->graph);
394 if (opt->reflog_info) {
396 * setup_revisions() ensures that opt->reflog_info
397 * and opt->graph cannot both be set,
398 * so we don't need to worry about printing the
399 * graph info here.
401 show_reflog_message(opt->reflog_info,
402 opt->commit_format == CMIT_FMT_ONELINE,
403 opt->date_mode);
404 if (opt->commit_format == CMIT_FMT_ONELINE)
405 return;
409 if (!commit->buffer)
410 return;
413 * And then the pretty-printed message itself
415 if (need_8bit_cte >= 0)
416 need_8bit_cte = has_non_ascii(opt->add_signoff);
417 pretty_print_commit(opt->commit_format, commit, &msgbuf,
418 abbrev, subject, extra_headers, opt->date_mode,
419 need_8bit_cte);
421 if (opt->add_signoff)
422 append_signoff(&msgbuf, opt->add_signoff);
423 if (opt->show_log_size) {
424 printf("log size %i\n", (int)msgbuf.len);
425 graph_show_oneline(opt->graph);
429 * Set opt->missing_newline if msgbuf doesn't
430 * end in a newline (including if it is empty)
432 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
433 opt->missing_newline = 1;
434 else
435 opt->missing_newline = 0;
437 if (opt->graph)
438 graph_show_commit_msg(opt->graph, &msgbuf);
439 else
440 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
441 if (opt->use_terminator) {
442 if (!opt->missing_newline)
443 graph_show_padding(opt->graph);
444 putchar('\n');
447 strbuf_release(&msgbuf);
450 int log_tree_diff_flush(struct rev_info *opt)
452 diffcore_std(&opt->diffopt);
454 if (diff_queue_is_empty()) {
455 int saved_fmt = opt->diffopt.output_format;
456 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
457 diff_flush(&opt->diffopt);
458 opt->diffopt.output_format = saved_fmt;
459 return 0;
462 if (opt->loginfo && !opt->no_commit_id) {
463 /* When showing a verbose header (i.e. log message),
464 * and not in --pretty=oneline format, we would want
465 * an extra newline between the end of log and the
466 * output for readability.
468 show_log(opt);
469 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
470 opt->verbose_header &&
471 opt->commit_format != CMIT_FMT_ONELINE) {
472 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
473 if ((pch & opt->diffopt.output_format) == pch)
474 printf("---");
475 putchar('\n');
478 diff_flush(&opt->diffopt);
479 return 1;
482 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
484 unsigned const char *sha1 = commit->object.sha1;
486 diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
487 return !opt->loginfo;
491 * Show the diff of a commit.
493 * Return true if we printed any log info messages
495 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
497 int showed_log;
498 struct commit_list *parents;
499 unsigned const char *sha1 = commit->object.sha1;
501 if (!opt->diff && !DIFF_OPT_TST(&opt->diffopt, EXIT_WITH_STATUS))
502 return 0;
504 /* Root commit? */
505 parents = commit->parents;
506 if (!parents) {
507 if (opt->show_root_diff) {
508 diff_root_tree_sha1(sha1, "", &opt->diffopt);
509 log_tree_diff_flush(opt);
511 return !opt->loginfo;
514 /* More than one parent? */
515 if (parents && parents->next) {
516 if (opt->ignore_merges)
517 return 0;
518 else if (opt->combine_merges)
519 return do_diff_combined(opt, commit);
521 /* If we show individual diffs, show the parent info */
522 log->parent = parents->item;
525 showed_log = 0;
526 for (;;) {
527 struct commit *parent = parents->item;
529 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
530 log_tree_diff_flush(opt);
532 showed_log |= !opt->loginfo;
534 /* Set up the log info for the next parent, if any.. */
535 parents = parents->next;
536 if (!parents)
537 break;
538 log->parent = parents->item;
539 opt->loginfo = log;
541 return showed_log;
544 int log_tree_commit(struct rev_info *opt, struct commit *commit)
546 struct log_info log;
547 int shown;
549 log.commit = commit;
550 log.parent = NULL;
551 opt->loginfo = &log;
553 shown = log_tree_diff(opt, commit, &log);
554 if (!shown && opt->loginfo && opt->always_show_header) {
555 log.parent = NULL;
556 show_log(opt);
557 shown = 1;
559 opt->loginfo = NULL;
560 maybe_flush_or_die(stdout, "stdout");
561 return shown;