git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Johannes Schindelin <johannes.schindelin@gmx.de>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>,
	Eric Sunshine <sunshine@sunshineco.com>
Subject: [PATCH v3 8/9] format-patch: avoid freopen()
Date: Tue, 21 Jun 2016 12:35:20 +0200 (CEST)	[thread overview]
Message-ID: <b923b096901728b2f82313a083285ed31df52942.1466505223.git.johannes.schindelin@gmx.de> (raw)
In-Reply-To: <cover.1466505222.git.johannes.schindelin@gmx.de>

We just taught the relevant functions to respect the diffopt.file field,
to allow writing somewhere else than stdout. Let's make use of it.

Technically, we do not need to avoid that call in a builtin: we assume
that builtins (as opposed to library functions) are stand-alone programs
that may do with their (global) state. Yet, we want to be able to reuse
that code in properly lib-ified code, e.g. when converting scripts into
builtins.

Further, while we did not *have* to touch the cmd_show() and cmd_cherry()
code paths (because they do not want to write anywhere but stdout as of
yet), it just makes sense to be consistent, making it easier and safer to
move the code later.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 builtin/log.c | 64 ++++++++++++++++++++++++++++++-----------------------------
 1 file changed, 33 insertions(+), 31 deletions(-)

diff --git a/builtin/log.c b/builtin/log.c
index abd889b..8dcf205 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -236,7 +236,7 @@ static void show_early_header(struct rev_info *rev, const char *stage, int nr)
 		if (rev->commit_format != CMIT_FMT_ONELINE)
 			putchar(rev->diffopt.line_termination);
 	}
-	printf(_("Final output: %d %s\n"), nr, stage);
+	fprintf(rev->diffopt.file, _("Final output: %d %s\n"), nr, stage);
 }
 
 static struct itimerval early_output_timer;
@@ -445,7 +445,7 @@ static void show_tagger(char *buf, int len, struct rev_info *rev)
 	pp.fmt = rev->commit_format;
 	pp.date_mode = rev->date_mode;
 	pp_user_info(&pp, "Tagger", &out, buf, get_log_output_encoding());
-	printf("%s", out.buf);
+	fprintf(rev->diffopt.file, "%s", out.buf);
 	strbuf_release(&out);
 }
 
@@ -456,7 +456,7 @@ static int show_blob_object(const unsigned char *sha1, struct rev_info *rev, con
 	char *buf;
 	unsigned long size;
 
-	fflush(stdout);
+	fflush(rev->diffopt.file);
 	if (!DIFF_OPT_TOUCHED(&rev->diffopt, ALLOW_TEXTCONV) ||
 	    !DIFF_OPT_TST(&rev->diffopt, ALLOW_TEXTCONV))
 		return stream_blob_to_fd(1, sha1, NULL, 0);
@@ -496,7 +496,7 @@ static int show_tag_object(const unsigned char *sha1, struct rev_info *rev)
 	}
 
 	if (offset < size)
-		fwrite(buf + offset, size - offset, 1, stdout);
+		fwrite(buf + offset, size - offset, 1, rev->diffopt.file);
 	free(buf);
 	return 0;
 }
