builtin/config: do not die in `get_color()`

When trying to parse an invalid color via `get_color()` we die. We're about to introduce another caller in a subsequent commit though that has its own error handling, so dying is a bit drastic there. Furthermore, the only caller that we already have right now already knows to handle errors in other branches that don't call `get_color()`. Convert the function to instead return an error code to improve its flexibility. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Sep 22, 2025 at 15:06 UTC 6e6ed3eaba315ceab0e0e9256474caac8520a819
1 file changed +11 -4
builtin/config.c
+11 -4
@@ -547,24 +547,31 @@ static int git_get_color_config(const char *var, const char *value,
547 return 0;
548 }
549
550 -static void get_color(const struct config_location_options *opts,
550 +static int get_color(const struct config_location_options *opts,
551 const char *var, const char *def_color)
552 {
553 struct get_color_config_data data = {
554 .get_color_slot = var,
555 .parsed_color[0] = '\0',
556 };
557 + int ret;
558
559 config_with_options(git_get_color_config, &data,
560 &opts->source, the_repository,
561 &opts->options);
562
563 if (!data.get_color_found && def_color) {
563 - if (color_parse(def_color, data.parsed_color) < 0)
564 - die(_("unable to parse default color value"));
564 + if (color_parse(def_color, data.parsed_color) < 0) {
565 + ret = error(_("unable to parse default color value"));
566 + goto out;
567 + }
568 }
569
570 + ret = 0;
571 +
572 +out:
573 fputs(data.parsed_color, stdout);
574 + return ret;
575 }
576
577 struct get_colorbool_config_data {
@@ -1390,7 +1397,7 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
1397 }
1398 else if (actions == ACTION_GET_COLOR) {
1399 check_argc(argc, 1, 2);
1393 - get_color(&location_opts, argv[0], argv[1]);
1400 + ret = get_color(&location_opts, argv[0], argv[1]);
1401 }
1402 else if (actions == ACTION_GET_COLORBOOL) {
1403 check_argc(argc, 1, 2);