builtin/config.c: support `--type=<type>` as preferred alias for `--<type>`

`git config` has long allowed the ability for callers to provide a 'type specifier', which instructs `git config` to (1) ensure that incoming values can be interpreted as that type, and (2) that outgoing values are canonicalized under that type. In another series, we propose to extend this functionality with `--type=color` and `--default` to replace `--get-color`. However, we traditionally use `--color` to mean "colorize this output", instead of "this value should be treated as a color". Currently, `git config` does not support this kind of colorization, but we should be careful to avoid squatting on this option too soon, so that `git config` can support `--color` (in the traditional sense) in the future, if that is desired. In this patch, we support `--type=<int|bool|bool-or-int|...>` in addition to `--int`, `--bool`, and etc. This allows the aforementioned upcoming patch to support querying a color value with a default via `--type=color --default=...`, without squandering `--color`. We retain the historic behavior of complaining when multiple, legacy-style `--<type>` flags are given, as well as extend this to conflicting new-style `--type=<type>` flags. `--int --type=int` (and its commutative pair) does not complain, but `--bool --type=int` (and its commutative pair) does. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed Apr 18, 2018 at 14:43 UTC fb0dc3bac135e9f6243bd6d293e8c9293c73b9cd
3 files changed +154 -40
Documentation/git-config.txt
+39 -32
@@ -9,13 +9,13 @@ git-config - Get and set repository or global options
9 SYNOPSIS
10 --------
11 [verse]
12 -'git config' [<file-option>] [type] [--show-origin] [-z|--null] name [value [value_regex]]
13 -'git config' [<file-option>] [type] --add name value
14 -'git config' [<file-option>] [type] --replace-all name value [value_regex]
15 -'git config' [<file-option>] [type] [--show-origin] [-z|--null] --get name [value_regex]
16 -'git config' [<file-option>] [type] [--show-origin] [-z|--null] --get-all name [value_regex]
17 -'git config' [<file-option>] [type] [--show-origin] [-z|--null] [--name-only] --get-regexp name_regex [value_regex]
18 -'git config' [<file-option>] [type] [-z|--null] --get-urlmatch name URL
12 +'git config' [<file-option>] [--type=<type>] [--show-origin] [-z|--null] name [value [value_regex]]
13 +'git config' [<file-option>] [--type=<type>] --add name value
14 +'git config' [<file-option>] [--type=<type>] --replace-all name value [value_regex]
15 +'git config' [<file-option>] [--type=<type>] [--show-origin] [-z|--null] --get name [value_regex]
16 +'git config' [<file-option>] [--type=<type>] [--show-origin] [-z|--null] --get-all name [value_regex]
17 +'git config' [<file-option>] [--type=<type>] [--show-origin] [-z|--null] [--name-only] --get-regexp name_regex [value_regex]
18 +'git config' [<file-option>] [--type=<type>] [-z|--null] --get-urlmatch name URL
19 'git config' [<file-option>] --unset name [value_regex]
20 'git config' [<file-option>] --unset-all name [value_regex]
21 'git config' [<file-option>] --rename-section old_name new_name
@@ -38,12 +38,10 @@ existing values that match the regexp are updated or unset. If
38 you want to handle the lines that do *not* match the regex, just
39 prepend a single exclamation mark in front (see also <<EXAMPLES>>).
40
41 -The type specifier can be either `--int` or `--bool`, to make
42 -'git config' ensure that the variable(s) are of the given type and
43 -convert the value to the canonical form (simple decimal number for int,
44 -a "true" or "false" string for bool), or `--path`, which does some
45 -path expansion (see `--path` below). If no type specifier is passed, no
46 -checks or transformations are performed on the value.
41 +The `--type=<type>` option instructs 'git config' to ensure that incoming and
42 +outgoing values are canonicalize-able under the given <type>. If no
43 +`--type=<type>` is given, no canonicalization will be performed. Callers may
44 +unset an existing `--type` specifier with `--no-type`.
45
46 When reading, the values are read from the system, global and
47 repository local configuration files by default, and options
@@ -160,30 +158,39 @@ See also <<FILES>>.
158 --list::
159 List all variables set in config file, along with their values.
160
163 ---bool::
164 - 'git config' will ensure that the output is "true" or "false"
161 +--type <type>::
162 + 'git config' will ensure that any input or output is valid under the given
163 + type constraint(s), and will canonicalize outgoing values in `<type>`'s
164 + canonical form.
165 ++
166 +Valid `<type>`'s include:
167 ++
168 +- 'bool': canonicalize values as either "true" or "false".
169 +- 'int': canonicalize values as simple decimal numbers. An optional suffix of
170 + 'k', 'm', or 'g' will cause the value to be multiplied by 1024, 1048576, or
171 + 1073741824 upon input.
172 +- 'bool-or-int': canonicalize according to either 'bool' or 'int', as described
173 + above.
174 +- 'path': canonicalize by adding a leading `~` to the value of `$HOME` and
175 + `~user` to the home directory for the specified user. This specifier has no
176 + effect when setting the value (but you can use `git config section.variable
177 + ~/` from the command line to let your shell do the expansion.)
178 +- 'expiry-date': canonicalize by converting from a fixed or relative date-string
179 + to a timestamp. This specifier has no effect when setting the value.
180 ++
181
182 +--bool::
183 --int::
167 - 'git config' will ensure that the output is a simple
168 - decimal number. An optional value suffix of 'k', 'm', or 'g'
169 - in the config file will cause the value to be multiplied
170 - by 1024, 1048576, or 1073741824 prior to output.
171 -
184 --bool-or-int::
173 - 'git config' will ensure that the output matches the format of
174 - either --bool or --int, as described above.
175 -
185 --path::
177 - `git config` will expand a leading `~` to the value of
178 - `$HOME`, and `~user` to the home directory for the
179 - specified user. This option has no effect when setting the
180 - value (but you can use `git config section.variable ~/`
181 - from the command line to let your shell do the expansion).
182 -
186 --expiry-date::
184 - `git config` will ensure that the output is converted from
185 - a fixed or relative date-string to a timestamp. This option
186 - has no effect when setting the value.
187 + Historical options for selecting a type specifier. Prefer instead `--type`,
188 + (see: above).
189 +
190 +--no-type::
191 + Un-sets the previously set type specifier (if one was previously set). This
192 + option requests that 'git config' not canonicalize the retrieved variable.
193 + `--no-type` has no effect without `--type=<type>` or `--<type>`.
194
195 -z::
196 --null::
builtin/config.c
+60 -5
@@ -61,6 +61,60 @@ static int show_origin;
61 #define TYPE_PATH 4
62 #define TYPE_EXPIRY_DATE 5
63
64 +#define OPT_CALLBACK_VALUE(s, l, v, h, i) \
65 + { OPTION_CALLBACK, (s), (l), (v), NULL, (h), PARSE_OPT_NOARG | \
66 + PARSE_OPT_NONEG, option_parse_type, (i) }
67 +
68 +static struct option builtin_config_options[];
69 +
70 +static int option_parse_type(const struct option *opt, const char *arg,
71 + int unset)
72 +{
73 + int new_type, *to_type;
74 +
75 + if (unset) {
76 + *((int *) opt->value) = 0;
77 + return 0;
78 + }
79 +
80 + /*
81 + * To support '--<type>' style flags, begin with new_type equal to
82 + * opt->defval.
83 + */
84 + new_type = opt->defval;
85 + if (!new_type) {
86 + if (!strcmp(arg, "bool"))
87 + new_type = TYPE_BOOL;
88 + else if (!strcmp(arg, "int"))
89 + new_type = TYPE_INT;
90 + else if (!strcmp(arg, "bool-or-int"))
91 + new_type = TYPE_BOOL_OR_INT;
92 + else if (!strcmp(arg, "path"))
93 + new_type = TYPE_PATH;
94 + else if (!strcmp(arg, "expiry-date"))
95 + new_type = TYPE_EXPIRY_DATE;
96 + else
97 + die(_("unrecognized --type argument, %s"), arg);
98 + }
99 +
100 + to_type = opt->value;
101 + if (*to_type && *to_type != new_type) {
102 + /*
103 + * Complain when there is a new type not equal to the old type.
104 + * This allows for combinations like '--int --type=int' and
105 + * '--type=int --type=int', but disallows ones like '--type=bool
106 + * --int' and '--type=bool
107 + * --type=int'.
108 + */
109 + error("only one type at a time.");
110 + usage_with_options(builtin_config_usage,
111 + builtin_config_options);
112 + }
113 + *to_type = new_type;
114 +
115 + return 0;
116 +}
117 +
118 static struct option builtin_config_options[] = {
119 OPT_GROUP(N_("Config file location")),
120 OPT_BOOL(0, "global", &use_global_config, N_("use global config file")),
@@ -84,11 +138,12 @@ static struct option builtin_config_options[] = {
138 OPT_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR),
139 OPT_BIT(0, "get-colorbool", &actions, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL),
140 OPT_GROUP(N_("Type")),
87 - OPT_SET_INT(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL),
88 - OPT_SET_INT(0, "int", &type, N_("value is decimal number"), TYPE_INT),
89 - OPT_SET_INT(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT),
90 - OPT_SET_INT(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH),
91 - OPT_SET_INT(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE),
141 + OPT_CALLBACK('t', "type", &type, "", N_("value is given this type"), option_parse_type),
142 + OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL),
143 + OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT),
144 + OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT),
145 + OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH),
146 + OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE),
147 OPT_GROUP(N_("Other")),
148 OPT_BOOL('z', "null", &end_null, N_("terminate values with NUL byte")),
149 OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
t/t1300-repo-config.sh
+55 -3
@@ -1613,13 +1613,65 @@ test_expect_success '--local requires a repo' '
1613
1614 cat >.git/config <<-\EOF &&
1615 [core]
1616 +foo = true
1617 number = 10
1618 +big = 1M
1619 EOF
1620
1619 -test_expect_success 'later legacy specifiers are given precedence' '
1620 - git config --bool --int core.number >actual &&
1621 - echo 10 >expect &&
1621 +test_expect_success 'identical modern --type specifiers are allowed' '
1622 + git config --type=int --type=int core.big >actual &&
1623 + echo 1048576 >expect &&
1624 test_cmp expect actual
1625 '
1626
1627 +test_expect_success 'identical legacy --type specifiers are allowed' '
1628 + git config --int --int core.big >actual &&
1629 + echo 1048576 >expect &&
1630 + test_cmp expect actual
1631 +'
1632 +
1633 +test_expect_success 'identical mixed --type specifiers are allowed' '
1634 + git config --int --type=int core.big >actual &&
1635 + echo 1048576 >expect &&
1636 + test_cmp expect actual
1637 +'
1638 +
1639 +test_expect_success 'non-identical modern --type specifiers are not allowed' '
1640 + test_must_fail git config --type=int --type=bool core.big 2>error &&
1641 + test_i18ngrep "only one type at a time" error
1642 +'
1643 +
1644 +test_expect_success 'non-identical legacy --type specifiers are not allowed' '
1645 + test_must_fail git config --int --bool core.big 2>error &&
1646 + test_i18ngrep "only one type at a time" error
1647 +'
1648 +
1649 +test_expect_success 'non-identical mixed --type specifiers are not allowed' '
1650 + test_must_fail git config --type=int --bool core.big 2>error &&
1651 + test_i18ngrep "only one type at a time" error
1652 +'
1653 +
1654 +test_expect_success '--type allows valid type specifiers' '
1655 + echo "true" >expect &&
1656 + git config --type=bool core.foo >actual &&
1657 + test_cmp expect actual
1658 +'
1659 +
1660 +test_expect_success '--no-type unsets type specifiers' '
1661 + echo "10" >expect &&
1662 + git config --type=bool --no-type core.number >actual &&
1663 + test_cmp expect actual
1664 +'
1665 +
1666 +test_expect_success 'unset type specifiers may be reset to conflicting ones' '
1667 + echo 1048576 >expect &&
1668 + git config --type=bool --no-type --type=int core.big >actual &&
1669 + test_cmp expect actual
1670 +'
1671 +
1672 +test_expect_success '--type rejects unknown specifiers' '
1673 + test_must_fail git config --type=nonsense core.foo 2>error &&
1674 + test_i18ngrep "unrecognized --type argument" error
1675 +'
1676 +
1677 test_done