Skip to content

Commit 2fbbd6b

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. 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 240afdd commit 2fbbd6b

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
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: 5 additions & 14 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

t/t1300-config.sh

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

24602460
cat >.git/config <<-\EOF &&
24612461
[section]
2462-
foo = true
2462+
foo = True
24632463
number = 10
24642464
big = 1M
2465+
path = ~/dir
24652466
EOF
24662467

24672468
test_expect_success 'identical modern --type specifiers are allowed' '
@@ -2503,6 +2504,29 @@ test_expect_success 'unset type specifiers may be reset to conflicting ones' '
25032504
test_cmp_config 1048576 --type=bool --no-type --type=int section.big
25042505
'
25052506

2507+
test_expect_success 'list --type=bool shows only canonicalizable bool values' '
2508+
cat >expect <<-EOF &&
2509+
section.foo=true
2510+
section.number=true
2511+
section.big=true
2512+
EOF
2513+
2514+
git config ${mode_prefix}list --type=bool >actual &&
2515+
test_cmp expect actual
2516+
'
2517+
2518+
test_expect_success 'list --type=path shows only canonicalizable path values' '
2519+
cat >expect <<-EOF &&
2520+
section.foo=True
2521+
section.number=10
2522+
section.big=1M
2523+
section.path=$HOME/dir
2524+
EOF
2525+
2526+
git config ${mode_prefix}list --type=path >actual &&
2527+
test_cmp expect actual
2528+
'
2529+
25062530
test_expect_success '--type rejects unknown specifiers' '
25072531
test_must_fail git config --type=nonsense section.foo 2>error &&
25082532
test_grep "unrecognized --type argument" error

0 commit comments

Comments
 (0)