@@ -29,10 +29,11 @@ static int use_worktree_config;
29
29
static struct git_config_source given_config_source ;
30
30
static int actions , type ;
31
31
static char * default_value ;
32
- static int end_null ;
32
+ static int end_nul ;
33
33
static int respect_includes_opt = -1 ;
34
34
static struct config_options config_options ;
35
35
static int show_origin ;
36
+ static int show_scope ;
36
37
37
38
#define ACTION_GET (1<<0)
38
39
#define ACTION_GET_ALL (1<<1)
@@ -151,10 +152,11 @@ static struct option builtin_config_options[] = {
151
152
OPT_CALLBACK_VALUE (0 , "path" , & type , N_ ("value is a path (file or directory name)" ), TYPE_PATH ),
152
153
OPT_CALLBACK_VALUE (0 , "expiry-date" , & type , N_ ("value is an expiry date" ), TYPE_EXPIRY_DATE ),
153
154
OPT_GROUP (N_ ("Other" )),
154
- OPT_BOOL ('z' , "null" , & end_null , N_ ("terminate values with NUL byte" )),
155
+ OPT_BOOL ('z' , "null" , & end_nul , N_ ("terminate values with NUL byte" )),
155
156
OPT_BOOL (0 , "name-only" , & omit_values , N_ ("show variable names only" )),
156
157
OPT_BOOL (0 , "includes" , & respect_includes_opt , N_ ("respect include directives on lookup" )),
157
158
OPT_BOOL (0 , "show-origin" , & show_origin , N_ ("show origin of config (file, standard input, blob, command line)" )),
159
+ OPT_BOOL (0 , "show-scope" , & show_scope , N_ ("show scope of config (system, global, local, command line)" )),
158
160
OPT_STRING (0 , "default" , & default_value , N_ ("value" ), N_ ("with --get, use default value when missing entry" )),
159
161
OPT_END (),
160
162
};
@@ -178,23 +180,63 @@ static void check_argc(int argc, int min, int max)
178
180
179
181
static void show_config_origin (struct strbuf * buf )
180
182
{
181
- const char term = end_null ? '\0' : '\t' ;
183
+ const char term = end_nul ? '\0' : '\t' ;
182
184
183
185
strbuf_addstr (buf , current_config_origin_type ());
184
186
strbuf_addch (buf , ':' );
185
- if (end_null )
187
+ if (end_nul )
186
188
strbuf_addstr (buf , current_config_name ());
187
189
else
188
190
quote_c_style (current_config_name (), buf , NULL , 0 );
189
191
strbuf_addch (buf , term );
190
192
}
191
193
194
+ static const char * scope_to_string (enum config_scope scope ) {
195
+ /*
196
+ * --local, --global, and --system work the same as --file so there's
197
+ * no easy way for the parser to tell the difference when it is
198
+ * setting the scope, so we use our information about which options
199
+ * were passed
200
+ */
201
+ if (use_local_config || scope == CONFIG_SCOPE_REPO ) {
202
+ return "local" ;
203
+ } else if (use_global_config || scope == CONFIG_SCOPE_GLOBAL ) {
204
+ return "global" ;
205
+ } else if (use_system_config || scope == CONFIG_SCOPE_SYSTEM ) {
206
+ return "system" ;
207
+ } else if (given_config_source .use_stdin ||
208
+ given_config_source .blob ||
209
+ given_config_source .file ||
210
+ scope == CONFIG_SCOPE_CMDLINE ) {
211
+ return "command line" ;
212
+ } else {
213
+ return "unknown" ;
214
+ }
215
+ }
216
+
217
+ static void show_config_scope (struct strbuf * buf )
218
+ {
219
+ const char term = end_nul ? '\0' : '\t' ;
220
+ const char * scope = scope_to_string (current_config_scope ());
221
+
222
+ strbuf_addch (buf , '(' );
223
+ if (end_nul )
224
+ strbuf_addstr (buf , N_ (scope ));
225
+ else
226
+ quote_c_style (scope , buf , NULL , 0 );
227
+ strbuf_addch (buf , ')' );
228
+ strbuf_addch (buf , term );
229
+ }
230
+
192
231
static int show_all_config (const char * key_ , const char * value_ , void * cb )
193
232
{
194
- if (show_origin ) {
233
+ if (show_origin || show_scope ) {
195
234
struct strbuf buf = STRBUF_INIT ;
196
- show_config_origin (& buf );
197
- /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
235
+ if (show_scope )
236
+ show_config_scope (& buf );
237
+ if (show_origin )
238
+ show_config_origin (& buf );
239
+ /* Use fwrite as "buf" can contain \0's if "end_nul" is set. */
198
240
fwrite (buf .buf , 1 , buf .len , stdout );
199
241
strbuf_release (& buf );
200
242
}
@@ -213,6 +255,8 @@ struct strbuf_list {
213
255
214
256
static int format_config (struct strbuf * buf , const char * key_ , const char * value_ )
215
257
{
258
+ if (show_scope )
259
+ show_config_scope (buf );
216
260
if (show_origin )
217
261
show_config_origin (buf );
218
262
if (show_keys )
@@ -678,7 +722,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
678
722
config_options .git_dir = get_git_dir ();
679
723
}
680
724
681
- if (end_null ) {
725
+ if (end_nul ) {
682
726
term = '\0' ;
683
727
delim = '\n' ;
684
728
key_delim = '\n' ;
0 commit comments