@@ -505,7 +505,8 @@ static int show_tree_object(const unsigned char *sha1,
 		struct strbuf *base,
 		const char *pathname, unsigned mode, int stage, void *context)
 {
-	printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
+	FILE *file = context;
+	fprintf(file, "%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
 	return 0;
 }
 
@@ -565,7 +566,7 @@ int cmd_show(int argc, const char **argv, const char *prefix)
 
 			if (rev.shown_one)
 				putchar('\n');
-			printf("%stag %s%s\n",
+			fprintf(rev.diffopt.file, "%stag %s%s\n",
 					diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
 					t->tag,
 					diff_get_color_opt(&rev.diffopt, DIFF_RESET));
@@ -584,12 +585,12 @@ int cmd_show(int argc, const char **argv, const char *prefix)
 		case OBJ_TREE:
 			if (rev.shown_one)
 				putchar('\n');
-			printf("%stree %s%s\n\n",
+			fprintf(rev.diffopt.file, "%stree %s%s\n\n",
 					diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
 					name,
 					diff_get_color_opt(&rev.diffopt, DIFF_RESET));
 			read_tree_recursive((struct tree *)o, "", 0, 0, &match_all,
-					show_tree_object, NULL);
+					show_tree_object, rev.diffopt.file);
 			rev.shown_one = 1;
 			break;
 		case OBJ_COMMIT:
@@ -799,7 +800,7 @@ static FILE *realstdout = NULL;
 static const char *output_directory = NULL;
 static int outdir_offset;
 
-static int reopen_stdout(struct commit *commit, const char *subject,
+static int open_next_file(struct commit *commit, const char *subject,
 			 struct rev_info *rev, int quiet)
 {
 	struct strbuf filename = STRBUF_INIT;
@@ -823,7 +824,7 @@ static int reopen_stdout(struct commit *commit, const char *subject,
 	if (!quiet)
 		fprintf(realstdout, "%s\n", filename.buf + outdir_offset);
 
-	if (freopen(filename.buf, "w", stdout) == NULL)
+	if ((rev->diffopt.file = fopen(filename.buf, "w")) == NULL)
 		return error(_("Cannot open patch file %s"), filename.buf);
 
 	strbuf_release(&filename);
@@ -882,15 +883,15 @@ static void gen_message_id(struct rev_info *info, char *base)
 	info->message_id = strbuf_detach(&buf, NULL);
 }
 
-static void print_signature(void)
+static void print_signature(FILE *file)
 {
 	if (!signature || !*signature)
 		return;
 
-	printf("-- \n%s", signature);
+	fprintf(file, "-- \n%s", signature);
 	if (signature[strlen(signature)-1] != '\n')
-		putchar('\n');
-	putchar('\n');
+		putc('\n', file);
+	putc('\n', file);
 }
 
 static void add_branch_description(struct strbuf *buf, const char *branch_name)
@@ -959,7 +960,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
 	committer = git_committer_info(0);
 
 	if (!use_stdout &&
-	    reopen_stdout(NULL, rev->numbered_files ? NULL : "cover-letter", rev, quiet))
+	    open_next_file(NULL, rev->numbered_files ? NULL : "cover-letter", rev, quiet))
 		return;
 
 	log_write_email_headers(rev, head, &pp.subject, &pp.after_subject,
@@ -982,7 +983,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
 	pp_title_line(&pp, &msg, &sb, encoding, need_8bit_cte);
 	pp_remainder(&pp, &msg, &sb, 0);
 	add_branch_description(&sb, branch_name);
-	printf("%s\n", sb.buf);
+	fprintf(rev->diffopt.file, "%s\n", sb.buf);
 
 	strbuf_release(&sb);
 
@@ -991,6 +992,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
 	log.wrap = 72;
 	log.in1 = 2;
 	log.in2 = 4;
+	log.file = rev->diffopt.file;
 	for (i = 0; i < nr; i++)
 		shortlog_add_commit(&log, list[i]);
 
@@ -1013,8 +1015,8 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
 	diffcore_std(&opts);
 	diff_flush(&opts);
 
-	printf("\n");
-	print_signature();
+	fprintf(rev->diffopt.file, "\n");
+	print_signature(rev->diffopt.file);
 }
 
 static const char *clean_message_id(const char *msg_id)
@@ -1324,7 +1326,7 @@ static void prepare_bases(struct base_tree_info *bases,
 	}
 }
 
-static void print_bases(struct base_tree_info *bases)
+static void print_bases(struct base_tree_info *bases, FILE *file)
 {
 	int i;
 
@@ -1333,11 +1335,11 @@ static void print_bases(struct base_tree_info *bases)
 		return;
 
 	/* Show the base commit */
-	printf("base-commit: %s\n", oid_to_hex(&bases->base_commit));
+	fprintf(file, "base-commit: %s\n", oid_to_hex(&bases->base_commit));
 
 	/* Show the prerequisite patches */
 	for (i = bases->nr_patch_id - 1; i >= 0; i--)
-		printf("prerequisite-patch-id: %s\n", oid_to_hex(&bases->patch_id[i]));
+		fprintf(file, "prerequisite-patch-id: %s\n", oid_to_hex(&bases->patch_id[i]));
 
 	free(bases->patch_id);
 	bases->nr_patch_id = 0;
@@ -1694,7 +1696,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 			gen_message_id(&rev, "cover");
 		make_cover_letter(&rev, use_stdout,
 				  origin, nr, list, branch_name, quiet);
-		print_bases(&bases);
+		print_bases(&bases, rev.diffopt.file);
 		total++;
 		start_number--;
 	}
@@ -1740,7 +1742,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 		}
 
 		if (!use_stdout &&
-		    reopen_stdout(rev.numbered_files ? NULL : commit, NULL, &rev, quiet))
+		    open_next_file(rev.numbered_files ? NULL : commit, NULL, &rev, quiet))
 			die(_("Failed to create output files"));
 		shown = log_tree_commit(&rev, commit);
 		free_commit_buffer(commit);
