From: Olga Telezhnaya <olyatelezhnaya@gmail.com>
To: git@vger.kernel.org
Subject: [PATCH RFC 17/20] cat-file: reuse ref-filter formatting logic
Date: Fri, 22 Feb 2019 16:05:45 +0000 [thread overview]
Message-ID: <0102016915f49a72-d28a8c0c-7594-4330-8c1c-e497622a8fd9-000000@eu-west-1.amazonses.com> (raw)
In-Reply-To: <0102016915f499b8-5813fc52-230b-469e-b939-a1244e83a2b9-000000@eu-west-1.amazonses.com>
Start using general ref-filter formatting logic in cat-file.
Signed-off-by: Olga Telezhnaia <olyatelezhnaya@gmail.com>
---
builtin/cat-file.c | 111 ++++++++-------------------------------------
ref-filter.c | 39 +++++++++++-----
ref-filter.h | 4 +-
3 files changed, 49 insertions(+), 105 deletions(-)
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index 6c0cbf71f0f0c..6fa100d1bea72 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -28,9 +28,7 @@ struct batch_options {
};
static const char *force_path;
-/* Next 3 vars will be deleted at the end of this patch */
-static int mark_query;
-static int skip_object_info;
+/* global rest will be deleted at the end of this patch */
static const char *rest;
static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
@@ -169,84 +167,29 @@ 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,
- void *vdata)
-{
- struct expand_data *data = vdata;
-
- if (is_atom("objectname", atom, len)) {
- if (!mark_query)
- strbuf_addstr(sb, oid_to_hex(&data->oid));
- } else if (is_atom("objecttype", atom, len)) {
- if (mark_query)
- data->info.typep = &data->type;
- else
- strbuf_addstr(sb, type_name(data->type));
- } else if (is_atom("objectsize", atom, len)) {
- if (mark_query)
- data->info.sizep = &data->size;
- else
- strbuf_addf(sb, "%"PRIuMAX , (uintmax_t)data->size);
- } else if (is_atom("objectsize:disk", atom, len)) {
- if (mark_query)
- data->info.disk_sizep = &data->disk_size;
- else
- strbuf_addf(sb, "%"PRIuMAX, (uintmax_t)data->disk_size);
- } else if (is_atom("rest", atom, len)) {
- if (rest)
- strbuf_addstr(sb, rest);
- } else if (is_atom("deltabase", atom, len)) {
- if (mark_query)
- data->info.delta_base_sha1 = data->delta_base_oid.hash;
- else
- strbuf_addstr(sb,
- oid_to_hex(&data->delta_base_oid));
- } else
- die("unknown format element: %.*s", len, atom);
-}
-
-static size_t expand_format(struct strbuf *sb, const char *start, void *data)
-{
- const char *end;
-
- 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, data);
-
- return end - start + 1;
-}
-
static void batch_object_write(const char *obj_name,
struct strbuf *scratch,
struct batch_options *opt,
struct expand_data *data)
{
- if (!skip_object_info &&
- oid_object_info_extended(the_repository, &data->oid, &data->info,
- OBJECT_INFO_LOOKUP_REPLACE) < 0) {
- printf("%s missing\n",
- obj_name ? obj_name : oid_to_hex(&data->oid));
+ struct strbuf err = STRBUF_INIT;
+ struct ref_array_item item = { data->oid };
+ item.request_rest = rest;
+ item.check_obj = 1;
+ strbuf_reset(scratch);
+
+ if (format_ref_array_item(&item, &opt->format, scratch, &err)) {
+ printf("%s missing\n", obj_name ? obj_name : oid_to_hex(&item.oid));
fflush(stdout);
return;
}
- strbuf_reset(scratch);
- strbuf_expand(scratch, opt->format.format, expand_format, data);
strbuf_addch(scratch, '\n');
write_or_die(1, scratch->buf, scratch->len);
+ strbuf_release(&err);
if (opt->print_contents) {
- print_object_or_die(data, opt->cmdmode, opt->buffer_output, rest);
+ print_raw_object_or_die(&item, opt->cmdmode, opt->buffer_output);
write_or_die(1, "\n", 1);
}
}
@@ -367,30 +310,7 @@ static int batch_objects(struct batch_options *opt)
int save_warning;
int retval = 0;
int is_rest = strstr(opt->format.format, "%(rest)") != NULL || opt->cmdmode;
-
- /*
- * Expand once with our special mark_query flag, which will prime the
- * object_info to be handed to oid_object_info_extended for each
- * object.
- */
memset(&data, 0, sizeof(data));
- mark_query = 1;
- strbuf_expand(&output, opt->format.format, expand_format, &data);
- mark_query = 0;
- strbuf_release(&output);
-
- if (opt->all_objects) {
- struct object_info empty = OBJECT_INFO_INIT;
- if (!memcmp(&data.info, &empty, sizeof(empty)))
- skip_object_info = 1;
- }
-
- /*
- * If we are printing out the object, then always fill in the type,
- * since we will want to decide whether or not to stream.
- */
- if (opt->print_contents)
- data.info.typep = &data.type;
if (opt->all_objects) {
struct object_cb_data cb;
@@ -581,6 +501,15 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
if (!batch.format.format)
batch.format.format = "%(objectname) %(objecttype) %(objectsize)";
+ if (batch.print_contents) {
+ const char *contents = "%(raw)";
+ char *format = (char *)calloc(strlen(batch.format.format) + strlen(contents) + 1, 1);
+ memcpy(format, batch.format.format, strlen(batch.format.format));
+ memcpy(format + strlen(format), contents, strlen(contents));
+ batch.format.format = format;
+ }
+ if (verify_ref_format(&batch.format))
+ usage_with_options(cat_file_usage, options);
if (batch.enabled)
return batch_objects(&batch);
diff --git a/ref-filter.c b/ref-filter.c
index bb963a4110fb2..45d163246e3f3 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -2231,6 +2231,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;
+ struct object_info empty = OBJECT_INFO_INIT;
state.quote_style = format->quote_style;
push_stack_element(&state.stack);
@@ -2253,6 +2254,11 @@ int format_ref_array_item(struct ref_array_item *info,
sp = cp + strlen(cp);
append_literal(cp, sp, &state);
}
+ if (info->check_obj &&
+ oid_object_info_extended(the_repository, &info->oid, &empty,
+ OBJECT_INFO_LOOKUP_REPLACE))
+ return strbuf_addf_ret(error_buf, -1, _("%s missing\n"),
+ oid_to_hex(&info->oid));
if (format->need_color_reset_at_eol) {
struct atom_value resetv;
resetv.s = GIT_COLOR_RESET;
@@ -2381,23 +2387,32 @@ int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset)
return 0;
}
-void print_object_or_die(struct expand_data *data, int cmdmode,
- int buffered, const char *rest)
+/*
+ * TODO: add support of %(*raw). Need to switch between oi and oi_deref for that.
+ * TODO: split logic and printing (as it is done in format_ref_array_item and
+ * show_ref_array_item).
+ * TODO: rewrite print_object_or_die so that it will reuse result of general
+ * oid_object_info_extended call.
+ * TODO: embed this function into general ref_filter flow, make it static.
+ * That will allow other ref-filter users to print raw file
+ * (now only cat_file can use it).
+ */
+void print_raw_object_or_die(struct ref_array_item *item, int cmdmode, int buffered)
{
- const struct object_id *oid = &data->oid;
+ const struct object_id *oid = &oi.oid;
unsigned long size;
char *contents;
- assert(data->info.typep);
+ assert(oi.info.typep);
- if (data->type != OBJ_BLOB) {
+ if (oi.type != OBJ_BLOB) {
enum object_type type;
contents = read_object_file(oid, &type, &size);
if (!contents)
die("object %s disappeared", oid_to_hex(oid));
- if (type != data->type)
+ if (type != oi.type)
die("object %s changed type!?", oid_to_hex(oid));
- if (data->info.sizep && size != data->size)
+ if (oi.info.sizep && size != oi.size)
die("object %s changed size!?", oid_to_hex(oid));
write_or_die(1, contents, size);
@@ -2413,19 +2428,19 @@ void print_object_or_die(struct expand_data *data, int cmdmode,
return;
}
- if (!rest)
+ if (!item->request_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);
+ if (filter_object(item->request_rest, 0100644, oid, &contents, &size))
+ die("could not convert '%s' %s", oid_to_hex(oid), item->request_rest);
} else if (cmdmode == 'c') {
enum object_type type;
- if (!textconv_object(the_repository, rest, 0100644, oid, 1,
+ if (!textconv_object(the_repository, item->request_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);
+ die("could not convert '%s' %s", oid_to_hex(oid), item->request_rest);
} else
BUG("invalid cmdmode: %c", cmdmode);
write_or_die(1, contents, size);
diff --git a/ref-filter.h b/ref-filter.h
index 3422f39e64b5b..e8cd97a49632c 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -42,6 +42,7 @@ struct ref_array_item {
struct commit *commit;
struct atom_value *value;
const char *request_rest;
+ int check_obj;
char refname[FLEX_ARRAY];
};
@@ -157,7 +158,6 @@ struct ref_array_item *ref_array_push(struct ref_array *array,
const char *refname,
const struct object_id *oid);
-void print_object_or_die(struct expand_data *data, int cmdmode,
- int buffered, const char *rest);
+void print_raw_object_or_die(struct ref_array_item *item, int cmdmode, int buffered);
#endif /* REF_FILTER_H */
--
https://github.com/git/git/pull/568
next prev 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 18/20] cat-file: get rid of expand_data Olga Telezhnaya
2019-02-22 16:05 ` [PATCH RFC 08/20] cat-file: remove rest from expand_data Olga Telezhnaya
2019-02-28 21:27 ` Jeff King
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 ` [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 13/20] cat-file: rewrite print_object_or_die Olga Telezhnaya
2019-02-22 16:05 ` [PATCH RFC 20/20] cat-file: update docs Olga Telezhnaya
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 12/20] cat-file: remove batch_write function Olga Telezhnaya
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 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 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 09/20] ref-filter: make expand_data global Olga Telezhnaya
2019-02-28 21:30 ` 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 03/20] ref-filter: add rest formatting option Olga Telezhnaya
2019-02-28 21:07 ` Jeff King
2019-02-22 16:05 ` [PATCH RFC 15/20] ref-filter: add raw " 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 19/20] cat-file: tests for new atoms added 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 ` Olga Telezhnaya [this message]
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
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=0102016915f49a72-d28a8c0c-7594-4330-8c1c-e497622a8fd9-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).