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 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 void append_signoff(struct strbuf *sb, const char *signoff)
84 static const char signed_off_by[] = "Signed-off-by: ";
85 size_t signoff_len = strlen(signoff);
91 /* First see if we already have the sign-off by the signer */
92 while ((cp = strstr(cp, signed_off_by))) {
96 cp += strlen(signed_off_by);
97 if (cp + signoff_len >= sb->buf + sb->len)
99 if (strncmp(cp, signoff, signoff_len))
101 if (!isspace(cp[signoff_len]))
103 /* we already have him */
108 has_signoff = detect_any_signoff(sb->buf, sb->len);
111 strbuf_addch(sb, '\n');
113 strbuf_addstr(sb, signed_off_by);
114 strbuf_add(sb, signoff, signoff_len);
115 strbuf_addch(sb, '\n');
118 static unsigned int digits_in_number(unsigned int number)
120 unsigned int i = 10, result = 1;
121 while (i <= number) {
128 static int has_non_ascii(const char *s)
133 while ((ch = *s++) != '\0') {
140 void log_write_email_headers(struct rev_info *opt, const char *name,
141 const char **subject_p, const char **extra_headers_p)
143 const char *subject = NULL;
144 const char *extra_headers = opt->extra_headers;
145 if (opt->total > 0) {
146 static char buffer[64];
147 snprintf(buffer, sizeof(buffer),
148 "Subject: [%s %0*d/%d] ",
150 digits_in_number(opt->total),
151 opt->nr, opt->total);
153 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
154 static char buffer[256];
155 snprintf(buffer, sizeof(buffer),
157 opt->subject_prefix);
160 subject = "Subject: ";
163 printf("From %s Mon Sep 17 00:00:00 2001\n", name);
165 printf("Message-Id: <%s>\n", opt->message_id);
166 if (opt->ref_message_id)
167 printf("In-Reply-To: <%s>\nReferences: <%s>\n",
168 opt->ref_message_id, opt->ref_message_id);
169 if (opt->mime_boundary) {
170 static char subject_buffer[1024];
171 static char buffer[1024];
172 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
174 "MIME-Version: 1.0\n"
175 "Content-Type: multipart/mixed;"
176 " boundary=\"%s%s\"\n"
178 "This is a multi-part message in MIME "
181 "Content-Type: text/plain; "
182 "charset=UTF-8; format=fixed\n"
183 "Content-Transfer-Encoding: 8bit\n\n",
184 extra_headers ? extra_headers : "",
185 mime_boundary_leader, opt->mime_boundary,
186 mime_boundary_leader, opt->mime_boundary);
187 extra_headers = subject_buffer;
189 snprintf(buffer, sizeof(buffer) - 1,
191 "Content-Type: text/x-patch;"
192 " name=\"%s.diff\"\n"
193 "Content-Transfer-Encoding: 8bit\n"
194 "Content-Disposition: %s;"
195 " filename=\"%s.diff\"\n\n",
196 mime_boundary_leader, opt->mime_boundary,
198 opt->no_inline ? "attachment" : "inline",
200 opt->diffopt.stat_sep = buffer;
202 *subject_p = subject;
203 *extra_headers_p = extra_headers;
206 void show_log(struct rev_info *opt, const char *sep)
208 struct strbuf msgbuf;
209 struct log_info *log = opt->loginfo;
210 struct commit *commit = log->commit, *parent = log->parent;
211 int abbrev = opt->diffopt.abbrev;
212 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
214 const char *subject = NULL, *extra_headers = opt->extra_headers;
217 if (!opt->verbose_header) {
218 if (commit->object.flags & BOUNDARY)
220 else if (commit->object.flags & UNINTERESTING)
222 else if (opt->left_right) {
223 if (commit->object.flags & SYMMETRIC_LEFT)
228 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
230 show_parents(commit, abbrev_commit);
231 show_decorations(commit);
232 putchar(opt->diffopt.line_termination);
237 * The "oneline" format has several special cases:
238 * - The pretty-printed commit lacks a newline at the end
239 * of the buffer, but we do want to make sure that we
240 * have a newline there. If the separator isn't already
241 * a newline, add an extra one.
242 * - unlike other log messages, the one-line format does
243 * not have an empty line between entries.
246 if (*sep != '\n' && opt->commit_format == CMIT_FMT_ONELINE)
248 if (opt->shown_one && opt->commit_format != CMIT_FMT_ONELINE)
249 putchar(opt->diffopt.line_termination);
253 * Print header line of header..
256 if (opt->commit_format == CMIT_FMT_EMAIL) {
257 log_write_email_headers(opt, sha1_to_hex(commit->object.sha1),
258 &subject, &extra_headers);
259 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
260 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
261 if (opt->commit_format != CMIT_FMT_ONELINE)
262 fputs("commit ", stdout);
263 if (commit->object.flags & BOUNDARY)
265 else if (commit->object.flags & UNINTERESTING)
267 else if (opt->left_right) {
268 if (commit->object.flags & SYMMETRIC_LEFT)
273 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit),
276 show_parents(commit, abbrev_commit);
279 diff_unique_abbrev(parent->object.sha1,
281 show_decorations(commit);
282 printf("%s", diff_get_color_opt(&opt->diffopt, DIFF_RESET));
283 putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n');
284 if (opt->reflog_info) {
285 show_reflog_message(opt->reflog_info,
286 opt->commit_format == CMIT_FMT_ONELINE,
288 if (opt->commit_format == CMIT_FMT_ONELINE) {
299 * And then the pretty-printed message itself
301 strbuf_init(&msgbuf, 0);
302 pretty_print_commit(opt->commit_format, commit, &msgbuf,
303 abbrev, subject, extra_headers, opt->date_mode,
304 has_non_ascii(opt->add_signoff));
306 if (opt->add_signoff)
307 append_signoff(&msgbuf, opt->add_signoff);
308 if (opt->show_log_size)
309 printf("log size %i\n", (int)msgbuf.len);
312 printf("%s%s%s", msgbuf.buf, extra, sep);
313 strbuf_release(&msgbuf);
316 int log_tree_diff_flush(struct rev_info *opt)
318 diffcore_std(&opt->diffopt);
320 if (diff_queue_is_empty()) {
321 int saved_fmt = opt->diffopt.output_format;
322 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
323 diff_flush(&opt->diffopt);
324 opt->diffopt.output_format = saved_fmt;
328 if (opt->loginfo && !opt->no_commit_id) {
329 /* When showing a verbose header (i.e. log message),
330 * and not in --pretty=oneline format, we would want
331 * an extra newline between the end of log and the
332 * output for readability.
334 show_log(opt, opt->diffopt.msg_sep);
335 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
336 opt->verbose_header &&
337 opt->commit_format != CMIT_FMT_ONELINE) {
338 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
339 if ((pch & opt->diffopt.output_format) == pch)
344 diff_flush(&opt->diffopt);
348 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
350 unsigned const char *sha1 = commit->object.sha1;
352 diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
353 return !opt->loginfo;
357 * Show the diff of a commit.
359 * Return true if we printed any log info messages
361 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
364 struct commit_list *parents;
365 unsigned const char *sha1 = commit->object.sha1;
371 parents = commit->parents;
373 if (opt->show_root_diff) {
374 diff_root_tree_sha1(sha1, "", &opt->diffopt);
375 log_tree_diff_flush(opt);
377 return !opt->loginfo;
380 /* More than one parent? */
381 if (parents && parents->next) {
382 if (opt->ignore_merges)
384 else if (opt->combine_merges)
385 return do_diff_combined(opt, commit);
387 /* If we show individual diffs, show the parent info */
388 log->parent = parents->item;
393 struct commit *parent = parents->item;
395 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
396 log_tree_diff_flush(opt);
398 showed_log |= !opt->loginfo;
400 /* Set up the log info for the next parent, if any.. */
401 parents = parents->next;
404 log->parent = parents->item;
410 int log_tree_commit(struct rev_info *opt, struct commit *commit)
419 shown = log_tree_diff(opt, commit, &log);
420 if (!shown && opt->loginfo && opt->always_show_header) {
426 maybe_flush_or_die(stdout, "stdout");