7 #include "reflog-walk.h"
9 #include "string-list.h"
11 struct decoration name_decoration = { "object names" };
13 enum decoration_type {
16 DECORATION_REF_REMOTE,
22 static void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
24 int nlen = strlen(name);
25 struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + nlen);
26 memcpy(res->name, name, nlen + 1);
28 res->next = add_decoration(&name_decoration, obj, res);
31 static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
33 struct object *obj = parse_object(sha1);
34 enum decoration_type type = DECORATION_NONE;
38 if (!prefixcmp(refname, "refs/heads"))
39 type = DECORATION_REF_LOCAL;
40 else if (!prefixcmp(refname, "refs/remotes"))
41 type = DECORATION_REF_REMOTE;
42 else if (!prefixcmp(refname, "refs/tags"))
43 type = DECORATION_REF_TAG;
44 else if (!prefixcmp(refname, "refs/stash"))
45 type = DECORATION_REF_STASH;
46 else if (!prefixcmp(refname, "HEAD"))
47 type = DECORATION_REF_HEAD;
49 if (!cb_data || *(int *)cb_data == DECORATE_SHORT_REFS)
50 refname = prettify_refname(refname);
51 add_name_decoration(type, refname, obj);
52 while (obj->type == OBJ_TAG) {
53 obj = ((struct tag *)obj)->tagged;
56 add_name_decoration(DECORATION_REF_TAG, refname, obj);
61 void load_ref_decorations(int flags)
66 for_each_ref(add_ref_decoration, &flags);
67 head_ref(add_ref_decoration, &flags);
71 static void show_parents(struct commit *commit, int abbrev)
73 struct commit_list *p;
74 for (p = commit->parents; p ; p = p->next) {
75 struct commit *parent = p->item;
76 printf(" %s", find_unique_abbrev(parent->object.sha1, abbrev));
80 void show_decorations(struct rev_info *opt, struct commit *commit)
83 struct name_decoration *decoration;
85 if (opt->show_source && commit->util)
86 printf("\t%s", (char *) commit->util);
87 if (!opt->show_decorations)
89 decoration = lookup_decoration(&name_decoration, &commit->object);
95 if (decoration->type == DECORATION_REF_TAG)
97 printf("%s", decoration->name);
99 decoration = decoration->next;
105 * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
106 * Signed-off-by: and Acked-by: lines.
108 static int detect_any_signoff(char *letter, int size)
117 while (letter <= --cp && *cp == '\n')
120 while (letter <= cp) {
139 if (('A' <= ch && ch <= 'Z') ||
140 ('a' <= ch && ch <= 'z') ||
145 /* no empty last line doesn't match */
148 return seen_head && seen_name;
151 static void append_signoff(struct strbuf *sb, const char *signoff)
153 static const char signed_off_by[] = "Signed-off-by: ";
154 size_t signoff_len = strlen(signoff);
160 /* First see if we already have the sign-off by the signer */
161 while ((cp = strstr(cp, signed_off_by))) {
165 cp += strlen(signed_off_by);
166 if (cp + signoff_len >= sb->buf + sb->len)
168 if (strncmp(cp, signoff, signoff_len))
170 if (!isspace(cp[signoff_len]))
172 /* we already have him */
177 has_signoff = detect_any_signoff(sb->buf, sb->len);
180 strbuf_addch(sb, '\n');
182 strbuf_addstr(sb, signed_off_by);
183 strbuf_add(sb, signoff, signoff_len);
184 strbuf_addch(sb, '\n');
187 static unsigned int digits_in_number(unsigned int number)
189 unsigned int i = 10, result = 1;
190 while (i <= number) {
197 void get_patch_filename(struct commit *commit, int nr, const char *suffix,
200 int suffix_len = strlen(suffix) + 1;
201 int start_len = buf->len;
203 strbuf_addf(buf, commit ? "%04d-" : "%d", nr);
205 int max_len = start_len + FORMAT_PATCH_NAME_MAX - suffix_len;
206 struct pretty_print_context ctx = {0};
207 ctx.date_mode = DATE_NORMAL;
209 format_commit_message(commit, "%f", buf, &ctx);
210 if (max_len < buf->len)
211 strbuf_setlen(buf, max_len);
212 strbuf_addstr(buf, suffix);
216 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
217 const char **subject_p,
218 const char **extra_headers_p,
219 int *need_8bit_cte_p)
221 const char *subject = NULL;
222 const char *extra_headers = opt->extra_headers;
223 const char *name = sha1_to_hex(commit->object.sha1);
225 *need_8bit_cte_p = 0; /* unknown */
226 if (opt->total > 0) {
227 static char buffer[64];
228 snprintf(buffer, sizeof(buffer),
229 "Subject: [%s %0*d/%d] ",
231 digits_in_number(opt->total),
232 opt->nr, opt->total);
234 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
235 static char buffer[256];
236 snprintf(buffer, sizeof(buffer),
238 opt->subject_prefix);
241 subject = "Subject: ";
244 printf("From %s Mon Sep 17 00:00:00 2001\n", name);
245 graph_show_oneline(opt->graph);
246 if (opt->message_id) {
247 printf("Message-Id: <%s>\n", opt->message_id);
248 graph_show_oneline(opt->graph);
250 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
252 n = opt->ref_message_ids->nr;
253 printf("In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
254 for (i = 0; i < n; i++)
255 printf("%s<%s>\n", (i > 0 ? "\t" : "References: "),
256 opt->ref_message_ids->items[i].string);
257 graph_show_oneline(opt->graph);
259 if (opt->mime_boundary) {
260 static char subject_buffer[1024];
261 static char buffer[1024];
262 struct strbuf filename = STRBUF_INIT;
263 *need_8bit_cte_p = -1; /* NEVER */
264 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
266 "MIME-Version: 1.0\n"
267 "Content-Type: multipart/mixed;"
268 " boundary=\"%s%s\"\n"
270 "This is a multi-part message in MIME "
273 "Content-Type: text/plain; "
274 "charset=UTF-8; format=fixed\n"
275 "Content-Transfer-Encoding: 8bit\n\n",
276 extra_headers ? extra_headers : "",
277 mime_boundary_leader, opt->mime_boundary,
278 mime_boundary_leader, opt->mime_boundary);
279 extra_headers = subject_buffer;
281 get_patch_filename(opt->numbered_files ? NULL : commit, opt->nr,
282 opt->patch_suffix, &filename);
283 snprintf(buffer, sizeof(buffer) - 1,
285 "Content-Type: text/x-patch;"
287 "Content-Transfer-Encoding: 8bit\n"
288 "Content-Disposition: %s;"
289 " filename=\"%s\"\n\n",
290 mime_boundary_leader, opt->mime_boundary,
292 opt->no_inline ? "attachment" : "inline",
294 opt->diffopt.stat_sep = buffer;
295 strbuf_release(&filename);
297 *subject_p = subject;
298 *extra_headers_p = extra_headers;
301 void show_log(struct rev_info *opt)
303 struct strbuf msgbuf = STRBUF_INIT;
304 struct log_info *log = opt->loginfo;
305 struct commit *commit = log->commit, *parent = log->parent;
306 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
307 const char *extra_headers = opt->extra_headers;
308 struct pretty_print_context ctx = {0};
311 ctx.show_notes = opt->show_notes;
312 if (!opt->verbose_header) {
313 graph_show_commit(opt->graph);
316 if (commit->object.flags & BOUNDARY)
318 else if (commit->object.flags & UNINTERESTING)
320 else if (opt->left_right) {
321 if (commit->object.flags & SYMMETRIC_LEFT)
327 fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
328 if (opt->print_parents)
329 show_parents(commit, abbrev_commit);
330 show_decorations(opt, commit);
331 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
333 graph_show_remainder(opt->graph);
335 putchar(opt->diffopt.line_termination);
340 * If use_terminator is set, we already handled any record termination
341 * at the end of the last record.
342 * Otherwise, add a diffopt.line_termination character before all
343 * entries but the first. (IOW, as a separator between entries)
345 if (opt->shown_one && !opt->use_terminator) {
347 * If entries are separated by a newline, the output
348 * should look human-readable. If the last entry ended
349 * with a newline, print the graph output before this
350 * newline. Otherwise it will end up as a completely blank
351 * line and will look like a gap in the graph.
353 * If the entry separator is not a newline, the output is
354 * primarily intended for programmatic consumption, and we
355 * never want the extra graph output before the entry
358 if (opt->diffopt.line_termination == '\n' &&
359 !opt->missing_newline)
360 graph_show_padding(opt->graph);
361 putchar(opt->diffopt.line_termination);
366 * If the history graph was requested,
367 * print the graph, up to this commit's line
369 graph_show_commit(opt->graph);
372 * Print header line of header..
375 if (opt->commit_format == CMIT_FMT_EMAIL) {
376 log_write_email_headers(opt, commit, &ctx.subject, &extra_headers,
378 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
379 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
380 if (opt->commit_format != CMIT_FMT_ONELINE)
381 fputs("commit ", stdout);
384 if (commit->object.flags & BOUNDARY)
386 else if (commit->object.flags & UNINTERESTING)
388 else if (opt->left_right) {
389 if (commit->object.flags & SYMMETRIC_LEFT)
395 fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit),
397 if (opt->print_parents)
398 show_parents(commit, abbrev_commit);
401 find_unique_abbrev(parent->object.sha1,
403 show_decorations(opt, commit);
404 printf("%s", diff_get_color_opt(&opt->diffopt, DIFF_RESET));
405 if (opt->commit_format == CMIT_FMT_ONELINE) {
409 graph_show_oneline(opt->graph);
411 if (opt->reflog_info) {
413 * setup_revisions() ensures that opt->reflog_info
414 * and opt->graph cannot both be set,
415 * so we don't need to worry about printing the
418 show_reflog_message(opt->reflog_info,
419 opt->commit_format == CMIT_FMT_ONELINE,
420 opt->date_mode_explicit ?
423 if (opt->commit_format == CMIT_FMT_ONELINE)
432 * And then the pretty-printed message itself
434 if (ctx.need_8bit_cte >= 0)
435 ctx.need_8bit_cte = has_non_ascii(opt->add_signoff);
436 ctx.date_mode = opt->date_mode;
437 ctx.abbrev = opt->diffopt.abbrev;
438 ctx.after_subject = extra_headers;
439 ctx.reflog_info = opt->reflog_info;
440 pretty_print_commit(opt->commit_format, commit, &msgbuf, &ctx);
442 if (opt->add_signoff)
443 append_signoff(&msgbuf, opt->add_signoff);
444 if (opt->show_log_size) {
445 printf("log size %i\n", (int)msgbuf.len);
446 graph_show_oneline(opt->graph);
450 * Set opt->missing_newline if msgbuf doesn't
451 * end in a newline (including if it is empty)
453 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
454 opt->missing_newline = 1;
456 opt->missing_newline = 0;
459 graph_show_commit_msg(opt->graph, &msgbuf);
461 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
462 if (opt->use_terminator) {
463 if (!opt->missing_newline)
464 graph_show_padding(opt->graph);
468 strbuf_release(&msgbuf);
471 int log_tree_diff_flush(struct rev_info *opt)
473 diffcore_std(&opt->diffopt);
475 if (diff_queue_is_empty()) {
476 int saved_fmt = opt->diffopt.output_format;
477 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
478 diff_flush(&opt->diffopt);
479 opt->diffopt.output_format = saved_fmt;
483 if (opt->loginfo && !opt->no_commit_id) {
484 /* When showing a verbose header (i.e. log message),
485 * and not in --pretty=oneline format, we would want
486 * an extra newline between the end of log and the
487 * output for readability.
490 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
491 opt->verbose_header &&
492 opt->commit_format != CMIT_FMT_ONELINE) {
493 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
494 if ((pch & opt->diffopt.output_format) == pch)
496 if (opt->diffopt.output_prefix) {
497 struct strbuf *msg = NULL;
498 msg = opt->diffopt.output_prefix(&opt->diffopt,
499 opt->diffopt.output_prefix_data);
500 fwrite(msg->buf, msg->len, 1, stdout);
505 diff_flush(&opt->diffopt);
509 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
511 unsigned const char *sha1 = commit->object.sha1;
513 diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
514 return !opt->loginfo;
518 * Show the diff of a commit.
520 * Return true if we printed any log info messages
522 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
525 struct commit_list *parents;
526 unsigned const char *sha1 = commit->object.sha1;
528 if (!opt->diff && !DIFF_OPT_TST(&opt->diffopt, EXIT_WITH_STATUS))
532 parents = commit->parents;
534 if (opt->show_root_diff) {
535 diff_root_tree_sha1(sha1, "", &opt->diffopt);
536 log_tree_diff_flush(opt);
538 return !opt->loginfo;
541 /* More than one parent? */
542 if (parents && parents->next) {
543 if (opt->ignore_merges)
545 else if (opt->combine_merges)
546 return do_diff_combined(opt, commit);
547 else if (opt->first_parent_only) {
549 * Generate merge log entry only for the first
550 * parent, showing summary diff of the others
553 diff_tree_sha1(parents->item->object.sha1, sha1, "", &opt->diffopt);
554 log_tree_diff_flush(opt);
555 return !opt->loginfo;
558 /* If we show individual diffs, show the parent info */
559 log->parent = parents->item;
564 struct commit *parent = parents->item;
566 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
567 log_tree_diff_flush(opt);
569 showed_log |= !opt->loginfo;
571 /* Set up the log info for the next parent, if any.. */
572 parents = parents->next;
575 log->parent = parents->item;
581 int log_tree_commit(struct rev_info *opt, struct commit *commit)
590 shown = log_tree_diff(opt, commit, &log);
591 if (!shown && opt->loginfo && opt->always_show_header) {
597 maybe_flush_or_die(stdout, "stdout");