7 #include "reflog-walk.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);
28 add_name_decoration("", refname, obj);
29 while (obj->type == OBJ_TAG) {
30 obj = ((struct tag *)obj)->tagged;
33 add_name_decoration("tag: ", refname, obj);
38 void load_ref_decorations(void)
43 for_each_ref(add_ref_decoration, NULL);
47 static void show_parents(struct commit *commit, int abbrev)
49 struct commit_list *p;
50 for (p = commit->parents; p ; p = p->next) {
51 struct commit *parent = p->item;
52 printf(" %s", find_unique_abbrev(parent->object.sha1, abbrev));
56 void show_decorations(struct rev_info *opt, struct commit *commit)
59 struct name_decoration *decoration;
61 if (opt->show_source && commit->util)
62 printf("\t%s", (char *) commit->util);
63 if (!opt->show_decorations)
65 decoration = lookup_decoration(&name_decoration, &commit->object);
70 printf("%s%s", prefix, decoration->name);
72 decoration = decoration->next;
78 * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
79 * Signed-off-by: and Acked-by: lines.
81 static int detect_any_signoff(char *letter, int size)
90 while (letter <= --cp && *cp == '\n')
93 while (letter <= cp) {
112 if (('A' <= ch && ch <= 'Z') ||
113 ('a' <= ch && ch <= 'z') ||
118 /* no empty last line doesn't match */
121 return seen_head && seen_name;
124 static void append_signoff(struct strbuf *sb, const char *signoff)
126 static const char signed_off_by[] = "Signed-off-by: ";
127 size_t signoff_len = strlen(signoff);
133 /* First see if we already have the sign-off by the signer */
134 while ((cp = strstr(cp, signed_off_by))) {
138 cp += strlen(signed_off_by);
139 if (cp + signoff_len >= sb->buf + sb->len)
141 if (strncmp(cp, signoff, signoff_len))
143 if (!isspace(cp[signoff_len]))
145 /* we already have him */
150 has_signoff = detect_any_signoff(sb->buf, sb->len);
153 strbuf_addch(sb, '\n');
155 strbuf_addstr(sb, signed_off_by);
156 strbuf_add(sb, signoff, signoff_len);
157 strbuf_addch(sb, '\n');
160 static unsigned int digits_in_number(unsigned int number)
162 unsigned int i = 10, result = 1;
163 while (i <= number) {
170 static int has_non_ascii(const char *s)
175 while ((ch = *s++) != '\0') {
182 void get_patch_filename(struct commit *commit, int nr, const char *suffix,
185 int suffix_len = strlen(suffix) + 1;
186 int start_len = buf->len;
188 strbuf_addf(buf, commit ? "%04d-" : "%d", nr);
190 format_commit_message(commit, "%f", buf, DATE_NORMAL);
192 * Replace characters at the end with the suffix if the
193 * filename is too long
195 if (buf->len + suffix_len > FORMAT_PATCH_NAME_MAX + start_len)
197 start_len + FORMAT_PATCH_NAME_MAX - suffix_len,
198 suffix_len, suffix, suffix_len);
200 strbuf_addstr(buf, suffix);
204 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
205 const char **subject_p,
206 const char **extra_headers_p,
207 int *need_8bit_cte_p)
209 const char *subject = NULL;
210 const char *extra_headers = opt->extra_headers;
211 const char *name = sha1_to_hex(commit->object.sha1);
213 *need_8bit_cte_p = 0; /* unknown */
214 if (opt->total > 0) {
215 static char buffer[64];
216 snprintf(buffer, sizeof(buffer),
217 "Subject: [%s %0*d/%d] ",
219 digits_in_number(opt->total),
220 opt->nr, opt->total);
222 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
223 static char buffer[256];
224 snprintf(buffer, sizeof(buffer),
226 opt->subject_prefix);
229 subject = "Subject: ";
232 printf("From %s Mon Sep 17 00:00:00 2001\n", name);
233 graph_show_oneline(opt->graph);
234 if (opt->message_id) {
235 printf("Message-Id: <%s>\n", opt->message_id);
236 graph_show_oneline(opt->graph);
238 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
240 n = opt->ref_message_ids->nr;
241 printf("In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
242 for (i = 0; i < n; i++)
243 printf("%s<%s>\n", (i > 0 ? "\t" : "References: "),
244 opt->ref_message_ids->items[i].string);
245 graph_show_oneline(opt->graph);
247 if (opt->mime_boundary) {
248 static char subject_buffer[1024];
249 static char buffer[1024];
250 struct strbuf filename = STRBUF_INIT;
251 *need_8bit_cte_p = -1; /* NEVER */
252 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
254 "MIME-Version: 1.0\n"
255 "Content-Type: multipart/mixed;"
256 " boundary=\"%s%s\"\n"
258 "This is a multi-part message in MIME "
261 "Content-Type: text/plain; "
262 "charset=UTF-8; format=fixed\n"
263 "Content-Transfer-Encoding: 8bit\n\n",
264 extra_headers ? extra_headers : "",
265 mime_boundary_leader, opt->mime_boundary,
266 mime_boundary_leader, opt->mime_boundary);
267 extra_headers = subject_buffer;
269 get_patch_filename(opt->numbered_files ? NULL : commit, opt->nr,
270 opt->patch_suffix, &filename);
271 snprintf(buffer, sizeof(buffer) - 1,
273 "Content-Type: text/x-patch;"
275 "Content-Transfer-Encoding: 8bit\n"
276 "Content-Disposition: %s;"
277 " filename=\"%s\"\n\n",
278 mime_boundary_leader, opt->mime_boundary,
280 opt->no_inline ? "attachment" : "inline",
282 opt->diffopt.stat_sep = buffer;
283 strbuf_release(&filename);
285 *subject_p = subject;
286 *extra_headers_p = extra_headers;
289 void show_log(struct rev_info *opt)
291 struct strbuf msgbuf = STRBUF_INIT;
292 struct log_info *log = opt->loginfo;
293 struct commit *commit = log->commit, *parent = log->parent;
294 int abbrev = opt->diffopt.abbrev;
295 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
296 const char *subject = NULL, *extra_headers = opt->extra_headers;
297 int need_8bit_cte = 0;
300 if (!opt->verbose_header) {
301 graph_show_commit(opt->graph);
304 if (commit->object.flags & BOUNDARY)
306 else if (commit->object.flags & UNINTERESTING)
308 else if (opt->left_right) {
309 if (commit->object.flags & SYMMETRIC_LEFT)
315 fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
316 if (opt->print_parents)
317 show_parents(commit, abbrev_commit);
318 show_decorations(opt, commit);
319 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
321 graph_show_remainder(opt->graph);
323 putchar(opt->diffopt.line_termination);
328 * If use_terminator is set, add a newline at the end of the entry.
329 * Otherwise, add a diffopt.line_termination character before all
330 * entries but the first. (IOW, as a separator between entries)
332 if (opt->shown_one && !opt->use_terminator) {
334 * If entries are separated by a newline, the output
335 * should look human-readable. If the last entry ended
336 * with a newline, print the graph output before this
337 * newline. Otherwise it will end up as a completely blank
338 * line and will look like a gap in the graph.
340 * If the entry separator is not a newline, the output is
341 * primarily intended for programmatic consumption, and we
342 * never want the extra graph output before the entry
345 if (opt->diffopt.line_termination == '\n' &&
346 !opt->missing_newline)
347 graph_show_padding(opt->graph);
348 putchar(opt->diffopt.line_termination);
353 * If the history graph was requested,
354 * print the graph, up to this commit's line
356 graph_show_commit(opt->graph);
359 * Print header line of header..
362 if (opt->commit_format == CMIT_FMT_EMAIL) {
363 log_write_email_headers(opt, commit, &subject, &extra_headers,
365 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
366 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
367 if (opt->commit_format != CMIT_FMT_ONELINE)
368 fputs("commit ", stdout);
371 if (commit->object.flags & BOUNDARY)
373 else if (commit->object.flags & UNINTERESTING)
375 else if (opt->left_right) {
376 if (commit->object.flags & SYMMETRIC_LEFT)
382 fputs(find_unique_abbrev(commit->object.sha1, abbrev_commit),
384 if (opt->print_parents)
385 show_parents(commit, abbrev_commit);
388 find_unique_abbrev(parent->object.sha1,
390 show_decorations(opt, commit);
391 printf("%s", diff_get_color_opt(&opt->diffopt, DIFF_RESET));
392 if (opt->commit_format == CMIT_FMT_ONELINE) {
396 graph_show_oneline(opt->graph);
398 if (opt->reflog_info) {
400 * setup_revisions() ensures that opt->reflog_info
401 * and opt->graph cannot both be set,
402 * so we don't need to worry about printing the
405 show_reflog_message(opt->reflog_info,
406 opt->commit_format == CMIT_FMT_ONELINE,
408 if (opt->commit_format == CMIT_FMT_ONELINE)
417 * And then the pretty-printed message itself
419 if (need_8bit_cte >= 0)
420 need_8bit_cte = has_non_ascii(opt->add_signoff);
421 pretty_print_commit(opt->commit_format, commit, &msgbuf,
422 abbrev, subject, extra_headers, opt->date_mode,
425 if (opt->add_signoff)
426 append_signoff(&msgbuf, opt->add_signoff);
427 if (opt->show_log_size) {
428 printf("log size %i\n", (int)msgbuf.len);
429 graph_show_oneline(opt->graph);
433 * Set opt->missing_newline if msgbuf doesn't
434 * end in a newline (including if it is empty)
436 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
437 opt->missing_newline = 1;
439 opt->missing_newline = 0;
442 graph_show_commit_msg(opt->graph, &msgbuf);
444 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
445 if (opt->use_terminator) {
446 if (!opt->missing_newline)
447 graph_show_padding(opt->graph);
451 strbuf_release(&msgbuf);
454 int log_tree_diff_flush(struct rev_info *opt)
456 diffcore_std(&opt->diffopt);
458 if (diff_queue_is_empty()) {
459 int saved_fmt = opt->diffopt.output_format;
460 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
461 diff_flush(&opt->diffopt);
462 opt->diffopt.output_format = saved_fmt;
466 if (opt->loginfo && !opt->no_commit_id) {
467 /* When showing a verbose header (i.e. log message),
468 * and not in --pretty=oneline format, we would want
469 * an extra newline between the end of log and the
470 * output for readability.
473 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
474 opt->verbose_header &&
475 opt->commit_format != CMIT_FMT_ONELINE) {
476 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
477 if ((pch & opt->diffopt.output_format) == pch)
482 diff_flush(&opt->diffopt);
486 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
488 unsigned const char *sha1 = commit->object.sha1;
490 diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
491 return !opt->loginfo;
495 * Show the diff of a commit.
497 * Return true if we printed any log info messages
499 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
502 struct commit_list *parents;
503 unsigned const char *sha1 = commit->object.sha1;
505 if (!opt->diff && !DIFF_OPT_TST(&opt->diffopt, EXIT_WITH_STATUS))
509 parents = commit->parents;
511 if (opt->show_root_diff) {
512 diff_root_tree_sha1(sha1, "", &opt->diffopt);
513 log_tree_diff_flush(opt);
515 return !opt->loginfo;
518 /* More than one parent? */
519 if (parents && parents->next) {
520 if (opt->ignore_merges)
522 else if (opt->combine_merges)
523 return do_diff_combined(opt, commit);
525 /* If we show individual diffs, show the parent info */
526 log->parent = parents->item;
531 struct commit *parent = parents->item;
533 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
534 log_tree_diff_flush(opt);
536 showed_log |= !opt->loginfo;
538 /* Set up the log info for the next parent, if any.. */
539 parents = parents->next;
542 log->parent = parents->item;
548 int log_tree_commit(struct rev_info *opt, struct commit *commit)
557 shown = log_tree_diff(opt, commit, &log);
558 if (!shown && opt->loginfo && opt->always_show_header) {
564 maybe_flush_or_die(stdout, "stdout");