Skip to content

Commit e3e042b

Browse files
committed
Merge branch 'tb/config-type'
The "git config" command uses separate options e.g. "--int", "--bool", etc. to specify what type the caller wants the value to be interpreted as. A new "--type=<typename>" option has been introduced, which would make it cleaner to define new types. * tb/config-type: builtin/config.c: support `--type=<type>` as preferred alias for `--<type>` builtin/config.c: treat type specifiers singularly
2 parents 278c251 + fb0dc3b commit e3e042b

File tree

3 files changed

+180
-60
lines changed

3 files changed

+180
-60
lines changed

Documentation/git-config.txt

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ git-config - Get and set repository or global options
99
SYNOPSIS
1010
--------
1111
[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
1919
'git config' [<file-option>] --unset name [value_regex]
2020
'git config' [<file-option>] --unset-all name [value_regex]
2121
'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
3838
you want to handle the lines that do *not* match the regex, just
3939
prepend a single exclamation mark in front (see also <<EXAMPLES>>).
4040

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`.
4745

4846
When reading, the values are read from the system, global and
4947
repository local configuration files by default, and options
@@ -160,30 +158,39 @@ See also <<FILES>>.
160158
--list::
161159
List all variables set in config file, along with their values.
162160

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+
+
165181

182+
--bool::
166183
--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-
172184
--bool-or-int::
173-
'git config' will ensure that the output matches the format of
174-
either --bool or --int, as described above.
175-
176185
--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-
183186
--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>`.
187194

188195
-z::
189196
--null::

builtin/config.c

Lines changed: 77 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ static char term = '\n';
2525

2626
static int use_global_config, use_system_config, use_local_config;
2727
static struct git_config_source given_config_source;
28-
static int actions, types;
28+
static int actions, type;
2929
static int end_null;
3030
static int respect_includes_opt = -1;
3131
static struct config_options config_options;
@@ -55,11 +55,65 @@ static int show_origin;
5555
#define PAGING_ACTIONS (ACTION_LIST | ACTION_GET_ALL | \
5656
ACTION_GET_REGEXP | ACTION_GET_URLMATCH)
5757

58-
#define TYPE_BOOL (1<<0)
59-
#define TYPE_INT (1<<1)
60-
#define TYPE_BOOL_OR_INT (1<<2)
61-
#define TYPE_PATH (1<<3)
62-
#define TYPE_EXPIRY_DATE (1<<4)
58+
#define TYPE_BOOL 1
59+
#define TYPE_INT 2
60+
#define TYPE_BOOL_OR_INT 3
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+
}
63117

