builtin/config: refactor collect_config()

In order to reuse the logic to format the configuration value while honouring the requested type, split this function into two. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jul 29, 2013 at 14:23 UTC d9b9169b3419edb0fa7a787ec1641ee75397f2a4
1 file changed +21 -21
builtin/config.c
+21 -21
@@ -100,25 +100,13 @@ struct strbuf_list {
100 int alloc;
101 };
102
103 -static int collect_config(const char *key_, const char *value_, void *cb)
103 +static int format_config(struct strbuf *buf, const char *key_, const char *value_)
104 {
105 - struct strbuf_list *values = cb;
106 - struct strbuf *buf;
107 - char value[256];
108 - const char *vptr = value;
105 int must_free_vptr = 0;
106 int must_print_delim = 0;
107 + char value[256];
108 + const char *vptr = value;
109
112 - if (!use_key_regexp && strcmp(key_, key))
113 - return 0;
114 - if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
115 - return 0;
116 - if (regexp != NULL &&
117 - (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
118 - return 0;
119 -
120 - ALLOC_GROW(values->items, values->nr + 1, values->alloc);
121 - buf = &values->items[values->nr++];
110 strbuf_init(buf, 0);
111
112 if (show_keys) {
@@ -126,7 +114,7 @@ static int collect_config(const char *key_, const char *value_, void *cb)
114 must_print_delim = 1;
115 }
116 if (types == TYPE_INT)
129 - sprintf(value, "%d", git_config_int(key_, value_?value_:""));
117 + sprintf(value, "%d", git_config_int(key_, value_ ? value_ : ""));
118 else if (types == TYPE_BOOL)
119 vptr = git_config_bool(key_, value_) ? "true" : "false";
120 else if (types == TYPE_BOOL_OR_INT) {
@@ -154,15 +142,27 @@ static int collect_config(const char *key_, const char *value_, void *cb)
142 strbuf_addch(buf, term);
143
144 if (must_free_vptr)
157 - /* If vptr must be freed, it's a pointer to a
158 - * dynamically allocated buffer, it's safe to cast to
159 - * const.
160 - */
145 free((char *)vptr);
162 -
146 return 0;
147 }
148
149 +static int collect_config(const char *key_, const char *value_, void *cb)
150 +{
151 + struct strbuf_list *values = cb;
152 +
153 + if (!use_key_regexp && strcmp(key_, key))
154 + return 0;
155 + if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
156 + return 0;
157 + if (regexp != NULL &&
158 + (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
159 + return 0;
160 +
161 + ALLOC_GROW(values->items, values->nr + 1, values->alloc);
162 +
163 + return format_config(&values->items[values->nr++], key_, value_);
164 +}
165 +
166 static int get_value(const char *key_, const char *regex_)
167 {
168 int ret = CONFIG_GENERIC_ERROR;