Skip to content

Commit 94c4693

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c0c1e26 commit 94c4693

File tree

1 file changed

+29
-31
lines changed

1 file changed

+29
-31
lines changed

builtin/config.c

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,32 @@ struct config_location_options {
8282
};
8383
#define CONFIG_LOCATION_OPTIONS_INIT {0}
8484

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+
8595
#define CONFIG_DISPLAY_OPTIONS(opts) \
8696
OPT_GROUP(N_("Display options")), \
8797
OPT_BOOL('z', "null", &opts.end_nul, N_("terminate values with NUL byte")), \
8898
OPT_BOOL(0, "name-only", &opts.omit_values, N_("show variable names only")), \
8999
OPT_BOOL(0, "show-origin", &opts.show_origin, N_("show origin of config (file, standard input, blob, command line)")), \
90100
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)
92103

93104
struct config_display_options {
94105
int end_nul;
95106
int omit_values;
96107
int show_origin;
97108
int show_scope;
98109
int show_keys;
110+
int type;
99111
/* Populated via `display_options_init()`. */
100112
int term;
101113
int delim;
@@ -114,8 +126,6 @@ static regex_t *regexp;
114126
static int use_key_regexp;
115127
static int do_all;
116128
static int do_not_match;
117-
118-
static int type;
119129
static char *default_value;
120130
static int respect_includes_opt = -1;
121131
static int fixed_value;
@@ -265,38 +275,38 @@ static int format_config(const struct config_display_options *opts,
265275
if (opts->show_keys)
266276
strbuf_addch(buf, opts->key_delim);
267277

268-
if (type == TYPE_INT)
278+
if (opts->type == TYPE_INT)
269279
strbuf_addf(buf, "%"PRId64,
270280
git_config_int64(key_, value_ ? value_ : "", kvi));
271-
else if (type == TYPE_BOOL)
281+
else if (opts->type == TYPE_BOOL)
272282
strbuf_addstr(buf, git_config_bool(key_, value_) ?
273283
"true" : "false");
274-
else if (type == TYPE_BOOL_OR_INT) {
284+
else if (opts->type == TYPE_BOOL_OR_INT) {
275285
int is_bool, v;
276286
v = git_config_bool_or_int(key_, value_, kvi,
277287
&is_bool);
278288
if (is_bool)
279289
strbuf_addstr(buf, v ? "true" : "false");
280290
else
281291
strbuf_addf(buf, "%d", v);
282-
} else if (type == TYPE_BOOL_OR_STR) {
292+
} else if (opts->type == TYPE_BOOL_OR_STR) {
283293
int v = git_parse_maybe_bool(value_);
284294
if (v < 0)
285295
strbuf_addstr(buf, value_);
286296
else
287297
strbuf_addstr(buf, v ? "true" : "false");
288-
} else if (type == TYPE_PATH) {
298+
} else if (opts->type == TYPE_PATH) {
289299
const char *v;
290300
if (git_config_pathname(&v, key_, value_) < 0)
291301
return -1;
292302
strbuf_addstr(buf, v);
293303
free((char *)v);
294-
} else if (type == TYPE_EXPIRY_DATE) {
304+
} else if (opts->type == TYPE_EXPIRY_DATE) {
295305
timestamp_t t;
296306
if (git_config_expiry_date(&t, key_, value_) < 0)
297307
return -1;
298308
strbuf_addf(buf, "%"PRItime, t);
299-
} else if (type == TYPE_COLOR) {
309+
} else if (opts->type == TYPE_COLOR) {
300310
char v[COLOR_MAXLEN];
301311
if (git_config_color(v, key_, value_) < 0)
302312
return -1;
@@ -444,7 +454,7 @@ static int get_value(const struct config_location_options *opts,
444454
}
445455

446456
static char *normalize_value(const char *key, const char *value,
447-
struct key_value_info *kvi)
457+
int type, struct key_value_info *kvi)
448458
{
449459
if (!value)
450460
return NULL;
@@ -789,16 +799,6 @@ static void display_options_init(struct config_display_options *opts)
789799
}
790800
}
791801

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-
802802
static int cmd_config_list(int argc, const char **argv, const char *prefix)
803803
{
804804
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)
841841
int flags = 0;
842842
struct option opts[] = {
843843
CONFIG_LOCATION_OPTIONS(location_opts),
844-
CONFIG_TYPE_OPTIONS,
845844
OPT_GROUP(N_("Filter options")),
846845
OPT_BOOL(0, "all", &do_all, N_("return all values for multi-valued config options")),
847846
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)
886885
struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
887886
const char *value_pattern = NULL, *comment_arg = NULL;
888887
char *comment = NULL;
889-
int flags = 0, append = 0;
888+
int flags = 0, append = 0, type = 0;
890889
struct option opts[] = {
891890
CONFIG_LOCATION_OPTIONS(location_opts),
892-
CONFIG_TYPE_OPTIONS,
891+
CONFIG_TYPE_OPTIONS(type),
893892
OPT_GROUP(N_("Filter")),
894893
OPT_BIT(0, "all", &flags, N_("replace multi-valued config option with new value"), CONFIG_FLAGS_MULTI_REPLACE),
895894
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)
919918
location_options_init(&location_opts, prefix);
920919
check_write(&location_opts.source);
921920

922-
value = normalize_value(argv[0], argv[1], &default_kvi);
921+
value = normalize_value(argv[0], argv[1], type, &default_kvi);
923922

924923
if ((flags & CONFIG_FLAGS_MULTI_REPLACE) || value_pattern) {
925924
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)
11261125
OPT_CMDMODE('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
11271126
OPT_CMDMODE(0, "get-color", &actions, N_("find the color configured: slot [<default>]"), ACTION_GET_COLOR),
11281127
OPT_CMDMODE(0, "get-colorbool", &actions, N_("find the color setting: slot [<stdout-is-tty>]"), ACTION_GET_COLORBOOL),
1129-
CONFIG_TYPE_OPTIONS,
11301128
CONFIG_DISPLAY_OPTIONS(display_opts),
11311129
OPT_GROUP(N_("Other")),
11321130
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)
11471145
location_options_init(&location_opts, prefix);
11481146
display_options_init(&display_opts);
11491147

1150-
if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && type) {
1148+
if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && display_opts.type) {
11511149
error(_("--get-color and variable type are incoherent"));
11521150
exit(129);
11531151
}
@@ -1248,7 +1246,7 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
12481246
else if (actions == ACTION_SET) {
12491247
check_write(&location_opts.source);
12501248
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);
12521250
ret = git_config_set_in_file_gently(location_opts.source.file, argv[0], comment, value);
12531251
if (ret == CONFIG_NOTHING_SET)
12541252
error(_("cannot overwrite multiple values with a single value\n"
@@ -1257,15 +1255,15 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
12571255
else if (actions == ACTION_SET_ALL) {
12581256
check_write(&location_opts.source);
12591257
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);
12611259
ret = git_config_set_multivar_in_file_gently(location_opts.source.file,
12621260
argv[0], value, argv[2],
12631261
comment, flags);
12641262
}
12651263
else if (actions == ACTION_ADD) {
12661264
check_write(&location_opts.source);
12671265
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);
12691267
ret = git_config_set_multivar_in_file_gently(location_opts.source.file,
12701268
argv[0], value,
12711269
CONFIG_REGEX_NONE,
@@ -1274,7 +1272,7 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
12741272
else if (actions == ACTION_REPLACE_ALL) {
12751273
check_write(&location_opts.source);
12761274
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);
12781276
ret = git_config_set_multivar_in_file_gently(location_opts.source.file,
12791277
argv[0], value, argv[2],
12801278
comment, flags | CONFIG_FLAGS_MULTI_REPLACE);

0 commit comments

Comments
 (0)