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

[PATCH 08/21] builtin/config: refactor functions to have common exit paths

From
Patrick Steinhardt <ps@pks.im>
Date
May 10, 2024, 11:25 UTC
Message-ID
<6bff0410e99d59de8cc302f6b788af851b5e005a.1715339393.git.ps@pks.im>
In-Reply-To
<cover.1715339393.git.ps@pks.im>

Refactor functions to have a single exit path. This will make it easier in subsequent commits to add common cleanup code.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 builtin/config.c | 64 ++++++++++++++++++++++++++++--------------------
 1 file changed, 38 insertions(+), 26 deletions(-)
diff --git a/builtin/config.c b/builtin/config.c
index 9295a2f737..9b90eda328 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -798,6 +798,7 @@ static int cmd_config_get(int argc, const char **argv, const char *prefix)
 		OPT_STRING(0, "default", &default_value, N_("value"), N_("use default value when missing entry")),
 		OPT_END(),
 	};
+	int ret;
 
 	argc = parse_options(argc, argv, prefix, opts, builtin_config_get_usage,
 			     PARSE_OPT_STOP_AT_NON_OPTION);
@@ -816,8 +817,11 @@ static int cmd_config_get(int argc, const char **argv, const char *prefix)
 	setup_auto_pager("config", 1);
 
 	if (url)
-		return get_urlmatch(argv[0], url);
-	return get_value(argv[0], value_pattern, flags);
+		ret = get_urlmatch(argv[0], url);
+	else
+		ret = get_value(argv[0], value_pattern, flags);
+
+	return ret;
 }
 
 static int cmd_config_set(int argc, const char **argv, const char *prefix)
@@ -888,6 +892,7 @@ static int cmd_config_unset(int argc, const char **argv, const char *prefix)
 		OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE),
 		OPT_END(),
 	};
+	int ret;
 
 	argc = parse_options(argc, argv, prefix, opts, builtin_config_unset_usage,
 			     PARSE_OPT_STOP_AT_NON_OPTION);
@@ -900,12 +905,14 @@ static int cmd_config_unset(int argc, const char **argv, const char *prefix)
 	check_write();
 
 	if ((flags & CONFIG_FLAGS_MULTI_REPLACE) || value_pattern)
-		return git_config_set_multivar_in_file_gently(given_config_source.file,
-							      argv[0], NULL, value_pattern,
-							      NULL, flags);
+		ret = git_config_set_multivar_in_file_gently(given_config_source.file,
+							     argv[0], NULL, value_pattern,
+							     NULL, flags);
 	else
-		return git_config_set_in_file_gently(given_config_source.file, argv[0],
-						     NULL, NULL);
+		ret = git_config_set_in_file_gently(given_config_source.file, argv[0],
+						    NULL, NULL);
+
+	return ret;
 }
 
 static int cmd_config_rename_section(int argc, const char **argv, const char *prefix)
@@ -926,11 +933,13 @@ static int cmd_config_rename_section(int argc, const char **argv, const char *pr
 	ret = git_config_rename_section_in_file(given_config_source.file,
 						argv[0], argv[1]);
 	if (ret < 0)
-		return ret;
+		goto out;
 	else if (!ret)
 		die(_("no such section: %s"), argv[0]);
+	ret = 0;
 
-	return 0;
+out:
+	return ret;
 }
 
 static int cmd_config_remove_section(int argc, const char **argv, const char *prefix)
@@ -951,11 +960,13 @@ static int cmd_config_remove_section(int argc, const char **argv, const char *pr
 	ret = git_config_rename_section_in_file(given_config_source.file,
 						argv[0], NULL);
 	if (ret < 0)
-		return ret;
+		goto out;
 	else if (!ret)
 		die(_("no such section: %s"), argv[0]);
+	ret = 0;
 
-	return 0;
+out:
+	return ret;
 }
 
 static int show_editor(void)
