7 #include "reflog-walk.h"
9 #include "string-list.h"
11 struct decoration name_decoration
= { "object names" };
13 static void add_name_decoration(const char *prefix
, const char *name
, struct object
*obj
)
15 int plen
= strlen(prefix
);
16 int nlen
= strlen(name
);
17 struct name_decoration
*res
= xmalloc(sizeof(struct name_decoration
) + plen
+ nlen
);
18 memcpy(res
->name
, prefix
, plen
);
19 memcpy(res
->name
+ plen
, name
, nlen
+ 1);
20 res
->next
= add_decoration(&name_decoration
, obj
, res
);
23 static int add_ref_decoration(const char *refname
, const unsigned char *sha1
, int flags
, void *cb_data
)
25 struct object
*obj
= parse_object(sha1
);
28 if (!cb_data
|| *(int *)cb_data
== DECORATE_SHORT_REFS
)
29 refname
= prettify_refname(refname
);
30 add_name_decoration("", refname
, obj
);
31 while (obj
->type
== OBJ_TAG
) {
32 obj
= ((struct tag
*)obj
)->tagged
;
35 add_name_decoration("tag: ", refname
, obj
);
40 void load_ref_decorations(int flags
)
45 for_each_ref(add_ref_decoration
, &flags
);
46 head_ref(add_ref_decoration
, &flags
);
50 static void show_parents(struct commit
*commit
, int abbrev
)
52 struct commit_list
*p
;
53 for (p
= commit
->parents
; p
; p
= p
->next
) {
54 struct commit
*parent
= p
->item
;
55 printf(" %s", find_unique_abbrev(parent
->object
.sha1
, abbrev
));
59 void show_decorations(struct rev_info
*opt
, struct commit
*commit
)
62 struct name_decoration
*decoration
;
64 if (opt
->show_source
&& commit
->util
)
65 printf("\t%s", (char *) commit
->util
);
66 if (!opt
->show_decorations
)
68 decoration
= lookup_decoration(&name_decoration
, &commit
->object
);
73 printf("%s%s", prefix
, decoration
->name
);
75 decoration
= decoration
->next
;
81 * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
82 * Signed-off-by: and Acked-by: lines.
84 static int detect_any_signoff(char *letter
, int size
)
93 while (letter
<= --cp
&& *cp
== '\n')
96 while (letter
<= cp
) {
115 if (('A' <= ch
&& ch
<= 'Z') ||
116 ('a' <= ch
&& ch
<= 'z') ||
121 /* no empty last line doesn't match */
124 return seen_head
&& seen_name
;
127 static void append_signoff(struct strbuf
*sb
, const char *signoff
)
129 static const char signed_off_by
[] = "Signed-off-by: ";
130 size_t signoff_len
= strlen(signoff
);
136 /* First see if we already have the sign-off by the signer */
137 while ((cp
= strstr(cp
, signed_off_by
))) {
141 cp
+= strlen(signed_off_by
);
142 if (cp
+ signoff_len
>= sb
->buf
+ sb
->len
)
144 if (strncmp(cp
, signoff
, signoff_len
))
146 if (!isspace(cp
[signoff_len
]))
148 /* we already have him */
153 has_signoff
= detect_any_signoff(sb
->buf
, sb
->len
);
156 strbuf_addch(sb
, '\n');
158 strbuf_addstr(sb
, signed_off_by
);
159 strbuf_add(sb
, signoff
, signoff_len
);
160 strbuf_addch(sb
, '\n');
163 static unsigned int digits_in_number(unsigned int number
)
165 unsigned int i
= 10, result
= 1;
166 while (i
<= number
) {
173 void get_patch_filename(struct commit
*commit
, int nr
, const char *suffix
,
176 int suffix_len
= strlen(suffix
) + 1;
177 int start_len
= buf
->len
;
179 strbuf_addf(buf
, commit
? "%04d-" : "%d", nr
);
181 int max_len
= start_len
+ FORMAT_PATCH_NAME_MAX
- suffix_len
;
183 format_commit_message(commit
, "%f", buf
, DATE_NORMAL
);
184 if (max_len
< buf
->len
)
185 strbuf_setlen(buf
, max_len
);
186 strbuf_addstr(buf
, suffix
);
190 void log_write_email_headers(struct rev_info
*opt
, struct commit
*commit
,
191 const char **subject_p
,
192 const char **extra_headers_p
,
193 int *need_8bit_cte_p
)
195 const char *subject
= NULL
;
196 const char *extra_headers
= opt
->extra_headers
;
197 const char *name
= sha1_to_hex(commit
->object
.sha1
);
199 *need_8bit_cte_p
= 0; /* unknown */
200 if (opt
->total
> 0) {
201 static char buffer
[64];
202 snprintf(buffer
, sizeof(buffer
),
203 "Subject: [%s %0*d/%d] ",
205 digits_in_number(opt
->total
),
206 opt
->nr
, opt
->total
);
208 } else if (opt
->total
== 0 && opt
->subject_prefix
&& *opt
->subject_prefix
) {
209 static char buffer
[256];
210 snprintf(buffer
, sizeof(buffer
),
212 opt
->subject_prefix
);
215 subject
= "Subject: ";
218 printf("From %s Mon Sep 17 00:00:00 2001\n", name
);
219 graph_show_oneline(opt
->graph
);
220 if (opt
->message_id
) {
221 printf("Message-Id: <%s>\n", opt
->message_id
);
222 graph_show_oneline(opt
->graph
);
224 if (opt
->ref_message_ids
&& opt
->ref_message_ids
->nr
> 0) {
226 n
= opt
->ref_message_ids
->nr
;
227 printf("In-Reply-To: <%s>\n", opt
->ref_message_ids
->items
[n
-1].string
);
228 for (i
= 0; i
< n
; i
++)
229 printf("%s<%s>\n", (i
> 0 ? "\t" : "References: "),
230 opt
->ref_message_ids
->items
[i
].string
);
231 graph_show_oneline(opt
->graph
);
233 if (opt
->mime_boundary
) {
234 static char subject_buffer
[1024];
235 static char buffer
[1024];
236 struct strbuf filename
= STRBUF_INIT
;
237 *need_8bit_cte_p
= -1; /* NEVER */
238 snprintf(subject_buffer
, sizeof(subject_buffer
) - 1,
240 "MIME-Version: 1.0\n"
241 "Content-Type: multipart/mixed;"
242 " boundary=\"%s%s\"\n"
244 "This is a multi-part message in MIME "
247 "Content-Type: text/plain; "
248 "charset=UTF-8; format=fixed\n"
249 "Content-Transfer-Encoding: 8bit\n\n",
250 extra_headers
? extra_headers
: "",
251 mime_boundary_leader
, opt
->mime_boundary
,
252 mime_boundary_leader
, opt
->mime_boundary
);
253 extra_headers
= subject_buffer
;
255 get_patch_filename(opt
->numbered_files
? NULL
: commit
, opt
->nr
,
256 opt
->patch_suffix
, &filename
);
257 snprintf(buffer
, sizeof(buffer
) - 1,
259 "Content-Type: text/x-patch;"
261 "Content-Transfer-Encoding: 8bit\n"
262 "Content-Disposition: %s;"
263 " filename=\"%s\"\n\n",
264 mime_boundary_leader
, opt
->mime_boundary
,
266 opt
->no_inline
? "attachment" : "inline",
268 opt
->diffopt
.stat_sep
= buffer
;
269 strbuf_release(&filename
);
271 *subject_p
= subject
;
272 *extra_headers_p
= extra_headers
;
275 void show_log(struct rev_info
*opt
)
277 struct strbuf msgbuf
= STRBUF_INIT
;
278 struct log_info
*log
= opt
->loginfo
;
279 struct commit
*commit
= log
->commit
, *parent
= log
->parent
;
280 int abbrev
= opt
->diffopt
.abbrev
;
281 int abbrev_commit
= opt
->abbrev_commit
? opt
->abbrev
: 40;
282 const char *subject
= NULL
, *extra_headers
= opt
->extra_headers
;
283 int need_8bit_cte
= 0;
286 if (!opt
->verbose_header
) {
287 graph_show_commit(opt
->graph
);
290 if (commit
->object
.flags
& BOUNDARY
)
292 else if (commit
->object
.flags
& UNINTERESTING
)
294 else if (opt
->left_right
) {
295 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
301 fputs(find_unique_abbrev(commit
->object
.sha1
, abbrev_commit
), stdout
);
302 if (opt
->print_parents
)
303 show_parents(commit
, abbrev_commit
);
304 show_decorations(opt
, commit
);
305 if (opt
->graph
&& !graph_is_commit_finished(opt
->graph
)) {
307 graph_show_remainder(opt
->graph
);
309 putchar(opt
->diffopt
.line_termination
);
314 * If use_terminator is set, we already handled any record termination
315 * at the end of the last record.
316 * Otherwise, add a diffopt.line_termination character before all
317 * entries but the first. (IOW, as a separator between entries)
319 if (opt
->shown_one
&& !opt
->use_terminator
) {
321 * If entries are separated by a newline, the output
322 * should look human-readable. If the last entry ended
323 * with a newline, print the graph output before this
324 * newline. Otherwise it will end up as a completely blank
325 * line and will look like a gap in the graph.
327 * If the entry separator is not a newline, the output is
328 * primarily intended for programmatic consumption, and we
329 * never want the extra graph output before the entry
332 if (opt
->diffopt
.line_termination
== '\n' &&
333 !opt
->missing_newline
)
334 graph_show_padding(opt
->graph
);
335 putchar(opt
->diffopt
.line_termination
);
340 * If the history graph was requested,
341 * print the graph, up to this commit's line
343 graph_show_commit(opt
->graph
);
346 * Print header line of header..
349 if (opt
->commit_format
== CMIT_FMT_EMAIL
) {
350 log_write_email_headers(opt
, commit
, &subject
, &extra_headers
,
352 } else if (opt
->commit_format
!= CMIT_FMT_USERFORMAT
) {
353 fputs(diff_get_color_opt(&opt
->diffopt
, DIFF_COMMIT
), stdout
);
354 if (opt
->commit_format
!= CMIT_FMT_ONELINE
)
355 fputs("commit ", stdout
);
358 if (commit
->object
.flags
& BOUNDARY
)
360 else if (commit
->object
.flags
& UNINTERESTING
)
362 else if (opt
->left_right
) {
363 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
369 fputs(find_unique_abbrev(commit
->object
.sha1
, abbrev_commit
),
371 if (opt
->print_parents
)
372 show_parents(commit
, abbrev_commit
);
375 find_unique_abbrev(parent
->object
.sha1
,
377 show_decorations(opt
, commit
);
378 printf("%s", diff_get_color_opt(&opt
->diffopt
, DIFF_RESET
));
379 if (opt
->commit_format
== CMIT_FMT_ONELINE
) {
383 graph_show_oneline(opt
->graph
);
385 if (opt
->reflog_info
) {
387 * setup_revisions() ensures that opt->reflog_info
388 * and opt->graph cannot both be set,
389 * so we don't need to worry about printing the
392 show_reflog_message(opt
->reflog_info
,
393 opt
->commit_format
== CMIT_FMT_ONELINE
,
394 opt
->date_mode_explicit
?
397 if (opt
->commit_format
== CMIT_FMT_ONELINE
)
406 * And then the pretty-printed message itself
408 if (need_8bit_cte
>= 0)
409 need_8bit_cte
= has_non_ascii(opt
->add_signoff
);
410 pretty_print_commit(opt
->commit_format
, commit
, &msgbuf
,
411 abbrev
, subject
, extra_headers
, opt
->date_mode
,
414 if (opt
->add_signoff
)
415 append_signoff(&msgbuf
, opt
->add_signoff
);
416 if (opt
->show_log_size
) {
417 printf("log size %i\n", (int)msgbuf
.len
);
418 graph_show_oneline(opt
->graph
);
422 * Set opt->missing_newline if msgbuf doesn't
423 * end in a newline (including if it is empty)
425 if (!msgbuf
.len
|| msgbuf
.buf
[msgbuf
.len
- 1] != '\n')
426 opt
->missing_newline
= 1;
428 opt
->missing_newline
= 0;
431 graph_show_commit_msg(opt
->graph
, &msgbuf
);
433 fwrite(msgbuf
.buf
, sizeof(char), msgbuf
.len
, stdout
);
434 if (opt
->use_terminator
) {
435 if (!opt
->missing_newline
)
436 graph_show_padding(opt
->graph
);
440 strbuf_release(&msgbuf
);
443 int log_tree_diff_flush(struct rev_info
*opt
)
445 diffcore_std(&opt
->diffopt
);
447 if (diff_queue_is_empty()) {
448 int saved_fmt
= opt
->diffopt
.output_format
;
449 opt
->diffopt
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
450 diff_flush(&opt
->diffopt
);
451 opt
->diffopt
.output_format
= saved_fmt
;
455 if (opt
->loginfo
&& !opt
->no_commit_id
) {
456 /* When showing a verbose header (i.e. log message),
457 * and not in --pretty=oneline format, we would want
458 * an extra newline between the end of log and the
459 * output for readability.
462 if ((opt
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
) &&
463 opt
->verbose_header
&&
464 opt
->commit_format
!= CMIT_FMT_ONELINE
) {
465 int pch
= DIFF_FORMAT_DIFFSTAT
| DIFF_FORMAT_PATCH
;
466 if ((pch
& opt
->diffopt
.output_format
) == pch
)
471 diff_flush(&opt
->diffopt
);
475 static int do_diff_combined(struct rev_info
*opt
, struct commit
*commit
)
477 unsigned const char *sha1
= commit
->object
.sha1
;
479 diff_tree_combined_merge(sha1
, opt
->dense_combined_merges
, opt
);
480 return !opt
->loginfo
;
484 * Show the diff of a commit.
486 * Return true if we printed any log info messages
488 static int log_tree_diff(struct rev_info
*opt
, struct commit
*commit
, struct log_info
*log
)
491 struct commit_list
*parents
;
492 unsigned const char *sha1
= commit
->object
.sha1
;
494 if (!opt
->diff
&& !DIFF_OPT_TST(&opt
->diffopt
, EXIT_WITH_STATUS
))
498 parents
= commit
->parents
;
500 if (opt
->show_root_diff
) {
501 diff_root_tree_sha1(sha1
, "", &opt
->diffopt
);
502 log_tree_diff_flush(opt
);
504 return !opt
->loginfo
;
507 /* More than one parent? */
508 if (parents
&& parents
->next
) {
509 if (opt
->ignore_merges
)
511 else if (opt
->combine_merges
)
512 return do_diff_combined(opt
, commit
);
514 /* If we show individual diffs, show the parent info */
515 log
->parent
= parents
->item
;
520 struct commit
*parent
= parents
->item
;
522 diff_tree_sha1(parent
->object
.sha1
, sha1
, "", &opt
->diffopt
);
523 log_tree_diff_flush(opt
);
525 showed_log
|= !opt
->loginfo
;
527 /* Set up the log info for the next parent, if any.. */
528 parents
= parents
->next
;
531 log
->parent
= parents
->item
;
537 int log_tree_commit(struct rev_info
*opt
, struct commit
*commit
)
546 shown
= log_tree_diff(opt
, commit
, &log
);
547 if (!shown
&& opt
->loginfo
&& opt
->always_show_header
) {
553 maybe_flush_or_die(stdout
, "stdout");