64118
static struct option builtin_config_options[] = {
65119
OPT_GROUP(N_("Config file location")),
@@ -84,11 +138,12 @@ static struct option builtin_config_options[] = {
84138
OPT_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR),
85139
OPT_BIT(0, "get-colorbool", &actions, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL),
86140
OPT_GROUP(N_("Type")),
87-
OPT_BIT(0, "bool", &types, N_("value is \"true\" or \"false\""), TYPE_BOOL),
88-
OPT_BIT(0, "int", &types, N_("value is decimal number"), TYPE_INT),
89-
OPT_BIT(0, "bool-or-int", &types, N_("value is --bool or --int"), TYPE_BOOL_OR_INT),
90-
OPT_BIT(0, "path", &types, N_("value is a path (file or directory name)"), TYPE_PATH),
91-
OPT_BIT(0, "expiry-date", &types, 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),
92147
OPT_GROUP(N_("Other")),
93148
OPT_BOOL('z', "null", &end_null, N_("terminate values with NUL byte")),
94149
OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
@@ -149,26 +204,26 @@ static int format_config(struct strbuf *buf, const char *key_, const char *value
149204
if (show_keys)
150205
strbuf_addch(buf, key_delim);
151206

152-
if (types == TYPE_INT)
207+
if (type == TYPE_INT)
153208
strbuf_addf(buf, "%"PRId64,
154209
git_config_int64(key_, value_ ? value_ : ""));
155-
else if (types == TYPE_BOOL)
210+
else if (type == TYPE_BOOL)
156211
strbuf_addstr(buf, git_config_bool(key_, value_) ?
157212
"true" : "false");
158-
else if (types == TYPE_BOOL_OR_INT) {
213+
else if (type == TYPE_BOOL_OR_INT) {
159214
int is_bool, v;
160215
v = git_config_bool_or_int(key_, value_, &is_bool);
161216
if (is_bool)
162217
strbuf_addstr(buf, v ? "true" : "false");
163218
else
164219
strbuf_addf(buf, "%d", v);
165-
} else if (types == TYPE_PATH) {
220+
} else if (type == TYPE_PATH) {
166221
const char *v;
167222
if (git_config_pathname(&v, key_, value_) < 0)
168223
return -1;
169224
strbuf_addstr(buf, v);
170225
free((char *)v);
171-
} else if (types == TYPE_EXPIRY_DATE) {
226+
} else if (type == TYPE_EXPIRY_DATE) {
172227
timestamp_t t;
173228
if (git_config_expiry_date(&t, key_, value_) < 0)
174229
return -1;
@@ -287,7 +342,7 @@ static char *normalize_value(const char *key, const char *value)
287342
if (!value)
288343
return NULL;
289344

290-
if (types == 0 || types == TYPE_PATH || types == TYPE_EXPIRY_DATE)
345+
if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE)
291346
/*
292347
* We don't do normalization for TYPE_PATH here: If
293348
* the path is like ~/foobar/, we prefer to store
@@ -296,11 +351,11 @@ static char *normalize_value(const char *key, const char *value)
296351
* Also don't do normalization for expiry dates.
297352
*/
298353
return xstrdup(value);
299-
if (types == TYPE_INT)
354+
if (type == TYPE_INT)
300355
return xstrfmt("%"PRId64, git_config_int64(key, value));
301-
if (types == TYPE_BOOL)
356+
if (type == TYPE_BOOL)
302357
return xstrdup(git_config_bool(key, value) ? "true" : "false");
303-
if (types == TYPE_BOOL_OR_INT) {
358+
if (type == TYPE_BOOL_OR_INT) {
304359
int is_bool, v;
305360
v = git_config_bool_or_int(key, value, &is_bool);
306361
if (!is_bool)
@@ -309,7 +364,7 @@ static char *normalize_value(const char *key, const char *value)
309364
return xstrdup(v ? "true" : "false");
310365
}
311366

312-
die("BUG: cannot normalize type %d", types);
367+
die("BUG: cannot normalize type %d", type);
313368
}
314369

315370
static int get_color_found;
@@ -566,12 +621,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
566621
key_delim = '\n';
567622
}
568623

569-
if (HAS_MULTI_BITS(types)) {
570-
error("only one type at a time.");
571-
usage_with_options(builtin_config_usage, builtin_config_options);
572-
}
573-
574-
if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && types) {
624+
if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && type) {
575625
error("--get-color and variable type are incoherent");
576626
usage_with_options(builtin_config_usage, builtin_config_options);
577627
}

t/t1300-config.sh

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ test_expect_success bool '
742742
do
743743
git config --bool --get bool.true$i >>result
744744
git config --bool --get bool.false$i >>result
745-
done &&
745+
done &&
746746
test_cmp expect result'
747747

748748
test_expect_success 'invalid bool (--get)' '
@@ -1686,6 +1686,69 @@ test_expect_success '--local requires a repo' '
16861686
test_expect_code 128 nongit git config --local foo.bar
16871687
'
16881688

1689+
cat >.git/config <<-\EOF &&
1690+
[core]
1691+
foo = true
1692+
number = 10
1693+
big = 1M
1694+
EOF
1695+
1696+
test_expect_success 'identical modern --type specifiers are allowed' '
1697+
git config --type=int --type=int core.big >actual &&
1698+
echo 1048576 >expect &&
1699+
test_cmp expect actual
1700+
'
1701+
1702+
test_expect_success 'identical legacy --type specifiers are allowed' '
1703+
git config --int --int core.big >actual &&
1704+
echo 1048576 >expect &&
1705+
test_cmp expect actual
1706+
'
1707+
1708+
test_expect_success 'identical mixed --type specifiers are allowed' '
1709+
git config --int --type=int core.big >actual &&
1710+
echo 1048576 >expect &&
1711+
test_cmp expect actual
1712+
'
1713+
1714+
test_expect_success 'non-identical modern --type specifiers are not allowed' '
1715+
test_must_fail git config --type=int --type=bool core.big 2>error &&
1716+
test_i18ngrep "only one type at a time" error
1717+
'
1718+
1719+
test_expect_success 'non-identical legacy --type specifiers are not allowed' '
1720+
test_must_fail git config --int --bool core.big 2>error &&
1721+
test_i18ngrep "only one type at a time" error
1722+
'
1723+
1724+
test_expect_success 'non-identical mixed --type specifiers are not allowed' '
1725+
test_must_fail git config --type=int --bool core.big 2>error &&
1726+
test_i18ngrep "only one type at a time" error
1727+
'
1728+
1729+
test_expect_success '--type allows valid type specifiers' '
1730+
echo "true" >expect &&
1731+
git config --type=bool core.foo >actual &&
1732+
test_cmp expect actual
1733+
'
1734+
1735+
test_expect_success '--no-type unsets type specifiers' '
1736+
echo "10" >expect &&
1737+
git config --type=bool --no-type core.number >actual &&
1738+
test_cmp expect actual
1739+
'
1740+
1741+
test_expect_success 'unset type specifiers may be reset to conflicting ones' '
1742+
echo 1048576 >expect &&
1743+
git config --type=bool --no-type --type=int core.big >actual &&
1744+
test_cmp expect actual
1745+
'
1746+
1747+
test_expect_success '--type rejects unknown specifiers' '
1748+
test_must_fail git config --type=nonsense core.foo 2>error &&
1749+
test_i18ngrep "unrecognized --type argument" error
1750+
'
1751+
16891752
test_expect_success '--replace-all does not invent newlines' '
16901753
q_to_tab >.git/config <<-\EOF &&
16911754
[abc]key

0 commit comments

Comments
 (0)