Files
git-mirror/t/t1311-config-optional.sh
Junio C Hamano ce1a5a22a5 config: really pretend missing :(optional) value is not there
Earlier we added support for a value spelled as ":(optional)path"
for configuration variables whose values are of type "path", with
the documented semantics "if the path is missing, behave as if such
a variable definition is not even there."

This has worked OK for code paths that reads configuration files and
stores the configured value as a string, where NULL in such a string
is treated as if the setting is not there, left as the default.

However, there are other code paths that do not _ignore_ such NULL
values and misbehave.  "git config get --path" is one of them.

When git_config_pathname() helper function finds that the value of
the variable is an optional path *and* the path is missing, it
leaves the destination pointer intact (which usually is left to
NULL) and returns 0 to signal a success.  format_config() helper
however assumed that the destination pointer always gets a string,
which no longer is the case, and segfaulted.

Make sure that git_config_pathname() clears the destination pointer
in such a case, and teach format_config() to react to the condition
by returning 1 (which is different from 0 that is a normal success
and negative that is an error) to its callers.  Adjust the callers
to react to this new return value that tells them to pretend as if
they did not even see this partcular <key, value> pair.

Reported-by: Han Jiang <jhcarl0814@gmail.com>
Helped-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2025-11-24 17:00:47 -08:00

39 lines
1.0 KiB
Bash
Executable File

#!/bin/sh
#
# Copyright (c) 2025 Google LLC
#
test_description=':(optional) paths'
. ./test-lib.sh
test_expect_success 'var=:(optional)path-exists' '
test_config a.path ":(optional)path-exists" &&
>path-exists &&
echo path-exists >expect &&
git config get --path a.path >actual &&
test_cmp expect actual
'
test_expect_success 'missing optional value is ignored' '
test_config a.path ":(optional)no-such-path" &&
# Using --show-scope ensures we skip writing not only the value
# but also any meta-information about the ignored key.
test_must_fail git config get --show-scope --path a.path >actual &&
test_line_count = 0 actual
'
test_expect_success 'missing optional value is ignored in multi-value config' '
test_when_finished "git config unset --all a.path" &&
git config set --append a.path ":(optional)path-exists" &&
git config set --append a.path ":(optional)no-such-path" &&
>path-exists &&
echo path-exists >expect &&
git config --get --path a.path >actual &&
test_cmp expect actual
'
test_done