OSDN Git Service

ref-filter: introduce handler function for each atom
authorKarthik Nayak <karthik.188@gmail.com>
Thu, 10 Sep 2015 15:48:20 +0000 (21:18 +0530)
committerJunio C Hamano <gitster@pobox.com>
Thu, 17 Sep 2015 17:02:48 +0000 (10:02 -0700)
Introduce a handler function for each atom, which is called when the
atom is processed in show_ref_array_item().

In this context make append_atom() as the default handler function and
extract quote_formatting() out of append_atom(). Bump this to the top.

Mentored-by: Christian Couder <christian.couder@gmail.com>
Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>
Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
ref-filter.c

index 432cea0..a993216 100644 (file)
@@ -69,6 +69,7 @@ struct ref_formatting_state {
 
 struct atom_value {
        const char *s;
+       void (*handler)(struct atom_value *atomv, struct ref_formatting_state *state);
        unsigned long ul; /* used for sorting when not FIELD_STR */
 };
 
@@ -141,6 +142,32 @@ int parse_ref_filter_atom(const char *atom, const char *ep)
        return at;
 }
 
+static void quote_formatting(struct strbuf *s, const char *str, int quote_style)
+{
+       switch (quote_style) {
+       case QUOTE_NONE:
+               strbuf_addstr(s, str);
+               break;
+       case QUOTE_SHELL:
+               sq_quote_buf(s, str);
+               break;
+       case QUOTE_PERL:
+               perl_quote_buf(s, str);
+               break;
+       case QUOTE_PYTHON:
+               python_quote_buf(s, str);
+               break;
+       case QUOTE_TCL:
+               tcl_quote_buf(s, str);
+               break;
+       }
+}
+
+static void append_atom(struct atom_value *v, struct ref_formatting_state *state)
+{
+       quote_formatting(&state->stack->output, v->s, state->quote_style);
+}
+
 static void push_stack_element(struct ref_formatting_stack **stack)
 {
        struct ref_formatting_stack *s = xcalloc(1, sizeof(struct ref_formatting_stack));
@@ -662,6 +689,8 @@ static void populate_value(struct ref_array_item *ref)
                const char *formatp;
                struct branch *branch = NULL;
 
+               v->handler = append_atom;
+
                if (*name == '*') {
                        deref = 1;
                        name++;
@@ -1228,29 +1257,6 @@ void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
        qsort(array->items, array->nr, sizeof(struct ref_array_item *), compare_refs);
 }
 
-static void append_atom(struct atom_value *v, struct ref_formatting_state *state)
-{
-       struct strbuf *s = &state->stack->output;
-
-       switch (state->quote_style) {
-       case QUOTE_NONE:
-               strbuf_addstr(s, v->s);
-               break;
-       case QUOTE_SHELL:
-               sq_quote_buf(s, v->s);
-               break;
-       case QUOTE_PERL:
-               perl_quote_buf(s, v->s);
-               break;
-       case QUOTE_PYTHON:
-               python_quote_buf(s, v->s);
-               break;
-       case QUOTE_TCL:
-               tcl_quote_buf(s, v->s);
-               break;
-       }
-}
-
 static int hex1(char ch)
 {
        if ('0' <= ch && ch <= '9')
@@ -1307,7 +1313,7 @@ void show_ref_array_item(struct ref_array_item *info, const char *format, int qu
                if (cp < sp)
                        append_literal(cp, sp, &state);
                get_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv);
-               append_atom(atomv, &state);
+               atomv->handler(atomv, &state);
        }
        if (*cp) {
                sp = cp + strlen(cp);