From: "D. Ben Knoble" <ben.knoble+github@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>,
"Noah Pendleton" <noah.pendleton@gmail.com>,
"Patrick Steinhardt" <ps@pks.im>,
"Phillip Wood" <phillip.wood123@gmail.com>,
"Thranur Andul" <thranur@gmail.com>,
"Michael Grosser" <grosser.michael@gmail.com>,
"Eric Sunshine" <sunshine@sunshineco.com>,
"Taylor Blau" <me@ttaylorr.com>,
"D. Ben Knoble" <ben.knoble+github@gmail.com>,
"Matheus Tavares" <matheus.tavb@gmail.com>,
"Johannes Schindelin" <Johannes.Schindelin@gmx.de>,
"Calvin Wan" <calvinwan@google.com>,
"brian m. carlson" <sandals@crustytoothpaste.net>,
"Martin Ågren" <martin.agren@gmail.com>
Subject: [PATCH v2 2/3] config: values of pathname type can be prefixed with :(optional)
Date: Sun, 28 Sep 2025 17:29:15 -0400 [thread overview]
Message-ID: <5c97f580a9e77c464bc6bf4ed9ea8546711c6637.1759094936.git.ben.knoble+github@gmail.com> (raw)
In-Reply-To: <cover.1759094936.git.ben.knoble+github@gmail.com>
From: Junio C Hamano <gitster@pobox.com>
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>
---
Notes:
The 2nd paragraph in this commit is wrapped strangely
I've kept the strange wrapping length for now, but can reflow it if
desired.
Documentation/config.adoc | 4 +++-
config.c | 16 ++++++++++++++--
t/t7500-commit-template-squash-signoff.sh | 9 +++++++++
wrapper.c | 13 +++++++++++++
wrapper.h | 4 +++-
5 files changed, 42 insertions(+), 4 deletions(-)
diff --git a/Documentation/config.adoc b/Documentation/config.adoc
index cc769251be..7301ced836 100644
--- a/Documentation/config.adoc
+++ b/Documentation/config.adoc
@@ -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
~~~~~~~~~
diff --git a/config.c b/config.c
index 97ffef4270..73fc74c8fa 100644
--- 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;
}
diff --git a/t/t7500-commit-template-squash-signoff.sh b/t/t7500-commit-template-squash-signoff.sh
index 05cda50186..366f7f23b3 100755
--- a/t/t7500-commit-template-squash-signoff.sh
+++ b/t/t7500-commit-template-squash-signoff.sh
@@ -46,6 +46,15 @@ commit_msg_is ()
)
'
+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
diff --git a/wrapper.c b/wrapper.c
index 2f00d2ac87..3d507d4204 100644
--- 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;
diff --git a/wrapper.h b/wrapper.h
index 7df824e34a..44a8597ac3 100644
--- 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 {
--
2.48.1
next prev parent reply other threads:[~2025-09-28 21:30 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-08-07 20:27 [PATCH 0/1] blame: Skip missing ignore-revs file Noah Pendleton
2021-08-07 20:58 ` Junio C Hamano
2021-08-07 21:34 ` Noah Pendleton
2021-08-08 5:43 ` Junio C Hamano
2021-08-08 17:50 ` Junio C Hamano
2021-08-08 18:21 ` Noah Pendleton
2021-08-09 15:47 ` Junio C Hamano
2024-10-14 20:44 ` [PATCH 0/3] specifying a file that can optionally exist Junio C Hamano
2024-10-14 20:44 ` [PATCH 1/3] t7500: make each piece more independent Junio C Hamano
2024-10-14 20:44 ` [PATCH 2/3] config: values of pathname type can be prefixed with :(optional) Junio C Hamano
2024-10-14 20:44 ` [PATCH 3/3] parseopt: " Junio C Hamano
2025-05-01 21:40 ` [PATCH 0/3] specifying a file that can optionally exist Junio C Hamano
2025-05-01 21:40 ` [PATCH 1/3] t7500: make each piece more independent Junio C Hamano
2025-05-01 21:40 ` [PATCH 2/3] config: values of pathname type can be prefixed with :(optional) Junio C Hamano
2025-05-02 8:52 ` Patrick Steinhardt
2025-05-02 14:28 ` Phillip Wood
2025-05-02 20:05 ` Junio C Hamano
2025-05-01 21:40 ` [PATCH 3/3] parseopt: " Junio C Hamano
2025-09-28 21:29 ` [PATCH v2 0/3] Support :(optional) filepaths D. Ben Knoble
2025-09-28 21:29 ` [PATCH v2 1/3] t7500: make each piece more independent D. Ben Knoble
2025-09-28 21:29 ` D. Ben Knoble [this message]
2025-09-30 15:26 ` [PATCH v2 2/3] config: values of pathname type can be prefixed with :(optional) Phillip Wood
2025-10-06 19:00 ` Junio C Hamano
2025-10-06 19:59 ` Junio C Hamano
2025-10-06 20:21 ` Junio C Hamano
2025-10-06 20:22 ` Junio C Hamano
2025-10-07 12:24 ` Kristoffer Haugsbakk
2025-10-07 17:04 ` Junio C Hamano
2025-09-28 21:29 ` [PATCH v2 3/3] parseopt: " D. Ben Knoble
2025-09-30 15:26 ` Phillip Wood
2025-09-28 22:40 ` [PATCH v2 0/3] Support :(optional) filepaths Junio C Hamano
2025-09-29 16:42 ` Ben Knoble
2022-03-04 9:51 ` [PATCH 0/1] blame: Skip missing ignore-revs file Thranur Andul
2021-08-08 17:48 ` [PATCH v2] blame: add config `blame.ignoreRevsFileIsOptional` Noah Pendleton
-- strict thread matches above, loose matches on Subject: below --
2025-04-25 18:41 Feature request: automatically read .git-blame-ignore-revs or allow global optional config Michael Grosser
2025-04-25 19:54 ` Eric Sunshine
2025-05-01 18:00 ` D. Ben Knoble
2025-05-01 18:28 ` Eric Sunshine
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5c97f580a9e77c464bc6bf4ed9ea8546711c6637.1759094936.git.ben.knoble+github@gmail.com \
--to=ben.knoble+github@gmail.com \
--cc=Johannes.Schindelin@gmx.de \
--cc=calvinwan@google.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=grosser.michael@gmail.com \
--cc=martin.agren@gmail.com \
--cc=matheus.tavb@gmail.com \
--cc=me@ttaylorr.com \
--cc=noah.pendleton@gmail.com \
--cc=phillip.wood123@gmail.com \
--cc=ps@pks.im \
--cc=sandals@crustytoothpaste.net \
--cc=sunshine@sunshineco.com \
--cc=thranur@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).