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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Aug 4, 2014 at 15:40 UTC a789ca70e7a5b02973b116d21674acd795238f99
3 files changed +26 -2
Documentation/git.txt
+5
@@ -429,6 +429,11 @@ example the following invocations are equivalent:
429 given will override values from configuration files.
430 The <name> is expected in the same format as listed by
431 '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.
437
438 --exec-path[=<path>]::
439 Path to wherever your core Git programs are installed.
config.c
+10 -2
@@ -164,19 +164,27 @@ void git_config_push_parameter(const char *text)
164 int git_config_parse_parameter(const char *text,
165 config_fn_t fn, void *data)
166 {
167 + const char *value;
168 struct strbuf **pair;
169 +
170 pair = strbuf_split_str(text, '=', 2);
171 if (!pair[0])
172 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] == '=') {
175 strbuf_setlen(pair[0], pair[0]->len - 1);
176 + value = pair[1] ? pair[1]->buf : "";
177 + } else {
178 + value = NULL;
179 + }
180 +
181 strbuf_trim(pair[0]);
182 if (!pair[0]->len) {
183 strbuf_list_free(pair);
184 return error("bogus config parameter: %s", text);
185 }
186 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) {
188 strbuf_list_free(pair);
189 return -1;
190 }
t/t1300-repo-config.sh
+11
@@ -1006,6 +1006,17 @@ test_expect_success 'git -c "key=value" support' '
1006 test_must_fail git -c name=value config core.name
1007 '
1008
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 +
1020 test_expect_success 'key sanity-checking' '
1021 test_must_fail git config foo=bar &&
1022 test_must_fail git config foo=.bar &&