git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 3/5] builtin/config: do not die in `get_color()`

From
Patrick Steinhardt <ps@pks.im>
Date
Sep 15, 2025, 12:52 UTC
Message-ID
<20250915-pks-config-color-v2-3-e4290bd8d13c@pks.im>
In-Reply-To
<20250915-pks-config-color-v2-0-e4290bd8d13c@pks.im>

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>
---
 builtin/config.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/builtin/config.c b/builtin/config.c
index 59fb113b07..afd48bfa51 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -547,24 +547,31 @@ static int git_get_color_config(const char *var, const char *value,
 	return 0;
 }
 
-static void get_color(const struct config_location_options *opts,
+static int get_color(const struct config_location_options *opts,
 		      const char *var, const char *def_color)
 {
 	struct get_color_config_data data = {
 		.get_color_slot = var,
 		.parsed_color[0] = '\0',
 	};
+	int ret;
 
 	config_with_options(git_get_color_config, &data,
 			    &opts->source, the_repository,
 			    &opts->options);
 
 	if (!data.get_color_found && def_color) {
-		if (color_parse(def_color, data.parsed_color) < 0)
-			die(_("unable to parse default color value"));
+		if (color_parse(def_color, data.parsed_color) < 0) {
+			ret = error(_("unable to parse default color value"));
+			goto out;
+		}
 	}
 
+	ret = 0;
+
+out:
 	fputs(data.parsed_color, stdout);
+	return ret;
 }
 
 struct get_colorbool_config_data {
@@ -1390,7 +1397,7 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
 	}
 	else if (actions == ACTION_GET_COLOR) {
 		check_argc(argc, 1, 2);
-		get_color(&location_opts, argv[0], argv[1]);
+		ret = get_color(&location_opts, argv[0], argv[1]);
 	}
 	else if (actions == ACTION_GET_COLORBOOL) {
 		check_argc(argc, 1, 2);
-- 
2.51.0.450.g87641ccf93.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 15 of 36 in “builtin/config: bug fixes for "get" subcommand with "--type=color"”
  1. 0/5 builtin/config: bug fixes for "get" subcommand with "--type=color"Patrick Steinhardt, Sep 11, 2025
  2. 1/5 t1300: write test expectations in the test's bodyPatrick Steinhardt, Sep 11, 2025
  3. Kristoffer HaugsbakkSep 11, 2025
  4. Patrick SteinhardtSep 15, 2025
  5. 2/5 t1300: small style fixupsPatrick Steinhardt, Sep 11, 2025
  6. 3/5 builtin/config: do not die in `get_color()`Patrick Steinhardt, Sep 11, 2025
  7. 4/5 builtin/config: special-case retrieving colors without a keyPatrick Steinhardt, Sep 11, 2025
  8. Kristoffer HaugsbakkSep 11, 2025
  9. Patrick SteinhardtSep 15, 2025
  10. 5/5 builtin/config: do not spawn pager when printing color codesPatrick Steinhardt, Sep 11, 2025
  11. Kristoffer HaugsbakkSep 11, 2025
  12. 0/5 builtin/config: bug fixes for "get" subcommand with "--type=color"Patrick Steinhardt, Sep 15, 2025
  13. 1/5 t1300: write test expectations in the test's bodyPatrick Steinhardt, Sep 15, 2025
  14. 2/5 t1300: small style fixupsPatrick Steinhardt, Sep 15, 2025
  15. 3/5 builtin/config: do not die in `get_color()`Patrick Steinhardt, Sep 15, 2025
  16. 4/5 builtin/config: special-case retrieving colors without a keyPatrick Steinhardt, Sep 15, 2025
  17. Junio C HamanoSep 15, 2025
  18. 5/5 builtin/config: do not spawn pager when printing color codesPatrick Steinhardt, Sep 15, 2025
  19. Junio C HamanoSep 15, 2025
  20. Kristoffer HaugsbakkSep 16, 2025
  21. Patrick SteinhardtSep 18, 2025
  22. 0/5 builtin/config: bug fixes for "get" subcommand with "--type=color"Patrick Steinhardt, Sep 18, 2025
  23. 1/5 t1300: write test expectations in the test's bodyPatrick Steinhardt, Sep 18, 2025
  24. 2/5 t1300: small style fixupsPatrick Steinhardt, Sep 18, 2025
  25. 3/5 builtin/config: do not die in `get_color()`Patrick Steinhardt, Sep 18, 2025
  26. 4/5 builtin/config: special-case retrieving colors without a keyPatrick Steinhardt, Sep 18, 2025
  27. Junio C HamanoSep 18, 2025
  28. Patrick SteinhardtSep 22, 2025
  29. Junio C HamanoSep 22, 2025
  30. 5/5 builtin/config: do not spawn pager when printing color codesPatrick Steinhardt, Sep 18, 2025
  31. 0/5 builtin/config: bug fixes for "get" subcommand with "--type=color"Patrick Steinhardt, Sep 22, 2025
  32. 1/5 t1300: write test expectations in the test's bodyPatrick Steinhardt, Sep 22, 2025
  33. 2/5 t1300: small style fixupsPatrick Steinhardt, Sep 22, 2025
  34. 3/5 builtin/config: do not die in `get_color()`Patrick Steinhardt, Sep 22, 2025
  35. 4/5 builtin/config: special-case retrieving colors without a keyPatrick Steinhardt, Sep 22, 2025
  36. 5/5 builtin/config: do not spawn pager when printing color codesPatrick Steinhardt, Sep 22, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.