@@ -1195,41 +1206,41 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
 	}
 	else if (actions == ACTION_GET) {
 		check_argc(argc, 1, 2);
-		return get_value(argv[0], argv[1], flags);
+		ret = get_value(argv[0], argv[1], flags);
 	}
 	else if (actions == ACTION_GET_ALL) {
 		do_all = 1;
 		check_argc(argc, 1, 2);
-		return get_value(argv[0], argv[1], flags);
+		ret = get_value(argv[0], argv[1], flags);
 	}
 	else if (actions == ACTION_GET_REGEXP) {
 		show_keys = 1;
 		use_key_regexp = 1;
 		do_all = 1;
 		check_argc(argc, 1, 2);
-		return get_value(argv[0], argv[1], flags);
+		ret = get_value(argv[0], argv[1], flags);
 	}
 	else if (actions == ACTION_GET_URLMATCH) {
 		check_argc(argc, 2, 2);
-		return get_urlmatch(argv[0], argv[1]);
+		ret = get_urlmatch(argv[0], argv[1]);
 	}
 	else if (actions == ACTION_UNSET) {
 		check_write();
 		check_argc(argc, 1, 2);
 		if (argc == 2)
-			return git_config_set_multivar_in_file_gently(given_config_source.file,
-								      argv[0], NULL, argv[1],
-								      NULL, flags);
+			ret = git_config_set_multivar_in_file_gently(given_config_source.file,
+								     argv[0], NULL, argv[1],
+								     NULL, flags);
 		else
-			return git_config_set_in_file_gently(given_config_source.file,
-							     argv[0], NULL, NULL);
+			ret = git_config_set_in_file_gently(given_config_source.file,
+							    argv[0], NULL, NULL);
 	}
 	else if (actions == ACTION_UNSET_ALL) {
 		check_write();
 		check_argc(argc, 1, 2);
-		return git_config_set_multivar_in_file_gently(given_config_source.file,
-							      argv[0], NULL, argv[1],
-							      NULL, flags | CONFIG_FLAGS_MULTI_REPLACE);
+		ret = git_config_set_multivar_in_file_gently(given_config_source.file,
+							     argv[0], NULL, argv[1],
+							     NULL, flags | CONFIG_FLAGS_MULTI_REPLACE);
 	}
 	else if (actions == ACTION_RENAME_SECTION) {
 		check_write();
@@ -1237,7 +1248,7 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
 		ret = git_config_rename_section_in_file(given_config_source.file,
 							argv[0], argv[1]);
 		if (ret < 0)
-			return ret;
+			goto out;
 		else if (!ret)
 			die(_("no such section: %s"), argv[0]);
 		else
@@ -1249,7 +1260,7 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
 		ret = git_config_rename_section_in_file(given_config_source.file,
 							argv[0], NULL);
 		if (ret < 0)
-			return ret;
+			goto out;
 		else if (!ret)
 			die(_("no such section: %s"), argv[0]);
 		else
@@ -1263,9 +1274,10 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
 		check_argc(argc, 1, 2);
 		if (argc == 2)
 			color_stdout_is_tty = git_config_bool("command line", argv[1]);
-		return get_colorbool(argv[0], argc == 2);
+		ret = get_colorbool(argv[0], argc == 2);
 	}
 
+out:
 	free(comment);
 	free(value);
 	return ret;
