Skip to content

Commit 066d944

Browse files
committed
config: add '--show-scope' to print the scope of a config value
When a user queries config values with --show-origin, often it's difficult to determine what the actual "scope" (local, global, etc.) of a given value is based on just the origin file. Teach 'git config' the '--show-scope' option to print the scope of all displayed config values. Signed-off-by: Matthew Rogers <[email protected]>
1 parent 24eec91 commit 066d944

File tree

5 files changed

+128
-21
lines changed

5 files changed

+128
-21
lines changed

Documentation/git-config.txt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ git-config - Get and set repository or global options
99
SYNOPSIS
1010
--------
1111
[verse]
12-
'git config' [<file-option>] [--type=<type>] [--show-origin] [-z|--null] name [value [value_regex]]
12+
'git config' [<file-option>] [--type=<type>] [--show-origin] [--show-scope] [-z|--null] name [value [value_regex]]
1313
'git config' [<file-option>] [--type=<type>] --add name value
1414
'git config' [<file-option>] [--type=<type>] --replace-all name value [value_regex]
15-
'git config' [<file-option>] [--type=<type>] [--show-origin] [-z|--null] --get name [value_regex]
16-
'git config' [<file-option>] [--type=<type>] [--show-origin] [-z|--null] --get-all name [value_regex]
17-
'git config' [<file-option>] [--type=<type>] [--show-origin] [-z|--null] [--name-only] --get-regexp name_regex [value_regex]
15+
'git config' [<file-option>] [--type=<type>] [--show-origin] [--show-scope] [-z|--null] --get name [value_regex]
16+
'git config' [<file-option>] [--type=<type>] [--show-origin] [--show-scope] [-z|--null] --get-all name [value_regex]
17+
'git config' [<file-option>] [--type=<type>] [--show-origin] [--show-scope] [-z|--null] [--name-only] --get-regexp name_regex [value_regex]
1818
'git config' [<file-option>] [--type=<type>] [-z|--null] --get-urlmatch name URL
1919
'git config' [<file-option>] --unset name [value_regex]
2020
'git config' [<file-option>] --unset-all name [value_regex]
2121
'git config' [<file-option>] --rename-section old_name new_name
2222
'git config' [<file-option>] --remove-section name
23-
'git config' [<file-option>] [--show-origin] [-z|--null] [--name-only] -l | --list
23+
'git config' [<file-option>] [--show-origin] [--show-scope] [-z|--null] [--name-only] -l | --list
2424
'git config' [<file-option>] --get-color name [default]
2525
'git config' [<file-option>] --get-colorbool name [stdout-is-tty]
2626
'git config' [<file-option>] -e | --edit
@@ -222,6 +222,10 @@ Valid `<type>`'s include:
222222
the actual origin (config file path, ref, or blob id if
223223
applicable).
224224

225+
--show-scope::
226+
Augment the output of all queried config options with its scope
227+
(file, blob, command line, worktree, local, system, global).
228+
225229
--get-colorbool name [stdout-is-tty]::
226230

227231
Find the color setting for `name` (e.g. `color.diff`) and output

builtin/config.c

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ static int end_nul;
3333
static int respect_includes_opt = -1;
3434
static struct config_options config_options;
3535
static int show_origin;
36+
static int show_scope;
3637

3738
#define ACTION_GET (1<<0)
3839
#define ACTION_GET_ALL (1<<1)
@@ -155,6 +156,7 @@ static struct option builtin_config_options[] = {
155156
OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
156157
OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")),
157158
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)")),
158160
OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")),
159161
OPT_END(),
160162
};
@@ -189,12 +191,45 @@ static void show_config_origin(struct strbuf *buf)
189191
strbuf_addch(buf, term);
190192
}
191193

