7 #include "reflog-walk.h"
9 #include "string-list.h"
12 struct decoration name_decoration
= { "object names" };
14 enum decoration_type
{
17 DECORATION_REF_REMOTE
,
23 static char decoration_colors
[][COLOR_MAXLEN
] = {
25 GIT_COLOR_BOLD_GREEN
, /* REF_LOCAL */
26 GIT_COLOR_BOLD_RED
, /* REF_REMOTE */
27 GIT_COLOR_BOLD_YELLOW
, /* REF_TAG */
28 GIT_COLOR_BOLD_MAGENTA
, /* REF_STASH */
29 GIT_COLOR_BOLD_CYAN
, /* REF_HEAD */
32 static const char *decorate_get_color(int decorate_use_color
, enum decoration_type ix
)
34 if (decorate_use_color
)
35 return decoration_colors
[ix
];
39 static int parse_decorate_color_slot(const char *slot
)
42 * We're comparing with 'ignore-case' on
43 * (because config.c sets them all tolower),
44 * but let's match the letters in the literal
45 * string values here with how they are
46 * documented in Documentation/config.txt, for
49 * We love being consistent, don't we?
51 if (!strcasecmp(slot
, "branch"))
52 return DECORATION_REF_LOCAL
;
53 if (!strcasecmp(slot
, "remoteBranch"))
54 return DECORATION_REF_REMOTE
;
55 if (!strcasecmp(slot
, "tag"))
56 return DECORATION_REF_TAG
;
57 if (!strcasecmp(slot
, "stash"))
58 return DECORATION_REF_STASH
;
59 if (!strcasecmp(slot
, "HEAD"))
60 return DECORATION_REF_HEAD
;
64 int parse_decorate_color_config(const char *var
, const int ofs
, const char *value
)
66 int slot
= parse_decorate_color_slot(var
+ ofs
);
70 return config_error_nonbool(var
);
71 color_parse(value
, var
, decoration_colors
[slot
]);
76 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
77 * for showing the commit sha1, use the same check for --decorate
79 #define decorate_get_color_opt(o, ix) \
80 decorate_get_color(DIFF_OPT_TST((o), COLOR_DIFF), ix)
82 static void add_name_decoration(enum decoration_type type
, const char *name
, struct object
*obj
)
84 int nlen
= strlen(name
);
85 struct name_decoration
*res
= xmalloc(sizeof(struct name_decoration
) + nlen
);
86 memcpy(res
->name
, name
, nlen
+ 1);
88 res
->next
= add_decoration(&name_decoration
, obj
, res
);
91 static int add_ref_decoration(const char *refname
, const unsigned char *sha1
, int flags
, void *cb_data
)
93 struct object
*obj
= parse_object(sha1
);
94 enum decoration_type type
= DECORATION_NONE
;
98 if (!prefixcmp(refname
, "refs/heads"))
99 type
= DECORATION_REF_LOCAL
;
100 else if (!prefixcmp(refname
, "refs/remotes"))
101 type
= DECORATION_REF_REMOTE
;
102 else if (!prefixcmp(refname
, "refs/tags"))
103 type
= DECORATION_REF_TAG
;
104 else if (!prefixcmp(refname
, "refs/stash"))
105 type
= DECORATION_REF_STASH
;
106 else if (!prefixcmp(refname
, "HEAD"))
107 type
= DECORATION_REF_HEAD
;
109 if (!cb_data
|| *(int *)cb_data
== DECORATE_SHORT_REFS
)
110 refname
= prettify_refname(refname
);
111 add_name_decoration(type
, refname
, obj
);
112 while (obj
->type
== OBJ_TAG
) {
113 obj
= ((struct tag
*)obj
)->tagged
;
116 add_name_decoration(DECORATION_REF_TAG
, refname
, obj
);
121 void load_ref_decorations(int flags
)
126 for_each_ref(add_ref_decoration
, &flags
);
127 head_ref(add_ref_decoration
, &flags
);
131 static void show_parents(struct commit
*commit
, int abbrev
)
133 struct commit_list
*p
;
134 for (p
= commit
->parents
; p
; p
= p
->next
) {
135 struct commit
*parent
= p
->item
;
136 printf(" %s", find_unique_abbrev(parent
->object
.sha1
, abbrev
));
140 void show_decorations(struct rev_info
*opt
, struct commit
*commit
)
143 struct name_decoration
*decoration
;
144 const char *color_commit
=
145 diff_get_color_opt(&opt
->diffopt
, DIFF_COMMIT
);
146 const char *color_reset
=
147 decorate_get_color_opt(&opt
->diffopt
, DECORATION_NONE
);
149 if (opt
->show_source
&& commit
->util
)
150 printf("\t%s", (char *) commit
->util
);
151 if (!opt
->show_decorations
)
153 decoration
= lookup_decoration(&name_decoration
, &commit
->object
);
158 printf("%s", prefix
);
159 fputs(decorate_get_color_opt(&opt
->diffopt
, decoration
->type
),
161 if (decoration
->type
== DECORATION_REF_TAG
)
162 fputs("tag: ", stdout
);
163 printf("%s", decoration
->name
);
164 fputs(color_reset
, stdout
);
165 fputs(color_commit
, stdout
);
167 decoration
= decoration
->next
;
173 * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
174 * Signed-off-by: and Acked-by: lines.
176 static int detect_any_signoff(char *letter
, int size
)
185 while (letter
<= --cp
&& *cp
== '\n')
188 while (letter
<= cp
) {
207 if (('A' <= ch
&& ch
<= 'Z') ||
208 ('a' <= ch
&& ch
<= 'z') ||
213 /* no empty last line doesn't match */
216 return seen_head
&& seen_name
;
219 static void append_signoff(struct strbuf
*sb
, const char *signoff
)
221 static const char signed_off_by
[] = "Signed-off-by: ";
222 size_t signoff_len
= strlen(signoff
);
228 /* First see if we already have the sign-off by the signer */
229 while ((cp
= strstr(cp
, signed_off_by
))) {
233 cp
+= strlen(signed_off_by
);
234 if (cp
+ signoff_len
>= sb
->buf
+ sb
->len
)
236 if (strncmp(cp
, signoff
, signoff_len
))
238 if (!isspace(cp
[signoff_len
]))
240 /* we already have him */
245 has_signoff
= detect_any_signoff(sb
->buf
, sb
->len
);
248 strbuf_addch(sb
, '\n');
250 strbuf_addstr(sb
, signed_off_by
);
251 strbuf_add(sb
, signoff
, signoff_len
);
252 strbuf_addch(sb
, '\n');
255 static unsigned int digits_in_number(unsigned int number
)
257 unsigned int i
= 10, result
= 1;
258 while (i
<= number
) {
265 void get_patch_filename(struct commit
*commit
, int nr
, const char *suffix
,
268 int suffix_len
= strlen(suffix
) + 1;
269 int start_len
= buf
->len
;
271 strbuf_addf(buf
, commit
? "%04d-" : "%d", nr
);
273 int max_len
= start_len
+ FORMAT_PATCH_NAME_MAX
- suffix_len
;
274 struct pretty_print_context ctx
= {0};
275 ctx
.date_mode
= DATE_NORMAL
;
277 format_commit_message(commit
, "%f", buf
, &ctx
);
278 if (max_len
< buf
->len
)
279 strbuf_setlen(buf
, max_len
);
280 strbuf_addstr(buf
, suffix
);
284 void log_write_email_headers(struct rev_info
*opt
, struct commit
*commit
,
285 const char **subject_p
,
286 const char **extra_headers_p
,
287 int *need_8bit_cte_p
)
289 const char *subject
= NULL
;
290 const char *extra_headers
= opt
->extra_headers
;
291 const char *name
= sha1_to_hex(commit
->object
.sha1
);
293 *need_8bit_cte_p
= 0; /* unknown */
294 if (opt
->total
> 0) {
295 static char buffer
[64];
296 snprintf(buffer
, sizeof(buffer
),
297 "Subject: [%s %0*d/%d] ",
299 digits_in_number(opt
->total
),
300 opt
->nr
, opt
->total
);
302 } else if (opt
->total
== 0 && opt
->subject_prefix
&& *opt
->subject_prefix
) {
303 static char buffer
[256];
304 snprintf(buffer
, sizeof(buffer
),
306 opt
->subject_prefix
);
309 subject
= "Subject: ";
312 printf("From %s Mon Sep 17 00:00:00 2001\n", name
);
313 graph_show_oneline(opt
->graph
);
314 if (opt
->message_id
) {
315 printf("Message-Id: <%s>\n", opt
->message_id
);
316 graph_show_oneline(opt
->graph
);
318 if (opt
->ref_message_ids
&& opt
->ref_message_ids
->nr
> 0) {
320 n
= opt
->ref_message_ids
->nr
;
321 printf("In-Reply-To: <%s>\n", opt
->ref_message_ids
->items
[n
-1].string
);
322 for (i
= 0; i
< n
; i
++)
323 printf("%s<%s>\n", (i
> 0 ? "\t" : "References: "),
324 opt
->ref_message_ids
->items
[i
].string
);
325 graph_show_oneline(opt
->graph
);
327 if (opt
->mime_boundary
) {
328 static char subject_buffer
[1024];
329 static char buffer
[1024];
330 struct strbuf filename
= STRBUF_INIT
;
331 *need_8bit_cte_p
= -1; /* NEVER */
332 snprintf(subject_buffer
, sizeof(subject_buffer
) - 1,
334 "MIME-Version: 1.0\n"
335 "Content-Type: multipart/mixed;"
336 " boundary=\"%s%s\"\n"
338 "This is a multi-part message in MIME "
341 "Content-Type: text/plain; "
342 "charset=UTF-8; format=fixed\n"
343 "Content-Transfer-Encoding: 8bit\n\n",
344 extra_headers
? extra_headers
: "",
345 mime_boundary_leader
, opt
->mime_boundary
,
346 mime_boundary_leader
, opt
->mime_boundary
);
347 extra_headers
= subject_buffer
;
349 get_patch_filename(opt
->numbered_files
? NULL
: commit
, opt
->nr
,
350 opt
->patch_suffix
, &filename
);
351 snprintf(buffer
, sizeof(buffer
) - 1,
353 "Content-Type: text/x-patch;"
355 "Content-Transfer-Encoding: 8bit\n"
356 "Content-Disposition: %s;"
357 " filename=\"%s\"\n\n",
358 mime_boundary_leader
, opt
->mime_boundary
,
360 opt
->no_inline
? "attachment" : "inline",
362 opt
->diffopt
.stat_sep
= buffer
;
363 strbuf_release(&filename
);
365 *subject_p
= subject
;
366 *extra_headers_p
= extra_headers
;
369 void show_log(struct rev_info
*opt
)
371 struct strbuf msgbuf
= STRBUF_INIT
;
372 struct log_info
*log
= opt
->loginfo
;
373 struct commit
*commit
= log
->commit
, *parent
= log
->parent
;
374 int abbrev_commit
= opt
->abbrev_commit
? opt
->abbrev
: 40;
375 const char *extra_headers
= opt
->extra_headers
;
376 struct pretty_print_context ctx
= {0};
379 ctx
.show_notes
= opt
->show_notes
;
380 if (!opt
->verbose_header
) {
381 graph_show_commit(opt
->graph
);
384 if (commit
->object
.flags
& BOUNDARY
)
386 else if (commit
->object
.flags
& UNINTERESTING
)
388 else if (opt
->left_right
) {
389 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
395 fputs(find_unique_abbrev(commit
->object
.sha1
, abbrev_commit
), stdout
);
396 if (opt
->print_parents
)
397 show_parents(commit
, abbrev_commit
);
398 show_decorations(opt
, commit
);
399 if (opt
->graph
&& !graph_is_commit_finished(opt
->graph
)) {
401 graph_show_remainder(opt
->graph
);
403 putchar(opt
->diffopt
.line_termination
);
408 * If use_terminator is set, we already handled any record termination
409 * at the end of the last record.
410 * Otherwise, add a diffopt.line_termination character before all
411 * entries but the first. (IOW, as a separator between entries)
413 if (opt
->shown_one
&& !opt
->use_terminator
) {
415 * If entries are separated by a newline, the output
416 * should look human-readable. If the last entry ended
417 * with a newline, print the graph output before this
418 * newline. Otherwise it will end up as a completely blank
419 * line and will look like a gap in the graph.
421 * If the entry separator is not a newline, the output is
422 * primarily intended for programmatic consumption, and we
423 * never want the extra graph output before the entry
426 if (opt
->diffopt
.line_termination
== '\n' &&
427 !opt
->missing_newline
)
428 graph_show_padding(opt
->graph
);
429 putchar(opt
->diffopt
.line_termination
);
434 * If the history graph was requested,
435 * print the graph, up to this commit's line
437 graph_show_commit(opt
->graph
);
440 * Print header line of header..
443 if (opt
->commit_format
== CMIT_FMT_EMAIL
) {
444 log_write_email_headers(opt
, commit
, &ctx
.subject
, &extra_headers
,
446 } else if (opt
->commit_format
!= CMIT_FMT_USERFORMAT
) {
447 fputs(diff_get_color_opt(&opt
->diffopt
, DIFF_COMMIT
), stdout
);
448 if (opt
->commit_format
!= CMIT_FMT_ONELINE
)
449 fputs("commit ", stdout
);
452 if (commit
->object
.flags
& BOUNDARY
)
454 else if (commit
->object
.flags
& UNINTERESTING
)
456 else if (opt
->left_right
) {
457 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
463 fputs(find_unique_abbrev(commit
->object
.sha1
, abbrev_commit
),
465 if (opt
->print_parents
)
466 show_parents(commit
, abbrev_commit
);
469 find_unique_abbrev(parent
->object
.sha1
,
471 show_decorations(opt
, commit
);
472 printf("%s", diff_get_color_opt(&opt
->diffopt
, DIFF_RESET
));
473 if (opt
->commit_format
== CMIT_FMT_ONELINE
) {
477 graph_show_oneline(opt
->graph
);
479 if (opt
->reflog_info
) {
481 * setup_revisions() ensures that opt->reflog_info
482 * and opt->graph cannot both be set,
483 * so we don't need to worry about printing the
486 show_reflog_message(opt
->reflog_info
,
487 opt
->commit_format
== CMIT_FMT_ONELINE
,
488 opt
->date_mode_explicit
?
491 if (opt
->commit_format
== CMIT_FMT_ONELINE
)
500 * And then the pretty-printed message itself
502 if (ctx
.need_8bit_cte
>= 0)
503 ctx
.need_8bit_cte
= has_non_ascii(opt
->add_signoff
);
504 ctx
.date_mode
= opt
->date_mode
;
505 ctx
.abbrev
= opt
->diffopt
.abbrev
;
506 ctx
.after_subject
= extra_headers
;
507 ctx
.reflog_info
= opt
->reflog_info
;
508 pretty_print_commit(opt
->commit_format
, commit
, &msgbuf
, &ctx
);
510 if (opt
->add_signoff
)
511 append_signoff(&msgbuf
, opt
->add_signoff
);
512 if (opt
->show_log_size
) {
513 printf("log size %i\n", (int)msgbuf
.len
);
514 graph_show_oneline(opt
->graph
);
518 * Set opt->missing_newline if msgbuf doesn't
519 * end in a newline (including if it is empty)
521 if (!msgbuf
.len
|| msgbuf
.buf
[msgbuf
.len
- 1] != '\n')
522 opt
->missing_newline
= 1;
524 opt
->missing_newline
= 0;
527 graph_show_commit_msg(opt
->graph
, &msgbuf
);
529 /* Do not use fwrite() to give MinGW reencoding a chance */
530 printf("%.*s", (int)msgbuf
.len
, msgbuf
.buf
);
531 if (opt
->use_terminator
) {
532 if (!opt
->missing_newline
)
533 graph_show_padding(opt
->graph
);
537 strbuf_release(&msgbuf
);
540 int log_tree_diff_flush(struct rev_info
*opt
)
542 diffcore_std(&opt
->diffopt
);
544 if (diff_queue_is_empty()) {
545 int saved_fmt
= opt
->diffopt
.output_format
;
546 opt
->diffopt
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
547 diff_flush(&opt
->diffopt
);
548 opt
->diffopt
.output_format
= saved_fmt
;
552 if (opt
->loginfo
&& !opt
->no_commit_id
) {
553 /* When showing a verbose header (i.e. log message),
554 * and not in --pretty=oneline format, we would want
555 * an extra newline between the end of log and the
556 * output for readability.
559 if ((opt
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
) &&
560 opt
->verbose_header
&&
561 opt
->commit_format
!= CMIT_FMT_ONELINE
) {
562 int pch
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_PATCH
;
563 if ((pch
& opt
->diffopt
.output_format
) == pch
)
565 if (opt
->diffopt
.output_prefix
) {
566 struct strbuf
*msg
= NULL
;
567 msg
= opt
->diffopt
.output_prefix(&opt
->diffopt
,
568 opt
->diffopt
.output_prefix_data
);
569 fwrite(msg
->buf
, msg
->len
, 1, stdout
);
574 diff_flush(&opt
->diffopt
);
578 static int do_diff_combined(struct rev_info
*opt
, struct commit
*commit
)
580 unsigned const char *sha1
= commit
->object
.sha1
;
582 diff_tree_combined_merge(sha1
, opt
->dense_combined_merges
, opt
);
583 return !opt
->loginfo
;
587 * Show the diff of a commit.
589 * Return true if we printed any log info messages
591 static int log_tree_diff(struct rev_info
*opt
, struct commit
*commit
, struct log_info
*log
)
594 struct commit_list
*parents
;
595 unsigned const char *sha1
= commit
->object
.sha1
;
597 if (!opt
->diff
&& !DIFF_OPT_TST(&opt
->diffopt
, EXIT_WITH_STATUS
))
601 parents
= commit
->parents
;
603 if (opt
->show_root_diff
) {
604 diff_root_tree_sha1(sha1
, "", &opt
->diffopt
);
605 log_tree_diff_flush(opt
);
607 return !opt
->loginfo
;
610 /* More than one parent? */
611 if (parents
&& parents
->next
) {
612 if (opt
->ignore_merges
)
614 else if (opt
->combine_merges
)
615 return do_diff_combined(opt
, commit
);
616 else if (opt
->first_parent_only
) {
618 * Generate merge log entry only for the first
619 * parent, showing summary diff of the others
622 diff_tree_sha1(parents
->item
->object
.sha1
, sha1
, "", &opt
->diffopt
);
623 log_tree_diff_flush(opt
);
624 return !opt
->loginfo
;
627 /* If we show individual diffs, show the parent info */
628 log
->parent
= parents
->item
;
633 struct commit
*parent
= parents
->item
;
635 diff_tree_sha1(parent
->object
.sha1
, sha1
, "", &opt
->diffopt
);
636 log_tree_diff_flush(opt
);
638 showed_log
|= !opt
->loginfo
;
640 /* Set up the log info for the next parent, if any.. */
641 parents
= parents
->next
;
644 log
->parent
= parents
->item
;
650 int log_tree_commit(struct rev_info
*opt
, struct commit
*commit
)
659 shown
= log_tree_diff(opt
, commit
, &log
);
660 if (!shown
&& opt
->loginfo
&& opt
->always_show_header
) {
666 maybe_flush_or_die(stdout
, "stdout");