@@ -82,20 +82,32 @@ struct config_location_options {
82
82
};
83
83
#define CONFIG_LOCATION_OPTIONS_INIT {0}
84
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
+
85
95
#define CONFIG_DISPLAY_OPTIONS (opts ) \
86
96
OPT_GROUP(N_("Display options")), \
87
97
OPT_BOOL('z', "null", &opts.end_nul, N_("terminate values with NUL byte")), \
88
98
OPT_BOOL(0, "name-only", &opts.omit_values, N_("show variable names only")), \
89
99
OPT_BOOL(0, "show-origin", &opts.show_origin, N_("show origin of config (file, standard input, blob, command line)")), \
90
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)
92
103
93
104
struct config_display_options {
94
105
int end_nul ;
95
106
int omit_values ;
96
107
int show_origin ;
97
108
int show_scope ;
98
109
int show_keys ;
110
+ int type ;
99
111
/* Populated via `display_options_init()`. */
100
112
int term ;
101
113
int delim ;
@@ -114,8 +126,6 @@ static regex_t *regexp;
114
126
static int use_key_regexp ;
115
127
static int do_all ;
116
128
static int do_not_match ;
117
-
118
- static int type ;
119
129
static char * default_value ;
120
130
static int respect_includes_opt = -1 ;
121
131
static int fixed_value ;
@@ -265,38 +275,38 @@ static int format_config(const struct config_display_options *opts,
265
275
if (opts -> show_keys )
266
276
strbuf_addch (buf , opts -> key_delim );
267
277
268
- if (type == TYPE_INT )
278
+ if (opts -> type == TYPE_INT )
269
279
strbuf_addf (buf , "%" PRId64 ,
270
280
git_config_int64 (key_ , value_ ? value_ : "" , kvi ));
271
- else if (type == TYPE_BOOL )
281
+ else if (opts -> type == TYPE_BOOL )
272
282
strbuf_addstr (buf , git_config_bool (key_ , value_ ) ?
273
283
"true" : "false" );
274
- else if (type == TYPE_BOOL_OR_INT ) {
284
+ else if (opts -> type == TYPE_BOOL_OR_INT ) {
275
285
int is_bool , v ;
276
286
v = git_config_bool_or_int (key_ , value_ , kvi ,
277
287
& is_bool );
278
288
if (is_bool )
279
289
strbuf_addstr (buf , v ? "true" : "false" );
280
290
else
281
291
strbuf_addf (buf , "%d" , v );
282
- } else if (type == TYPE_BOOL_OR_STR ) {
292
+ } else if (opts -> type == TYPE_BOOL_OR_STR ) {
283
293
int v = git_parse_maybe_bool (value_ );
284
294
if (v < 0 )
285
295
strbuf_addstr (buf , value_ );
286
296
else
287
297
strbuf_addstr (buf , v ? "true" : "false" );
288
- } else if (type == TYPE_PATH ) {
298
+ } else if (opts -> type == TYPE_PATH ) {
289
299
const char * v ;
290
300
if (git_config_pathname (& v , key_ , value_ ) < 0 )
291
301
return -1 ;
292
302
strbuf_addstr (buf , v );
293
303
free ((char * )v );
294
- } else if (type == TYPE_EXPIRY_DATE ) {
304
+ } else if (opts -> type == TYPE_EXPIRY_DATE ) {
295
305
timestamp_t t ;
296
306
if (git_config_expiry_date (& t , key_ , value_ ) < 0 )
297
307
return -1 ;
298
308
strbuf_addf (buf , "%" PRItime , t );
299
- } else if (type == TYPE_COLOR ) {
309
+ } else if (opts -> type == TYPE_COLOR ) {
300
310
char v [COLOR_MAXLEN ];
301
311
if (git_config_color (v , key_ , value_ ) < 0 )
302
312
return -1 ;
@@ -444,7 +454,7 @@ static int get_value(const struct config_location_options *opts,
444
454
}
445
455
446
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 )
448
458
{
449
459
if (!value )
450
460
return NULL ;
@@ -789,16 +799,6 @@ static void display_options_init(struct config_display_options *opts)
789
799
}
790
800
}
791
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
802
static int cmd_config_list (int argc , const char * * argv , const char * prefix )
803
803
{
804
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
841
int flags = 0 ;
842
842
struct option opts [] = {
843
843
CONFIG_LOCATION_OPTIONS (location_opts ),
844
- CONFIG_TYPE_OPTIONS ,
845
844
OPT_GROUP (N_ ("Filter options" )),
846
845
OPT_BOOL (0 , "all" , & do_all , N_ ("return all values for multi-valued config options" )),
847
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)
886
885
struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT ;
887
886
const char * value_pattern = NULL , * comment_arg = NULL ;
888
887
char * comment = NULL ;
889
- int flags = 0 , append = 0 ;
888
+ int flags = 0 , append = 0 , type = 0 ;
890
889
struct option opts [] = {
891
890
CONFIG_LOCATION_OPTIONS (location_opts ),
892
- CONFIG_TYPE_OPTIONS ,
891
+ CONFIG_TYPE_OPTIONS ( type ) ,
893
892
OPT_GROUP (N_ ("Filter" )),
894
893
OPT_BIT (0 , "all" , & flags , N_ ("replace multi-valued config option with new value" ), CONFIG_FLAGS_MULTI_REPLACE ),
895
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)
919
918
location_options_init (& location_opts , prefix );
920
919
check_write (& location_opts .source );
921
920
922
- value = normalize_value (argv [0 ], argv [1 ], & default_kvi );
921
+ value = normalize_value (argv [0 ], argv [1 ], type , & default_kvi );
923
922
924
923
if ((flags & CONFIG_FLAGS_MULTI_REPLACE ) || value_pattern ) {
925
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)
1126
1125
OPT_CMDMODE ('e' , "edit" , & actions , N_ ("open an editor" ), ACTION_EDIT ),
1127
1126
OPT_CMDMODE (0 , "get-color" , & actions , N_ ("find the color configured: slot [<default>]" ), ACTION_GET_COLOR ),
1128
1127
OPT_CMDMODE (0 , "get-colorbool" , & actions , N_ ("find the color setting: slot [<stdout-is-tty>]" ), ACTION_GET_COLORBOOL ),
1129
- CONFIG_TYPE_OPTIONS ,
1130
1128
CONFIG_DISPLAY_OPTIONS (display_opts ),
1131
1129
OPT_GROUP (N_ ("Other" )),
1132
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)
1147
1145
location_options_init (& location_opts , prefix );
1148
1146
display_options_init (& display_opts );
1149
1147
1150
- if ((actions & (ACTION_GET_COLOR |ACTION_GET_COLORBOOL )) && type ) {
1148
+ if ((actions & (ACTION_GET_COLOR |ACTION_GET_COLORBOOL )) && display_opts . type ) {
1151
1149
error (_ ("--get-color and variable type are incoherent" ));
1152
1150
exit (129 );
1153
1151
}
@@ -1248,7 +1246,7 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
1248
1246
else if (actions == ACTION_SET ) {
1249
1247
check_write (& location_opts .source );
1250
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 );
1252
1250
ret = git_config_set_in_file_gently (location_opts .source .file , argv [0 ], comment , value );
1253
1251
if (ret == CONFIG_NOTHING_SET )
1254
1252
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)
1257
1255
else if (actions == ACTION_SET_ALL ) {
1258
1256
check_write (& location_opts .source );
1259
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 );
1261
1259
ret = git_config_set_multivar_in_file_gently (location_opts .source .file ,
1262
1260
argv [0 ], value , argv [2 ],
1263
1261
comment , flags );
1264
1262
}
1265
1263
else if (actions == ACTION_ADD ) {
1266
1264
check_write (& location_opts .source );
1267
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 );
1269
1267
ret = git_config_set_multivar_in_file_gently (location_opts .source .file ,
1270
1268
argv [0 ], value ,
1271
1269
CONFIG_REGEX_NONE ,
@@ -1274,7 +1272,7 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
1274
1272
else if (actions == ACTION_REPLACE_ALL ) {
1275
1273
check_write (& location_opts .source );
1276
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 );
1278
1276
ret = git_config_set_multivar_in_file_gently (location_opts .source .file ,
1279
1277
argv [0 ], value , argv [2 ],
1280
1278
comment , flags | CONFIG_FLAGS_MULTI_REPLACE );
0 commit comments