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 show_log(struct rev_info
*opt
, const char *sep
)
142 struct strbuf msgbuf
;
143 struct log_info
*log
= opt
->loginfo
;
144 struct commit
*commit
= log
->commit
, *parent
= log
->parent
;
145 int abbrev
= opt
->diffopt
.abbrev
;
146 int abbrev_commit
= opt
->abbrev_commit
? opt
->abbrev
: 40;
148 const char *subject
= NULL
, *extra_headers
= opt
->extra_headers
;
151 if (!opt
->verbose_header
) {
152 if (commit
->object
.flags
& BOUNDARY
)
154 else if (commit
->object
.flags
& UNINTERESTING
)
156 else if (opt
->left_right
) {
157 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
162 fputs(diff_unique_abbrev(commit
->object
.sha1
, abbrev_commit
), stdout
);
164 show_parents(commit
, abbrev_commit
);
165 show_decorations(commit
);
166 putchar(opt
->diffopt
.line_termination
);
171 * The "oneline" format has several special cases:
172 * - The pretty-printed commit lacks a newline at the end
173 * of the buffer, but we do want to make sure that we
174 * have a newline there. If the separator isn't already
175 * a newline, add an extra one.
176 * - unlike other log messages, the one-line format does
177 * not have an empty line between entries.
180 if (*sep
!= '\n' && opt
->commit_format
== CMIT_FMT_ONELINE
)
182 if (opt
->shown_one
&& opt
->commit_format
!= CMIT_FMT_ONELINE
)
183 putchar(opt
->diffopt
.line_termination
);
187 * Print header line of header..
190 if (opt
->commit_format
== CMIT_FMT_EMAIL
) {
191 char *sha1
= sha1_to_hex(commit
->object
.sha1
);
192 if (opt
->total
> 0) {
193 static char buffer
[64];
194 snprintf(buffer
, sizeof(buffer
),
195 "Subject: [%s %0*d/%d] ",
197 digits_in_number(opt
->total
),
198 opt
->nr
, opt
->total
);
200 } else if (opt
->total
== 0 && opt
->subject_prefix
&& *opt
->subject_prefix
) {
201 static char buffer
[256];
202 snprintf(buffer
, sizeof(buffer
),
204 opt
->subject_prefix
);
207 subject
= "Subject: ";
210 printf("From %s Mon Sep 17 00:00:00 2001\n", sha1
);
212 printf("Message-Id: <%s>\n", opt
->message_id
);
213 if (opt
->ref_message_id
)
214 printf("In-Reply-To: <%s>\nReferences: <%s>\n",
215 opt
->ref_message_id
, opt
->ref_message_id
);
216 if (opt
->mime_boundary
) {
217 static char subject_buffer
[1024];
218 static char buffer
[1024];
219 snprintf(subject_buffer
, sizeof(subject_buffer
) - 1,
221 "MIME-Version: 1.0\n"
222 "Content-Type: multipart/mixed;"
223 " boundary=\"%s%s\"\n"
225 "This is a multi-part message in MIME "
228 "Content-Type: text/plain; "
229 "charset=UTF-8; format=fixed\n"
230 "Content-Transfer-Encoding: 8bit\n\n",
231 extra_headers
? extra_headers
: "",
232 mime_boundary_leader
, opt
->mime_boundary
,
233 mime_boundary_leader
, opt
->mime_boundary
);
234 extra_headers
= subject_buffer
;
236 snprintf(buffer
, sizeof(buffer
) - 1,
238 "Content-Type: text/x-patch;"
239 " name=\"%s.diff\"\n"
240 "Content-Transfer-Encoding: 8bit\n"
241 "Content-Disposition: %s;"
242 " filename=\"%s.diff\"\n\n",
243 mime_boundary_leader
, opt
->mime_boundary
,
245 opt
->no_inline
? "attachment" : "inline",
247 opt
->diffopt
.stat_sep
= buffer
;
249 } else if (opt
->commit_format
!= CMIT_FMT_USERFORMAT
) {
250 fputs(diff_get_color_opt(&opt
->diffopt
, DIFF_COMMIT
), stdout
);
251 if (opt
->commit_format
!= CMIT_FMT_ONELINE
)
252 fputs("commit ", stdout
);
253 if (commit
->object
.flags
& BOUNDARY
)
255 else if (commit
->object
.flags
& UNINTERESTING
)
257 else if (opt
->left_right
) {
258 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
263 fputs(diff_unique_abbrev(commit
->object
.sha1
, abbrev_commit
),
266 show_parents(commit
, abbrev_commit
);
269 diff_unique_abbrev(parent
->object
.sha1
,
271 show_decorations(commit
);
272 printf("%s", diff_get_color_opt(&opt
->diffopt
, DIFF_RESET
));
273 putchar(opt
->commit_format
== CMIT_FMT_ONELINE
? ' ' : '\n');
274 if (opt
->reflog_info
) {
275 show_reflog_message(opt
->reflog_info
,
276 opt
->commit_format
== CMIT_FMT_ONELINE
,
278 if (opt
->commit_format
== CMIT_FMT_ONELINE
) {
289 * And then the pretty-printed message itself
291 strbuf_init(&msgbuf
, 0);
292 pretty_print_commit(opt
->commit_format
, commit
, &msgbuf
,
293 abbrev
, subject
, extra_headers
, opt
->date_mode
,
294 has_non_ascii(opt
->add_signoff
));
296 if (opt
->add_signoff
)
297 append_signoff(&msgbuf
, opt
->add_signoff
);
298 if (opt
->show_log_size
)
299 printf("log size %i\n", (int)msgbuf
.len
);
302 printf("%s%s%s", msgbuf
.buf
, extra
, sep
);
303 strbuf_release(&msgbuf
);
306 int log_tree_diff_flush(struct rev_info
*opt
)
308 diffcore_std(&opt
->diffopt
);
310 if (diff_queue_is_empty()) {
311 int saved_fmt
= opt
->diffopt
.output_format
;
312 opt
->diffopt
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
313 diff_flush(&opt
->diffopt
);
314 opt
->diffopt
.output_format
= saved_fmt
;
318 if (opt
->loginfo
&& !opt
->no_commit_id
) {
319 /* When showing a verbose header (i.e. log message),
320 * and not in --pretty=oneline format, we would want
321 * an extra newline between the end of log and the
322 * output for readability.
324 show_log(opt
, opt
->diffopt
.msg_sep
);
325 if ((opt
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
) &&
326 opt
->verbose_header
&&
327 opt
->commit_format
!= CMIT_FMT_ONELINE
) {
328 int pch
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_PATCH
;
329 if ((pch
& opt
->diffopt
.output_format
) == pch
)
334 diff_flush(&opt
->diffopt
);
338 static int do_diff_combined(struct rev_info
*opt
, struct commit
*commit
)
340 unsigned const char *sha1
= commit
->object
.sha1
;
342 diff_tree_combined_merge(sha1
, opt
->dense_combined_merges
, opt
);
343 return !opt
->loginfo
;
347 * Show the diff of a commit.
349 * Return true if we printed any log info messages
351 static int log_tree_diff(struct rev_info
*opt
, struct commit
*commit
, struct log_info
*log
)
354 struct commit_list
*parents
;
355 unsigned const char *sha1
= commit
->object
.sha1
;
361 parents
= commit
->parents
;
363 if (opt
->show_root_diff
) {
364 diff_root_tree_sha1(sha1
, "", &opt
->diffopt
);
365 log_tree_diff_flush(opt
);
367 return !opt
->loginfo
;
370 /* More than one parent? */
371 if (parents
&& parents
->next
) {
372 if (opt
->ignore_merges
)
374 else if (opt
->combine_merges
)
375 return do_diff_combined(opt
, commit
);
377 /* If we show individual diffs, show the parent info */
378 log
->parent
= parents
->item
;
383 struct commit
*parent
= parents
->item
;
385 diff_tree_sha1(parent
->object
.sha1
, sha1
, "", &opt
->diffopt
);
386 log_tree_diff_flush(opt
);
388 showed_log
|= !opt
->loginfo
;
390 /* Set up the log info for the next parent, if any.. */
391 parents
= parents
->next
;
394 log
->parent
= parents
->item
;
400 int log_tree_commit(struct rev_info
*opt
, struct commit
*commit
)
409 shown
= log_tree_diff(opt
, commit
, &log
);
410 if (!shown
&& opt
->loginfo
&& opt
->always_show_header
) {
416 maybe_flush_or_die(stdout
, "stdout");