summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOlga Telezhnaya <olyatelezhnaya@gmail.com>2018-02-12 08:08:54 +0000
committerJunio C Hamano <gitster@pobox.com>2018-02-12 15:03:58 -0800
commit4d892e7d1570a082daec11b553a17013b6e284fc (patch)
tree863ac76647f554e52cbcb816045785094485ef2b
parent74a6b623adb1b034d48923a62bf84217c8dd5cfb (diff)
downloadgit-4d892e7d1570a082daec11b553a17013b6e284fc.tar.gz
cat-file: reuse printing logic from ref-filter
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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rw-r--r--builtin/cat-file.c51
-rw-r--r--ref-filter.c21
2 files changed, 23 insertions, 49 deletions
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index 273b4038e3..21007995c5 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->oid));
- 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,23 +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.oid = data->oid;
item.rest = data->rest;
item.objectname = obj_name;
- if (populate_value(&item))
+ if (show_ref_array_item(&item, &opt->format))
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 (!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 92c2e0179e..857974a1a9 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -1465,7 +1465,21 @@ int populate_value(struct ref_array_item *ref)
name++;
}
- if (starts_with(name, "refname"))
+ if (cat_file_info.is_cat_file) {
+ if (starts_with(name, "objectname"))
+ v->s = oid_to_hex(&ref->oid);
+ 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);
@@ -2208,6 +2222,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);
@@ -2224,6 +2239,8 @@ int format_ref_array_item(struct ref_array_item *info,
return -1;
atomv->handler(atomv, &state);
}
+ if (cat_file_info.is_cat_file && strlen(format->format) == 0)
+ retval = check_and_fill_for_cat(info);
if (*cp) {
sp = cp + strlen(cp);
append_literal(cp, sp, &state);
@@ -2237,7 +2254,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,