config: values of pathname type can be prefixed with :(optional)

Sometimes people want to specify additional configuration data
as "best effort" basis.  Maybe commit.template configuration file points
at somewhere in ~/template/ but on a particular system, the file may not
exist and the user may be OK without using the template in such a case.

When the value given to a configuration variable whose type is
pathname wants to signal such an optional file, it can be marked by
prepending ":(optional)" in front of it.  Such a setting that is
marked optional would avoid getting the command barf for a missing
file, as an optional configuration setting that names a missing or
an empty file is not even seen.

cf. <xmqq5ywehb69.fsf@gitster.g>

Signed-off-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Taylor Blau <me@ttaylorr.com>
This commit is contained in:
Junio C Hamano
2024-10-14 13:44:26 -07:00
committed by Taylor Blau
parent 82d283c626
commit dbafaff13b
3 changed files with 27 additions and 3 deletions

View File

@@ -358,7 +358,10 @@ compiled without runtime prefix support, the compiled-in prefix will be
substituted instead. In the unlikely event that a literal path needs to
be specified that should _not_ be expanded, it needs to be prefixed by
`./`, like so: `./%(prefix)/bin`.
+
If prefixed with `:(optional)`, the configuration variable is treated
as if it does not exist, if the named path does not exist or names an
empty file.
Variables
~~~~~~~~~

View File

@@ -1364,11 +1364,23 @@ int git_config_string(char **dest, const char *var, const char *value)
int git_config_pathname(char **dest, const char *var, const char *value)
{
int is_optional;
char *path;
if (!value)
return config_error_nonbool(var);
*dest = interpolate_path(value, 0);
if (!*dest)
is_optional = skip_prefix(value, ":(optional)", &value);
path = interpolate_path(value, 0);
if (!path)
die(_("failed to expand user dir in: '%s'"), value);
if (is_optional && is_empty_or_missing_file(path)) {
free(path);
return 0;
}
*dest = path;
return 0;
}

View File

@@ -46,6 +46,15 @@ test_expect_success 'nonexistent template file in config should return error' '
)
'
test_expect_success 'nonexistent optional template file in config' '
test_config commit.template ":(optional)$PWD"/notexist &&
(
GIT_EDITOR="echo hello >\"\$1\"" &&
export GIT_EDITOR &&
git commit --allow-empty
)
'
# From now on we'll use a template file that exists.
TEMPLATE="$PWD"/template