Skip to content

Commit 749d6d1

Browse files
committed
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 file is not even seen. cf. <[email protected]> Signed-off-by: Junio C Hamano <[email protected]> Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: D. Ben Knoble <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6b4f073 commit 749d6d1

File tree

5 files changed

+41
-4
lines changed

5 files changed

+41
-4
lines changed

Documentation/config.adoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,9 @@ 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.
362364

363365
Variables
364366
~~~~~~~~~

config.c

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

12801280
int git_config_pathname(char **dest, const char *var, const char *value)
12811281
{
1282+
int is_optional;
1283+
char *path;
1284+
12821285
if (!value)
12831286
return config_error_nonbool(var);
1284-
*dest = interpolate_path(value, 0);
1285-
if (!*dest)
1287+
1288+
is_optional = skip_prefix(value, ":(optional)", &value);
1289+
path = interpolate_path(value, 0);
1290+
if (!path)
12861291
die(_("failed to expand user dir in: '%s'"), value);
1292+
1293+
if (is_optional && is_missing_file(path)) {
1294+
free(path);
1295+
return 0;
1296+
}
1297+
1298+
*dest = path;
12871299
return 0;
12881300
}
12891301

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ 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+
GIT_EDITOR="echo hello >" git commit --allow-empty &&
52+
git cat-file commit HEAD | sed -e "1,/^$/d" >actual &&
53+
echo hello >expect &&
54+
test_cmp expect actual
55+
'
56+
4957
# From now on we'll use a template file that exists.
5058
TEMPLATE="$PWD"/template
5159

wrapper.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,19 @@ int xgethostname(char *buf, size_t len)
721721
return ret;
722722
}
723723

724+
int is_missing_file(const char *filename)
725+
{
726+
struct stat st;
727+
728+
if (stat(filename, &st) < 0) {
729+
if (errno == ENOENT)
730+
return 1;
731+
die_errno(_("could not stat %s"), filename);
732+
}
733+
734+
return 0;
735+
}
736+
724737
int is_empty_or_missing_file(const char *filename)
725738
{
726739
struct stat st;

wrapper.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ void write_file_buf(const char *path, const char *buf, size_t len);
6666
__attribute__((format (printf, 2, 3)))
6767
void write_file(const char *path, const char *fmt, ...);
6868

69-
/* Return 1 if the file is empty or does not exists, 0 otherwise. */
69+
/* Return 1 if the file does not exist, 0 otherwise. */
70+
int is_missing_file(const char *filename);
71+
/* Return 1 if the file is empty or does not exist, 0 otherwise. */
7072
int is_empty_or_missing_file(const char *filename);
7173

7274
enum fsync_action {

0 commit comments

Comments
 (0)