Skip to content

Commit

Permalink
builtin/config: refactor collect_config()
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Junio C Hamano committed Aug 5, 2013
1 parent 6a56993 commit d9b9169
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions builtin/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,33 +100,21 @@ struct strbuf_list {
int alloc;
};

static int collect_config(const char *key_, const char *value_, void *cb)
static int format_config(struct strbuf *buf, const char *key_, const char *value_)
{
struct strbuf_list *values = cb;
struct strbuf *buf;
char value[256];
const char *vptr = value;
int must_free_vptr = 0;
int must_print_delim = 0;
char value[256];
const char *vptr = value;

if (!use_key_regexp && strcmp(key_, key))
return 0;
if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
return 0;
if (regexp != NULL &&
(do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
return 0;

ALLOC_GROW(values->items, values->nr + 1, values->alloc);
buf = &values->items[values->nr++];
strbuf_init(buf, 0);

if (show_keys) {
strbuf_addstr(buf, key_);
must_print_delim = 1;
}
if (types == TYPE_INT)
sprintf(value, "%d", git_config_int(key_, value_?value_:""));
sprintf(value, "%d", git_config_int(key_, value_ ? value_ : ""));
else if (types == TYPE_BOOL)
vptr = git_config_bool(key_, value_) ? "true" : "false";
else if (types == TYPE_BOOL_OR_INT) {
Expand Down Expand Up @@ -154,15 +142,27 @@ static int collect_config(const char *key_, const char *value_, void *cb)
strbuf_addch(buf, term);

if (must_free_vptr)
/* If vptr must be freed, it's a pointer to a
* dynamically allocated buffer, it's safe to cast to
* const.
*/
free((char *)vptr);

return 0;
}

static int collect_config(const char *key_, const char *value_, void *cb)
{
struct strbuf_list *values = cb;

if (!use_key_regexp && strcmp(key_, key))
return 0;
if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
return 0;
if (regexp != NULL &&
(do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
return 0;

ALLOC_GROW(values->items, values->nr + 1, values->alloc);

return format_config(&values->items[values->nr++], key_, value_);
}

static int get_value(const char *key_, const char *regex_)
{
int ret = CONFIG_GENERIC_ERROR;
Expand Down

0 comments on commit d9b9169

Please sign in to comment.