]> git.ipfire.org Git - thirdparty/git.git/commitdiff
config: values of pathname type can be prefixed with :(optional)
authorJunio C Hamano <gitster@pobox.com>
Sun, 28 Sep 2025 21:29:15 +0000 (17:29 -0400)
committerJunio C Hamano <gitster@pobox.com>
Sun, 28 Sep 2025 22:40:30 +0000 (15:40 -0700)
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. <xmqq5ywehb69.fsf@gitster.g>

Signed-off-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Taylor Blau <me@ttaylorr.com>
Signed-off-by: D. Ben Knoble <ben.knoble+github@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/config.adoc
config.c
t/t7500-commit-template-squash-signoff.sh
wrapper.c
wrapper.h

index cc769251be339bb67a84161a0d561285b10f906b..7301ced836045b1008f84b5f6095f623fe98436f 100644 (file)
@@ -358,7 +358,9 @@ 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.
 
 Variables
 ~~~~~~~~~
index 97ffef427001111eff159bb2ceee7557c3521f1e..73fc74c8fa1a35133d4d333356845cd1e73f6acd 100644 (file)
--- a/config.c
+++ b/config.c
@@ -1279,11 +1279,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_missing_file(path)) {
+               free(path);
+               return 0;
+       }
+
+       *dest = path;
        return 0;
 }
 
index 05cda50186f0eef938cdb3a750bfe1b5d1af7f39..366f7f23b373d5c2a15637bb0365bd323b755036 100755 (executable)
@@ -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
 
index 2f00d2ac876c16b4addf96ec2ba14dcc90795748..3d507d420452030e77fee9e181762513e5aa9fe8 100644 (file)
--- a/wrapper.c
+++ b/wrapper.c
@@ -721,6 +721,19 @@ int xgethostname(char *buf, size_t len)
        return ret;
 }
 
+int is_missing_file(const char *filename)
+{
+       struct stat st;
+
+       if (stat(filename, &st) < 0) {
+               if (errno == ENOENT)
+                       return 1;
+               die_errno(_("could not stat %s"), filename);
+       }
+
+       return 0;
+}
+
 int is_empty_or_missing_file(const char *filename)
 {
        struct stat st;
index 7df824e34a906e9f822ad3bee27b8b1b0000621b..44a8597ac314267d80b5cd45b11d2684bbde0cf9 100644 (file)
--- a/wrapper.h
+++ b/wrapper.h
@@ -66,7 +66,9 @@ void write_file_buf(const char *path, const char *buf, size_t len);
 __attribute__((format (printf, 2, 3)))
 void write_file(const char *path, const char *fmt, ...);
 
-/* Return 1 if the file is empty or does not exists, 0 otherwise. */
+/* Return 1 if the file does not exist, 0 otherwise. */
+int is_missing_file(const char *filename);
+/* Return 1 if the file is empty or does not exist, 0 otherwise. */
 int is_empty_or_missing_file(const char *filename);
 
 enum fsync_action {