log-tree: respect diffopt's configured output file stream

The diff options already know how to print the output anywhere else than stdout. The same is needed for log output in general, e.g. when writing patches to files in `git format-patch`. Let's allow users to use log_tree_commit() *without* changing global state via freopen(). Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Jun 22, 2016 at 17:01 UTC 4d7b0efc5e2eea1923803dd9c5d5d1288c99cd00
1 file changed +32 -32
log-tree.c
+32 -32
@@ -159,12 +159,12 @@ void load_ref_decorations(int flags)
159 }
160 }
161
162 -static void show_parents(struct commit *commit, int abbrev)
162 +static void show_parents(struct commit *commit, int abbrev, FILE *file)
163 {
164 struct commit_list *p;
165 for (p = commit->parents; p ; p = p->next) {
166 struct commit *parent = p->item;
167 - printf(" %s", find_unique_abbrev(parent->object.oid.hash, abbrev));
167 + fprintf(file, " %s", find_unique_abbrev(parent->object.oid.hash, abbrev));
168 }
169 }
170
@@ -172,7 +172,7 @@ static void show_children(struct rev_info *opt, struct commit *commit, int abbre
172 {
173 struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
174 for ( ; p; p = p->next) {
175 - printf(" %s", find_unique_abbrev(p->item->object.oid.hash, abbrev));
175 + fprintf(opt->diffopt.file, " %s", find_unique_abbrev(p->item->object.oid.hash, abbrev));
176 }
177 }
178
@@ -286,11 +286,11 @@ void show_decorations(struct rev_info *opt, struct commit *commit)
286 struct strbuf sb = STRBUF_INIT;
287
288 if (opt->show_source && commit->util)
289 - printf("\t%s", (char *) commit->util);
289 + fprintf(opt->diffopt.file, "\t%s", (char *) commit->util);
290 if (!opt->show_decorations)
291 return;
292 format_decorations(&sb, commit, opt->diffopt.use_color);
293 - fputs(sb.buf, stdout);
293 + fputs(sb.buf, opt->diffopt.file);
294 strbuf_release(&sb);
295 }
296
@@ -364,18 +364,18 @@ void log_write_email_headers(struct rev_info *opt, struct commit *commit,
364 subject = "Subject: ";
365 }
366
367 - printf("From %s Mon Sep 17 00:00:00 2001\n", name);
367 + fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
368 graph_show_oneline(opt->graph);
369 if (opt->message_id) {
370 - printf("Message-Id: <%s>\n", opt->message_id);
370 + fprintf(opt->diffopt.file, "Message-Id: <%s>\n", opt->message_id);
371 graph_show_oneline(opt->graph);
372 }
373 if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
374 int i, n;
375 n = opt->ref_message_ids->nr;
376 - printf("In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
376 + fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
377 for (i = 0; i < n; i++)
378 - printf("%s<%s>\n", (i > 0 ? "\t" : "References: "),
378 + fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
379 opt->ref_message_ids->items[i].string);
380 graph_show_oneline(opt->graph);
381 }
@@ -432,7 +432,7 @@ static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
432 reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
433 while (*bol) {
434 eol = strchrnul(bol, '\n');
435 - printf("%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
435 + fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
436 *eol ? "\n" : "");
437 graph_show_oneline(opt->graph);
438 bol = (*eol) ? (eol + 1) : eol;
@@ -553,17 +553,17 @@ void show_log(struct rev_info *opt)
553
554 if (!opt->graph)
555 put_revision_mark(opt, commit);
556 - fputs(find_unique_abbrev(commit->object.oid.hash, abbrev_commit), stdout);
556 + fputs(find_unique_abbrev(commit->object.oid.hash, abbrev_commit), opt->diffopt.file);
557 if (opt->print_parents)
558 - show_parents(commit, abbrev_commit);
558 + show_parents(commit, abbrev_commit, opt->diffopt.file);
559 if (opt->children.name)
560 show_children(opt, commit, abbrev_commit);
561 show_decorations(opt, commit);
562 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
563 - putchar('\n');
563 + putc('\n', opt->diffopt.file);
564 graph_show_remainder(opt->graph);
565 }
566 - putchar(opt->diffopt.line_termination);
566 + putc(opt->diffopt.line_termination, opt->diffopt.file);
567 return;
568 }
569
@@ -589,7 +589,7 @@ void show_log(struct rev_info *opt)
589 if (opt->diffopt.line_termination == '\n' &&
590 !opt->missing_newline)
591 graph_show_padding(opt->graph);
592 - putchar(opt->diffopt.line_termination);
592 + putc(opt->diffopt.line_termination, opt->diffopt.file);
593 }
594 opt->shown_one = 1;
595
@@ -607,28 +607,28 @@ void show_log(struct rev_info *opt)
607 log_write_email_headers(opt, commit, &ctx.subject, &extra_headers,
608 &ctx.need_8bit_cte);
609 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
610 - fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
610 + fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
611 if (opt->commit_format != CMIT_FMT_ONELINE)
612 - fputs("commit ", stdout);
612 + fputs("commit ", opt->diffopt.file);
613
614 if (!opt->graph)
615 put_revision_mark(opt, commit);
616 fputs(find_unique_abbrev(commit->object.oid.hash, abbrev_commit),
617 - stdout);
617 + opt->diffopt.file);
618 if (opt->print_parents)
619 - show_parents(commit, abbrev_commit);
619 + show_parents(commit, abbrev_commit, opt->diffopt.file);
620 if (opt->children.name)
621 show_children(opt, commit, abbrev_commit);
622 if (parent)
623 - printf(" (from %s)",
623 + fprintf(opt->diffopt.file, " (from %s)",
624 find_unique_abbrev(parent->object.oid.hash,
625 abbrev_commit));
626 - fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), stdout);
626 + fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
627 show_decorations(opt, commit);
628 if (opt->commit_format == CMIT_FMT_ONELINE) {
629 - putchar(' ');
629 + putc(' ', opt->diffopt.file);
630 } else {
631 - putchar('\n');
631 + putc('\n', opt->diffopt.file);
632 graph_show_oneline(opt->graph);
633 }
634 if (opt->reflog_info) {
@@ -702,7 +702,7 @@ void show_log(struct rev_info *opt)
702 }
703
704 if (opt->show_log_size) {
705 - printf("log size %i\n", (int)msgbuf.len);
705 + fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
706 graph_show_oneline(opt->graph);
707 }
708
@@ -718,11 +718,11 @@ void show_log(struct rev_info *opt)
718 if (opt->graph)
719 graph_show_commit_msg(opt->graph, &msgbuf);
720 else
721 - fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
721 + fwrite(msgbuf.buf, sizeof(char), msgbuf.len, opt->diffopt.file);
722 if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
723 if (!opt->missing_newline)
724 graph_show_padding(opt->graph);
725 - putchar(opt->diffopt.line_termination);
725 + putc(opt->diffopt.line_termination, opt->diffopt.file);
726 }
727
728 strbuf_release(&msgbuf);
@@ -759,7 +759,7 @@ int log_tree_diff_flush(struct rev_info *opt)
759 struct strbuf *msg = NULL;
760 msg = opt->diffopt.output_prefix(&opt->diffopt,
761 opt->diffopt.output_prefix_data);
762 - fwrite(msg->buf, msg->len, 1, stdout);
762 + fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
763 }
764
765 /*
@@ -774,8 +774,8 @@ int log_tree_diff_flush(struct rev_info *opt)
774 */
775 if (!opt->shown_dashes &&
776 (pch & opt->diffopt.output_format) == pch)
777 - printf("---");
778 - putchar('\n');
777 + fprintf(opt->diffopt.file, "---");
778 + putc('\n', opt->diffopt.file);
779 }
780 }
781 diff_flush(&opt->diffopt);
@@ -873,7 +873,7 @@ int log_tree_commit(struct rev_info *opt, struct commit *commit)
873 return line_log_print(opt, commit);
874
875 if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
876 - printf("\n%s\n", opt->break_bar);
876 + fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
877 shown = log_tree_diff(opt, commit, &log);
878 if (!shown && opt->loginfo && opt->always_show_header) {
879 log.parent = NULL;
@@ -881,9 +881,9 @@ int log_tree_commit(struct rev_info *opt, struct commit *commit)
881 shown = 1;
882 }
883 if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
884 - printf("\n%s\n", opt->break_bar);
884 + fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
885 opt->loginfo = NULL;
886 - maybe_flush_or_die(stdout, "stdout");
886 + maybe_flush_or_die(opt->diffopt.file, "stdout");
887 if (close_file)
888 fclose(opt->diffopt.file);
889 return shown;