-- 
2.45.GIT
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 13 of 81 in “builtin/config: remove global state”
  1. 00/21 builtin/config: remove global statePatrick Steinhardt, May 10, 2024
  2. 01/21 builtin/config: stop printing full usage on misusePatrick Steinhardt, May 10, 2024
  3. 02/21 builtin/config: move legacy mode into its own functionPatrick Steinhardt, May 10, 2024
  4. 03/21 builtin/config: move subcommand options into `cmd_config()`Patrick Steinhardt, May 10, 2024
  5. 04/21 builtin/config: move legacy options into `cmd_config()`Patrick Steinhardt, May 10, 2024
  6. 05/21 builtin/config: move actions into `cmd_config_actions()`Patrick Steinhardt, May 10, 2024
  7. Kyle LippincottMay 10, 2024
  8. Patrick SteinhardtMay 13, 2024
  9. 06/21 builtin/config: check for writeability after source is set upPatrick Steinhardt, May 10, 2024
  10. Kyle LippincottMay 10, 2024
  11. Patrick SteinhardtMay 13, 2024
  12. 07/21 config: make the config source constPatrick Steinhardt, May 10, 2024
  13. 08/21 builtin/config: refactor functions to have common exit pathsPatrick Steinhardt, May 10, 2024
  14. 09/21 builtin/config: move location options into local variablesPatrick Steinhardt, May 10, 2024
  15. Patrick SteinhardtMay 10, 2024
  16. 10/21 builtin/config: move display options into local variablesPatrick Steinhardt, May 10, 2024
  17. 11/21 builtin/config: move type options into display optionsPatrick Steinhardt, May 10, 2024
  18. 12/21 builtin/config: move default value into display optionsPatrick Steinhardt, May 10, 2024
  19. 13/21 builtin/config: move `respect_includes_opt` into location optionsPatrick Steinhardt, May 10, 2024
  20. 14/21 builtin/config: convert `do_not_match` to a local variablePatrick Steinhardt, May 10, 2024
  21. Eric SunshineMay 11, 2024
  22. 15/21 builtin/config: convert `value_pattern` to a local variablePatrick Steinhardt, May 10, 2024
  23. 16/21 builtin/config: convert `regexp` to a local variablePatrick Steinhardt, May 10, 2024
  24. 17/21 builtin/config: convert `key_regexp` to a local variablePatrick Steinhardt, May 10, 2024
  25. 18/21 builtin/config: convert `key` to a local variablePatrick Steinhardt, May 10, 2024
  26. 19/21 builtin/config: track "fixed value" option via flags onlyPatrick Steinhardt, May 10, 2024
  27. Eric SunshineMay 11, 2024
  28. 20/21 builtin/config: convert flags to a local variablePatrick Steinhardt, May 10, 2024
  29. 21/21 builtin/config: pass data between callbacks via local variablesPatrick Steinhardt, May 10, 2024
  30. Junio C HamanoMay 10, 2024
  31. Kyle LippincottMay 10, 2024
  32. 00/21 builtin/config: remove global statePatrick Steinhardt, May 13, 2024
  33. 01/21 builtin/config: stop printing full usage on misusePatrick Steinhardt, May 13, 2024
  34. 02/21 builtin/config: move legacy mode into its own functionPatrick Steinhardt, May 13, 2024
  35. 03/21 builtin/config: move subcommand options into `cmd_config()`Patrick Steinhardt, May 13, 2024
  36. 04/21 builtin/config: move legacy options into `cmd_config()`Patrick Steinhardt, May 13, 2024
  37. 05/21 builtin/config: move actions into `cmd_config_actions()`Patrick Steinhardt, May 13, 2024
  38. 06/21 builtin/config: check for writeability after source is set upPatrick Steinhardt, May 13, 2024
  39. Taylor BlauMay 14, 2024
  40. Patrick SteinhardtMay 15, 2024
  41. 07/21 config: make the config source constPatrick Steinhardt, May 13, 2024
  42. 08/21 builtin/config: refactor functions to have common exit pathsPatrick Steinhardt, May 13, 2024
  43. 09/21 builtin/config: move location options into local variablesPatrick Steinhardt, May 13, 2024
  44. 10/21 builtin/config: move display options into local variablesPatrick Steinhardt, May 13, 2024
  45. 11/21 builtin/config: move type options into display optionsPatrick Steinhardt, May 13, 2024
  46. 12/21 builtin/config: move default value into display optionsPatrick Steinhardt, May 13, 2024
  47. 13/21 builtin/config: move `respect_includes_opt` into location optionsPatrick Steinhardt, May 13, 2024
  48. 14/21 builtin/config: convert `do_not_match` to a local variablePatrick Steinhardt, May 13, 2024
  49. 15/21 builtin/config: convert `value_pattern` to a local variablePatrick Steinhardt, May 13, 2024
  50. 16/21 builtin/config: convert `regexp` to a local variablePatrick Steinhardt, May 13, 2024
  51. 17/21 builtin/config: convert `key_regexp` to a local variablePatrick Steinhardt, May 13, 2024
  52. 18/21 builtin/config: convert `key` to a local variablePatrick Steinhardt, May 13, 2024
  53. 19/21 builtin/config: track "fixed value" option via flags onlyPatrick Steinhardt, May 13, 2024
  54. 20/21 builtin/config: convert flags to a local variablePatrick Steinhardt, May 13, 2024
  55. 21/21 builtin/config: pass data between callbacks via local variablesPatrick Steinhardt, May 13, 2024
  56. Kyle LippincottMay 13, 2024
  57. Junio C HamanoMay 14, 2024
  58. Patrick SteinhardtMay 14, 2024
  59. Patrick SteinhardtMay 15, 2024
  60. 00/21 builtin/config: remove global statePatrick Steinhardt, May 15, 2024
  61. 01/21 builtin/config: stop printing full usage on misusePatrick Steinhardt, May 15, 2024
  62. 02/21 builtin/config: move legacy mode into its own functionPatrick Steinhardt, May 15, 2024
  63. 03/21 builtin/config: move subcommand options into `cmd_config()`Patrick Steinhardt, May 15, 2024
  64. 04/21 builtin/config: move legacy options into `cmd_config()`Patrick Steinhardt, May 15, 2024
  65. 05/21 builtin/config: move actions into `cmd_config_actions()`Patrick Steinhardt, May 15, 2024
  66. 06/21 builtin/config: check for writeability after source is set upPatrick Steinhardt, May 15, 2024
  67. 07/21 config: make the config source constPatrick Steinhardt, May 15, 2024
  68. 08/21 builtin/config: refactor functions to have common exit pathsPatrick Steinhardt, May 15, 2024
  69. 09/21 builtin/config: move location options into local variablesPatrick Steinhardt, May 15, 2024
  70. 10/21 builtin/config: move display options into local variablesPatrick Steinhardt, May 15, 2024
  71. 11/21 builtin/config: move type options into display optionsPatrick Steinhardt, May 15, 2024
  72. 12/21 builtin/config: move default value into display optionsPatrick Steinhardt, May 15, 2024
  73. 13/21 builtin/config: move `respect_includes_opt` into location optionsPatrick Steinhardt, May 15, 2024
  74. 14/21 builtin/config: convert `do_not_match` to a local variablePatrick Steinhardt, May 15, 2024
  75. 15/21 builtin/config: convert `value_pattern` to a local variablePatrick Steinhardt, May 15, 2024
  76. 16/21 builtin/config: convert `regexp` to a local variablePatrick Steinhardt, May 15, 2024
  77. 17/21 builtin/config: convert `key_regexp` to a local variablePatrick Steinhardt, May 15, 2024
  78. 18/21 builtin/config: convert `key` to a local variablePatrick Steinhardt, May 15, 2024
  79. 19/21 builtin/config: track "fixed value" option via flags onlyPatrick Steinhardt, May 15, 2024
  80. 20/21 builtin/config: convert flags to a local variablePatrick Steinhardt, May 15, 2024
  81. 21/21 builtin/config: pass data between callbacks via local variablesPatrick Steinhardt, May 15, 2024

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.