Introduce a stack machinery for ref_formatting_state, this allows us
to push and pop states. Whenever we pop a state the strbuf from that
state is appended into the next state on the stack, this will allow us
to support nesting of modifier atoms.

Mentored-by: Christian Couder <christian.cou...@gmail.com>
Mentored-by: Matthieu Moy <matthieu....@grenoble-inp.fr>
Signed-off-by: Karthik Nayak <karthik....@gmail.com>
---
 ref-filter.c | 42 +++++++++++++++++++++++++++++++++---------
 1 file changed, 33 insertions(+), 9 deletions(-)

diff --git a/ref-filter.c b/ref-filter.c
index dd62640..74532d3 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -56,6 +56,7 @@ static struct {
 };
 
 struct ref_formatting_state {
+       struct ref_formatting_state *prev;
        struct strbuf output;
        int quote_style;
 };
@@ -134,6 +135,27 @@ int parse_ref_filter_atom(const char *atom, const char *ep)
        return at;
 }
 
+static void push_new_state(struct ref_formatting_state **stack)
+{
+       struct ref_formatting_state *s = xcalloc(1, sizeof(struct 
ref_formatting_state));
+
+       strbuf_init(&s->output, 0);
+       s->prev = *stack;
+       *stack = s;
+}
+
+static void pop_state(struct ref_formatting_state **stack)
+{
+       struct ref_formatting_state *current = *stack;
+       struct ref_formatting_state *prev = current->prev;
+
+       if (prev)
+               strbuf_addbuf(&prev->output, &current->output);
+       strbuf_release(&current->output);
+       free(current);
+       *stack = prev;
+}
+
 /*
  * In a format string, find the next occurrence of %(atom).
  */
@@ -1262,23 +1284,24 @@ static void append_literal(const char *cp, const char 
*ep, struct ref_formatting
 void show_ref_array_item(struct ref_array_item *info, const char *format, int 
quote_style)
 {
        const char *cp, *sp, *ep;
-       struct ref_formatting_state state;
+       struct strbuf *final_buf;
+       struct ref_formatting_state *state = NULL;
 
-       strbuf_init(&state.output, 0);
-       state.quote_style = quote_style;
+       push_new_state(&state);
+       state->quote_style = quote_style;
 
        for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) {
                struct atom_value *atomv;
 
                ep = strchr(sp, ')');
                if (cp < sp)
-                       append_literal(cp, sp, &state);
+                       append_literal(cp, sp, state);
                get_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), 
&atomv);
-               append_atom(atomv, &state);
+               append_atom(atomv, state);
        }
        if (*cp) {
                sp = cp + strlen(cp);
-               append_literal(cp, sp, &state);
+               append_literal(cp, sp, state);
        }
        if (need_color_reset_at_eol) {
                struct atom_value resetv;
@@ -1287,11 +1310,12 @@ void show_ref_array_item(struct ref_array_item *info, 
const char *format, int qu
                if (color_parse("reset", color) < 0)
                        die("BUG: couldn't parse 'reset' as a color");
                resetv.s = color;
-               append_atom(&resetv, &state);
+               append_atom(&resetv, state);
        }
-       fwrite(state.output.buf, 1, state.output.len, stdout);
+       final_buf = &state->output;
+       fwrite(final_buf->buf, 1, final_buf->len, stdout);
+       pop_state(&state);
        putchar('\n');
-       strbuf_release(&state.output);
 }
 
 /*  If no sorting option is given, use refname to sort as default */
-- 
2.5.0

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to