All of lore.kernel.org
 help / color / mirror / Atom feed
From: Olga Telezhnaya <olyatelezhnaya@gmail.com>
To: git@vger.kernel.org
Subject: [PATCH RFC 13/20] cat-file: rewrite print_object_or_die
Date: Fri, 22 Feb 2019 16:05:45 +0000	[thread overview]
Message-ID: <0102016915f49a66-0b8bf78d-6879-41da-b5e6-de6e7edbe71a-000000@eu-west-1.amazonses.com> (raw)
In-Reply-To: <0102016915f499b8-5813fc52-230b-469e-b939-a1244e83a2b9-000000@eu-west-1.amazonses.com>

In the next commit I will move function print_object_or_die
to ref-filter, and I decided to rewrite it a little so that it
becomes much more flatten and a little bit shorter.
I also changed input parameters, it allows me to move it
to ref-filter, ref-filter knows nothing about batch_options.

The logic of the function remains the same.

Signed-off-by: Olga Telezhnaia <olyatelezhnaya@gmail.com>
---
 builtin/cat-file.c | 72 +++++++++++++++++++++-------------------------
 1 file changed, 33 insertions(+), 39 deletions(-)

diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index a4e56762f9e56..2066ff1e697e4 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -226,50 +226,17 @@ static size_t expand_format(struct strbuf *sb, const char *start, void *data)
 	return end - start + 1;
 }
 
