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 int append_signoff(char *buf, int buf_sz, int at, const char *signoff)
84 static const char signed_off_by[] = "Signed-off-by: ";
85 int signoff_len = strlen(signoff);
89 /* Do we have enough space to add it? */
90 if (buf_sz - at <= strlen(signed_off_by) + signoff_len + 3)
93 /* First see if we already have the sign-off by the signer */
94 while ((cp = strstr(cp, signed_off_by))) {
98 cp += strlen(signed_off_by);
99 if (cp + signoff_len >= buf + at)
101 if (strncmp(cp, signoff, signoff_len))
103 if (!isspace(cp[signoff_len]))
105 /* we already have him */
110 has_signoff = detect_any_signoff(buf, at);
115 strcpy(buf + at, signed_off_by);
116 at += strlen(signed_off_by);
117 strcpy(buf + at, signoff);
124 static unsigned int digits_in_number(unsigned int number)
126 unsigned int i = 10, result = 1;
127 while (i <= number) {
134 void show_log(struct rev_info *opt, const char *sep)
136 static char this_header[16384];
137 struct log_info *log = opt->loginfo;
138 struct commit *commit = log->commit, *parent = log->parent;
139 int abbrev = opt->diffopt.abbrev;
140 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
143 const char *subject = NULL, *extra_headers = opt->extra_headers;
146 if (!opt->verbose_header) {
147 if (opt->left_right) {
148 if (commit->object.flags & BOUNDARY)
150 else if (commit->object.flags & SYMMETRIC_LEFT)
155 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
157 show_parents(commit, abbrev_commit);
158 show_decorations(commit);
159 putchar(opt->diffopt.line_termination);
164 * The "oneline" format has several special cases:
165 * - The pretty-printed commit lacks a newline at the end
166 * of the buffer, but we do want to make sure that we
167 * have a newline there. If the separator isn't already
168 * a newline, add an extra one.
169 * - unlike other log messages, the one-line format does
170 * not have an empty line between entries.
173 if (*sep != '\n' && opt->commit_format == CMIT_FMT_ONELINE)
175 if (opt->shown_one && opt->commit_format != CMIT_FMT_ONELINE)
176 putchar(opt->diffopt.line_termination);
180 * Print header line of header..
183 if (opt->commit_format == CMIT_FMT_EMAIL) {
184 char *sha1 = sha1_to_hex(commit->object.sha1);
185 if (opt->total > 0) {
186 static char buffer[64];
187 snprintf(buffer, sizeof(buffer),
188 "Subject: [%s %0*d/%d] ",
190 digits_in_number(opt->total),
191 opt->nr, opt->total);
193 } else if (opt->total == 0) {
194 static char buffer[256];
195 snprintf(buffer, sizeof(buffer),
197 opt->subject_prefix);
200 subject = "Subject: ";
203 printf("From %s Mon Sep 17 00:00:00 2001\n", sha1);
205 printf("Message-Id: <%s>\n", opt->message_id);
206 if (opt->ref_message_id)
207 printf("In-Reply-To: <%s>\nReferences: <%s>\n",
208 opt->ref_message_id, opt->ref_message_id);
209 if (opt->mime_boundary) {
210 static char subject_buffer[1024];
211 static char buffer[1024];
212 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
214 "MIME-Version: 1.0\n"
215 "Content-Type: multipart/mixed;"
216 " boundary=\"%s%s\"\n"
218 "This is a multi-part message in MIME "
221 "Content-Type: text/plain; "
222 "charset=UTF-8; format=fixed\n"
223 "Content-Transfer-Encoding: 8bit\n\n",
224 extra_headers ? extra_headers : "",
225 mime_boundary_leader, opt->mime_boundary,
226 mime_boundary_leader, opt->mime_boundary);
227 extra_headers = subject_buffer;
229 snprintf(buffer, sizeof(buffer) - 1,
231 "Content-Type: text/x-patch;"
232 " name=\"%s.diff\"\n"
233 "Content-Transfer-Encoding: 8bit\n"
234 "Content-Disposition: %s;"
235 " filename=\"%s.diff\"\n\n",
236 mime_boundary_leader, opt->mime_boundary,
238 opt->no_inline ? "attachment" : "inline",
240 opt->diffopt.stat_sep = buffer;
242 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
243 fputs(diff_get_color(opt->diffopt.color_diff, DIFF_COMMIT),
245 if (opt->commit_format != CMIT_FMT_ONELINE)
246 fputs("commit ", stdout);
247 if (commit->object.flags & BOUNDARY)
249 else if (opt->left_right) {
250 if (commit->object.flags & SYMMETRIC_LEFT)
255 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit),
258 show_parents(commit, abbrev_commit);
261 diff_unique_abbrev(parent->object.sha1,
263 show_decorations(commit);
265 diff_get_color(opt->diffopt.color_diff, DIFF_RESET));
266 putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n');
267 if (opt->reflog_info) {
268 show_reflog_message(opt->reflog_info,
269 opt->commit_format == CMIT_FMT_ONELINE,
271 if (opt->commit_format == CMIT_FMT_ONELINE) {
279 * And then the pretty-printed message itself
281 len = pretty_print_commit(opt->commit_format, commit, ~0u, this_header,
282 sizeof(this_header), abbrev, subject,
283 extra_headers, opt->date_mode);
285 if (opt->add_signoff)
286 len = append_signoff(this_header, sizeof(this_header), len,
288 printf("%s%s%s", this_header, extra, sep);
291 int log_tree_diff_flush(struct rev_info *opt)
293 diffcore_std(&opt->diffopt);
295 if (diff_queue_is_empty()) {
296 int saved_fmt = opt->diffopt.output_format;
297 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
298 diff_flush(&opt->diffopt);
299 opt->diffopt.output_format = saved_fmt;
303 if (opt->loginfo && !opt->no_commit_id) {
304 /* When showing a verbose header (i.e. log message),
305 * and not in --pretty=oneline format, we would want
306 * an extra newline between the end of log and the
307 * output for readability.
309 show_log(opt, opt->diffopt.msg_sep);
310 if (opt->verbose_header &&
311 opt->commit_format != CMIT_FMT_ONELINE) {
312 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
313 if ((pch & opt->diffopt.output_format) == pch)
318 diff_flush(&opt->diffopt);
322 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
324 unsigned const char *sha1 = commit->object.sha1;
326 diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
327 return !opt->loginfo;
331 * Show the diff of a commit.
333 * Return true if we printed any log info messages
335 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
338 struct commit_list *parents;
339 unsigned const char *sha1 = commit->object.sha1;
345 parents = commit->parents;
347 if (opt->show_root_diff) {
348 diff_root_tree_sha1(sha1, "", &opt->diffopt);
349 log_tree_diff_flush(opt);
351 return !opt->loginfo;
354 /* More than one parent? */
355 if (parents && parents->next) {
356 if (opt->ignore_merges)
358 else if (opt->combine_merges)
359 return do_diff_combined(opt, commit);
361 /* If we show individual diffs, show the parent info */
362 log->parent = parents->item;
367 struct commit *parent = parents->item;
369 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
370 log_tree_diff_flush(opt);
372 showed_log |= !opt->loginfo;
374 /* Set up the log info for the next parent, if any.. */
375 parents = parents->next;
378 log->parent = parents->item;
384 int log_tree_commit(struct rev_info *opt, struct commit *commit)
393 shown = log_tree_diff(opt, commit, &log);
394 if (!shown && opt->loginfo && opt->always_show_header) {