Start moving formatting stuff related to data preparation
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 <olyatelezhn...@gmail.com>
Mentored-by: Christian Couder <christian.cou...@gmail.com>
Mentored by: Jeff King <p...@peff.net>
---
 builtin/cat-file.c | 32 +++-----------------------------
 ref-filter.c       | 41 ++++++++++++++++++++++++++++++++++++-----
 ref-filter.h       | 12 ++++++------
 3 files changed, 45 insertions(+), 40 deletions(-)

diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index 490d9f8a8a922..909412747cbd2 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -182,25 +182,6 @@ static int is_atom(const char *atom, const char *s, int 
slen)
        return alen == slen && !memcmp(atom, s, alen);
 }
 
-static void expand_atom_into_fields(struct strbuf *sb, 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);
-}
-
 static void expand_atom(struct strbuf *sb, const char *atom, int len,
                         struct expand_data *data)
 {
@@ -217,8 +198,6 @@ static void expand_atom(struct strbuf *sb, const char 
*atom, int len,
                        strbuf_addstr(sb, data->rest);
        } else if (is_atom("deltabase", atom, len))
                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 *vdata)
@@ -232,11 +211,7 @@ static size_t expand_format(struct strbuf *sb, const char 
*start, void *vdata)
        if (!end)
                die("format element '%s' does not end in ')'", start);
 
-       if (data->mark_query)
-               expand_atom_into_fields(sb, start + 1, end - start - 1, data);
-       else
-               expand_atom(sb, start + 1, end - start - 1, data);
-
+       expand_atom(sb, start + 1, end - start - 1, data);
        return end - start + 1;
 }
 
@@ -420,9 +395,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 ee2aa7d6d072b..8e384b2818b22 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 56093a85d52b8..b50c8e6aaf2c4 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -80,12 +80,6 @@ struct expand_data {
        const char *rest;
        struct object_id delta_base_oid;
 
-       /*
-        * If mark_query is true, we do not expand anything, but rather
-        * just mark the object_info with items we wish to query.
-        */
-       int mark_query;
-
        /*
         * Whether to split the input on whitespace before feeding it to
         * get_sha1; this is decided during the mark_query phase based on
@@ -119,6 +113,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/452

Reply via email to