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

[PATCH 11/21] builtin/config: move type options into display options

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

The type options are tracked via a global variable. Move it into the display options instead.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 builtin/config.c | 60 +++++++++++++++++++++++-------------------------
 1 file changed, 29 insertions(+), 31 deletions(-)
diff --git a/builtin/config.c b/builtin/config.c
index 7089692c4b..1f864f71c4 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -81,13 +81,24 @@ struct config_location_options {
 };
 #define CONFIG_LOCATION_OPTIONS_INIT {0}
 
+#define CONFIG_TYPE_OPTIONS(type) \
+	OPT_GROUP(N_("Type")), \
+	OPT_CALLBACK('t', "type", &type, N_("type"), N_("value is given this type"), option_parse_type), \
+	OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL), \
+	OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT), \
+	OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT), \
+	OPT_CALLBACK_VALUE(0, "bool-or-str", &type, N_("value is --bool or string"), TYPE_BOOL_OR_STR), \
+	OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH), \
+	OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE)
+
 #define CONFIG_DISPLAY_OPTIONS(opts) \
 	OPT_GROUP(N_("Display options")), \
 	OPT_BOOL('z', "null", &opts.end_nul, N_("terminate values with NUL byte")), \
 	OPT_BOOL(0, "name-only", &opts.omit_values, N_("show variable names only")), \
 	OPT_BOOL(0, "show-origin", &opts.show_origin, N_("show origin of config (file, standard input, blob, command line)")), \
 	OPT_BOOL(0, "show-scope", &opts.show_scope, N_("show scope of config (worktree, local, global, system, command)")), \