-static void print_object_or_die(struct batch_options *opt, struct expand_data *data)
+static void print_object_or_die(struct expand_data *data, int cmdmode,
+				int buffered, const char *rest)
 {
 	const struct object_id *oid = &data->oid;
+	unsigned long size;
+	char *contents;
 
 	assert(data->info.typep);
 
-	if (data->type == OBJ_BLOB) {
-		if (opt->buffer_output)
-			fflush(stdout);
-		if (opt->cmdmode) {
-			char *contents;
-			unsigned long size;
-
-			if (!rest)
-				die("missing path for '%s'", oid_to_hex(oid));
-
-			if (opt->cmdmode == 'w') {
-				if (filter_object(rest, 0100644, oid,
-						  &contents, &size))
-					die("could not convert '%s' %s",
-					    oid_to_hex(oid), rest);
-			} else if (opt->cmdmode == 'c') {
-				enum object_type type;
-				if (!textconv_object(the_repository,
-						     rest, 0100644, oid,
-						     1, &contents, &size))
-					contents = read_object_file(oid,
-								    &type,
-								    &size);
-				if (!contents)
-					die("could not convert '%s' %s",
-					    oid_to_hex(oid), rest);
-			} else
-				BUG("invalid cmdmode: %c", opt->cmdmode);
-			write_or_die(1, contents, size);
-			free(contents);
-		} else if (stream_blob_to_fd(1, oid, NULL, 0))
-			die("unable to stream %s to stdout", oid_to_hex(oid));
-	}
-	else {
+	if (data->type != OBJ_BLOB) {
 		enum object_type type;
-		unsigned long size;
-		void *contents;
-
 		contents = read_object_file(oid, &type, &size);
 		if (!contents)
 			die("object %s disappeared", oid_to_hex(oid));
@@ -280,7 +247,34 @@ static void print_object_or_die(struct batch_options *opt, struct expand_data *d
 
 		write_or_die(1, contents, size);
 		free(contents);
+		return;
+	}
+
+	if (buffered)
+		fflush(stdout);
+	if (!cmdmode) {
+		if (stream_blob_to_fd(1, oid, NULL, 0))
+			die("unable to stream %s to stdout", oid_to_hex(oid));
+		return;
 	}
+
+	if (!rest)
+		die("missing path for '%s'", oid_to_hex(oid));
+
+	if (cmdmode == 'w') {
+		if (filter_object(rest, 0100644, oid, &contents, &size))
+			die("could not convert '%s' %s", oid_to_hex(oid), rest);
+	} else if (cmdmode == 'c') {
+		enum object_type type;
+		if (!textconv_object(the_repository, rest, 0100644, oid, 1,
+				     &contents, &size))
+			contents = read_object_file(oid, &type, &size);
+		if (!contents)
+			die("could not convert '%s' %s", oid_to_hex(oid), rest);
+	} else
+		BUG("invalid cmdmode: %c", cmdmode);
+	write_or_die(1, contents, size);
+	free(contents);
 }
 
 static void batch_object_write(const char *obj_name,
@@ -303,7 +297,7 @@ static void batch_object_write(const char *obj_name,
 	write_or_die(1, scratch->buf, scratch->len);
 
 	if (opt->print_contents) {
-		print_object_or_die(opt, data);
+		print_object_or_die(data, opt->cmdmode, opt->buffer_output, rest);
 		write_or_die(1, "\n", 1);
 	}
 }

--
https://github.com/git/git/pull/568

  parent reply	other threads:[~2019-02-22 16:05 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-22 15:50 [PATCH RFC 0/20] cat-file: start using formatting logic from ref-filter Olga Telezhnaya
2019-02-22 16:05 ` [PATCH RFC 01/20] cat-file: reuse struct ref_format Olga Telezhnaya
2019-02-22 16:05   ` [PATCH RFC 04/20] for-each-ref: tests for new atom %(rest) added Olga Telezhnaya
2019-02-28 21:11     ` Jeff King
2019-03-01  6:10       ` Olga Telezhnaya
2019-02-22 16:05   ` [PATCH RFC 19/20] cat-file: tests for new atoms added Olga Telezhnaya
2019-02-22 16:05   ` [PATCH RFC 02/20] ref-filter: rename field in ref_array_item stuct Olga Telezhnaya
2019-02-28 21:06     ` Jeff King
2019-02-22 16:05   ` [PATCH RFC 18/20] cat-file: get rid of expand_data Olga Telezhnaya
2019-02-22 16:05   ` [PATCH RFC 05/20] cat-file: remove split_on_whitespace Olga Telezhnaya
2019-02-28 21:22     ` Jeff King
2019-02-22 16:05   ` Olga Telezhnaya [this message]
2019-02-22 16:05   ` [PATCH RFC 14/20] cat-file: move print_object_or_die to ref-filter Olga Telezhnaya
2019-02-22 16:05   ` [PATCH RFC 03/20] ref-filter: add rest formatting option Olga Telezhnaya
2019-02-28 21:07     ` Jeff King
2019-02-22 16:05   ` [PATCH RFC 16/20] for-each-ref: tests for new atom %(raw) added Olga Telezhnaya
2019-02-22 16:05   ` [PATCH RFC 10/20] cat-file: inline stream_blob Olga Telezhnaya
2019-02-28 21:33     ` Jeff King
2019-02-22 16:05   ` [PATCH RFC 15/20] ref-filter: add raw formatting option Olga Telezhnaya
2019-02-22 16:05   ` [PATCH RFC 06/20] cat-file: remove mark_query from expand_data Olga Telezhnaya
2019-02-28 21:25     ` Jeff King
2019-03-03  9:41     ` Christian Couder
2019-02-22 16:05   ` [PATCH RFC 08/20] cat-file: remove rest " Olga Telezhnaya
2019-02-28 21:27     ` Jeff King
2019-02-22 16:05   ` [PATCH RFC 07/20] cat-file: remove skip_object_info Olga Telezhnaya
2019-02-28 21:26     ` Jeff King
2019-02-22 16:05   ` [PATCH RFC 12/20] cat-file: remove batch_write function Olga Telezhnaya
2019-02-22 16:05   ` [PATCH RFC 11/20] cat-file: move filter_object to diff.c Olga Telezhnaya
2019-02-22 16:05   ` [PATCH RFC 17/20] cat-file: reuse ref-filter formatting logic Olga Telezhnaya
2019-02-22 16:05   ` [PATCH RFC 09/20] ref-filter: make expand_data global Olga Telezhnaya
2019-02-28 21:30     ` Jeff King
2019-02-22 16:05   ` [PATCH RFC 20/20] cat-file: update docs Olga Telezhnaya
2019-02-28 21:04   ` [PATCH RFC 01/20] cat-file: reuse struct ref_format Jeff King
2019-02-22 16:09 ` [PATCH RFC 0/20] cat-file: start using formatting logic from ref-filter Eric Sunshine
2019-02-22 16:19   ` Olga Telezhnaya
2019-02-28 21:41 ` Jeff King
2019-03-01  6:16   ` Olga Telezhnaya
2019-02-28 21:43 ` Jeff King
2019-03-01  6:17   ` Olga Telezhnaya
2019-03-03  1:21   ` Junio C Hamano

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

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

  git send-email \
    --in-reply-to=0102016915f49a66-0b8bf78d-6879-41da-b5e6-de6e7edbe71a-000000@eu-west-1.amazonses.com \
    --to=olyatelezhnaya@gmail.com \
    --cc=git@vger.kernel.org \
    /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.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.