Skip to content

Commit de359d3

Browse files
derrickstoleegitster
authored andcommitted
config: allow format_config() to filter
The format_config() method in builtin/config.c currently only uses git_config_*() methods for parsing. This allows parsing errors to result in die() messages appropriate with keys in the error message. In a future change we will want to use format_config() within 'git config list' to help format the output, including when --type=<X> arguments are provided. When the parsing fails in that case, that key-value pair should be omitted instead of causing a failure across the entire command. This change is formatted in such a way that the if/else-if structure allows the default die_on_error version to appear first and then be followed by the gentle parsing mode immediately afterwards. The only callers right now have die_on_parse set to 1. Signed-off-by: Derrick Stolee <stolee@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 5e04738 commit de359d3

File tree

1 file changed

+62
-13
lines changed

1 file changed

+62
-13
lines changed

builtin/config.c

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "abspath.h"
44
#include "config.h"
55
#include "color.h"
6+
#include "date.h"
67
#include "editor.h"
78
#include "environment.h"
89
#include "gettext.h"
@@ -245,7 +246,8 @@ struct strbuf_list {
245246
*/
246247
static int format_config(const struct config_display_options *opts,
247248
struct strbuf *buf, const char *key_,
248-
const char *value_, const struct key_value_info *kvi)
249+
const char *value_, const struct key_value_info *kvi,
250+
int die_on_parse)
249251
{
250252
if (opts->show_scope)
251253
show_config_scope(opts, kvi, buf);
@@ -257,27 +259,55 @@ static int format_config(const struct config_display_options *opts,
257259
if (opts->show_keys)
258260
strbuf_addch(buf, opts->key_delim);
259261

260-
if (opts->type == TYPE_INT)
262+
if (opts->type == TYPE_INT && die_on_parse) {
261263
strbuf_addf(buf, "%"PRId64,
262264
git_config_int64(key_, value_ ? value_ : "", kvi));
263-
else if (opts->type == TYPE_BOOL)
265+
} else if (opts->type == TYPE_INT) {
266+
int64_t v;
267+
int ret = git_parse_int64(value_, &v);
268+
269+
if (ret)
270+
return -1;
271+
272+
strbuf_addf(buf, "%"PRId64, v);
273+
}
274+
else if (opts->type == TYPE_BOOL && die_on_parse) {
264275
strbuf_addstr(buf, git_config_bool(key_, value_) ?
265276
"true" : "false");
266-
else if (opts->type == TYPE_BOOL_OR_INT) {
267-
int is_bool, v;
268-
v = git_config_bool_or_int(key_, value_, kvi,
269-
&is_bool);
277+
} else if (opts->type == TYPE_BOOL) {
278+
int value = git_parse_maybe_bool(value_);
279+
280+
if (value < 0)
281+
return -1;
282+
283+
strbuf_addstr(buf, value ? "true" : "false");
284+
} else if (opts->type == TYPE_BOOL_OR_INT && die_on_parse) {
285+
int is_bool = 0;
286+
int v = git_config_bool_or_int(key_, value_, kvi,
287+
&is_bool);
288+
if (is_bool)
289+
strbuf_addstr(buf, v ? "true" : "false");
290+
else
291+
strbuf_addf(buf, "%d", v);
292+
} else if (opts->type == TYPE_BOOL_OR_INT) {
293+
int is_bool = 0;
294+
int v = git_parse_maybe_bool_text(value_);
295+
296+
if (v < 0)
297+
return -1;
298+
270299
if (is_bool)
271300
strbuf_addstr(buf, v ? "true" : "false");
272301
else
273302
strbuf_addf(buf, "%d", v);
274303
} else if (opts->type == TYPE_BOOL_OR_STR) {
304+
/* Note: this can't fail to parse! */
275305
int v = git_parse_maybe_bool(value_);
276306
if (v < 0)
277307
strbuf_addstr(buf, value_);
278308
else
279309
strbuf_addstr(buf, v ? "true" : "false");
280-
} else if (opts->type == TYPE_PATH) {
310+
} else if (opts->type == TYPE_PATH && die_on_parse) {
281311
char *v;
282312
if (git_config_pathname(&v, key_, value_) < 0)
283313
return -1;
@@ -286,16 +316,35 @@ static int format_config(const struct config_display_options *opts,
286316
else
287317
return 1; /* :(optional)no-such-file */
288318
free((char *)v);
289-
} else if (opts->type == TYPE_EXPIRY_DATE) {
319+
} else if (opts->type == TYPE_PATH) {
320+
char *v;
321+
if (git_parse_maybe_pathname(value_, &v) < 0)
322+
return -1;
323+
if (v)
324+
strbuf_addstr(buf, v);
325+
else
326+
return 1; /* :(optional)no-such-file */
327+
free((char *)v);
328+
} else if (opts->type == TYPE_EXPIRY_DATE && die_on_parse) {
290329
timestamp_t t;
291330
if (git_config_expiry_date(&t, key_, value_) < 0)
292331
return -1;
293332
strbuf_addf(buf, "%"PRItime, t);
294-
} else if (opts->type == TYPE_COLOR) {
333+
} else if (opts->type == TYPE_EXPIRY_DATE) {
334+
timestamp_t t;
335+
if (parse_expiry_date(value_, &t) < 0)
336+
return -1;
337+
strbuf_addf(buf, "%"PRItime, t);
338+
} else if (opts->type == TYPE_COLOR && die_on_parse) {
295339
char v[COLOR_MAXLEN];
296340
if (git_config_color(v, key_, value_) < 0)
297341
return -1;
298342
strbuf_addstr(buf, v);
343+
} else if (opts->type == TYPE_COLOR) {
344+
char v[COLOR_MAXLEN];
345+
if (color_parse(value_, v) < 0)
346+
return -1;
347+
strbuf_addstr(buf, v);
299348
} else if (value_) {
300349
strbuf_addstr(buf, value_);
301350
} else {
@@ -372,7 +421,7 @@ static int collect_config(const char *key_, const char *value_,
372421
strbuf_init(&values->items[values->nr], 0);
373422

374423
status = format_config(data->display_opts, &values->items[values->nr++],
375-
key_, value_, kvi);
424+
key_, value_, kvi, 1);
376425
if (status < 0)
377426
return status;
378427
if (status) {
@@ -463,7 +512,7 @@ static int get_value(const struct config_location_options *opts,
463512
strbuf_init(item, 0);
464513

465514
status = format_config(display_opts, item, key_,
466-
display_opts->default_value, &kvi);
515+
display_opts->default_value, &kvi, 1);
467516
if (status < 0)
468517
die(_("failed to format default config value: %s"),
469518
display_opts->default_value);
@@ -743,7 +792,7 @@ static int get_urlmatch(const struct config_location_options *opts,
743792

744793
status = format_config(&display_opts, &buf, item->string,
745794
matched->value_is_null ? NULL : matched->value.buf,
746-
&matched->kvi);
795+
&matched->kvi, 1);
747796
if (!status)
748797
fwrite(buf.buf, 1, buf.len, stdout);
749798
strbuf_release(&buf);

0 commit comments

Comments
 (0)