6 #include "reflog-walk.h"
8 struct decoration name_decoration
= { "object names" };
10 static void show_parents(struct commit
*commit
, int abbrev
)
12 struct commit_list
*p
;
13 for (p
= commit
->parents
; p
; p
= p
->next
) {
14 struct commit
*parent
= p
->item
;
15 printf(" %s", diff_unique_abbrev(parent
->object
.sha1
, abbrev
));
19 void show_decorations(struct commit
*commit
)
22 struct name_decoration
*decoration
;
24 decoration
= lookup_decoration(&name_decoration
, &commit
->object
);
29 printf("%s%s", prefix
, decoration
->name
);
31 decoration
= decoration
->next
;
37 * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
38 * Signed-off-by: and Acked-by: lines.
40 static int detect_any_signoff(char *letter
, int size
)
49 while (letter
<= --cp
&& (ch
= *cp
) == '\n')
52 while (letter
<= cp
) {
71 if (('A' <= ch
&& ch
<= 'Z') ||
72 ('a' <= ch
&& ch
<= 'z') ||
77 /* no empty last line doesn't match */
80 return seen_head
&& seen_name
;
83 static void append_signoff(struct strbuf
*sb
, const char *signoff
)
85 static const char signed_off_by
[] = "Signed-off-by: ";
86 size_t signoff_len
= strlen(signoff
);
92 /* First see if we already have the sign-off by the signer */
93 while ((cp
= strstr(cp
, signed_off_by
))) {
97 cp
+= strlen(signed_off_by
);
98 if (cp
+ signoff_len
>= sb
->buf
+ sb
->len
)
100 if (strncmp(cp
, signoff
, signoff_len
))
102 if (!isspace(cp
[signoff_len
]))
104 /* we already have him */
109 has_signoff
= detect_any_signoff(sb
->buf
, sb
->len
);
112 strbuf_addch(sb
, '\n');
114 strbuf_addstr(sb
, signed_off_by
);
115 strbuf_add(sb
, signoff
, signoff_len
);
116 strbuf_addch(sb
, '\n');
119 static unsigned int digits_in_number(unsigned int number
)
121 unsigned int i
= 10, result
= 1;
122 while (i
<= number
) {
129 static int has_non_ascii(const char *s
)
134 while ((ch
= *s
++) != '\0') {
141 void log_write_email_headers(struct rev_info
*opt
, const char *name
,
142 const char **subject_p
,
143 const char **extra_headers_p
,
144 int *need_8bit_cte_p
)
146 const char *subject
= NULL
;
147 const char *extra_headers
= opt
->extra_headers
;
149 *need_8bit_cte_p
= 0; /* unknown */
150 if (opt
->total
> 0) {
151 static char buffer
[64];
152 snprintf(buffer
, sizeof(buffer
),
153 "Subject: [%s %0*d/%d] ",
155 digits_in_number(opt
->total
),
156 opt
->nr
, opt
->total
);
158 } else if (opt
->total
== 0 && opt
->subject_prefix
&& *opt
->subject_prefix
) {
159 static char buffer
[256];
160 snprintf(buffer
, sizeof(buffer
),
162 opt
->subject_prefix
);
165 subject
= "Subject: ";
168 printf("From %s Mon Sep 17 00:00:00 2001\n", name
);
169 graph_show_oneline(opt
->graph
);
170 if (opt
->message_id
) {
171 printf("Message-Id: <%s>\n", opt
->message_id
);
172 graph_show_oneline(opt
->graph
);
174 if (opt
->ref_message_id
) {
175 printf("In-Reply-To: <%s>\nReferences: <%s>\n",
176 opt
->ref_message_id
, opt
->ref_message_id
);
177 graph_show_oneline(opt
->graph
);
179 if (opt
->mime_boundary
) {
180 static char subject_buffer
[1024];
181 static char buffer
[1024];
182 *need_8bit_cte_p
= -1; /* NEVER */
183 snprintf(subject_buffer
, sizeof(subject_buffer
) - 1,
185 "MIME-Version: 1.0\n"
186 "Content-Type: multipart/mixed;"
187 " boundary=\"%s%s\"\n"
189 "This is a multi-part message in MIME "
192 "Content-Type: text/plain; "
193 "charset=UTF-8; format=fixed\n"
194 "Content-Transfer-Encoding: 8bit\n\n",
195 extra_headers
? extra_headers
: "",
196 mime_boundary_leader
, opt
->mime_boundary
,
197 mime_boundary_leader
, opt
->mime_boundary
);
198 extra_headers
= subject_buffer
;
200 snprintf(buffer
, sizeof(buffer
) - 1,
202 "Content-Type: text/x-patch;"
203 " name=\"%s.diff\"\n"
204 "Content-Transfer-Encoding: 8bit\n"
205 "Content-Disposition: %s;"
206 " filename=\"%s.diff\"\n\n",
207 mime_boundary_leader
, opt
->mime_boundary
,
209 opt
->no_inline
? "attachment" : "inline",
211 opt
->diffopt
.stat_sep
= buffer
;
213 *subject_p
= subject
;
214 *extra_headers_p
= extra_headers
;
217 void show_log(struct rev_info
*opt
)
219 struct strbuf msgbuf
;
220 struct log_info
*log
= opt
->loginfo
;
221 struct commit
*commit
= log
->commit
, *parent
= log
->parent
;
222 int abbrev
= opt
->diffopt
.abbrev
;
223 int abbrev_commit
= opt
->abbrev_commit
? opt
->abbrev
: 40;
224 const char *subject
= NULL
, *extra_headers
= opt
->extra_headers
;
225 int need_8bit_cte
= 0;
228 if (!opt
->verbose_header
) {
229 graph_show_commit(opt
->graph
);
232 if (commit
->object
.flags
& BOUNDARY
)
234 else if (commit
->object
.flags
& UNINTERESTING
)
236 else if (opt
->left_right
) {
237 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
243 fputs(diff_unique_abbrev(commit
->object
.sha1
, abbrev_commit
), stdout
);
244 if (opt
->print_parents
)
245 show_parents(commit
, abbrev_commit
);
246 show_decorations(commit
);
247 if (opt
->graph
&& !graph_is_commit_finished(opt
->graph
)) {
249 graph_show_remainder(opt
->graph
);
251 putchar(opt
->diffopt
.line_termination
);
256 * If use_terminator is set, add a newline at the end of the entry.
257 * Otherwise, add a diffopt.line_termination character before all
258 * entries but the first. (IOW, as a separator between entries)
260 if (opt
->shown_one
&& !opt
->use_terminator
) {
262 * If entries are separated by a newline, the output
263 * should look human-readable. If the last entry ended
264 * with a newline, print the graph output before this
265 * newline. Otherwise it will end up as a completely blank
266 * line and will look like a gap in the graph.
268 * If the entry separator is not a newline, the output is
269 * primarily intended for programmatic consumption, and we
270 * never want the extra graph output before the entry
273 if (opt
->diffopt
.line_termination
== '\n' &&
274 !opt
->missing_newline
)
275 graph_show_padding(opt
->graph
);
276 putchar(opt
->diffopt
.line_termination
);
281 * If the history graph was requested,
282 * print the graph, up to this commit's line
284 graph_show_commit(opt
->graph
);
287 * Print header line of header..
290 if (opt
->commit_format
== CMIT_FMT_EMAIL
) {
291 log_write_email_headers(opt
, sha1_to_hex(commit
->object
.sha1
),
292 &subject
, &extra_headers
,
294 } else if (opt
->commit_format
!= CMIT_FMT_USERFORMAT
) {
295 fputs(diff_get_color_opt(&opt
->diffopt
, DIFF_COMMIT
), stdout
);
296 if (opt
->commit_format
!= CMIT_FMT_ONELINE
)
297 fputs("commit ", stdout
);
300 if (commit
->object
.flags
& BOUNDARY
)
302 else if (commit
->object
.flags
& UNINTERESTING
)
304 else if (opt
->left_right
) {
305 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
311 fputs(diff_unique_abbrev(commit
->object
.sha1
, abbrev_commit
),
313 if (opt
->print_parents
)
314 show_parents(commit
, abbrev_commit
);
317 diff_unique_abbrev(parent
->object
.sha1
,
319 show_decorations(commit
);
320 printf("%s", diff_get_color_opt(&opt
->diffopt
, DIFF_RESET
));
321 if (opt
->commit_format
== CMIT_FMT_ONELINE
) {
325 graph_show_oneline(opt
->graph
);
327 if (opt
->reflog_info
) {
329 * setup_revisions() ensures that opt->reflog_info
330 * and opt->graph cannot both be set,
331 * so we don't need to worry about printing the
334 show_reflog_message(opt
->reflog_info
,
335 opt
->commit_format
== CMIT_FMT_ONELINE
,
337 if (opt
->commit_format
== CMIT_FMT_ONELINE
)
346 * And then the pretty-printed message itself
348 strbuf_init(&msgbuf
, 0);
349 if (need_8bit_cte
>= 0)
350 need_8bit_cte
= has_non_ascii(opt
->add_signoff
);
351 pretty_print_commit(opt
->commit_format
, commit
, &msgbuf
,
352 abbrev
, subject
, extra_headers
, opt
->date_mode
,
355 if (opt
->add_signoff
)
356 append_signoff(&msgbuf
, opt
->add_signoff
);
357 if (opt
->show_log_size
) {
358 printf("log size %i\n", (int)msgbuf
.len
);
359 graph_show_oneline(opt
->graph
);
363 * Set opt->missing_newline if msgbuf doesn't
364 * end in a newline (including if it is empty)
366 if (!msgbuf
.len
|| msgbuf
.buf
[msgbuf
.len
- 1] != '\n')
367 opt
->missing_newline
= 1;
369 opt
->missing_newline
= 0;
372 graph_show_commit_msg(opt
->graph
, &msgbuf
);
374 fwrite(msgbuf
.buf
, sizeof(char), msgbuf
.len
, stdout
);
375 if (opt
->use_terminator
) {
376 if (!opt
->missing_newline
)
377 graph_show_padding(opt
->graph
);
381 strbuf_release(&msgbuf
);
384 int log_tree_diff_flush(struct rev_info
*opt
)
386 diffcore_std(&opt
->diffopt
);
388 if (diff_queue_is_empty()) {
389 int saved_fmt
= opt
->diffopt
.output_format
;
390 opt
->diffopt
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
391 diff_flush(&opt
->diffopt
);
392 opt
->diffopt
.output_format
= saved_fmt
;
396 if (opt
->loginfo
&& !opt
->no_commit_id
) {
397 /* When showing a verbose header (i.e. log message),
398 * and not in --pretty=oneline format, we would want
399 * an extra newline between the end of log and the
400 * output for readability.
403 if ((opt
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
) &&
404 opt
->verbose_header
&&
405 opt
->commit_format
!= CMIT_FMT_ONELINE
) {
406 int pch
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_PATCH
;
407 if ((pch
& opt
->diffopt
.output_format
) == pch
)
412 diff_flush(&opt
->diffopt
);
416 static int do_diff_combined(struct rev_info
*opt
, struct commit
*commit
)
418 unsigned const char *sha1
= commit
->object
.sha1
;
420 diff_tree_combined_merge(sha1
, opt
->dense_combined_merges
, opt
);
421 return !opt
->loginfo
;
425 * Show the diff of a commit.
427 * Return true if we printed any log info messages
429 static int log_tree_diff(struct rev_info
*opt
, struct commit
*commit
, struct log_info
*log
)
432 struct commit_list
*parents
;
433 unsigned const char *sha1
= commit
->object
.sha1
;
439 parents
= commit
->parents
;
441 if (opt
->show_root_diff
) {
442 diff_root_tree_sha1(sha1
, "", &opt
->diffopt
);
443 log_tree_diff_flush(opt
);
445 return !opt
->loginfo
;
448 /* More than one parent? */
449 if (parents
&& parents
->next
) {
450 if (opt
->ignore_merges
)
452 else if (opt
->combine_merges
)
453 return do_diff_combined(opt
, commit
);
455 /* If we show individual diffs, show the parent info */
456 log
->parent
= parents
->item
;
461 struct commit
*parent
= parents
->item
;
463 diff_tree_sha1(parent
->object
.sha1
, sha1
, "", &opt
->diffopt
);
464 log_tree_diff_flush(opt
);
466 showed_log
|= !opt
->loginfo
;
468 /* Set up the log info for the next parent, if any.. */
469 parents
= parents
->next
;
472 log
->parent
= parents
->item
;
478 int log_tree_commit(struct rev_info
*opt
, struct commit
*commit
)
487 shown
= log_tree_diff(opt
, commit
, &log
);
488 if (!shown
&& opt
->loginfo
&& opt
->always_show_header
) {
494 maybe_flush_or_die(stdout
, "stdout");