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
,
142 const char **extra_headers_p
,
143 int *need_8bit_cte_p
)
145 const char *subject
= NULL
;
146 const char *extra_headers
= opt
->extra_headers
;
148 *need_8bit_cte_p
= 0; /* unknown */
149 if (opt
->total
> 0) {
150 static char buffer
[64];
151 snprintf(buffer
, sizeof(buffer
),
152 "Subject: [%s %0*d/%d] ",
154 digits_in_number(opt
->total
),
155 opt
->nr
, opt
->total
);
157 } else if (opt
->total
== 0 && opt
->subject_prefix
&& *opt
->subject_prefix
) {
158 static char buffer
[256];
159 snprintf(buffer
, sizeof(buffer
),
161 opt
->subject_prefix
);
164 subject
= "Subject: ";
167 printf("From %s Mon Sep 17 00:00:00 2001\n", name
);
169 printf("Message-Id: <%s>\n", opt
->message_id
);
170 if (opt
->ref_message_id
)
171 printf("In-Reply-To: <%s>\nReferences: <%s>\n",
172 opt
->ref_message_id
, opt
->ref_message_id
);
173 if (opt
->mime_boundary
) {
174 static char subject_buffer
[1024];
175 static char buffer
[1024];
176 *need_8bit_cte_p
= -1; /* NEVER */
177 snprintf(subject_buffer
, sizeof(subject_buffer
) - 1,
179 "MIME-Version: 1.0\n"
180 "Content-Type: multipart/mixed;"
181 " boundary=\"%s%s\"\n"
183 "This is a multi-part message in MIME "
186 "Content-Type: text/plain; "
187 "charset=UTF-8; format=fixed\n"
188 "Content-Transfer-Encoding: 8bit\n\n",
189 extra_headers
? extra_headers
: "",
190 mime_boundary_leader
, opt
->mime_boundary
,
191 mime_boundary_leader
, opt
->mime_boundary
);
192 extra_headers
= subject_buffer
;
194 snprintf(buffer
, sizeof(buffer
) - 1,
196 "Content-Type: text/x-patch;"
197 " name=\"%s.diff\"\n"
198 "Content-Transfer-Encoding: 8bit\n"
199 "Content-Disposition: %s;"
200 " filename=\"%s.diff\"\n\n",
201 mime_boundary_leader
, opt
->mime_boundary
,
203 opt
->no_inline
? "attachment" : "inline",
205 opt
->diffopt
.stat_sep
= buffer
;
207 *subject_p
= subject
;
208 *extra_headers_p
= extra_headers
;
211 void show_log(struct rev_info
*opt
, const char *sep
)
213 struct strbuf msgbuf
;
214 struct log_info
*log
= opt
->loginfo
;
215 struct commit
*commit
= log
->commit
, *parent
= log
->parent
;
216 int abbrev
= opt
->diffopt
.abbrev
;
217 int abbrev_commit
= opt
->abbrev_commit
? opt
->abbrev
: 40;
219 const char *subject
= NULL
, *extra_headers
= opt
->extra_headers
;
220 int need_8bit_cte
= 0;
223 if (!opt
->verbose_header
) {
224 if (commit
->object
.flags
& BOUNDARY
)
226 else if (commit
->object
.flags
& UNINTERESTING
)
228 else if (opt
->left_right
) {
229 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
234 fputs(diff_unique_abbrev(commit
->object
.sha1
, abbrev_commit
), stdout
);
236 show_parents(commit
, abbrev_commit
);
237 show_decorations(commit
);
238 putchar(opt
->diffopt
.line_termination
);
243 * The "oneline" format has several special cases:
244 * - The pretty-printed commit lacks a newline at the end
245 * of the buffer, but we do want to make sure that we
246 * have a newline there. If the separator isn't already
247 * a newline, add an extra one.
248 * - unlike other log messages, the one-line format does
249 * not have an empty line between entries.
252 if (*sep
!= '\n' && opt
->use_terminator
)
254 if (opt
->shown_one
&& !opt
->use_terminator
)
255 putchar(opt
->diffopt
.line_termination
);
259 * Print header line of header..
262 if (opt
->commit_format
== CMIT_FMT_EMAIL
) {
263 log_write_email_headers(opt
, sha1_to_hex(commit
->object
.sha1
),
264 &subject
, &extra_headers
,
266 } else if (opt
->commit_format
!= CMIT_FMT_USERFORMAT
) {
267 fputs(diff_get_color_opt(&opt
->diffopt
, DIFF_COMMIT
), stdout
);
268 if (opt
->commit_format
!= CMIT_FMT_ONELINE
)
269 fputs("commit ", stdout
);
270 if (commit
->object
.flags
& BOUNDARY
)
272 else if (commit
->object
.flags
& UNINTERESTING
)
274 else if (opt
->left_right
) {
275 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
280 fputs(diff_unique_abbrev(commit
->object
.sha1
, abbrev_commit
),
283 show_parents(commit
, abbrev_commit
);
286 diff_unique_abbrev(parent
->object
.sha1
,
288 show_decorations(commit
);
289 printf("%s", diff_get_color_opt(&opt
->diffopt
, DIFF_RESET
));
290 putchar(opt
->commit_format
== CMIT_FMT_ONELINE
? ' ' : '\n');
291 if (opt
->reflog_info
) {
292 show_reflog_message(opt
->reflog_info
,
293 opt
->commit_format
== CMIT_FMT_ONELINE
,
295 if (opt
->commit_format
== CMIT_FMT_ONELINE
) {
306 * And then the pretty-printed message itself
308 strbuf_init(&msgbuf
, 0);
309 if (need_8bit_cte
>= 0)
310 need_8bit_cte
= has_non_ascii(opt
->add_signoff
);
311 pretty_print_commit(opt
->commit_format
, commit
, &msgbuf
,
312 abbrev
, subject
, extra_headers
, opt
->date_mode
,
315 if (opt
->add_signoff
)
316 append_signoff(&msgbuf
, opt
->add_signoff
);
317 if (opt
->show_log_size
)
318 printf("log size %i\n", (int)msgbuf
.len
);
321 fwrite(msgbuf
.buf
, sizeof(char), msgbuf
.len
, stdout
);
322 printf("%s%s", extra
, sep
);
324 strbuf_release(&msgbuf
);
327 int log_tree_diff_flush(struct rev_info
*opt
)
329 diffcore_std(&opt
->diffopt
);
331 if (diff_queue_is_empty()) {
332 int saved_fmt
= opt
->diffopt
.output_format
;
333 opt
->diffopt
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
334 diff_flush(&opt
->diffopt
);
335 opt
->diffopt
.output_format
= saved_fmt
;
339 if (opt
->loginfo
&& !opt
->no_commit_id
) {
340 /* When showing a verbose header (i.e. log message),
341 * and not in --pretty=oneline format, we would want
342 * an extra newline between the end of log and the
343 * output for readability.
345 show_log(opt
, opt
->diffopt
.msg_sep
);
346 if ((opt
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
) &&
347 opt
->verbose_header
&&
348 opt
->commit_format
!= CMIT_FMT_ONELINE
) {
349 int pch
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_PATCH
;
350 if ((pch
& opt
->diffopt
.output_format
) == pch
)
355 diff_flush(&opt
->diffopt
);
359 static int do_diff_combined(struct rev_info
*opt
, struct commit
*commit
)
361 unsigned const char *sha1
= commit
->object
.sha1
;
363 diff_tree_combined_merge(sha1
, opt
->dense_combined_merges
, opt
);
364 return !opt
->loginfo
;
368 * Show the diff of a commit.
370 * Return true if we printed any log info messages
372 static int log_tree_diff(struct rev_info
*opt
, struct commit
*commit
, struct log_info
*log
)
375 struct commit_list
*parents
;
376 unsigned const char *sha1
= commit
->object
.sha1
;
382 parents
= commit
->parents
;
384 if (opt
->show_root_diff
) {
385 diff_root_tree_sha1(sha1
, "", &opt
->diffopt
);
386 log_tree_diff_flush(opt
);
388 return !opt
->loginfo
;
391 /* More than one parent? */
392 if (parents
&& parents
->next
) {
393 if (opt
->ignore_merges
)
395 else if (opt
->combine_merges
)
396 return do_diff_combined(opt
, commit
);
398 /* If we show individual diffs, show the parent info */
399 log
->parent
= parents
->item
;
404 struct commit
*parent
= parents
->item
;
406 diff_tree_sha1(parent
->object
.sha1
, sha1
, "", &opt
->diffopt
);
407 log_tree_diff_flush(opt
);
409 showed_log
|= !opt
->loginfo
;
411 /* Set up the log info for the next parent, if any.. */
412 parents
= parents
->next
;
415 log
->parent
= parents
->item
;
421 int log_tree_commit(struct rev_info
*opt
, struct commit
*commit
)
430 shown
= log_tree_diff(opt
, commit
, &log
);
431 if (!shown
&& opt
->loginfo
&& opt
->always_show_header
) {
437 maybe_flush_or_die(stdout
, "stdout");