builtin/config: move type options into display options

The type options are tracked via a global variable. Move it into the display options instead. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed May 15, 2024 at 08:42 UTC 94c4693079efd107ddb941550f68c98601d26123
1 file changed +29 -31
builtin/config.c
+29 -31
@@ -82,13 +82,24 @@ struct config_location_options {
82 };
83 #define CONFIG_LOCATION_OPTIONS_INIT {0}
84
85 +#define CONFIG_TYPE_OPTIONS(type) \
86 + OPT_GROUP(N_("Type")), \
87 + OPT_CALLBACK('t', "type", &type, N_("type"), N_("value is given this type"), option_parse_type), \
88 + OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL), \
89 + OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT), \
90 + OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT), \
91 + OPT_CALLBACK_VALUE(0, "bool-or-str", &type, N_("value is --bool or string"), TYPE_BOOL_OR_STR), \
92 + OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH), \
93 + OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE)
94 +
95 #define CONFIG_DISPLAY_OPTIONS(opts) \
96 OPT_GROUP(N_("Display options")), \
97 OPT_BOOL('z', "null", &opts.end_nul, N_("terminate values with NUL byte")), \
98 OPT_BOOL(0, "name-only", &opts.omit_values, N_("show variable names only")), \
99 OPT_BOOL(0, "show-origin", &opts.show_origin, N_("show origin of config (file, standard input, blob, command line)")), \
100 OPT_BOOL(0, "show-scope", &opts.show_scope, N_("show scope of config (worktree, local, global, system, command)")), \
91 - OPT_BOOL(0, "show-names", &opts.show_keys, N_("show config keys in addition to their values"))
101 + OPT_BOOL(0, "show-names", &opts.show_keys, N_("show config keys in addition to their values")), \
102 + CONFIG_TYPE_OPTIONS(opts.type)
103
104 struct config_display_options {
105 int end_nul;
@@ -96,6 +107,7 @@ struct config_display_options {
107 int show_origin;
108 int show_scope;
109 int show_keys;
110 + int type;
111 /* Populated via `display_options_init()`. */
112 int term;
113 int delim;
@@ -114,8 +126,6 @@ static regex_t *regexp;
126 static int use_key_regexp;
127 static int do_all;
128 static int do_not_match;
117 -
118 -static int type;
129 static char *default_value;
130 static int respect_includes_opt = -1;
131 static int fixed_value;
@@ -265,13 +275,13 @@ static int format_config(const struct config_display_options *opts,
275 if (opts->show_keys)
276 strbuf_addch(buf, opts->key_delim);
277
268 - if (type == TYPE_INT)
278 + if (opts->type == TYPE_INT)
279 strbuf_addf(buf, "%"PRId64,
280 git_config_int64(key_, value_ ? value_ : "", kvi));
271 - else if (type == TYPE_BOOL)
281 + else if (opts->type == TYPE_BOOL)
282 strbuf_addstr(buf, git_config_bool(key_, value_) ?
283 "true" : "false");
274 - else if (type == TYPE_BOOL_OR_INT) {
284 + else if (opts->type == TYPE_BOOL_OR_INT) {
285 int is_bool, v;
286 v = git_config_bool_or_int(key_, value_, kvi,
287 &is_bool);
@@ -279,24 +289,24 @@ static int format_config(const struct config_display_options *opts,
289 strbuf_addstr(buf, v ? "true" : "false");
290 else
291 strbuf_addf(buf, "%d", v);
282 - } else if (type == TYPE_BOOL_OR_STR) {
292 + } else if (opts->type == TYPE_BOOL_OR_STR) {
293 int v = git_parse_maybe_bool(value_);
294 if (v < 0)
295 strbuf_addstr(buf, value_);
296 else
297 strbuf_addstr(buf, v ? "true" : "false");
288 - } else if (type == TYPE_PATH) {
298 + } else if (opts->type == TYPE_PATH) {
299 const char *v;
300 if (git_config_pathname(&v, key_, value_) < 0)
301 return -1;
302 strbuf_addstr(buf, v);
303 free((char *)v);
294 - } else if (type == TYPE_EXPIRY_DATE) {
304 + } else if (opts->type == TYPE_EXPIRY_DATE) {
305 timestamp_t t;
306 if (git_config_expiry_date(&t, key_, value_) < 0)
307 return -1;
308 strbuf_addf(buf, "%"PRItime, t);
299 - } else if (type == TYPE_COLOR) {
309 + } else if (opts->type == TYPE_COLOR) {
310 char v[COLOR_MAXLEN];
311 if (git_config_color(v, key_, value_) < 0)
312 return -1;
@@ -444,7 +454,7 @@ free_strings:
454 }
455
456 static char *normalize_value(const char *key, const char *value,
447 - struct key_value_info *kvi)
457 + int type, struct key_value_info *kvi)
458 {
459 if (!value)
460 return NULL;
@@ -789,16 +799,6 @@ static void display_options_init(struct config_display_options *opts)
799 }
800 }
801
792 -#define CONFIG_TYPE_OPTIONS \
793 - OPT_GROUP(N_("Type")), \
794 - OPT_CALLBACK('t', "type", &type, N_("type"), N_("value is given this type"), option_parse_type), \
795 - OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL), \
796 - OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT), \
797 - OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT), \
798 - OPT_CALLBACK_VALUE(0, "bool-or-str", &type, N_("value is --bool or string"), TYPE_BOOL_OR_STR), \
799 - OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH), \
800 - OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE)
801 -
802 static int cmd_config_list(int argc, const char **argv, const char *prefix)
803 {
804 struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
@@ -841,7 +841,6 @@ static int cmd_config_get(int argc, const char **argv, const char *prefix)
841 int flags = 0;
842 struct option opts[] = {
843 CONFIG_LOCATION_OPTIONS(location_opts),
844 - CONFIG_TYPE_OPTIONS,
844 OPT_GROUP(N_("Filter options")),
845 OPT_BOOL(0, "all", &do_all, N_("return all values for multi-valued config options")),
846 OPT_BOOL(0, "regexp", &use_key_regexp, N_("interpret the name as a regular expression")),
@@ -886,10 +885,10 @@ static int cmd_config_set(int argc, const char **argv, const char *prefix)
885 struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
886 const char *value_pattern = NULL, *comment_arg = NULL;
887 char *comment = NULL;
889 - int flags = 0, append = 0;
888 + int flags = 0, append = 0, type = 0;
889 struct option opts[] = {
890 CONFIG_LOCATION_OPTIONS(location_opts),
892 - CONFIG_TYPE_OPTIONS,
891 + CONFIG_TYPE_OPTIONS(type),
892 OPT_GROUP(N_("Filter")),
893 OPT_BIT(0, "all", &flags, N_("replace multi-valued config option with new value"), CONFIG_FLAGS_MULTI_REPLACE),
894 OPT_STRING(0, "value", &value_pattern, N_("pattern"), N_("show config with values matching the pattern")),
@@ -919,7 +918,7 @@ static int cmd_config_set(int argc, const char **argv, const char *prefix)
918 location_options_init(&location_opts, prefix);
919 check_write(&location_opts.source);
920
922 - value = normalize_value(argv[0], argv[1], &default_kvi);
921 + value = normalize_value(argv[0], argv[1], type, &default_kvi);
922
923 if ((flags & CONFIG_FLAGS_MULTI_REPLACE) || value_pattern) {
924 ret = git_config_set_multivar_in_file_gently(location_opts.source.file,
@@ -1126,7 +1125,6 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
1125 OPT_CMDMODE('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
1126 OPT_CMDMODE(0, "get-color", &actions, N_("find the color configured: slot [<default>]"), ACTION_GET_COLOR),
1127 OPT_CMDMODE(0, "get-colorbool", &actions, N_("find the color setting: slot [<stdout-is-tty>]"), ACTION_GET_COLORBOOL),
1129 - CONFIG_TYPE_OPTIONS,
1128 CONFIG_DISPLAY_OPTIONS(display_opts),
1129 OPT_GROUP(N_("Other")),
1130 OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")),
@@ -1147,7 +1145,7 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
1145 location_options_init(&location_opts, prefix);
1146 display_options_init(&display_opts);
1147
1150 - if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && type) {
1148 + if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && display_opts.type) {
1149 error(_("--get-color and variable type are incoherent"));
1150 exit(129);
1151 }
@@ -1248,7 +1246,7 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
1246 else if (actions == ACTION_SET) {
1247 check_write(&location_opts.source);
1248 check_argc(argc, 2, 2);
1251 - value = normalize_value(argv[0], argv[1], &default_kvi);
1249 + value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi);
1250 ret = git_config_set_in_file_gently(location_opts.source.file, argv[0], comment, value);
1251 if (ret == CONFIG_NOTHING_SET)
1252 error(_("cannot overwrite multiple values with a single value\n"
@@ -1257,7 +1255,7 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
1255 else if (actions == ACTION_SET_ALL) {
1256 check_write(&location_opts.source);
1257 check_argc(argc, 2, 3);
1260 - value = normalize_value(argv[0], argv[1], &default_kvi);
1258 + value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi);
1259 ret = git_config_set_multivar_in_file_gently(location_opts.source.file,
1260 argv[0], value, argv[2],
1261 comment, flags);
@@ -1265,7 +1263,7 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
1263 else if (actions == ACTION_ADD) {
1264 check_write(&location_opts.source);
1265 check_argc(argc, 2, 2);
1268 - value = normalize_value(argv[0], argv[1], &default_kvi);
1266 + value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi);
1267 ret = git_config_set_multivar_in_file_gently(location_opts.source.file,
1268 argv[0], value,
1269 CONFIG_REGEX_NONE,
@@ -1274,7 +1272,7 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
1272 else if (actions == ACTION_REPLACE_ALL) {
1273 check_write(&location_opts.source);
1274 check_argc(argc, 2, 3);
1277 - value = normalize_value(argv[0], argv[1], &default_kvi);
1275 + value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi);
1276 ret = git_config_set_multivar_in_file_gently(location_opts.source.file,
1277 argv[0], value, argv[2],
1278 comment, flags | CONFIG_FLAGS_MULTI_REPLACE);