Skip to content

Commit dbafaff

Browse files
gitsterttaylorr
authored andcommitted
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. <[email protected]> Signed-off-by: Junio C Hamano <[email protected]> Signed-off-by: Taylor Blau <[email protected]>
1 parent 82d283c commit dbafaff

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

Documentation/config.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,10 @@ compiled without runtime prefix support, the compiled-in prefix will be
358358
substituted instead. In the unlikely event that a literal path needs to
359359
be specified that should _not_ be expanded, it needs to be prefixed by
360360
`./`, like so: `./%(prefix)/bin`.
361-
361+
+
362+
If prefixed with `:(optional)`, the configuration variable is treated
363+
as if it does not exist, if the named path does not exist or names an
364+
empty file.
362365

363366
Variables
364367
~~~~~~~~~

config.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,11 +1364,23 @@ int git_config_string(char **dest, const char *var, const char *value)
13641364

13651365
int git_config_pathname(char **dest, const char *var, const char *value)
13661366
{
1367+
int is_optional;
1368+
char *path;
1369+
13671370
if (!value)
13681371
return config_error_nonbool(var);
1369-
*dest = interpolate_path(value, 0);
1370-
if (!*dest)
1372+
1373+
is_optional = skip_prefix(value, ":(optional)", &value);
1374+
path = interpolate_path(value, 0);
1375+
if (!path)
13711376
die(_("failed to expand user dir in: '%s'"), value);
1377+
1378+
if (is_optional && is_empty_or_missing_file(path)) {
1379+
free(path);
1380+
return 0;
1381+
}
1382+
1383+
*dest = path;
13721384
return 0;
13731385
}
13741386

t/t7500-commit-template-squash-signoff.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ test_expect_success 'nonexistent template file in config should return error' '
4646
)
4747
'
4848

49+
test_expect_success 'nonexistent optional template file in config' '
50+
test_config commit.template ":(optional)$PWD"/notexist &&
51+
(
52+
GIT_EDITOR="echo hello >\"\$1\"" &&
53+
export GIT_EDITOR &&
54+
git commit --allow-empty
55+
)
56+
'
57+
4958
# From now on we'll use a template file that exists.
5059
TEMPLATE="$PWD"/template
5160

0 commit comments

Comments
 (0)