@@ -1755,15 +1757,15 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 			rev.shown_one = 0;
 		if (shown) {
 			if (rev.mime_boundary)
-				printf("\n--%s%s--\n\n\n",
+				fprintf(rev.diffopt.file, "\n--%s%s--\n\n\n",
 				       mime_boundary_leader,
 				       rev.mime_boundary);
 			else
-				print_signature();
-			print_bases(&bases);
+				print_signature(rev.diffopt.file);
+			print_bases(&bases, rev.diffopt.file);
 		}
 		if (!use_stdout)
-			fclose(stdout);
+			fclose(rev.diffopt.file);
 	}
 	free(list);
 	free(branch_name);
@@ -1795,15 +1797,15 @@ static const char * const cherry_usage[] = {
 };
 
 static void print_commit(char sign, struct commit *commit, int verbose,
-			 int abbrev)
+			 int abbrev, FILE *file)
 {
 	if (!verbose) {
-		printf("%c %s\n", sign,
+		fprintf(file, "%c %s\n", sign,
 		       find_unique_abbrev(commit->object.oid.hash, abbrev));
 	} else {
 		struct strbuf buf = STRBUF_INIT;
 		pp_commit_easy(CMIT_FMT_ONELINE, commit, &buf);
-		printf("%c %s %s\n", sign,
+		fprintf(file, "%c %s %s\n", sign,
 		       find_unique_abbrev(commit->object.oid.hash, abbrev),
 		       buf.buf);
 		strbuf_release(&buf);
@@ -1884,7 +1886,7 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)
 		commit = list->item;
 		if (has_commit_patch_id(commit, &ids))
 			sign = '-';
-		print_commit(sign, commit, verbose, abbrev);
+		print_commit(sign, commit, verbose, abbrev, revs.diffopt.file);
 		list = list->next;
 	}
 
-- 
2.9.0.118.g0e1a633



  parent reply	other threads:[~2016-06-21 10:39 UTC|newest]

Thread overview: 67+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-18 10:03 [PATCH 0/5] Let log-tree and friends respect diffopt's `file` field Johannes Schindelin
2016-06-18 10:03 ` [PATCH 1/5] log-tree: respect diffopt's configured output file stream Johannes Schindelin
2016-06-18 10:03 ` [PATCH 2/5] line-log: " Johannes Schindelin
2016-06-18 10:04 ` [PATCH 3/5] graph: respect the diffopt.file setting Johannes Schindelin
2016-06-18 10:04 ` [PATCH 4/5] shortlog: support outputting to streams other than stdout Johannes Schindelin
2016-06-18 10:04 ` [PATCH 5/5] format-patch: avoid freopen() Johannes Schindelin
2016-06-19 20:01   ` Eric Sunshine
2016-06-20  6:26     ` Johannes Schindelin
2016-06-20  6:32       ` Eric Sunshine
2016-06-20 10:09         ` Johannes Schindelin
2016-06-20 16:03       ` Junio C Hamano
2016-06-21  7:15         ` Johannes Schindelin
2016-06-21 16:50           ` Junio C Hamano
2016-06-22  7:24             ` Johannes Schindelin
2016-06-22 15:49               ` Junio C Hamano
2016-06-22 16:14                 ` Johannes Schindelin
2016-06-22 17:37                   ` Junio C Hamano
2016-06-22 17:53                   ` Junio C Hamano
2016-06-20 10:55 ` [PATCH v2 0/7] Let log-tree and friends respect diffopt's `file` field Johannes Schindelin
2016-06-20 10:55   ` [PATCH v2 1/7] log-tree: respect diffopt's configured output file stream Johannes Schindelin
2016-06-20 17:01     ` Junio C Hamano
2016-06-21  7:31       ` Johannes Schindelin
2016-06-21  7:38         ` Johannes Schindelin
2016-06-21 10:39           ` Johannes Schindelin
2016-06-20 10:55   ` [PATCH v2 2/7] line-log: " Johannes Schindelin
2016-06-20 10:55   ` [PATCH v2 3/7] graph: respect the diffopt.file setting Johannes Schindelin
2016-06-20 10:55   ` [PATCH v2 4/7] shortlog: support outputting to streams other than stdout Johannes Schindelin
2016-06-20 10:55   ` [PATCH v2 5/7] format-patch: explicitly switch off color when writing to files Johannes Schindelin
2016-06-20 10:55   ` [PATCH v2 6/7] format-patch: avoid freopen() Johannes Schindelin
2016-06-20 10:56   ` [PATCH v2 7/7] format-patch: use stdout directly Johannes Schindelin
2016-06-20 18:57     ` Junio C Hamano
2016-06-20 11:50   ` [PATCH v2 0/7] Let log-tree and friends respect diffopt's `file` field Johannes Schindelin
2016-06-21 10:34   ` [PATCH v3 0/9] " Johannes Schindelin
2016-06-21 10:34     ` [PATCH v3 1/9] am: stop ignoring errors reported by log_tree_diff() Johannes Schindelin
2016-06-21 18:59       ` Junio C Hamano
2016-06-22 12:21         ` Johannes Schindelin
2016-06-21 10:34     ` [PATCH v3 2/9] Disallow diffopt.close_file when using the log_tree machinery Johannes Schindelin
2016-06-21 18:14       ` Junio C Hamano
2016-06-21 19:05         ` Junio C Hamano
2016-06-21 19:32           ` Junio C Hamano
2016-06-22 15:17             ` Johannes Schindelin
2016-06-21 10:34     ` [PATCH v3 3/9] log-tree: respect diffopt's configured output file stream Johannes Schindelin
2016-06-21 10:35     ` [PATCH v3 4/9] line-log: " Johannes Schindelin
2016-06-21 10:35     ` [PATCH v3 5/9] graph: respect the diffopt.file setting Johannes Schindelin
2016-06-21 10:35     ` [PATCH v3 6/9] shortlog: support outputting to streams other than stdout Johannes Schindelin
2016-06-21 10:35     ` [PATCH v3 7/9] format-patch: explicitly switch off color when writing to files Johannes Schindelin
2016-06-21 10:35     ` Johannes Schindelin [this message]
2016-06-21 10:35     ` [PATCH v3 9/9] format-patch: use stdout directly Johannes Schindelin
2016-06-21 13:47     ` [PATCH v3 0/9] Let log-tree and friends respect diffopt's `file` field Paul Tan
2016-06-21 14:12       ` Johannes Schindelin
2016-06-22  9:23         ` Paul Tan
2016-06-22 15:01     ` [PATCH v4 00/10] " Johannes Schindelin
2016-06-22 15:01       ` [PATCH v4 01/10] Prepare log/log-tree to reuse the diffopt.close_file attribute Johannes Schindelin
2016-06-24 20:56         ` Junio C Hamano
2016-06-26  6:56           ` Johannes Schindelin
2016-06-22 15:01       ` [PATCH v4 02/10] log-tree: respect diffopt's configured output file stream Johannes Schindelin
2016-06-22 15:01       ` [PATCH v4 03/10] line-log: " Johannes Schindelin
2016-06-22 15:01       ` [PATCH v4 04/10] graph: respect the diffopt.file setting Johannes Schindelin
2016-06-22 15:01       ` [PATCH v4 05/10] shortlog: support outputting to streams other than stdout Johannes Schindelin
2016-06-22 15:01       ` [PATCH v4 06/10] format-patch: explicitly switch off color when writing to files Johannes Schindelin
2016-06-24 22:01         ` Junio C Hamano
2016-06-26  6:49           ` Johannes Schindelin
2016-06-22 15:01       ` [PATCH v4 07/10] format-patch: avoid freopen() Johannes Schindelin
2016-06-22 15:02       ` [PATCH v4 08/10] format-patch: use stdout directly Johannes Schindelin
2016-06-24 22:03         ` Junio C Hamano
2016-06-22 15:02       ` [PATCH v4 09/10] shortlog: respect the --output=<file> setting Johannes Schindelin
2016-06-22 15:02       ` [PATCH v4 10/10] Ensure that log respects --output=<file> Johannes Schindelin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b923b096901728b2f82313a083285ed31df52942.1466505223.git.johannes.schindelin@gmx.de \
    --to=johannes.schindelin@gmx.de \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=sunshine@sunshineco.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).