Skip to content

Commit 9304a46

Browse files
committed
config: make 'git config list --type=<X>' work
Previously, the --type=<X> argument to 'git config list' was ignored and did nothing. Now, we add the use of format_config() to the show_all_config() function so each key-value pair is attempted to be parsed. This is our first use of the 'gently' parameter with a nonzero value. When listing multiple values, our initial settings for the output format is different. Add a new init helper to specify the fact that keys should be shown and also add the default delimiters as they were unset in some cases. If there is an error in parsing, then the row is not output. This is a change in behavior! We are starting to respect an option that was previously ignored, leading to potential user confusion. This is probably still a good option, since the --type argument did not change behavior at all previously, so users can get the behavior they expect by removing the --type argument or adding the --no-type argument. Signed-off-by: Derrick Stolee <stolee@gmail.com>
1 parent 8d50967 commit 9304a46

File tree

3 files changed

+65
-16
lines changed

3 files changed

+65
-16
lines changed

Documentation/git-config.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ Valid `<type>`'s include:
240240
that the given value is canonicalize-able as an ANSI color, but it is written
241241
as-is.
242242
+
243+
If the command is in `list` mode, then the `--type <type>` argument will apply
244+
to each listed config value. If the value does not successfully parse in that
245+
format, then it will be omitted from the list.
243246

244247
--bool::
245248
--int::

builtin/config.c

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -450,21 +450,12 @@ static int show_all_config(const char *key_, const char *value_,
450450
{
451451
const struct config_display_options *opts = cb;
452452
const struct key_value_info *kvi = ctx->kvi;
453+
struct strbuf formatted = STRBUF_INIT;
453454

454-
if (opts->show_origin || opts->show_scope) {
455-
struct strbuf buf = STRBUF_INIT;
456-
if (opts->show_scope)
457-
show_config_scope(opts, kvi, &buf);
458-
if (opts->show_origin)
459-
show_config_origin(opts, kvi, &buf);
460-
/* Use fwrite as "buf" can contain \0's if "end_null" is set. */
461-
fwrite(buf.buf, 1, buf.len, stdout);
462-
strbuf_release(&buf);
463-
}
464-
if (!opts->omit_values && value_)
465-
printf("%s%c%s%c", key_, opts->delim, value_, opts->term);
466-
else
467-
printf("%s%c", key_, opts->term);
455+
if (format_config(opts, &formatted, key_, value_, kvi, 1) >= 0)
456+
fwrite(formatted.buf, 1, formatted.len, stdout);
457+
458+
strbuf_release(&formatted);
468459
return 0;
469460
}
470461

@@ -1004,6 +995,19 @@ static void display_options_init(struct config_display_options *opts)
1004995
}
1005996
}
1006997

998+
static void display_options_init_list(struct config_display_options *opts)
999+
{
1000+
opts->show_keys = 1;
1001+
1002+
if (opts->end_nul) {
1003+
display_options_init(opts);
1004+
} else {
1005+
opts->term = '\n';
1006+
opts->delim = ' ';
1007+
opts->key_delim = '=';
1008+
}
1009+
}
1010+
10071011
static int cmd_config_list(int argc, const char **argv, const char *prefix,
10081012
struct repository *repo UNUSED)
10091013
{
@@ -1022,7 +1026,7 @@ static int cmd_config_list(int argc, const char **argv, const char *prefix,
10221026
check_argc(argc, 0, 0);
10231027

10241028
location_options_init(&location_opts, prefix);
1025-
display_options_init(&display_opts);
1029+
display_options_init_list(&display_opts);
10261030

10271031
setup_auto_pager("config", 1);
10281032

@@ -1453,6 +1457,7 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
14531457

14541458
if (actions == ACTION_LIST) {
14551459
check_argc(argc, 0, 0);
1460+
display_options_init_list(&display_opts);
14561461
if (config_with_options(show_all_config, &display_opts,
14571462
&location_opts.source, the_repository,
14581463
&location_opts.options) < 0) {

t/t1300-config.sh

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2459,9 +2459,12 @@ done
24592459

24602460
cat >.git/config <<-\EOF &&
24612461
[section]
2462-
foo = true
2462+
foo = True
24632463
number = 10
24642464
big = 1M
2465+
path = ~/dir
2466+
red = red
2467+
blue = Blue
24652468
EOF
24662469

24672470
test_expect_success 'identical modern --type specifiers are allowed' '
@@ -2503,6 +2506,44 @@ test_expect_success 'unset type specifiers may be reset to conflicting ones' '
25032506
test_cmp_config 1048576 --type=bool --no-type --type=int section.big
25042507
'
25052508

2509+
test_expect_success 'list --type=bool shows only canonicalizable bool values' '
2510+
cat >expect <<-EOF &&
2511+
section.foo=true
2512+
section.number=true
2513+
section.big=true
2514+
EOF
2515+
2516+
git config ${mode_prefix}list --type=bool >actual &&
2517+
test_cmp expect actual
2518+
'
2519+
2520+
test_expect_success 'list --type=path shows only canonicalizable path values' '
2521+
cat >expect <<-EOF &&
2522+
section.foo=True
2523+
section.number=10
2524+
section.big=1M
2525+
section.path=$HOME/dir
2526+
section.red=red
2527+
section.blue=Blue
2528+
EOF
2529+
2530+
git config ${mode_prefix}list --type=path >actual &&
2531+
test_cmp expect actual
2532+
'
2533+
2534+
test_expect_success 'list --type=color shows only canonicalizable color values' '
2535+
cat >expect <<-EOF &&
2536+
section.number=<>
2537+
section.red=<RED>
2538+
section.blue=<BLUE>
2539+
EOF
2540+
2541+
git config ${mode_prefix}list --type=color >actual.raw 2>err &&
2542+
test_decode_color <actual.raw >actual &&
2543+
test_cmp expect actual &&
2544+
test_must_be_empty err
2545+
'
2546+
25062547
test_expect_success '--type rejects unknown specifiers' '
25072548
test_must_fail git config --type=nonsense section.foo 2>error &&
25082549
test_grep "unrecognized --type argument" error

0 commit comments

Comments
 (0)