Skip to content

Commit d9b9169

Browse files
committed
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 <[email protected]>
1 parent 6a56993 commit d9b9169

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

builtin/config.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -100,33 +100,21 @@ struct strbuf_list {
100100
int alloc;
101101
};
102102

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_)
104104
{
105-
struct strbuf_list *values = cb;
106-
struct strbuf *buf;
107-
char value[256];
108-
const char *vptr = value;
109105
int must_free_vptr = 0;
110106
int must_print_delim = 0;
107+
char value[256];
108+
const char *vptr = value;
111109

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++];
122110
strbuf_init(buf, 0);
123111

124112
if (show_keys) {
125113
strbuf_addstr(buf, key_);
126114
must_print_delim = 1;
127115
}
128116
if (types == TYPE_INT)
129-
sprintf(value, "%d", git_config_int(key_, value_?value_:""));
117+
sprintf(value, "%d", git_config_int(key_, value_ ? value_ : ""));
130118
else if (types == TYPE_BOOL)
131119
vptr = git_config_bool(key_, value_) ? "true" : "false";
132120
else if (types == TYPE_BOOL_OR_INT) {
@@ -154,15 +142,27 @@ static int collect_config(const char *key_, const char *value_, void *cb)
154142
strbuf_addch(buf, term);
155143

156144
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-
*/
161145
free((char *)vptr);
162-
163146
return 0;
164147
}
165148

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+
166166
static int get_value(const char *key_, const char *regex_)
167167
{
168168
int ret = CONFIG_GENERIC_ERROR;

0 commit comments

Comments
 (0)