Start moving all formatting stuff from cat-file to ref-filter. Start from simple moving, it would be integrated into all ref-filter processes further. Signed-off-by: Olga Telezhnaia <olyatelezhnaya@xxxxxxxxx> Mentored-by: Christian Couder <christian.couder@xxxxxxxxx> Mentored by: Jeff King <peff@xxxxxxxx> --- builtin/cat-file.c | 5 ++--- ref-filter.c | 41 ++++++++++++++++++++++++++++++++++++----- ref-filter.h | 6 ++++++ 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/builtin/cat-file.c b/builtin/cat-file.c index 7fd5b960ad698..0a3f4a47bf1ae 100644 --- a/builtin/cat-file.c +++ b/builtin/cat-file.c @@ -420,9 +420,8 @@ static int batch_objects(struct batch_options *opt) * object. */ memset(&data, 0, sizeof(data)); - data.mark_query = 1; - strbuf_expand(&buf, opt->format->format, expand_format, &data); - data.mark_query = 0; + opt->format->cat_file_data = &data; + verify_ref_format(opt->format); if (opt->cmdmode) data.split_on_whitespace = 1; diff --git a/ref-filter.c b/ref-filter.c index 32b7e918bca75..6ca4a671086ee 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -392,6 +392,31 @@ struct atom_value { struct used_atom *atom; }; +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_into_fields(const char *atom, int len, + struct expand_data *data) +{ + if (is_atom("objectname", atom, len)) + ; /* do nothing */ + else if (is_atom("objecttype", atom, len)) + data->info.typep = &data->type; + else if (is_atom("objectsize", atom, len)) + data->info.sizep = &data->size; + else if (is_atom("objectsize:disk", atom, len)) + data->info.disk_sizep = &data->disk_size; + else if (is_atom("rest", atom, len)) + data->split_on_whitespace = 1; + else if (is_atom("deltabase", atom, len)) + data->info.delta_base_sha1 = data->delta_base_oid.hash; + else + die("unknown format element: %.*s", len, atom); +} + /* * Used to parse format string and sort specifiers */ @@ -709,12 +734,18 @@ int verify_ref_format(struct ref_format *format) if (!ep) return error(_("malformed format string %s"), sp); /* sp points at "%(" and ep points at the closing ")" */ - at = parse_ref_filter_atom(format, valid_atom, - ARRAY_SIZE(valid_atom), sp + 2, ep); - cp = ep + 1; - if (skip_prefix(used_atom[at].name, "color:", &color)) - format->need_color_reset_at_eol = !!strcmp(color, "reset"); + if (format->cat_file_data) + expand_atom_into_fields(sp + 2, ep - sp - 2, + format->cat_file_data); + else { + at = parse_ref_filter_atom(format, valid_atom, + ARRAY_SIZE(valid_atom), sp + 2, ep); + if (skip_prefix(used_atom[at].name, "color:", &color)) + format->need_color_reset_at_eol = !!strcmp(color, "reset"); + } + + cp = ep + 1; } if (format->need_color_reset_at_eol && !want_color(format->use_color)) format->need_color_reset_at_eol = 0; diff --git a/ref-filter.h b/ref-filter.h index 16d00e4b1bded..97169548939cd 100644 --- a/ref-filter.h +++ b/ref-filter.h @@ -119,6 +119,12 @@ struct ref_format { /* Internal state to ref-filter */ int need_color_reset_at_eol; + + /* + * Helps to move all formatting logic from cat-file to ref-filter, + * hopefully would be reduced later. + */ + struct expand_data *cat_file_data; }; #define REF_FORMAT_INIT { NULL, 0, -1 } -- https://github.com/git/git/pull/450