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 <olyatelezhn...@gmail.com>
Mentored-by: Christian Couder <christian.cou...@gmail.com>
Mentored by: Jeff King <p...@peff.net>
---
 builtin/cat-file.c |  5 ++---
 ref-filter.c       | 42 +++++++++++++++++++++++++++++++++++++-----
 ref-filter.h       |  6 ++++++
 3 files changed, 45 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 2d6e81fe1ab00..98bb10185ae96 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -693,6 +693,31 @@ static const char *find_next(const char *cp)
        return NULL;
 }
 
+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);
+}
+
 /*
  * Make sure the format string is well formed, and parse out
  * the used atoms.
@@ -709,12 +734,19 @@ 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_atoms,
-                                          ARRAY_SIZE(valid_atoms), sp + 2, ep);
-               cp = ep + 1;
 
-               if (skip_prefix(used_atoms[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_atoms,
+                                                  ARRAY_SIZE(valid_atoms), sp 
+ 2, ep);
+                       if (skip_prefix(used_atoms[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

Reply via email to