Skip to content

Commit fc141e8

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. Note that we should never see anything of "submodule" scope as that is only ever used by submodule-config.c when parsing the '.gitmodules' file. Signed-off-by: Matthew Rogers <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 936ce91 commit fc141e8

File tree

7 files changed

+141
-39
lines changed

7 files changed

+141
-39
lines changed

Documentation/git-config.txt

Lines changed: 10 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,11 @@ Valid `<type>`'s include:
222222
the actual origin (config file path, ref, or blob id if
223223
applicable).
224224

225+
--show-scope::
226+
Similar to `--show-origin` in that it augments the output of
227+
all queried config options with the scope of that value
228+
(local, global, system, command).
229+
225230
--get-colorbool name [stdout-is-tty]::
226231

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

builtin/config.c

Lines changed: 31 additions & 5 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 (worktree, local, global, system, command)")),
158160
OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")),
159161
OPT_END(),
160162
};
@@ -189,11 +191,23 @@ static void show_config_origin(struct strbuf *buf)
189191
strbuf_addch(buf, term);
190192
}
191193

194+
static void show_config_scope(struct strbuf *buf)
195+
{
196+
const char term = end_nul ? '\0' : '\t';
197+
const char *scope = config_scope_name(current_config_scope());
198+
199+
strbuf_addstr(buf, N_(scope));
200+
strbuf_addch(buf, term);
201+
}
202+
192203
static int show_all_config(const char *key_, const char *value_, void *cb)
193204
{
194-
if (show_origin) {
205+
if (show_origin || show_scope) {
195206
struct strbuf buf = STRBUF_INIT;
196-
show_config_origin(&buf);
207+
if (show_scope)
208+
show_config_scope(&buf);
209+
if (show_origin)
210+
show_config_origin(&buf);
197211
/* Use fwrite as "buf" can contain \0's if "end_null" is set. */
198212
fwrite(buf.buf, 1, buf.len, stdout);
199213
strbuf_release(&buf);
@@ -213,6 +227,8 @@ struct strbuf_list {
213227

214228
static int format_config(struct strbuf *buf, const char *key_, const char *value_)
215229
{
230+
if (show_scope)
231+
show_config_scope(buf);
216232
if (show_origin)
217233
show_config_origin(buf);
218234
if (show_keys)
@@ -622,6 +638,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
622638
!strcmp(given_config_source.file, "-")) {
623639
given_config_source.file = NULL;
624640
given_config_source.use_stdin = 1;
641+
given_config_source.scope = CONFIG_SCOPE_COMMAND;
625642
}
626643

627644
if (use_global_config) {
@@ -637,6 +654,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
637654
*/
638655
die(_("$HOME not set"));
639656

657+
given_config_source.scope = CONFIG_SCOPE_GLOBAL;
658+
640659
if (access_or_warn(user_config, R_OK, 0) &&
641660
xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
642661
given_config_source.file = xdg_config;
@@ -646,11 +665,13 @@ int cmd_config(int argc, const char **argv, const char *prefix)
646665
free(xdg_config);
647666
}
648667
}
649-
else if (use_system_config)
668+
else if (use_system_config) {
650669
given_config_source.file = git_etc_gitconfig();
651-
else if (use_local_config)
670+
given_config_source.scope = CONFIG_SCOPE_SYSTEM;
671+
} else if (use_local_config) {
652672
given_config_source.file = git_pathdup("config");
653-
else if (use_worktree_config) {
673+
given_config_source.scope = CONFIG_SCOPE_LOCAL;
674+
} else if (use_worktree_config) {
654675
struct worktree **worktrees = get_worktrees(0);
655676
if (repository_format_worktree_config)
656677
given_config_source.file = git_pathdup("config.worktree");
@@ -662,13 +683,18 @@ int cmd_config(int argc, const char **argv, const char *prefix)
662683
"section in \"git help worktree\" for details"));
663684
else
664685
given_config_source.file = git_pathdup("config");
686+
given_config_source.scope = CONFIG_SCOPE_LOCAL;
665687
free_worktrees(worktrees);
666688
} else if (given_config_source.file) {
667689
if (!is_absolute_path(given_config_source.file) && prefix)
668690
given_config_source.file =
669691
prefix_filename(prefix, given_config_source.file);
692+
given_config_source.scope = CONFIG_SCOPE_COMMAND;
693+
} else if (given_config_source.blob) {
694+
given_config_source.scope = CONFIG_SCOPE_COMMAND;
670695
}
671696

697+
672698
if (respect_includes_opt == -1)
673699
config_options.respect_includes = !given_config_source.file;
674700
else

config.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,7 @@ static int do_git_config_sequence(const struct config_options *opts,
17021702
char *xdg_config = xdg_config_home("config");
17031703
char *user_config = expand_user_path("~/.gitconfig", 0);
17041704
char *repo_config;
1705+
enum config_scope prev_parsing_scope = current_parsing_scope;
17051706

17061707
if (opts->commondir)
17071708
repo_config = mkpathdup("%s/config", opts->commondir);
@@ -1741,7 +1742,7 @@ static int do_git_config_sequence(const struct config_options *opts,
17411742
if (!opts->ignore_cmdline && git_config_from_parameters(fn, data) < 0)
17421743
die(_("unable to parse command-line config"));
17431744

1744-
current_parsing_scope = CONFIG_SCOPE_UNKNOWN;
1745+
current_parsing_scope = prev_parsing_scope;
17451746
free(xdg_config);
17461747
free(user_config);
17471748
free(repo_config);
@@ -1762,6 +1763,9 @@ int config_with_options(config_fn_t fn, void *data,
17621763
data = &inc;
17631764
}
17641765

1766+
if (config_source)
1767+
current_parsing_scope = config_source->scope;
1768+
17651769
/*
17661770
* If we have a specific filename, use it. Otherwise, follow the
17671771
* regular lookup sequence.
@@ -3294,6 +3298,26 @@ const char *current_config_origin_type(void)
32943298
}
32953299
}
32963300

3301+
const char *config_scope_name(enum config_scope scope)
3302+
{
3303+
switch (scope) {
3304+
case CONFIG_SCOPE_SYSTEM:
3305+
return "system";
3306+
case CONFIG_SCOPE_GLOBAL:
3307+
return "global";
3308+
case CONFIG_SCOPE_LOCAL:
3309+
return "local";
3310+
case CONFIG_SCOPE_WORKTREE:
3311+
return "worktree";
3312+
case CONFIG_SCOPE_COMMAND:
3313+
return "command";
3314+
case CONFIG_SCOPE_SUBMODULE:
3315+
return "submodule";
3316+
default:
3317+
return "unknown";
3318+
}
3319+
}
3320+
32973321
const char *current_config_name(void)
32983322
{
32993323
const char *name;

config.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,22 @@ 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_COMMAND,
45+
CONFIG_SCOPE_SUBMODULE,
46+
};
47+
const char *config_scope_name(enum config_scope scope);
48+
3849
struct git_config_source {
3950
unsigned int use_stdin:1;
4051
const char *file;
4152
const char *blob;
53+
enum config_scope scope;
4254
};
4355

4456
enum config_origin_type {
@@ -294,15 +306,6 @@ int config_error_nonbool(const char *);
294306

295307
int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
296308

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_COMMAND,
304-
};
305-
306309
enum config_scope current_config_scope(void);
307310
const char *current_config_origin_type(void);
308311
const char *current_config_name(void);

submodule-config.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,9 @@ static void submodule_cache_check_init(struct repository *repo)
635635
static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data)
636636
{
637637
if (repo->worktree) {
638-
struct git_config_source config_source = { 0 };
638+
struct git_config_source config_source = {
639+
0, .scope = CONFIG_SCOPE_SUBMODULE
640+
};
639641
const struct config_options opts = { 0 };
640642
struct object_id oid;
641643
char *file;

t/helper/test-config.c

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,6 @@
3737
*
3838
*/
3939

40-
static const char *scope_name(enum config_scope scope)
41-
{
42-
switch (scope) {
43-
case CONFIG_SCOPE_SYSTEM:
44-
return "system";
45-
case CONFIG_SCOPE_GLOBAL:
46-
return "global";
47-
case CONFIG_SCOPE_LOCAL:
48-
return "local";
49-
case CONFIG_SCOPE_WORKTREE:
50-
return "worktree";
51-
case CONFIG_SCOPE_COMMAND:
52-
return "command";
53-
default:
54-
return "unknown";
55-
}
56-
}
5740
static int iterate_cb(const char *var, const char *value, void *data)
5841
{
5942
static int nr;
@@ -65,7 +48,7 @@ static int iterate_cb(const char *var, const char *value, void *data)
6548
printf("value=%s\n", value ? value : "(null)");
6649
printf("origin=%s\n", current_config_origin_type());
6750
printf("name=%s\n", current_config_name());
68-
printf("scope=%s\n", scope_name(current_config_scope()));
51+
printf("scope=%s\n", config_scope_name(current_config_scope()));
6952

7053
return 0;
7154
}

t/t1300-config.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,6 +1771,65 @@ test_expect_success '--show-origin blob ref' '
17711771
test_cmp expect output
17721772
'
17731773

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

0 commit comments

Comments
 (0)