7 #include "reflog-walk.h"
10 struct decoration name_decoration
= { "object names" };
12 static void add_name_decoration(const char *prefix
, const char *name
, struct object
*obj
)
14 int plen
= strlen(prefix
);
15 int nlen
= strlen(name
);
16 struct name_decoration
*res
= xmalloc(sizeof(struct name_decoration
) + plen
+ nlen
);
17 memcpy(res
->name
, prefix
, plen
);
18 memcpy(res
->name
+ plen
, name
, nlen
+ 1);
19 res
->next
= add_decoration(&name_decoration
, obj
, res
);
22 static int add_ref_decoration(const char *refname
, const unsigned char *sha1
, int flags
, void *cb_data
)
24 struct object
*obj
= parse_object(sha1
);
27 add_name_decoration("", refname
, obj
);
28 while (obj
->type
== OBJ_TAG
) {
29 obj
= ((struct tag
*)obj
)->tagged
;
32 add_name_decoration("tag: ", refname
, obj
);
37 void load_ref_decorations(void)
42 for_each_ref(add_ref_decoration
, NULL
);
46 static void show_parents(struct commit
*commit
, int abbrev
)
48 struct commit_list
*p
;
49 for (p
= commit
->parents
; p
; p
= p
->next
) {
50 struct commit
*parent
= p
->item
;
51 printf(" %s", find_unique_abbrev(parent
->object
.sha1
, abbrev
));
55 void show_decorations(struct rev_info
*opt
, struct commit
*commit
)
58 struct name_decoration
*decoration
;
60 if (opt
->show_source
&& commit
->util
)
61 printf("\t%s", (char *) commit
->util
);
62 if (!opt
->show_decorations
)
64 decoration
= lookup_decoration(&name_decoration
, &commit
->object
);
69 printf("%s%s", prefix
, decoration
->name
);
71 decoration
= decoration
->next
;
77 * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
78 * Signed-off-by: and Acked-by: lines.
80 static int detect_any_signoff(char *letter
, int size
)
89 while (letter
<= --cp
&& (ch
= *cp
) == '\n')
92 while (letter
<= cp
) {
111 if (('A' <= ch
&& ch
<= 'Z') ||
112 ('a' <= ch
&& ch
<= 'z') ||
117 /* no empty last line doesn't match */
120 return seen_head
&& seen_name
;
123 static void append_signoff(struct strbuf
*sb
, const char *signoff
)
125 static const char signed_off_by
[] = "Signed-off-by: ";
126 size_t signoff_len
= strlen(signoff
);
132 /* First see if we already have the sign-off by the signer */
133 while ((cp
= strstr(cp
, signed_off_by
))) {
137 cp
+= strlen(signed_off_by
);
138 if (cp
+ signoff_len
>= sb
->buf
+ sb
->len
)
140 if (strncmp(cp
, signoff
, signoff_len
))
142 if (!isspace(cp
[signoff_len
]))
144 /* we already have him */
149 has_signoff
= detect_any_signoff(sb
->buf
, sb
->len
);
152 strbuf_addch(sb
, '\n');
154 strbuf_addstr(sb
, signed_off_by
);
155 strbuf_add(sb
, signoff
, signoff_len
);
156 strbuf_addch(sb
, '\n');
159 static unsigned int digits_in_number(unsigned int number
)
161 unsigned int i
= 10, result
= 1;
162 while (i
<= number
) {
169 static int has_non_ascii(const char *s
)
174 while ((ch
= *s
++) != '\0') {
181 void log_write_email_headers(struct rev_info
*opt
, const char *name
,
182 const char **subject_p
,
183 const char **extra_headers_p
,
184 int *need_8bit_cte_p
)
186 const char *subject
= NULL
;
187 const char *extra_headers
= opt
->extra_headers
;
189 *need_8bit_cte_p
= 0; /* unknown */
190 if (opt
->total
> 0) {
191 static char buffer
[64];
192 snprintf(buffer
, sizeof(buffer
),
193 "Subject: [%s %0*d/%d] ",
195 digits_in_number(opt
->total
),
196 opt
->nr
, opt
->total
);
198 } else if (opt
->total
== 0 && opt
->subject_prefix
&& *opt
->subject_prefix
) {
199 static char buffer
[256];
200 snprintf(buffer
, sizeof(buffer
),
202 opt
->subject_prefix
);
205 subject
= "Subject: ";
208 printf("From %s Mon Sep 17 00:00:00 2001\n", name
);
209 graph_show_oneline(opt
->graph
);
210 if (opt
->message_id
) {
211 printf("Message-Id: <%s>\n", opt
->message_id
);
212 graph_show_oneline(opt
->graph
);
214 if (opt
->ref_message_id
) {
215 printf("In-Reply-To: <%s>\nReferences: <%s>\n",
216 opt
->ref_message_id
, opt
->ref_message_id
);
217 graph_show_oneline(opt
->graph
);
219 if (opt
->mime_boundary
) {
220 static char subject_buffer
[1024];
221 static char buffer
[1024];
222 *need_8bit_cte_p
= -1; /* NEVER */
223 snprintf(subject_buffer
, sizeof(subject_buffer
) - 1,
225 "MIME-Version: 1.0\n"
226 "Content-Type: multipart/mixed;"
227 " boundary=\"%s%s\"\n"
229 "This is a multi-part message in MIME "
232 "Content-Type: text/plain; "
233 "charset=UTF-8; format=fixed\n"
234 "Content-Transfer-Encoding: 8bit\n\n",
235 extra_headers
? extra_headers
: "",
236 mime_boundary_leader
, opt
->mime_boundary
,
237 mime_boundary_leader
, opt
->mime_boundary
);
238 extra_headers
= subject_buffer
;
240 snprintf(buffer
, sizeof(buffer
) - 1,
242 "Content-Type: text/x-patch;"
243 " name=\"%s.diff\"\n"
244 "Content-Transfer-Encoding: 8bit\n"
245 "Content-Disposition: %s;"
246 " filename=\"%s.diff\"\n\n",
247 mime_boundary_leader
, opt
->mime_boundary
,
249 opt
->no_inline
? "attachment" : "inline",
251 opt
->diffopt
.stat_sep
= buffer
;
253 *subject_p
= subject
;
254 *extra_headers_p
= extra_headers
;
257 void show_log(struct rev_info
*opt
)
259 struct strbuf msgbuf
= STRBUF_INIT
;
260 struct log_info
*log
= opt
->loginfo
;
261 struct commit
*commit
= log
->commit
, *parent
= log
->parent
;
262 int abbrev
= opt
->diffopt
.abbrev
;
263 int abbrev_commit
= opt
->abbrev_commit
? opt
->abbrev
: 40;
264 const char *subject
= NULL
, *extra_headers
= opt
->extra_headers
;
265 int need_8bit_cte
= 0;
268 if (!opt
->verbose_header
) {
269 graph_show_commit(opt
->graph
);
272 if (commit
->object
.flags
& BOUNDARY
)
274 else if (commit
->object
.flags
& UNINTERESTING
)
276 else if (opt
->left_right
) {
277 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
283 fputs(find_unique_abbrev(commit
->object
.sha1
, abbrev_commit
), stdout
);
284 if (opt
->print_parents
)
285 show_parents(commit
, abbrev_commit
);
286 show_decorations(opt
, commit
);
287 if (opt
->graph
&& !graph_is_commit_finished(opt
->graph
)) {
289 graph_show_remainder(opt
->graph
);
291 putchar(opt
->diffopt
.line_termination
);
296 * If use_terminator is set, add a newline at the end of the entry.
297 * Otherwise, add a diffopt.line_termination character before all
298 * entries but the first. (IOW, as a separator between entries)
300 if (opt
->shown_one
&& !opt
->use_terminator
) {
302 * If entries are separated by a newline, the output
303 * should look human-readable. If the last entry ended
304 * with a newline, print the graph output before this
305 * newline. Otherwise it will end up as a completely blank
306 * line and will look like a gap in the graph.
308 * If the entry separator is not a newline, the output is
309 * primarily intended for programmatic consumption, and we
310 * never want the extra graph output before the entry
313 if (opt
->diffopt
.line_termination
== '\n' &&
314 !opt
->missing_newline
)
315 graph_show_padding(opt
->graph
);
316 putchar(opt
->diffopt
.line_termination
);
321 * If the history graph was requested,
322 * print the graph, up to this commit's line
324 graph_show_commit(opt
->graph
);
327 * Print header line of header..
330 if (opt
->commit_format
== CMIT_FMT_EMAIL
) {
331 log_write_email_headers(opt
, sha1_to_hex(commit
->object
.sha1
),
332 &subject
, &extra_headers
,
334 } else if (opt
->commit_format
!= CMIT_FMT_USERFORMAT
) {
335 fputs(diff_get_color_opt(&opt
->diffopt
, DIFF_COMMIT
), stdout
);
336 if (opt
->commit_format
!= CMIT_FMT_ONELINE
)
337 fputs("commit ", stdout
);
340 if (commit
->object
.flags
& BOUNDARY
)
342 else if (commit
->object
.flags
& UNINTERESTING
)
344 else if (opt
->left_right
) {
345 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
351 fputs(find_unique_abbrev(commit
->object
.sha1
, abbrev_commit
),
353 if (opt
->print_parents
)
354 show_parents(commit
, abbrev_commit
);
357 find_unique_abbrev(parent
->object
.sha1
,
359 show_decorations(opt
, commit
);
360 printf("%s", diff_get_color_opt(&opt
->diffopt
, DIFF_RESET
));
361 if (opt
->commit_format
== CMIT_FMT_ONELINE
) {
365 graph_show_oneline(opt
->graph
);
367 if (opt
->reflog_info
) {
369 * setup_revisions() ensures that opt->reflog_info
370 * and opt->graph cannot both be set,
371 * so we don't need to worry about printing the
374 show_reflog_message(opt
->reflog_info
,
375 opt
->commit_format
== CMIT_FMT_ONELINE
,
377 if (opt
->commit_format
== CMIT_FMT_ONELINE
)
386 * And then the pretty-printed message itself
388 if (need_8bit_cte
>= 0)
389 need_8bit_cte
= has_non_ascii(opt
->add_signoff
);
390 pretty_print_commit(opt
->commit_format
, commit
, &msgbuf
,
391 abbrev
, subject
, extra_headers
, opt
->date_mode
,
394 if (opt
->add_signoff
)
395 append_signoff(&msgbuf
, opt
->add_signoff
);
396 if (opt
->show_log_size
) {
397 printf("log size %i\n", (int)msgbuf
.len
);
398 graph_show_oneline(opt
->graph
);
402 * Set opt->missing_newline if msgbuf doesn't
403 * end in a newline (including if it is empty)
405 if (!msgbuf
.len
|| msgbuf
.buf
[msgbuf
.len
- 1] != '\n')
406 opt
->missing_newline
= 1;
408 opt
->missing_newline
= 0;
411 graph_show_commit_msg(opt
->graph
, &msgbuf
);
413 fwrite(msgbuf
.buf
, sizeof(char), msgbuf
.len
, stdout
);
414 if (opt
->use_terminator
) {
415 if (!opt
->missing_newline
)
416 graph_show_padding(opt
->graph
);
420 strbuf_release(&msgbuf
);
423 int log_tree_diff_flush(struct rev_info
*opt
)
425 diffcore_std(&opt
->diffopt
);
427 if (diff_queue_is_empty()) {
428 int saved_fmt
= opt
->diffopt
.output_format
;
429 opt
->diffopt
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
430 diff_flush(&opt
->diffopt
);
431 opt
->diffopt
.output_format
= saved_fmt
;
435 if (opt
->loginfo
&& !opt
->no_commit_id
) {
436 /* When showing a verbose header (i.e. log message),
437 * and not in --pretty=oneline format, we would want
438 * an extra newline between the end of log and the
439 * output for readability.
442 if ((opt
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
) &&
443 opt
->verbose_header
&&
444 opt
->commit_format
!= CMIT_FMT_ONELINE
) {
445 int pch
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_PATCH
;
446 if ((pch
& opt
->diffopt
.output_format
) == pch
)
451 diff_flush(&opt
->diffopt
);
455 static int do_diff_combined(struct rev_info
*opt
, struct commit
*commit
)
457 unsigned const char *sha1
= commit
->object
.sha1
;
459 diff_tree_combined_merge(sha1
, opt
->dense_combined_merges
, opt
);
460 return !opt
->loginfo
;
464 * Show the diff of a commit.
466 * Return true if we printed any log info messages
468 static int log_tree_diff(struct rev_info
*opt
, struct commit
*commit
, struct log_info
*log
)
471 struct commit_list
*parents
;
472 unsigned const char *sha1
= commit
->object
.sha1
;
474 if (!opt
->diff
&& !DIFF_OPT_TST(&opt
->diffopt
, EXIT_WITH_STATUS
))
478 parents
= commit
->parents
;
480 if (opt
->show_root_diff
) {
481 diff_root_tree_sha1(sha1
, "", &opt
->diffopt
);
482 log_tree_diff_flush(opt
);
484 return !opt
->loginfo
;
487 /* More than one parent? */
488 if (parents
&& parents
->next
) {
489 if (opt
->ignore_merges
)
491 else if (opt
->combine_merges
)
492 return do_diff_combined(opt
, commit
);
494 /* If we show individual diffs, show the parent info */
495 log
->parent
= parents
->item
;
500 struct commit
*parent
= parents
->item
;
502 diff_tree_sha1(parent
->object
.sha1
, sha1
, "", &opt
->diffopt
);
503 log_tree_diff_flush(opt
);
505 showed_log
|= !opt
->loginfo
;
507 /* Set up the log info for the next parent, if any.. */
508 parents
= parents
->next
;
511 log
->parent
= parents
->item
;
517 int log_tree_commit(struct rev_info
*opt
, struct commit
*commit
)
526 shown
= log_tree_diff(opt
, commit
, &log
);
527 if (!shown
&& opt
->loginfo
&& opt
->always_show_header
) {
533 maybe_flush_or_die(stdout
, "stdout");