194+
static const char *scope_to_string(enum config_scope scope) {
195+
switch (scope) {
196+
case CONFIG_SCOPE_LOCAL:
197+
return "local";
198+
case CONFIG_SCOPE_GLOBAL:
199+
return "global";
200+
case CONFIG_SCOPE_SYSTEM:
201+
return "system";
202+
case CONFIG_SCOPE_CMDLINE:
203+
return "command line";
204+
case CONFIG_SCOPE_FILE:
205+
return "file";
206+
case CONFIG_SCOPE_BLOB:
207+
return "blob";
208+
case CONFIG_SCOPE_STDIN:
209+
return "stdin";
210+
default:
211+
return "unknown";
212+
}
213+
}
214+
215+
static void show_config_scope(struct strbuf *buf)
216+
{
217+
const char term = end_nul ? '\0' : '\t';
218+
const char *scope = scope_to_string(current_config_scope());
219+
220+
strbuf_addstr(buf, N_(scope));
221+
strbuf_addch(buf, term);
222+
}
223+
192224
static int show_all_config(const char *key_, const char *value_, void *cb)
193225
{
194-
if (show_origin) {
226+
if (show_origin || show_scope) {
195227
struct strbuf buf = STRBUF_INIT;
196-
show_config_origin(&buf);
197-
/* Use fwrite as "buf" can contain \0's if "end_null" is set. */
228+
if (show_scope)
229+
show_config_scope(&buf);
230+
if (show_origin)
231+
show_config_origin(&buf);
232+
/* Use fwrite as "buf" can contain \0's if "end_nul" is set. */
198233
fwrite(buf.buf, 1, buf.len, stdout);
199234
strbuf_release(&buf);
200235
}
@@ -213,6 +248,8 @@ struct strbuf_list {
213248

214249
static int format_config(struct strbuf *buf, const char *key_, const char *value_)
215250
{
251+
if (show_scope)
252+
show_config_scope(buf);
216253
if (show_origin)
217254
show_config_origin(buf);
218255
if (show_keys)
@@ -622,6 +659,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
622659
!strcmp(given_config_source.file, "-")) {
623660
given_config_source.file = NULL;
624661
given_config_source.use_stdin = 1;
662+
given_config_source.scope = CONFIG_SCOPE_STDIN;
625663
}
626664

627665
if (use_global_config) {
@@ -645,12 +683,15 @@ int cmd_config(int argc, const char **argv, const char *prefix)
645683
given_config_source.file = user_config;
646684
free(xdg_config);
647685
}
648-
}
649-
else if (use_system_config)
686+
687+
given_config_source.scope = CONFIG_SCOPE_GLOBAL;
688+
} else if (use_system_config) {
650689
given_config_source.file = git_etc_gitconfig();
651-
else if (use_local_config)
690+
given_config_source.scope = CONFIG_SCOPE_SYSTEM;
691+
} else if (use_local_config) {
652692
given_config_source.file = git_pathdup("config");
653-
else if (use_worktree_config) {
693+
given_config_source.scope = CONFIG_SCOPE_LOCAL;
694+
} else if (use_worktree_config) {
654695
struct worktree **worktrees = get_worktrees(0);
655696
if (repository_format_worktree_config)
656697
given_config_source.file = git_pathdup("config.worktree");
@@ -662,11 +703,15 @@ int cmd_config(int argc, const char **argv, const char *prefix)
662703
"section in \"git help worktree\" for details"));
663704
else
664705
given_config_source.file = git_pathdup("config");
706+
given_config_source.scope = CONFIG_SCOPE_WORKTREE;
665707
free_worktrees(worktrees);
666708
} else if (given_config_source.file) {
667709
if (!is_absolute_path(given_config_source.file) && prefix)
668710
given_config_source.file =
669711
prefix_filename(prefix, given_config_source.file);
712+
given_config_source.scope = CONFIG_SCOPE_FILE;
713+
} else if (given_config_source.blob) {
714+
given_config_source.scope = CONFIG_SCOPE_BLOB;
670715
}
671716

672717
if (respect_includes_opt == -1)

config.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,6 +1757,9 @@ int config_with_options(config_fn_t fn, void *data,
17571757
data = &inc;
17581758
}
17591759

