[PATCH 2/5] parse: add git_parse_maybe_pathname()
- From
Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com>
- Date
- Feb 10, 2026, 04:42 UTC
- Message-ID
- <8d3a6a8265714c5e4bae0f2e5a587ea46a6adddc.1770698579.git.gitgitgadget@gmail.com>
- In-Reply-To
- <pull.2044.git.1770698579.gitgitgadget@gmail.com>
From: Derrick Stolee <stolee@gmail.com>
This extraction of logic from config.c's git_config_pathname() allows for parsing a fully-qualified path from a relative path along with validation of the existence of the path without failing with a die().
Signed-off-by: Derrick Stolee <stolee@gmail.com> --- config.c | 14 +------------- parse.c | 24 ++++++++++++++++++++++++ parse.h | 2 ++ 3 files changed, 27 insertions(+), 13 deletions(-)
diff --git a/config.c b/config.c index 7f6d53b473..83257b7a97 100644 --- a/config.c +++ b/config.c @@ -1278,24 +1278,12 @@ int git_config_string(char **dest, const char *var, const char *value) int git_config_pathname(char **dest, const char *var, const char *value) { - bool is_optional; - char *path; - if (!value) return config_error_nonbool(var); - is_optional = skip_prefix(value, ":(optional)", &value); - path = interpolate_path(value, 0); - if (!path) + if (git_parse_maybe_pathname(value, dest) < 0) die(_("failed to expand user dir in: '%s'"), value); - if (is_optional && is_missing_file(path)) { - free(path); - *dest = NULL; - return 0; - } - - *dest = path; return 0; } diff --git a/parse.c b/parse.c index 48313571aa..3f37f0b93a 100644 --- a/parse.c +++ b/parse.c @@ -1,6 +1,7 @@ #include "git-compat-util.h" #include "gettext.h" #include "parse.h" +#include "path.h" static uintmax_t get_unit_factor(const char *end) { @@ -209,3 +210,26 @@ unsigned long git_env_ulong(const char *k, unsigned long val) die(_("failed to parse %s"), k); return val; } + +int git_parse_maybe_pathname(const char *value, char **dest) +{ + bool is_optional; + char *path; + + if (!value) + return -1; + + is_optional = skip_prefix(value, ":(optional)", &value); + path = interpolate_path(value, 0); + if (!path) + return -1; + + if (is_optional && is_missing_file(path)) { + free(path); + *dest = NULL; + return 0; + } + + *dest = path; + return 0; +} diff --git a/parse.h b/parse.h index ea32de9a91..4f97c3727a 100644 --- a/parse.h +++ b/parse.h @@ -19,4 +19,6 @@ int git_parse_maybe_bool_text(const char *value); int git_env_bool(const char *, int); unsigned long git_env_ulong(const char *, unsigned long); +int git_parse_maybe_pathname(const char *value, char **dest); + #endif /* PARSE_H */
-- gitgitgadget