aboutsummaryrefslogtreecommitdiff
path: root/builtin
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2025-12-05 14:49:56 +0900
committerJunio C Hamano <gitster@pobox.com>2025-12-05 14:49:56 +0900
commit0534b78576b410d10e2cfb61802ea829713bde03 (patch)
tree1c6c71b66cf0491260d82ae7008782e7b9ddcc68 /builtin
parent5eadcbf8151f04d5c5bf0b8865ee8fa8447878f7 (diff)
parent0bd16856ffb3968de73699ad0555d1fae6c45406 (diff)
downloadgit-0534b78576b410d10e2cfb61802ea829713bde03.tar.xz
Merge branch 'jc/optional-path'
"git config get --path" segfaulted on an ":(optional)path" that does not exist, which has been corrected. * jc/optional-path: config: really treat missing optional path as not configured config: really pretend missing :(optional) value is not there config: mark otherwise unused function as file-scope static
Diffstat (limited to 'builtin')
-rw-r--r--builtin/blame.c3
-rw-r--r--builtin/config.c45
-rw-r--r--builtin/receive-pack.c5
3 files changed, 41 insertions, 12 deletions
diff --git a/builtin/blame.c b/builtin/blame.c
index 27b513d27f..6044973462 100644
--- a/builtin/blame.c
+++ b/builtin/blame.c
@@ -739,7 +739,8 @@ static int git_blame_config(const char *var, const char *value,
ret = git_config_pathname(&str, var, value);
if (ret)
return ret;
- string_list_insert(&ignore_revs_file_list, str);
+ if (str)
+ string_list_insert(&ignore_revs_file_list, str);
free(str);
return 0;
}
diff --git a/builtin/config.c b/builtin/config.c
index 75852bd79d..e58eb6a176 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -261,6 +261,12 @@ struct strbuf_list {
int alloc;
};
+/*
+ * Format the configuration key-value pair (`key_`, `value_`) and
+ * append it into strbuf `buf`. Returns a negative value on failure,
+ * 0 on success, 1 on a missing optional value (i.e., telling the
+ * caller to pretend that <key_,value_> did not exist).
+ */
static int format_config(const struct config_display_options *opts,
struct strbuf *buf, const char *key_,
const char *value_, const struct key_value_info *kvi)
@@ -299,7 +305,10 @@ static int format_config(const struct config_display_options *opts,
char *v;
if (git_config_pathname(&v, key_, value_) < 0)
return -1;
- strbuf_addstr(buf, v);
+ if (v)
+ strbuf_addstr(buf, v);
+ else
+ return 1; /* :(optional)no-such-file */
free((char *)v);
} else if (opts->type == TYPE_EXPIRY_DATE) {
timestamp_t t;
@@ -344,6 +353,7 @@ static int collect_config(const char *key_, const char *value_,
struct collect_config_data *data = cb;
struct strbuf_list *values = data->values;
const struct key_value_info *kvi = ctx->kvi;
+ int status;
if (!(data->get_value_flags & GET_VALUE_KEY_REGEXP) &&
strcmp(key_, data->key))
@@ -361,8 +371,15 @@ static int collect_config(const char *key_, const char *value_,
ALLOC_GROW(values->items, values->nr + 1, values->alloc);
strbuf_init(&values->items[values->nr], 0);
- return format_config(data->display_opts, &values->items[values->nr++],
- key_, value_, kvi);
+ status = format_config(data->display_opts, &values->items[values->nr++],
+ key_, value_, kvi);
+ if (status < 0)
+ return status;
+ if (status) {
+ strbuf_release(&values->items[--values->nr]);
+ status = 0;
+ }
+ return status;
}
static int get_value(const struct config_location_options *opts,
@@ -438,15 +455,23 @@ static int get_value(const struct config_location_options *opts,
if (!values.nr && display_opts->default_value) {
struct key_value_info kvi = KVI_INIT;
struct strbuf *item;
+ int status;
kvi_from_param(&kvi);
ALLOC_GROW(values.items, values.nr + 1, values.alloc);
item = &values.items[values.nr++];
strbuf_init(item, 0);
- if (format_config(display_opts, item, key_,
- display_opts->default_value, &kvi) < 0)
+
+ status = format_config(display_opts, item, key_,
+ display_opts->default_value, &kvi);
+ if (status < 0)
die(_("failed to format default config value: %s"),
display_opts->default_value);
+ if (status) {
+ /* default was a missing optional value */
+ values.nr--;
+ strbuf_release(item);
+ }
}
ret = !values.nr;
@@ -714,11 +739,13 @@ static int get_urlmatch(const struct config_location_options *opts,
for_each_string_list_item(item, &values) {
struct urlmatch_current_candidate_value *matched = item->util;
struct strbuf buf = STRBUF_INIT;
+ int status;
- format_config(&display_opts, &buf, item->string,
- matched->value_is_null ? NULL : matched->value.buf,
- &matched->kvi);
- fwrite(buf.buf, 1, buf.len, stdout);
+ status = format_config(&display_opts, &buf, item->string,
+ matched->value_is_null ? NULL : matched->value.buf,
+ &matched->kvi);
+ if (!status)
+ fwrite(buf.buf, 1, buf.len, stdout);
strbuf_release(&buf);
strbuf_release(&matched->value);
diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index e8ee0e7321..28a6404219 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -177,8 +177,9 @@ static int receive_pack_config(const char *var, const char *value,
if (git_config_pathname(&path, var, value))
return -1;
- strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
- fsck_msg_types.len ? ',' : '=', path);
+ if (path)
+ strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
+ fsck_msg_types.len ? ',' : '=', path);
free(path);
return 0;
}