5 #include "reflog-walk.h"
7 static void show_parents(struct commit *commit, int abbrev)
10 for (p = commit->parents; p ; p = p->next) {
11 struct commit *parent = p->item;
12 printf(" %s", diff_unique_abbrev(parent->object.sha1, abbrev));
17 * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
18 * Signed-off-by: and Acked-by: lines.
20 static int detect_any_signoff(char *letter, int size)
29 while (letter <= --cp && (ch = *cp) == '\n')
32 while (letter <= cp) {
51 if (('A' <= ch && ch <= 'Z') ||
52 ('a' <= ch && ch <= 'z') ||
57 /* no empty last line doesn't match */
60 return seen_head && seen_name;
63 static int append_signoff(char *buf, int buf_sz, int at, const char *signoff)
65 static const char signed_off_by[] = "Signed-off-by: ";
66 int signoff_len = strlen(signoff);
70 /* Do we have enough space to add it? */
71 if (buf_sz - at <= strlen(signed_off_by) + signoff_len + 3)
74 /* First see if we already have the sign-off by the signer */
75 while ((cp = strstr(cp, signed_off_by))) {
79 cp += strlen(signed_off_by);
80 if (cp + signoff_len >= buf + at)
82 if (strncmp(cp, signoff, signoff_len))
84 if (!isspace(cp[signoff_len]))
86 /* we already have him */
91 has_signoff = detect_any_signoff(buf, at);
96 strcpy(buf + at, signed_off_by);
97 at += strlen(signed_off_by);
98 strcpy(buf + at, signoff);
105 static unsigned int digits_in_number(unsigned int number)
107 unsigned int i = 10, result = 1;
108 while (i <= number) {
115 void show_log(struct rev_info *opt, const char *sep)
117 static char this_header[16384];
118 struct log_info *log = opt->loginfo;
119 struct commit *commit = log->commit, *parent = log->parent;
120 int abbrev = opt->diffopt.abbrev;
121 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
124 const char *subject = NULL, *extra_headers = opt->extra_headers;
127 if (!opt->verbose_header) {
128 if (opt->left_right) {
129 if (commit->object.flags & BOUNDARY)
131 else if (commit->object.flags & SYMMETRIC_LEFT)
136 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
138 show_parents(commit, abbrev_commit);
139 putchar(opt->diffopt.line_termination);
144 * The "oneline" format has several special cases:
145 * - The pretty-printed commit lacks a newline at the end
146 * of the buffer, but we do want to make sure that we
147 * have a newline there. If the separator isn't already
148 * a newline, add an extra one.
149 * - unlike other log messages, the one-line format does
150 * not have an empty line between entries.
153 if (*sep != '\n' && opt->commit_format == CMIT_FMT_ONELINE)
155 if (opt->shown_one && opt->commit_format != CMIT_FMT_ONELINE)
156 putchar(opt->diffopt.line_termination);
160 * Print header line of header..
163 if (opt->commit_format == CMIT_FMT_EMAIL) {
164 char *sha1 = sha1_to_hex(commit->object.sha1);
165 if (opt->total > 0) {
166 static char buffer[64];
167 snprintf(buffer, sizeof(buffer),
168 "Subject: [%s %0*d/%d] ",
170 digits_in_number(opt->total),
171 opt->nr, opt->total);
173 } else if (opt->total == 0) {
174 static char buffer[256];
175 snprintf(buffer, sizeof(buffer),
177 opt->subject_prefix);
180 subject = "Subject: ";
183 printf("From %s Mon Sep 17 00:00:00 2001\n", sha1);
185 printf("Message-Id: <%s>\n", opt->message_id);
186 if (opt->ref_message_id)
187 printf("In-Reply-To: <%s>\nReferences: <%s>\n",
188 opt->ref_message_id, opt->ref_message_id);
189 if (opt->mime_boundary) {
190 static char subject_buffer[1024];
191 static char buffer[1024];
192 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
194 "MIME-Version: 1.0\n"
195 "Content-Type: multipart/mixed;"
196 " boundary=\"%s%s\"\n"
198 "This is a multi-part message in MIME "
201 "Content-Type: text/plain; "
202 "charset=UTF-8; format=fixed\n"
203 "Content-Transfer-Encoding: 8bit\n\n",
204 extra_headers ? extra_headers : "",
205 mime_boundary_leader, opt->mime_boundary,
206 mime_boundary_leader, opt->mime_boundary);
207 extra_headers = subject_buffer;
209 snprintf(buffer, sizeof(buffer) - 1,
211 "Content-Type: text/x-patch;"
212 " name=\"%s.diff\"\n"
213 "Content-Transfer-Encoding: 8bit\n"
214 "Content-Disposition: %s;"
215 " filename=\"%s.diff\"\n\n",
216 mime_boundary_leader, opt->mime_boundary,
218 opt->no_inline ? "attachment" : "inline",
220 opt->diffopt.stat_sep = buffer;
222 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
223 fputs(diff_get_color(opt->diffopt.color_diff, DIFF_COMMIT),
225 if (opt->commit_format != CMIT_FMT_ONELINE)
226 fputs("commit ", stdout);
227 if (opt->left_right) {
228 if (commit->object.flags & BOUNDARY)
230 else if (commit->object.flags & SYMMETRIC_LEFT)
235 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit),
238 show_parents(commit, abbrev_commit);
241 diff_unique_abbrev(parent->object.sha1,
244 diff_get_color(opt->diffopt.color_diff, DIFF_RESET));
245 putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n');
246 if (opt->reflog_info) {
247 show_reflog_message(opt->reflog_info,
248 opt->commit_format == CMIT_FMT_ONELINE,
250 if (opt->commit_format == CMIT_FMT_ONELINE) {
258 * And then the pretty-printed message itself
260 len = pretty_print_commit(opt->commit_format, commit, ~0u, this_header,
261 sizeof(this_header), abbrev, subject,
262 extra_headers, opt->relative_date);
264 if (opt->add_signoff)
265 len = append_signoff(this_header, sizeof(this_header), len,
267 printf("%s%s%s", this_header, extra, sep);
270 int log_tree_diff_flush(struct rev_info *opt)
272 diffcore_std(&opt->diffopt);
274 if (diff_queue_is_empty()) {
275 int saved_fmt = opt->diffopt.output_format;
276 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
277 diff_flush(&opt->diffopt);
278 opt->diffopt.output_format = saved_fmt;
282 if (opt->loginfo && !opt->no_commit_id) {
283 /* When showing a verbose header (i.e. log message),
284 * and not in --pretty=oneline format, we would want
285 * an extra newline between the end of log and the
286 * output for readability.
288 show_log(opt, opt->diffopt.msg_sep);
289 if (opt->verbose_header &&
290 opt->commit_format != CMIT_FMT_ONELINE) {
291 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
292 if ((pch & opt->diffopt.output_format) == pch)
297 diff_flush(&opt->diffopt);
301 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
303 unsigned const char *sha1 = commit->object.sha1;
305 diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
306 return !opt->loginfo;
310 * Show the diff of a commit.
312 * Return true if we printed any log info messages
314 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
317 struct commit_list *parents;
318 unsigned const char *sha1 = commit->object.sha1;
324 parents = commit->parents;
326 if (opt->show_root_diff) {
327 diff_root_tree_sha1(sha1, "", &opt->diffopt);
328 log_tree_diff_flush(opt);
330 return !opt->loginfo;
333 /* More than one parent? */
334 if (parents && parents->next) {
335 if (opt->ignore_merges)
337 else if (opt->combine_merges)
338 return do_diff_combined(opt, commit);
340 /* If we show individual diffs, show the parent info */
341 log->parent = parents->item;
346 struct commit *parent = parents->item;
348 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
349 log_tree_diff_flush(opt);
351 showed_log |= !opt->loginfo;
353 /* Set up the log info for the next parent, if any.. */
354 parents = parents->next;
357 log->parent = parents->item;
363 int log_tree_commit(struct rev_info *opt, struct commit *commit)
372 shown = log_tree_diff(opt, commit, &log);
373 if (!shown && opt->loginfo && opt->always_show_header) {