-	OPT_BOOL(0, "show-names", &opts.show_keys, N_("show config keys in addition to their values"))
+	OPT_BOOL(0, "show-names", &opts.show_keys, N_("show config keys in addition to their values")), \
+	CONFIG_TYPE_OPTIONS(opts.type)
 
 struct config_display_options {
 	int end_nul;
@@ -95,6 +106,7 @@ struct config_display_options {
 	int show_origin;
 	int show_scope;
 	int show_keys;
+	int type;
 	/* Populated via `display_options_init()`. */
 	int term;
 	int delim;
@@ -113,8 +125,6 @@ static regex_t *regexp;
 static int use_key_regexp;
 static int do_all;
 static int do_not_match;
-
-static int type;
 static char *default_value;
 static int respect_includes_opt = -1;
 static int fixed_value;
@@ -264,13 +274,13 @@ static int format_config(const struct config_display_options *opts,
 		if (opts->show_keys)
 			strbuf_addch(buf, opts->key_delim);
 
-		if (type == TYPE_INT)
+		if (opts->type == TYPE_INT)
 			strbuf_addf(buf, "%"PRId64,
 				    git_config_int64(key_, value_ ? value_ : "", kvi));
-		else if (type == TYPE_BOOL)
+		else if (opts->type == TYPE_BOOL)
 			strbuf_addstr(buf, git_config_bool(key_, value_) ?
 				      "true" : "false");
-		else if (type == TYPE_BOOL_OR_INT) {
+		else if (opts->type == TYPE_BOOL_OR_INT) {
 			int is_bool, v;
 			v = git_config_bool_or_int(key_, value_, kvi,
 						   &is_bool);
@@ -278,24 +288,24 @@ static int format_config(const struct config_display_options *opts,
 				strbuf_addstr(buf, v ? "true" : "false");
 			else
 				strbuf_addf(buf, "%d", v);
-		} else if (type == TYPE_BOOL_OR_STR) {
+		} else if (opts->type == TYPE_BOOL_OR_STR) {
 			int v = git_parse_maybe_bool(value_);
 			if (v < 0)
 				strbuf_addstr(buf, value_);
 			else
 				strbuf_addstr(buf, v ? "true" : "false");
-		} else if (type == TYPE_PATH) {
+		} else if (opts->type == TYPE_PATH) {
 			const char *v;
 			if (git_config_pathname(&v, key_, value_) < 0)
 				return -1;
 			strbuf_addstr(buf, v);
 			free((char *)v);
-		} else if (type == TYPE_EXPIRY_DATE) {
+		} else if (opts->type == TYPE_EXPIRY_DATE) {
 			timestamp_t t;
 			if (git_config_expiry_date(&t, key_, value_) < 0)
 				return -1;
 			strbuf_addf(buf, "%"PRItime, t);
-		} else if (type == TYPE_COLOR) {
+		} else if (opts->type == TYPE_COLOR) {
 			char v[COLOR_MAXLEN];
 			if (git_config_color(v, key_, value_) < 0)
 				return -1;
@@ -443,7 +453,7 @@ static int get_value(const struct config_location_options *opts,
 }
 
 static char *normalize_value(const char *key, const char *value,
-			     struct key_value_info *kvi)
+			     int type, struct key_value_info *kvi)
 {
 	if (!value)
 		return NULL;
@@ -787,16 +797,6 @@ static void display_options_init(struct config_display_options *opts)
 	}
 }
 
-#define CONFIG_TYPE_OPTIONS \
-	OPT_GROUP(N_("Type")), \
-	OPT_CALLBACK('t', "type", &type, N_("type"), N_("value is given this type"), option_parse_type), \
-	OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL), \
-	OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT), \
-	OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT), \
-	OPT_CALLBACK_VALUE(0, "bool-or-str", &type, N_("value is --bool or string"), TYPE_BOOL_OR_STR), \
-	OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH), \
-	OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE)
-
 static int cmd_config_list(int argc, const char **argv, const char *prefix)
 {
 	struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
@@ -839,7 +839,6 @@ static int cmd_config_get(int argc, const char **argv, const char *prefix)
 	int flags = 0;
 	struct option opts[] = {
 		CONFIG_LOCATION_OPTIONS(location_opts),
-		CONFIG_TYPE_OPTIONS,
 		OPT_GROUP(N_("Filter options")),
 		OPT_BOOL(0, "all", &do_all, N_("return all values for multi-valued config options")),
 		OPT_BOOL(0, "regexp", &use_key_regexp, N_("interpret the name as a regular expression")),
@@ -884,10 +883,10 @@ static int cmd_config_set(int argc, const char **argv, const char *prefix)
 	struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
 	const char *value_pattern = NULL, *comment_arg = NULL;
 	char *comment = NULL;
-	int flags = 0, append = 0;
+	int flags = 0, append = 0, type = 0;
 	struct option opts[] = {
 		CONFIG_LOCATION_OPTIONS(location_opts),
-		CONFIG_TYPE_OPTIONS,
+		CONFIG_TYPE_OPTIONS(type),
 		OPT_GROUP(N_("Filter")),
 		OPT_BIT(0, "all", &flags, N_("replace multi-valued config option with new value"), CONFIG_FLAGS_MULTI_REPLACE),
 		OPT_STRING(0, "value", &value_pattern, N_("pattern"), N_("show config with values matching the pattern")),
@@ -917,7 +916,7 @@ static int cmd_config_set(int argc, const char **argv, const char *prefix)
 	location_options_init(&location_opts, prefix);
 	check_write(&location_opts.source);
 
-	value = normalize_value(argv[0], argv[1], &default_kvi);
+	value = normalize_value(argv[0], argv[1], type, &default_kvi);
 
 	if ((flags & CONFIG_FLAGS_MULTI_REPLACE) || value_pattern) {
 		ret = git_config_set_multivar_in_file_gently(location_opts.source.file,
@@ -1124,7 +1123,6 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
 		OPT_CMDMODE('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
 		OPT_CMDMODE(0, "get-color", &actions, N_("find the color configured: slot [<default>]"), ACTION_GET_COLOR),
 		OPT_CMDMODE(0, "get-colorbool", &actions, N_("find the color setting: slot [<stdout-is-tty>]"), ACTION_GET_COLORBOOL),
-		CONFIG_TYPE_OPTIONS,
 		CONFIG_DISPLAY_OPTIONS(display_opts),
 		OPT_GROUP(N_("Other")),
 		OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")),
@@ -1145,7 +1143,7 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
 	location_options_init(&location_opts, prefix);
 	display_options_init(&display_opts);
 
-	if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && type) {
+	if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && display_opts.type) {
 		error(_("--get-color and variable type are incoherent"));
 		exit(129);
 	}
@@ -1242,7 +1240,7 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
 	else if (actions == ACTION_SET) {
 		check_write(&location_opts.source);
 		check_argc(argc, 2, 2);
-		value = normalize_value(argv[0], argv[1], &default_kvi);
+		value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi);
 		ret = git_config_set_in_file_gently(location_opts.source.file, argv[0], comment, value);
 		if (ret == CONFIG_NOTHING_SET)
 			error(_("cannot overwrite multiple values with a single value\n"
@@ -1251,7 +1249,7 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
 	else if (actions == ACTION_SET_ALL) {
 		check_write(&location_opts.source);
 		check_argc(argc, 2, 3);
-		value = normalize_value(argv[0], argv[1], &default_kvi);
+		value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi);
 		ret = git_config_set_multivar_in_file_gently(location_opts.source.file,
 							     argv[0], value, argv[2],
 							     comment, flags);
@@ -1259,7 +1257,7 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
 	else if (actions == ACTION_ADD) {
 		check_write(&location_opts.source);
 		check_argc(argc, 2, 2);
-		value = normalize_value(argv[0], argv[1], &default_kvi);
+		value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi);
 		ret = git_config_set_multivar_in_file_gently(location_opts.source.file,
 							     argv[0], value,
 							     CONFIG_REGEX_NONE,
@@ -1268,7 +1266,7 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
 	else if (actions == ACTION_REPLACE_ALL) {
 		check_write(&location_opts.source);
 		check_argc(argc, 2, 3);
-		value = normalize_value(argv[0], argv[1], &default_kvi);
+		value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi);
 		ret = git_config_set_multivar_in_file_gently(location_opts.source.file,
 							     argv[0], value, argv[2],
 							     comment, flags | CONFIG_FLAGS_MULTI_REPLACE);
-- 
2.45.GIT
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 17 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.