git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Olga Telezhnaya <olyatelezhnaya@gmail.com>
To: git@vger.kernel.org
Subject: [PATCH RFC 17/24] cat-file: reuse printing logic from ref-filter
Date: Fri, 26 Jan 2018 19:43:40 +0000	[thread overview]
Message-ID: <0102016133ff3b3f-26fceb2a-c1ef-447b-9013-caccccc052f9-000000@eu-west-1.amazonses.com> (raw)
In-Reply-To: <0102016133ff3a86-44d354ec-13c6-4c38-bc75-1ba4422db5a7-000000@eu-west-1.amazonses.com>

Reuse code from ref-filter to print resulting message.

Signed-off-by: Olga Telezhnaia <olyatelezhnaya@gmail.com>
Mentored-by: Christian Couder <christian.couder@gmail.com>
Mentored by: Jeff King <peff@peff.net>
---
 builtin/cat-file.c | 52 +++++-----------------------------------------------
 ref-filter.c       | 30 +++++++++++++++++++++++++++---
 2 files changed, 32 insertions(+), 50 deletions(-)

diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index bd803856537b8..26b35bef4ba02 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -176,45 +176,6 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
 	return 0;
 }
 
-static int is_atom(const char *atom, const char *s, int slen)
-{
-	int alen = strlen(atom);
-	return alen == slen && !memcmp(atom, s, alen);
-}
-
-static void expand_atom(struct strbuf *sb, const char *atom, int len,
-			 struct ref_array_item *item)
-{
-	if (is_atom("objectname", atom, len))
-		strbuf_addstr(sb, oid_to_hex(&item->objectname));
-	else if (is_atom("objecttype", atom, len))
-		strbuf_addstr(sb, typename(item->type));
-	else if (is_atom("objectsize", atom, len))
-		strbuf_addf(sb, "%lu", item->size);
-	else if (is_atom("objectsize:disk", atom, len))
-		strbuf_addf(sb, "%"PRIuMAX, (uintmax_t)item->disk_size);
-	else if (is_atom("rest", atom, len)) {
-		if (item->rest)
-			strbuf_addstr(sb, item->rest);
-	} else if (is_atom("deltabase", atom, len))
-		strbuf_addstr(sb, oid_to_hex(item->delta_base_oid));
-}
-
-static size_t expand_format(struct strbuf *sb, const char *start, void *data)
-{
-	const char *end;
-	struct ref_array_item *item = data;
-
-	if (*start != '(')
-		return 0;
-	end = strchr(start + 1, ')');
-	if (!end)
-		die("format element '%s' does not end in ')'", start);
-
-	expand_atom(sb, start + 1, end - start - 1, item);
-	return end - start + 1;
-}
-
 static void batch_write(struct batch_options *opt, const void *data, int len)
 {
 	if (opt->buffer_output) {
@@ -282,22 +243,19 @@ static void print_object_or_die(struct batch_options *opt, struct expand_data *d
 static void batch_object_write(const char *obj_name, struct batch_options *opt,
 			       struct expand_data *data)
 {
-	struct strbuf buf = STRBUF_INIT;
 	struct ref_array_item item = {0};
 
 	item.objectname = data->oid;
 	item.rest = data->rest;
 	item.start_of_request = obj_name;
 
-	if (populate_value(&item)) return;
-	data->type = item.type;
-
-	strbuf_expand(&buf, opt->format.format, expand_format, &item);
-	strbuf_addch(&buf, '\n');
-	batch_write(opt, buf.buf, buf.len);
-	strbuf_release(&buf);
+	if (show_ref_array_item(&item, &opt->format))
+		return;
+	if (!opt->buffer_output)
+		fflush(stdout);
 
 	if (opt->print_contents) {
+		data->type = item.type;
 		print_object_or_die(opt, data);
 		batch_write(opt, "\n", 1);
 	}
diff --git a/ref-filter.c b/ref-filter.c
index 906a5344949f7..bfbc7c83fdd47 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -1522,7 +1522,21 @@ int populate_value(struct ref_array_item *ref)
 			name++;
 		}
 
-		if (starts_with(name, "refname"))
+		if (is_cat) {
+			if (starts_with(name, "objectname"))
+				v->s = oid_to_hex(&ref->objectname);
+			else if (starts_with(name, "objecttype"))
+				v->s = typename(ref->type);
+			else if (starts_with(name, "objectsize")) {
+				v->s = xstrfmt("%lu", ref->size);
+			} else if (starts_with(name, "objectsize:disk")) {
+				v->s = xstrfmt("%"PRIuMAX, (uintmax_t)ref->disk_size);
+			} else if (starts_with(name, "rest"))
+				v->s = ref->rest;
+			else if (starts_with(name, "deltabase"))
+				v->s = xstrdup(oid_to_hex(ref->delta_base_oid));
+			continue;
+		} else if (starts_with(name, "refname"))
 			refname = get_refname(atom, ref);
 		else if (starts_with(name, "symref"))
 			refname = get_symref(atom, ref);
@@ -2219,6 +2233,7 @@ int format_ref_array_item(struct ref_array_item *info,
 {
 	const char *cp, *sp, *ep;
 	struct ref_formatting_state state = REF_FORMATTING_STATE_INIT;
+	int retval = 0;
 
 	state.quote_style = format->quote_style;
 	push_stack_element(&state.stack);
@@ -2229,13 +2244,22 @@ int format_ref_array_item(struct ref_array_item *info,
 		ep = strchr(sp, ')');
 		if (cp < sp)
 			append_literal(cp, sp, &state);
-		get_ref_atom_value(info,
+		if (is_cat) {
+			if(get_ref_atom_value(info,
+				parse_ref_filter_atom(format, valid_cat_file_atom,
+					ARRAY_SIZE(valid_cat_file_atom), sp + 2, ep),
+					&atomv))
+				return -1;
+		} else
+			get_ref_atom_value(info,
 				   parse_ref_filter_atom(format, valid_atom,
 							 ARRAY_SIZE(valid_atom),
 							 sp + 2, ep),
 				   &atomv);
 		atomv->handler(atomv, &state);
 	}
+	if (is_cat && strlen(format->format) == 0)
+		retval = check_and_fill_for_cat(info);
 	if (*cp) {
 		sp = cp + strlen(cp);
 		append_literal(cp, sp, &state);
@@ -2249,7 +2273,7 @@ int format_ref_array_item(struct ref_array_item *info,
 		die(_("format: %%(end) atom missing"));
 	strbuf_addbuf(final_buf, &state.stack->output);
 	pop_stack_element(&state.stack);
-	return 0;
+	return retval;
 }
 
 int show_ref_array_item(struct ref_array_item *info,

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

  parent reply	other threads:[~2018-01-26 19:44 UTC|newest]

Thread overview: 115+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-26 19:43 [PATCH RFC 01/24] ref-filter: get rid of goto Olga Telezhnaya
2018-01-26 19:43 ` [PATCH RFC 14/24] ref_filter: add is_rest_atom_used function Olga Telezhnaya
2018-01-26 19:43 ` [PATCH RFC 11/24] cat-file: start reusing populate_value Olga Telezhnaya
2018-01-26 19:43 ` [PATCH RFC 13/24] ref-filter: add is_cat flag Olga Telezhnaya
2018-01-26 19:43 ` [PATCH RFC 20/24] ref-filter: unifying formatting of cat-file opts Olga Telezhnaya
2018-01-26 19:43 ` [PATCH RFC 03/24] cat-file: split expand_atom into 2 functions Olga Telezhnaya
2018-01-26 21:46   ` Junio C Hamano
2018-01-29  7:26     ` Оля Тележная
2018-01-26 19:43 ` [PATCH RFC 15/24] cat-file: move skip_object_info into ref-filter Olga Telezhnaya
2018-01-26 19:43 ` Olga Telezhnaya [this message]
2018-01-26 19:43 ` [PATCH RFC 24/24] cat-file: update of docs Olga Telezhnaya
2018-01-26 19:43 ` [PATCH RFC 06/24] cat-file: move struct expand_data into ref-filter Olga Telezhnaya
2018-01-26 19:43 ` [PATCH RFC 05/24] ref-filter: make valid_atom as function parameter Olga Telezhnaya
2018-01-26 19:43 ` [PATCH RFC 22/24] for-each-ref: tests for new atoms added Olga Telezhnaya
2018-01-26 19:43 ` [PATCH RFC 10/24] ref-filter: make populate_value global Olga Telezhnaya
2018-01-26 19:43 ` [PATCH RFC 04/24] cat-file: reuse struct ref_format Olga Telezhnaya
2018-01-26 22:18   ` Junio C Hamano
2018-01-26 19:43 ` [PATCH RFC 18/24] ref-filter: make valid_atom general again Olga Telezhnaya
2018-01-26 19:43 ` [PATCH RFC 23/24] cat-file: tests for new atoms added Olga Telezhnaya
2018-01-26 19:43 ` [PATCH RFC 09/24] cat-file: start use ref_array_item struct Olga Telezhnaya
2018-01-26 19:43 ` [PATCH RFC 08/24] ref-filter: reuse parse_ref_filter_atom Olga Telezhnaya
2018-01-26 19:43 ` [PATCH RFC 12/24] ref-filter: get rid of expand_atom_into_fields Olga Telezhnaya
2018-01-26 19:43 ` [PATCH RFC 16/24] ref-filter: make cat_file_info independent Olga Telezhnaya
2018-01-26 19:43 ` [PATCH RFC 02/24] ref-filter: add return value to some functions Olga Telezhnaya
2018-01-26 21:05   ` Junio C Hamano
2018-01-29  7:36     ` Оля Тележная
2018-01-26 19:43 ` [PATCH RFC 07/24] cat-file: start migrating to ref-filter Olga Telezhnaya
2018-01-26 19:43 ` [PATCH RFC 21/24] ref-filter: work with objectsize:disk Olga Telezhnaya
2018-01-26 19:43 ` [PATCH RFC 19/24] ref-filter: make populate_value internal again Olga Telezhnaya
2018-01-26 20:19 ` [PATCH RFC 01/24] ref-filter: get rid of goto Junio C Hamano
2018-01-29  7:13   ` Оля Тележная
2018-01-30 20:49     ` Junio C Hamano
2018-01-31  6:39       ` Оля Тележная
2018-02-05 11:27 ` [PATCH RFC v2 01/25] " Olga Telezhnaya
2018-02-05 11:27   ` [PATCH RFC v2 21/25] ref-filter: unifying formatting of cat-file opts Olga Telezhnaya
2018-02-05 11:27   ` [PATCH RFC v2 19/25] cat-file: reuse printing logic from ref-filter Olga Telezhnaya
2018-02-05 11:27   ` [PATCH RFC v2 20/25] ref-filter: make populate_value() internal again Olga Telezhnaya
2018-02-05 11:27   ` [PATCH RFC v2 23/25] for-each-ref: tests for new atoms added Olga Telezhnaya
2018-02-05 11:27   ` [PATCH RFC v2 09/25] cat-file: start use ref_array_item struct Olga Telezhnaya
2018-02-05 11:27   ` [PATCH RFC v2 11/25] ref-filter: rename field in ref_array_item stuct Olga Telezhnaya
2018-02-05 11:27   ` [PATCH RFC v2 05/25] cat-file: move struct expand_data into ref-filter Olga Telezhnaya
2018-02-05 11:27   ` [PATCH RFC v2 02/25] ref-filter: add return value to some functions Olga Telezhnaya
2018-02-05 11:27   ` [PATCH RFC v2 06/25] cat-file: split expand_atom() into 2 functions Olga Telezhnaya
2018-02-05 11:27   ` [PATCH RFC v2 17/25] ref-filter: make cat_file_info independent Olga Telezhnaya
2018-02-05 11:27   ` [PATCH RFC v2 12/25] cat-file: start reusing populate_value() Olga Telezhnaya
2018-02-05 11:27   ` [PATCH RFC v2 24/25] cat-file: tests for new atoms added Olga Telezhnaya
2018-02-05 11:27   ` [PATCH RFC v2 07/25] cat-file: start migrating formatting to ref-filter Olga Telezhnaya
2018-02-05 11:27   ` [PATCH RFC v2 15/25] ref_filter: add is_atom_used function Olga Telezhnaya
2018-02-05 11:27   ` [PATCH RFC v2 16/25] cat-file: move skip_object_info into ref-filter Olga Telezhnaya
2018-02-05 11:27   ` [PATCH RFC v2 10/25] ref-filter: make populate_value() global Olga Telezhnaya
2018-02-05 11:27   ` [PATCH RFC v2 22/25] ref-filter: work with objectsize:disk Olga Telezhnaya
2018-02-05 11:27   ` [PATCH RFC v2 14/25] ref-filter: add is_cat flag Olga Telezhnaya
2018-02-05 21:19     ` Junio C Hamano
2018-02-05 11:27   ` [PATCH RFC v2 18/25] ref-filter: make valid_atom general again Olga Telezhnaya
2018-02-05 11:27   ` [PATCH RFC v2 13/25] ref-filter: get rid of mark_atom_in_object_info() Olga Telezhnaya
2018-02-05 11:27   ` [PATCH RFC v2 03/25] cat-file: reuse struct ref_format Olga Telezhnaya
2018-02-05 11:27   ` [PATCH RFC v2 08/25] ref-filter: reuse parse_ref_filter_atom() Olga Telezhnaya
2018-02-05 11:27   ` [PATCH RFC v2 04/25] ref-filter: make valid_atom as function parameter Olga Telezhnaya
2018-02-05 11:27   ` [PATCH RFC v2 25/25] cat-file: update of docs Olga Telezhnaya
2018-02-12  8:08   ` [PATCH v3 01/23] ref-filter: get rid of goto Olga Telezhnaya
2018-02-12  8:08     ` [PATCH v3 03/23] cat-file: reuse struct ref_format Olga Telezhnaya
2018-02-15  5:18       ` Jeff King
2018-02-12  8:08     ` [PATCH v3 13/23] ref-filter: get rid of mark_atom_in_object_info() Olga Telezhnaya
2018-02-15  5:45       ` Jeff King
2018-02-15 10:19         ` Оля Тележная
2018-02-12  8:08     ` [PATCH v3 06/23] cat-file: split expand_atom() into 2 functions Olga Telezhnaya
2018-02-15  5:28       ` Jeff King
2018-02-12  8:08     ` [PATCH v3 02/23] ref-filter: add return value to some functions Olga Telezhnaya
2018-02-15  5:16       ` Jeff King
2018-02-15  9:59         ` Оля Тележная
2018-02-12  8:08     ` [PATCH v3 11/23] ref-filter: rename field in ref_array_item stuct Olga Telezhnaya
2018-02-12  8:08     ` [PATCH v3 15/23] cat-file: move skip_object_info into ref-filter Olga Telezhnaya
2018-02-15  5:51       ` Jeff King
2018-02-15 10:27         ` Оля Тележная
2018-02-12  8:08     ` [PATCH v3 16/23] ref-filter: make cat_file_info independent Olga Telezhnaya
2018-02-15  5:53       ` Jeff King
2018-02-15 10:33         ` Оля Тележная
2018-02-16 21:54           ` Jeff King
2018-02-12  8:08     ` [PATCH v3 08/23] ref-filter: reuse parse_ref_filter_atom() Olga Telezhnaya
2018-02-15  5:37       ` Jeff King
2018-02-15 10:11         ` Оля Тележная
2018-02-12  8:08     ` [PATCH v3 19/23] ref-filter: make populate_value() internal again Olga Telezhnaya
2018-02-12  8:08     ` [PATCH v3 04/23] ref-filter: make valid_atom as function parameter Olga Telezhnaya
2018-02-15  5:23       ` Jeff King
2018-02-15 10:03         ` Оля Тележная
2018-02-12  8:08     ` [PATCH v3 20/23] ref-filter: unifying formatting of cat-file opts Olga Telezhnaya
2018-02-15  5:56       ` Jeff King
2018-02-15 10:34         ` Оля Тележная
2018-02-15 18:31           ` Junio C Hamano
2018-02-12  8:08     ` [PATCH v3 21/23] for-each-ref: tests for new atoms added Olga Telezhnaya
2018-02-15  5:57       ` Jeff King
2018-02-15 10:38         ` Оля Тележная
2018-02-12  8:08     ` [PATCH v3 18/23] cat-file: reuse printing logic from ref-filter Olga Telezhnaya
2018-02-12  8:08     ` [PATCH v3 10/23] ref-filter: make populate_value() global Olga Telezhnaya
2018-02-12  8:08     ` [PATCH v3 09/23] cat-file: start use ref_array_item struct Olga Telezhnaya
2018-02-15  5:40       ` Jeff King
2018-02-15 10:13         ` Оля Тележная
2018-02-12  8:08     ` [PATCH v3 12/23] cat-file: start reusing populate_value() Olga Telezhnaya
2018-02-15  5:42       ` Jeff King
2018-02-12  8:08     ` [PATCH v3 07/23] cat-file: start migrating formatting to ref-filter Olga Telezhnaya
2018-02-15  5:32       ` Jeff King
2018-02-12  8:08     ` [PATCH v3 22/23] cat-file: tests for new atoms added Olga Telezhnaya
2018-02-16 14:55       ` Adam Dinwoodie
2018-02-18 22:55         ` Ramsay Jones
2018-02-19  1:04           ` Ramsay Jones
2018-02-12  8:08     ` [PATCH v3 05/23] cat-file: move struct expand_data into ref-filter Olga Telezhnaya
2018-02-15  5:26       ` Jeff King
2018-02-12  8:08     ` [PATCH v3 17/23] ref-filter: make valid_atom general again Olga Telezhnaya
2018-02-15  5:53       ` Jeff King
2018-02-12  8:08     ` [PATCH v3 14/23] ref_filter: add is_atom_used function Olga Telezhnaya
2018-02-15  5:49       ` Jeff King
2018-02-15 10:20         ` Оля Тележная
2018-02-12  8:08     ` [PATCH v3 23/23] cat-file: update of docs Olga Telezhnaya
2018-02-15  6:00       ` Jeff King
2018-02-15  5:08     ` [PATCH v3 01/23] ref-filter: get rid of goto Jeff King

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=0102016133ff3b3f-26fceb2a-c1ef-447b-9013-caccccc052f9-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.
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).