@@ -25,7 +25,7 @@ static char term = '\n';
25
25
26
26
static int use_global_config , use_system_config , use_local_config ;
27
27
static struct git_config_source given_config_source ;
28
- static int actions , types ;
28
+ static int actions , type ;
29
29
static int end_null ;
30
30
static int respect_includes_opt = -1 ;
31
31
static struct config_options config_options ;
@@ -55,11 +55,65 @@ static int show_origin;
55
55
#define PAGING_ACTIONS (ACTION_LIST | ACTION_GET_ALL | \
56
56
ACTION_GET_REGEXP | ACTION_GET_URLMATCH)
57
57
58
- #define TYPE_BOOL (1<<0)
59
- #define TYPE_INT (1<<1)
60
- #define TYPE_BOOL_OR_INT (1<<2)
61
- #define TYPE_PATH (1<<3)
62
- #define TYPE_EXPIRY_DATE (1<<4)
58
+ #define TYPE_BOOL 1
59
+ #define TYPE_INT 2
60
+ #define TYPE_BOOL_OR_INT 3
61
+ #define TYPE_PATH 4
62
+ #define TYPE_EXPIRY_DATE 5
63
+
64
+ #define OPT_CALLBACK_VALUE (s , l , v , h , i ) \
65
+ { OPTION_CALLBACK, (s), (l), (v), NULL, (h), PARSE_OPT_NOARG | \
66
+ PARSE_OPT_NONEG, option_parse_type, (i) }
67
+
68
+ static struct option builtin_config_options [];
69
+
70
+ static int option_parse_type (const struct option * opt , const char * arg ,
71
+ int unset )
72
+ {
73
+ int new_type , * to_type ;
74
+
75
+ if (unset ) {
76
+ * ((int * ) opt -> value ) = 0 ;
77
+ return 0 ;
78
+ }
79
+
80
+ /*
81
+ * To support '--<type>' style flags, begin with new_type equal to
82
+ * opt->defval.
83
+ */
84
+ new_type = opt -> defval ;
85
+ if (!new_type ) {
86
+ if (!strcmp (arg , "bool" ))
87
+ new_type = TYPE_BOOL ;
88
+ else if (!strcmp (arg , "int" ))
89
+ new_type = TYPE_INT ;
90
+ else if (!strcmp (arg , "bool-or-int" ))
91
+ new_type = TYPE_BOOL_OR_INT ;
92
+ else if (!strcmp (arg , "path" ))
93
+ new_type = TYPE_PATH ;
94
+ else if (!strcmp (arg , "expiry-date" ))
95
+ new_type = TYPE_EXPIRY_DATE ;
96
+ else
97
+ die (_ ("unrecognized --type argument, %s" ), arg );
98
+ }
99
+
100
+ to_type = opt -> value ;
101
+ if (* to_type && * to_type != new_type ) {
102
+ /*
103
+ * Complain when there is a new type not equal to the old type.
104
+ * This allows for combinations like '--int --type=int' and
105
+ * '--type=int --type=int', but disallows ones like '--type=bool
106
+ * --int' and '--type=bool
107
+ * --type=int'.
108
+ */
109
+ error ("only one type at a time." );
110
+ usage_with_options (builtin_config_usage ,
111
+ builtin_config_options );
112
+ }
113
+ * to_type = new_type ;
114
+
115
+ return 0 ;
116
+ }
63
117
64
118
static struct option builtin_config_options [] = {
65
119
OPT_GROUP (N_ ("Config file location" )),
@@ -84,11 +138,12 @@ static struct option builtin_config_options[] = {
84
138
OPT_BIT (0 , "get-color" , & actions , N_ ("find the color configured: slot [default]" ), ACTION_GET_COLOR ),
85
139
OPT_BIT (0 , "get-colorbool" , & actions , N_ ("find the color setting: slot [stdout-is-tty]" ), ACTION_GET_COLORBOOL ),
86
140
OPT_GROUP (N_ ("Type" )),
87
- OPT_BIT (0 , "bool" , & types , N_ ("value is \"true\" or \"false\"" ), TYPE_BOOL ),
88
- OPT_BIT (0 , "int" , & types , N_ ("value is decimal number" ), TYPE_INT ),
89
- OPT_BIT (0 , "bool-or-int" , & types , N_ ("value is --bool or --int" ), TYPE_BOOL_OR_INT ),
90
- OPT_BIT (0 , "path" , & types , N_ ("value is a path (file or directory name)" ), TYPE_PATH ),
91
- OPT_BIT (0 , "expiry-date" , & types , N_ ("value is an expiry date" ), TYPE_EXPIRY_DATE ),
141
+ OPT_CALLBACK ('t' , "type" , & type , "" , N_ ("value is given this type" ), option_parse_type ),
142
+ OPT_CALLBACK_VALUE (0 , "bool" , & type , N_ ("value is \"true\" or \"false\"" ), TYPE_BOOL ),
143
+ OPT_CALLBACK_VALUE (0 , "int" , & type , N_ ("value is decimal number" ), TYPE_INT ),
144
+ OPT_CALLBACK_VALUE (0 , "bool-or-int" , & type , N_ ("value is --bool or --int" ), TYPE_BOOL_OR_INT ),
145
+ OPT_CALLBACK_VALUE (0 , "path" , & type , N_ ("value is a path (file or directory name)" ), TYPE_PATH ),
146
+ OPT_CALLBACK_VALUE (0 , "expiry-date" , & type , N_ ("value is an expiry date" ), TYPE_EXPIRY_DATE ),
92
147
OPT_GROUP (N_ ("Other" )),
93
148
OPT_BOOL ('z' , "null" , & end_null , N_ ("terminate values with NUL byte" )),
94
149
OPT_BOOL (0 , "name-only" , & omit_values , N_ ("show variable names only" )),
@@ -149,26 +204,26 @@ static int format_config(struct strbuf *buf, const char *key_, const char *value
149
204
if (show_keys )
150
205
strbuf_addch (buf , key_delim );
151
206
152
- if (types == TYPE_INT )
207
+ if (type == TYPE_INT )
153
208
strbuf_addf (buf , "%" PRId64 ,
154
209
git_config_int64 (key_ , value_ ? value_ : "" ));
155
- else if (types == TYPE_BOOL )
210
+ else if (type == TYPE_BOOL )
156
211
strbuf_addstr (buf , git_config_bool (key_ , value_ ) ?
157
212
"true" : "false" );
158
- else if (types == TYPE_BOOL_OR_INT ) {
213
+ else if (type == TYPE_BOOL_OR_INT ) {
159
214
int is_bool , v ;
160
215
v = git_config_bool_or_int (key_ , value_ , & is_bool );
161
216
if (is_bool )
162
217
strbuf_addstr (buf , v ? "true" : "false" );
163
218
else
164
219
strbuf_addf (buf , "%d" , v );
165
- } else if (types == TYPE_PATH ) {
220
+ } else if (type == TYPE_PATH ) {
166
221
const char * v ;
167
222
if (git_config_pathname (& v , key_ , value_ ) < 0 )
168
223
return -1 ;
169
224
strbuf_addstr (buf , v );
170
225
free ((char * )v );
171
- } else if (types == TYPE_EXPIRY_DATE ) {
226
+ } else if (type == TYPE_EXPIRY_DATE ) {
172
227
timestamp_t t ;
173
228
if (git_config_expiry_date (& t , key_ , value_ ) < 0 )
174
229
return -1 ;
@@ -287,7 +342,7 @@ static char *normalize_value(const char *key, const char *value)
287
342
if (!value )
288
343
return NULL ;
289
344
290
- if (types == 0 || types == TYPE_PATH || types == TYPE_EXPIRY_DATE )
345
+ if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE )
291
346
/*
292
347
* We don't do normalization for TYPE_PATH here: If
293
348
* the path is like ~/foobar/, we prefer to store
@@ -296,11 +351,11 @@ static char *normalize_value(const char *key, const char *value)
296
351
* Also don't do normalization for expiry dates.
297
352
*/
298
353
return xstrdup (value );
299
- if (types == TYPE_INT )
354
+ if (type == TYPE_INT )
300
355
return xstrfmt ("%" PRId64 , git_config_int64 (key , value ));
301
- if (types == TYPE_BOOL )
356
+ if (type == TYPE_BOOL )
302
357
return xstrdup (git_config_bool (key , value ) ? "true" : "false" );
303
- if (types == TYPE_BOOL_OR_INT ) {
358
+ if (type == TYPE_BOOL_OR_INT ) {
304
359
int is_bool , v ;
305
360
v = git_config_bool_or_int (key , value , & is_bool );
306
361
if (!is_bool )
@@ -309,7 +364,7 @@ static char *normalize_value(const char *key, const char *value)
309
364
return xstrdup (v ? "true" : "false" );
310
365
}
311
366
312
- die ("BUG: cannot normalize type %d" , types );
367
+ die ("BUG: cannot normalize type %d" , type );
313
368
}
314
369
315
370
static int get_color_found ;
@@ -566,12 +621,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
566
621
key_delim = '\n' ;
567
622
}
568
623
569
- if (HAS_MULTI_BITS (types )) {
570
- error ("only one type at a time." );
571
- usage_with_options (builtin_config_usage , builtin_config_options );
572
- }
573
-
574
- if ((actions & (ACTION_GET_COLOR |ACTION_GET_COLORBOOL )) && types ) {
624
+ if ((actions & (ACTION_GET_COLOR |ACTION_GET_COLORBOOL )) && type ) {
575
625
error ("--get-color and variable type are incoherent" );
576
626
usage_with_options (builtin_config_usage , builtin_config_options );
577
627
}
0 commit comments