Skip to content

Commit a789ca7

Browse files
committed
config: teach "git -c" to recognize an empty string
In a config file, you can do: [foo] bar to turn the "foo.bar" boolean flag on, and you can do: [foo] bar= to set "foo.bar" to the empty string. However, git's "-c" parameter treats both: git -c foo.bar and git -c foo.bar= as the boolean flag, and there is no way to set a variable to the empty string. This patch enables the latter form to do that. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e6aaa39 commit a789ca7

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

Documentation/git.txt

+5
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,11 @@ example the following invocations are equivalent:
429429
given will override values from configuration files.
430430
The <name> is expected in the same format as listed by
431431
'git config' (subkeys separated by dots).
432+
+
433+
Note that omitting the `=` in `git -c foo.bar ...` is allowed and sets
434+
`foo.bar` to the boolean true value (just like `[foo]bar` would in a
435+
config file). Including the equals but with an empty value (like `git -c
436+
foo.bar= ...`) sets `foo.bar` to the empty string.
432437

433438
--exec-path[=<path>]::
434439
Path to wherever your core Git programs are installed.

config.c

+10-2
Original file line numberDiff line numberDiff line change
@@ -164,19 +164,27 @@ void git_config_push_parameter(const char *text)
164164
int git_config_parse_parameter(const char *text,
165165
config_fn_t fn, void *data)
166166
{
167+
const char *value;
167168
struct strbuf **pair;
169+
168170
pair = strbuf_split_str(text, '=', 2);
169171
if (!pair[0])
170172
return error("bogus config parameter: %s", text);
171-
if (pair[0]->len && pair[0]->buf[pair[0]->len - 1] == '=')
173+
174+
if (pair[0]->len && pair[0]->buf[pair[0]->len - 1] == '=') {
172175
strbuf_setlen(pair[0], pair[0]->len - 1);
176+
value = pair[1] ? pair[1]->buf : "";
177+
} else {
178+
value = NULL;
179+
}
180+
173181
strbuf_trim(pair[0]);
174182
if (!pair[0]->len) {
175183
strbuf_list_free(pair);
176184
return error("bogus config parameter: %s", text);
177185
}
178186
lowercase(pair[0]->buf);
179-
if (fn(pair[0]->buf, pair[1] ? pair[1]->buf : NULL, data) < 0) {
187+
if (fn(pair[0]->buf, value, data) < 0) {
180188
strbuf_list_free(pair);
181189
return -1;
182190
}

t/t1300-repo-config.sh

+11
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,17 @@ test_expect_success 'git -c "key=value" support' '
10061006
test_must_fail git -c name=value config core.name
10071007
'
10081008

1009+
# We just need a type-specifier here that cares about the
1010+
# distinction internally between a NULL boolean and a real
1011+
# string (because most of git's internal parsers do care).
1012+
# Using "--path" works, but we do not otherwise care about
1013+
# its semantics.
1014+
test_expect_success 'git -c can represent empty string' '
1015+
echo >expect &&
1016+
git -c foo.empty= config --path foo.empty >actual &&
1017+
test_cmp expect actual
1018+
'
1019+
10091020
test_expect_success 'key sanity-checking' '
10101021
test_must_fail git config foo=bar &&
10111022
test_must_fail git config foo=.bar &&

0 commit comments

Comments
 (0)