1760+
if (config_source)
1761+
current_parsing_scope = config_source->scope;
1762+
17601763
/*
17611764
* If we have a specific filename, use it. Otherwise, follow the
17621765
* regular lookup sequence.

config.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,23 @@ struct object_id;
3535

3636
#define CONFIG_REGEX_NONE ((void *)1)
3737

38+
enum config_scope {
39+
CONFIG_SCOPE_UNKNOWN = 0,
40+
CONFIG_SCOPE_SYSTEM,
41+
CONFIG_SCOPE_GLOBAL,
42+
CONFIG_SCOPE_LOCAL,
43+
CONFIG_SCOPE_WORKTREE,
44+
CONFIG_SCOPE_CMDLINE,
45+
CONFIG_SCOPE_FILE,
46+
CONFIG_SCOPE_BLOB,
47+
CONFIG_SCOPE_STDIN,
48+
};
49+
3850
struct git_config_source {
3951
unsigned int use_stdin:1;
4052
const char *file;
4153
const char *blob;
54+
enum config_scope scope;
4255
};
4356

4457
enum config_origin_type {
@@ -294,15 +307,6 @@ int config_error_nonbool(const char *);
294307

295308
int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
296309

297-
enum config_scope {
298-
CONFIG_SCOPE_UNKNOWN = 0,
299-
CONFIG_SCOPE_SYSTEM,
300-
CONFIG_SCOPE_GLOBAL,
301-
CONFIG_SCOPE_LOCAL,
302-
CONFIG_SCOPE_WORKTREE,
303-
CONFIG_SCOPE_CMDLINE,
304-
};
305-
306310
enum config_scope current_config_scope(void);
307311
const char *current_config_origin_type(void);
308312
const char *current_config_name(void);

t/t1300-config.sh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,6 +1766,57 @@ test_expect_success !MINGW '--show-origin blob ref' '
17661766
test_cmp expect output
17671767
'
17681768

1769+
1770+
test_expect_success '--show-scope with --list' '
1771+
cat >expect <<-EOF &&
1772+
global user.global=true
1773+
global user.override=global
1774+
global include.path=$INCLUDE_DIR/absolute.include
1775+
global user.absolute=include
1776+
local user.local=true
1777+
local user.override=local
1778+
local include.path=../include/relative.include
1779+
local user.relative=include
1780+
command line user.cmdline=true
1781+
EOF
1782+
git -c user.cmdline=true config --list --show-scope >output &&
1783+
test_cmp expect output
1784+
'
1785+
1786+
test_expect_success !MINGW '--show-scope with --blob' '
1787+
blob=$(git hash-object -w "$CUSTOM_CONFIG_FILE") &&
1788+
cat >expect <<-EOF &&
1789+
command line user.custom=true
1790+
EOF
1791+
git config --blob=$blob --show-scope --list >output &&
1792+
test_cmp expect output
1793+
'
1794+
test_expect_success '--show-scope with --local' '
1795+
cat >expect <<-\EOF &&
1796+
local user.local=true
1797+
local user.override=local
1798+
local include.path=../include/relative.include
1799+
EOF
1800+
git config --local --list --show-scope >output &&
1801+
test_cmp expect output
1802+
'
1803+
1804+
test_expect_success '--show-scope with --show-origin' '
1805+
cat >expect <<-EOF &&
1806+
global file:$HOME/.gitconfig user.global=true
1807+
global file:$HOME/.gitconfig user.override=global
1808+
global file:$HOME/.gitconfig include.path=$INCLUDE_DIR/absolute.include
1809+
global file:$INCLUDE_DIR/absolute.include user.absolute=include
1810+
local file:.git/config user.local=true
1811+
local file:.git/config user.override=local
1812+
local file:.git/config include.path=../include/relative.include
1813+
local file:.git/../include/relative.include user.relative=include
1814+
command line command line: user.cmdline=true
1815+
EOF
1816+
git -c user.cmdline=true config --list --show-origin --show-scope >output &&
1817+
test_cmp expect output
1818+
'
1819+
17691820
test_expect_success '--local requires a repo' '
17701821
# we expect 128 to ensure that we do not simply
17711822
# fail to find anything and return code "1"

0 commit comments

Comments
 (0)