5 #include "reflog-walk.h"
7 struct decoration name_decoration = { "object names" };
9 static void show_parents(struct commit *commit, int abbrev)
11 struct commit_list *p;
12 for (p = commit->parents; p ; p = p->next) {
13 struct commit *parent = p->item;
14 printf(" %s", diff_unique_abbrev(parent->object.sha1, abbrev));
18 static void show_decorations(struct commit *commit)
21 struct name_decoration *decoration;
23 decoration = lookup_decoration(&name_decoration, &commit->object);
28 printf("%s%s", prefix, decoration->name);
30 decoration = decoration->next;
36 * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
37 * Signed-off-by: and Acked-by: lines.
39 static int detect_any_signoff(char *letter, int size)
48 while (letter <= --cp && (ch = *cp) == '\n')
51 while (letter <= cp) {
70 if (('A' <= ch && ch <= 'Z') ||
71 ('a' <= ch && ch <= 'z') ||
76 /* no empty last line doesn't match */
79 return seen_head && seen_name;
82 static unsigned long append_signoff(char **buf_p, unsigned long *buf_sz_p,
83 unsigned long at, const char *signoff)
85 static const char signed_off_by[] = "Signed-off-by: ";
86 size_t signoff_len = strlen(signoff);
94 if (buf_sz <= at + strlen(signed_off_by) + signoff_len + 3) {
95 buf_sz += strlen(signed_off_by) + signoff_len + 3;
96 buf = xrealloc(buf, buf_sz);
102 /* First see if we already have the sign-off by the signer */
103 while ((cp = strstr(cp, signed_off_by))) {
107 cp += strlen(signed_off_by);
108 if (cp + signoff_len >= buf + at)
110 if (strncmp(cp, signoff, signoff_len))
112 if (!isspace(cp[signoff_len]))
114 /* we already have him */
119 has_signoff = detect_any_signoff(buf, at);
124 strcpy(buf + at, signed_off_by);
125 at += strlen(signed_off_by);
126 strcpy(buf + at, signoff);
133 static unsigned int digits_in_number(unsigned int number)
135 unsigned int i = 10, result = 1;
136 while (i <= number) {
143 void show_log(struct rev_info *opt, const char *sep)
146 unsigned long msgbuf_len = 0;
147 struct log_info *log = opt->loginfo;
148 struct commit *commit = log->commit, *parent = log->parent;
149 int abbrev = opt->diffopt.abbrev;
150 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
153 const char *subject = NULL, *extra_headers = opt->extra_headers;
156 if (!opt->verbose_header) {
157 if (opt->left_right) {
158 if (commit->object.flags & BOUNDARY)
160 else if (commit->object.flags & SYMMETRIC_LEFT)
165 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
167 show_parents(commit, abbrev_commit);
168 show_decorations(commit);
169 putchar(opt->diffopt.line_termination);
174 * The "oneline" format has several special cases:
175 * - The pretty-printed commit lacks a newline at the end
176 * of the buffer, but we do want to make sure that we
177 * have a newline there. If the separator isn't already
178 * a newline, add an extra one.
179 * - unlike other log messages, the one-line format does
180 * not have an empty line between entries.
183 if (*sep != '\n' && opt->commit_format == CMIT_FMT_ONELINE)
185 if (opt->shown_one && opt->commit_format != CMIT_FMT_ONELINE)
186 putchar(opt->diffopt.line_termination);
190 * Print header line of header..
193 if (opt->commit_format == CMIT_FMT_EMAIL) {
194 char *sha1 = sha1_to_hex(commit->object.sha1);
195 if (opt->total > 0) {
196 static char buffer[64];
197 snprintf(buffer, sizeof(buffer),
198 "Subject: [%s %0*d/%d] ",
200 digits_in_number(opt->total),
201 opt->nr, opt->total);
203 } else if (opt->total == 0) {
204 static char buffer[256];
205 snprintf(buffer, sizeof(buffer),
207 opt->subject_prefix);
210 subject = "Subject: ";
213 printf("From %s Mon Sep 17 00:00:00 2001\n", sha1);
215 printf("Message-Id: <%s>\n", opt->message_id);
216 if (opt->ref_message_id)
217 printf("In-Reply-To: <%s>\nReferences: <%s>\n",
218 opt->ref_message_id, opt->ref_message_id);
219 if (opt->mime_boundary) {
220 static char subject_buffer[1024];
221 static char buffer[1024];
222 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
224 "MIME-Version: 1.0\n"
225 "Content-Type: multipart/mixed;"
226 " boundary=\"%s%s\"\n"
228 "This is a multi-part message in MIME "
231 "Content-Type: text/plain; "
232 "charset=UTF-8; format=fixed\n"
233 "Content-Transfer-Encoding: 8bit\n\n",
234 extra_headers ? extra_headers : "",
235 mime_boundary_leader, opt->mime_boundary,
236 mime_boundary_leader, opt->mime_boundary);
237 extra_headers = subject_buffer;
239 snprintf(buffer, sizeof(buffer) - 1,
241 "Content-Type: text/x-patch;"
242 " name=\"%s.diff\"\n"
243 "Content-Transfer-Encoding: 8bit\n"
244 "Content-Disposition: %s;"
245 " filename=\"%s.diff\"\n\n",
246 mime_boundary_leader, opt->mime_boundary,
248 opt->no_inline ? "attachment" : "inline",
250 opt->diffopt.stat_sep = buffer;
252 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
253 fputs(diff_get_color(opt->diffopt.color_diff, DIFF_COMMIT),
255 if (opt->commit_format != CMIT_FMT_ONELINE)
256 fputs("commit ", stdout);
257 if (commit->object.flags & BOUNDARY)
259 else if (opt->left_right) {
260 if (commit->object.flags & SYMMETRIC_LEFT)
265 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit),
268 show_parents(commit, abbrev_commit);
271 diff_unique_abbrev(parent->object.sha1,
273 show_decorations(commit);
275 diff_get_color(opt->diffopt.color_diff, DIFF_RESET));
276 putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n');
277 if (opt->reflog_info) {
278 show_reflog_message(opt->reflog_info,
279 opt->commit_format == CMIT_FMT_ONELINE,
281 if (opt->commit_format == CMIT_FMT_ONELINE) {
289 * And then the pretty-printed message itself
291 len = pretty_print_commit(opt->commit_format, commit, ~0u,
292 &msgbuf, &msgbuf_len, abbrev, subject,
293 extra_headers, opt->date_mode);
295 if (opt->add_signoff)
296 len = append_signoff(&msgbuf, &msgbuf_len, len,
298 printf("%s%s%s", msgbuf, extra, sep);
302 int log_tree_diff_flush(struct rev_info *opt)
304 diffcore_std(&opt->diffopt);
306 if (diff_queue_is_empty()) {
307 int saved_fmt = opt->diffopt.output_format;
308 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
309 diff_flush(&opt->diffopt);
310 opt->diffopt.output_format = saved_fmt;
314 if (opt->loginfo && !opt->no_commit_id) {
315 /* When showing a verbose header (i.e. log message),
316 * and not in --pretty=oneline format, we would want
317 * an extra newline between the end of log and the
318 * output for readability.
320 show_log(opt, opt->diffopt.msg_sep);
321 if (opt->verbose_header &&
322 opt->commit_format != CMIT_FMT_ONELINE) {
323 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
324 if ((pch & opt->diffopt.output_format) == pch)
329 diff_flush(&opt->diffopt);
333 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
335 unsigned const char *sha1 = commit->object.sha1;
337 diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
338 return !opt->loginfo;
342 * Show the diff of a commit.
344 * Return true if we printed any log info messages
346 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
349 struct commit_list *parents;
350 unsigned const char *sha1 = commit->object.sha1;
356 parents = commit->parents;
358 if (opt->show_root_diff) {
359 diff_root_tree_sha1(sha1, "", &opt->diffopt);
360 log_tree_diff_flush(opt);
362 return !opt->loginfo;
365 /* More than one parent? */
366 if (parents && parents->next) {
367 if (opt->ignore_merges)
369 else if (opt->combine_merges)
370 return do_diff_combined(opt, commit);
372 /* If we show individual diffs, show the parent info */
373 log->parent = parents->item;
378 struct commit *parent = parents->item;
380 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
381 log_tree_diff_flush(opt);
383 showed_log |= !opt->loginfo;
385 /* Set up the log info for the next parent, if any.. */
386 parents = parents->next;
389 log->parent = parents->item;
395 int log_tree_commit(struct rev_info *opt, struct commit *commit)
404 shown = log_tree_diff(opt, commit, &log);
405 if (!shown && opt->loginfo && opt->always_show_header) {
411 maybe_flush_or_die(stdout, "stdout");